分享

Torch7 教程 Supervised Learning CNN

 心不留意外尘 2016-05-06

http://blog.csdn.net/guoyilin/article/details/38440413

2014

全部代码放在:https://github.com/guoyilin/CNN_Torch7

在搭建好Torch7之后,我们开始进行监督式Supervised Learning for CNN, Torch7提供了代码和一些说明文件:

http://code./wiki/doku.php?id=tutorial_supervised_1_data 和http://torch./doc/tutorials_supervised/说的比较详细。

结合http://ufldl./wiki/index.php/Feature_extraction_using_convolution了解CNN的做法,最关键的是要熟悉http://ufldl./wiki/index.php/Backpropagation_Algorithm 算法的主要做法。bp算法的目的是为了一次性计算所有的参数导数,该算法利用了chain rule进行error的后向传播。这篇文章写了bp算法: http:///chap2.html, 写的比较详细。

如果背景不熟悉,可以看看Linear Classification, Neutral Network, SGD算法。

由于该教程使用了torch自己的数据格式,因此如果你要使用自己的数据,需要预先转换下。这里我训练的是图像分类,因此可以使用

https://github.com/clementfarabet/graphicsmagick 进行数据的加载。
如下是加载图像的代码:
[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. height = 200  
  2. width = 200  
  3. --see if the file exists  
  4. function file_exists(file)  
  5.   local f = io.open(file, "rb")  
  6.   if f then f:close() end  
  7.   return f ~= nil  
  8. end  
  9.   
  10. function read_file (file)  
  11.   if not file_exists(file) then return {} end  
  12.   lines = {}  
  13.   for line in io.lines(file) do  
  14.     lines[#lines + 1] = line  
  15.   end  
  16.   return lines  
  17. end  
  18.   
  19. -- read all label name. hash them to id.  
  20. labels_id = {}  
  21. label_lines = read_file('labels.txt')  
  22. for i = 1, #label_lines do  
  23.   labels_id[label_lines[i]] = i  
  24. end  
  25.   
  26. -- read train data. iterate train.txt  
  27.   
  28. local train_lines = read_file("train.txt")  
  29. local train_features = torch.Tensor(#train_lines, 3, height, width) -- dimension: sample number, YUV, height, width  
  30. local train_labels = torch.Tensor(#train_lines) -- dimension: sample number  
  31.   
  32. for i = 1, #train_lines do  
  33.   local image = gm.Image("/train_images/" .. train_lines[i])  
  34.   image:size(width, height)  
  35.   img_yuv = image:toTensor('float', 'YUV', 'DHW')  
  36.   --print(img_yuv:size())  
  37.   --print(img_yuv:size())  
  38.   train_features[i] = img_yuv  
  39.   local label_name = train_lines[i]:match("([^,]+)/([^,]+)")  
  40.   train_labels[i] = labels_id[label_name]  
  41.   --print(train_labels[i])  
  42.   if(i % 100 == 0) then  
  43.     print("train data: " .. i)  
  44.   end  
  45. end  
  46.   
  47. trainData = {  
  48.   data = train_features:transpose(3,4),  
  49.   labels = train_labels,  
  50.   --size = function() return #train_lines end  
  51.   size = function() return #train_lines end  
  52. }  
  53.   
  54. -- read test data. iterate test.txt  
  55. local test_lines = read_file("test.txt")  
  56.   
  57. local test_features = torch.Tensor(#test_lines, 3, height, width) -- dimension: sample number, YUV, height, width  
  58. local test_labels = torch.Tensor(#test_lines) -- dimension: sample number  
  59.   
  60. for i = 1, #test_lines do  
  61.   -- if image size is zero, gm.Imge may throw error, we need to dispose it later.  
  62.   local image = gm.Image("test_images/" .. test_lines[i])  
  63.   --print(test_lines[i])  
  64.   
  65.   image:size(width, height)  
  66.   local img_yuv = image:toTensor('float', 'YUV', 'DHW')  
  67.   --print(img_yuv:size())  
  68.   test_features[i] = img_yuv  
  69.   local label_name = test_lines[i]:match("([^,]+)/([^,]+)")  
  70.   test_labels[i] = labels_id[label_name]  
  71.   --print(test_labels[i])  
  72.   if(i % 100 == 0) then  
  73.     print("test data: " .. i)  
  74.   end  
  75. end  
  76.   
  77. testData = {  
  78.   data = test_features:transpose(3,4),  
  79.   labels = test_labels,  
  80.   --size = function() return #test_lines end  
  81.   size = function() return #test_lines end  
  82. }  
  83. trsize = #train_lines  
  84. tesize = #test_lines  

由于图像的大小从32*32变成了200*200, 因此需要修改相应的model中的每一层的大小。
假定其他层没有变化,最后一层需要修改:
[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. -- stage 3 : standard 2-layer neural network  
  2.  model:add(nn.Reshape(nstates[2]*47*47))  
  3.  model:add(nn.Linear(nstates[2]*47*47, nstates[3]))  
  4.  model:add(nn.Tanh())  
  5.  model:add(nn.Linear(nstates[3], noutputs))  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多