深度学习 5. MatConvNet 相关函数解释说明,MatConvNet 代码理解(一)cnn_mnist.m 的注释

深度学习 5. MatConvNet 相关函数解释说明,MatConvNet 代码理解(一)cnn_mnist.m 的注释

本文为原创文章转载必须注明本文出处以及附上 本文地址超链接  以及 博主博客地址:http://blog.csdn.net/qq_20259459  和 作者邮箱( jinweizhi93@gmai.com )。

(如果喜欢本文,欢迎大家关注我的博客或者动手点个赞,有需要可以邮件联系我)

 

接上一篇文章(阅读上一篇文章:http://blog.csdn.net/qq_20259459/article/details/54293054),我还是决定给大家写一下相关代码的注释。希望给大家带来帮助。

 

(一):cnn_mnist.m 

 

[plain] view plain copy
 
  1. function [net, info] = cnn_mnist(varargin)  
  2. %% --------------------------------------------------------------   
  3. %   主函数:cnn_mnist  
  4. %   功能:  1.初始化CNN  
  5. %           2.设置各项参数  
  6. %           3.读取和保存数据集  
  7. %           4.初始化train  
  8. % ------------------------------------------------------------------------  
  9.   
  10. %CNN_MNIST  Demonstrates MatConvNet on MNIST  
  11.   
  12. %运行matlab文件夹下的<span style="font-family:SimSun;">vl_setupnn.m</span>  
  13. run('C:\Users\Desktop\matconvnet-1.0-beta23\matconvnet-1.0-beta23\matlab/vl_setupnn.m') ;  
  14.   
  15. opts.batchNormalization = false ;                   %选择batchNormalization的真假  
  16. opts.network = [] ;                                 %初始化一个网络  
  17. opts.networkType = 'simplenn' ;                     %选择网络结构 %%% simplenn %%% dagnn  
  18. [opts, varargin] = vl_argparse(opts, varargin) ;    %调用vl_argparse函数  
  19.   
  20. sfx = opts.networkType ;                                                %sfx=simplenn  
  21. if opts.batchNormalization, sfx = [sfx '-bnorm'] ; end                  %这里条件为假  
  22. opts.expDir = fullfile(vl_rootnn, 'data', ['mnist-baseline-' sfx]) ;    %选择数据存放的路径:data\mnist-baseline-simplenn  
  23. [opts, varargin] = vl_argparse(opts, varargin) ;                        %调用vl_argparse函数  
  24.   
  25. opts.dataDir = fullfile(vl_rootnn, 'data', 'mnist') ;                   %选择数据读取的路径:data\matconvnet-1.0-beta23\data\mnist  
  26. opts.imdbPath = fullfile(opts.expDir, 'imdb.mat');                      %选择imdb结构体的路径:data\data\mnist-baseline-simplenn\imdb  
  27. opts.train = struct() ;                                                 %选择训练集返回为struct型  
  28. opts = vl_argparse(opts, varargin) ;                                    %调用vl_argparse函数  
  29.   
  30. %选择是否使用GPU,使用opts.train.gpus = 1,不使用:opts.train.gpus = []。  
  31. %有关GPU的安装配置请看我的博客:http://blog.csdn.net/qq_20259459/article/details/54093550  
  32. if ~isfield(opts.train, 'gpus'), opts.train.gpus = 1; end;                
  33.   
  34. % --------------------------------------------------------------------  
  35. %                                                              准备网络  
  36. % --------------------------------------------------------------------  
  37. if isempty(opts.network)                                                    %如果原网络为空:  
  38.   net = cnn_mnist_init('batchNormalization', opts.batchNormalization, ...   %   则调用cnn_mnist_init网络结构  
  39.     'networkType', opts.networkType) ;  
  40. else                                                                        %否则:  
  41.   net = opts.network ;                                                      %   使用上面选择的数值带入现有网络  
  42.   opts.network = [] ;  
  43. end  
  44.   
  45. % --------------------------------------------------------------------  
  46. %                                                              准备数据  
  47. % --------------------------------------------------------------------  
  48. if exist(opts.imdbPath, 'file')                         %如果mnist中存在imdb的结构体:  
  49.   imdb = load(opts.imdbPath) ;                          %   载入imdb  
  50. else                                                    %否则:  
  51.   imdb = getMnistImdb(opts) ;                           %   调用getMnistImdb函数得到imdb并保存  
  52.   mkdir(opts.expDir) ;                                    
  53.   save(opts.imdbPath, '-struct', 'imdb') ;  
  54. end  
  55.   
  56. %arrayfun函数通过应用sprintf函数得到array中从1到10的元素并且将其数字标签转化为char文字型  
  57. net.meta.classes.name = arrayfun(@(x)sprintf('%d',x),1:10,'UniformOutput',false) ;  
  58.   
  59. % --------------------------------------------------------------------  
  60. %                                                              开始训练  
  61. % --------------------------------------------------------------------  
  62.   
  63. switch opts.networkType                                     %选择网络类型:  
  64.   case 'simplenn', trainfn = @cnn_train ;                   %   1.simplenn  
  65.   case 'dagnn', trainfn = @cnn_train_dag ;                  %   2.dagnn  
  66. end  
  67.   
  68. [net, info] = trainfn(net, imdb, getBatch(opts), ...        %调用训练函数,开始训练:find(imdb.images.set == 3)为验证集的样本  
  69.   'expDir', opts.expDir, ...  
  70.   net.meta.trainOpts, ...  
  71.   opts.train, ...  
  72.   'val', find(imdb.images.set == 3)) ;  
  73.   
  74.   
  75. % ------------------------------------------------------------------------  
  76. function fn = getBatch(opts)  
  77. %% --------------------------------------------------------------  
  78. %   函数名:getBatch  
  79. %   功能:  1.由opts返回函数  
  80. %           2.从imdb结构体取出数据  
  81. %   备注: 如果不理解Batc的意义的话,请查看我的博客:http://blog.csdn.net/qq_20259459/article/details/53943413  
  82. % ------------------------------------------------------------------------  
  83. switch lower(opts.networkType)                              %根据网络类型使用不同的getBatcch  
  84.   case 'simplenn'  
  85.     fn = @(x,y) getSimpleNNBatch(x,y) ;  
  86.   case 'dagnn'  
  87.     bopts = struct('numGpus', numel(opts.train.gpus)) ;  
  88.     fn = @(x,y) getDagNNBatch(bopts,x,y) ;  
  89. end  
  90.   
  91.   
  92. % --------------------------------------------------------------------  
  93. function [images, labels] = getSimpleNNBatch(imdb, batch)  
  94. %% --------------------------------------------------------------  
  95. %   函数名:getSimpleNNBatch  
  96. %   功能:  1.由SimpleNN网络的批得到函数  
  97. %           2.batch为样本的索引值  
  98. % ------------------------------------------------------------------------  
  99. images = imdb.images.data(:,:,:,batch) ;                %返回训练集  
  100. labels = imdb.images.labels(1,batch) ;                  %返回集标签  
  101.   
  102. % --------------------------------------------------------------------  
  103. function inputs = getDagNNBatch(opts, imdb, batch)  
  104. %% --------------------------------------------------------------  
  105. %   函数名:getDagNNBatch  
  106. %   功能:  类似上面的函数,这里的网络结构是DagNN  
  107. % ------------------------------------------------------------------------  
  108. images = imdb.images.data(:,:,:,batch) ;  
  109. labels = imdb.images.labels(1,batch) ;  
  110. if opts.numGpus > 0                                     %使用GPU进行并行运算  
  111.   images = gpuArray(images) ;  
  112. end  
  113. inputs = {'input', images, 'label', labels} ;             
  114.   
  115. % --------------------------------------------------------------------  
  116. function imdb = getMnistImdb(opts)  
  117. %% --------------------------------------------------------------  
  118. %   函数名:getMnistImdb  
  119. %   功能:  1.从mnist数据集中获取data  
  120. %           2.将得到的数据减去mean值  
  121. %           3.将处理后的数据存放如imdb结构中  
  122. % ------------------------------------------------------------------------  
  123. % Preapre the imdb structure, returns image data with mean image subtracted  
  124. files = {'train-images-idx3-ubyte', ...                     %载入mnist数据集  
  125.          'train-labels-idx1-ubyte', ...  
  126.          't10k-images-idx3-ubyte', ...  
  127.          't10k-labels-idx1-ubyte'} ;  
  128.   
  129. if ~exist(opts.dataDir, 'dir')                              %如果不存在读取路径:  
  130.   mkdir(opts.dataDir) ;                                     %   建立读取路径  
  131. end  
  132.   
  133. for i=1:4                                                   %如果不存在mnist数据集则下载  
  134.   if ~exist(fullfile(opts.dataDir, files{i}), 'file')  
  135.     url = sprintf('http://yann.lecun.com/exdb/mnist/%s.gz',files{i}) ;  
  136.     fprintf('downloading %s\n', url) ;  
  137.     gunzip(url, opts.dataDir) ;  
  138.   end  
  139. end  
  140.   
  141. f=fopen(fullfile(opts.dataDir, 'train-images-idx3-ubyte'),'r') ;    %载入第一个文件,训练数据集大小为28*28,数量为6万  
  142. x1=fread(f,inf,'uint8');                                              
  143. fclose(f) ;   
  144. x1=permute(reshape(x1(17:end),28,28,60e3),[2 1 3]) ;                %通过permute函数将数组的维度由原来的[1 2 3]变为[2 1 3] ...  
  145.                                                                     %reshape将原数据从第17位开始构成28*28*60000的数组  
  146.   
  147. f=fopen(fullfile(opts.dataDir, 't10k-images-idx3-ubyte'),'r') ;     %载入第二个文件,测试数据集大小为28*28,数量为1万  
  148. x2=fread(f,inf,'uint8');  
  149. fclose(f) ;  
  150. x2=permute(reshape(x2(17:end),28,28,10e3),[2 1 3]) ;                %同上解释  
  151.   
  152. f=fopen(fullfile(opts.dataDir, 'train-labels-idx1-ubyte'),'r') ;    %载入第三个文件:训练数据集的类标签  
  153. y1=fread(f,inf,'uint8');  
  154. fclose(f) ;  
  155. y1=double(y1(9:end)')+1 ;                                             
  156.   
  157. f=fopen(fullfile(opts.dataDir, 't10k-labels-idx1-ubyte'),'r') ;     %载入第四个文件:测试数据集的类标签  
  158. y2=fread(f,inf,'uint8');  
  159. fclose(f) ;  
  160. y2=double(y2(9:end)')+1 ;  
  161.   
  162. %set = 1 对应训练;set = 3 对应的是测试  
  163. set = [ones(1,numel(y1)) 3*ones(1,numel(y2))];              %numel返回元素的总数  
  164. data = single(reshape(cat(3, x1, x2),28,28,1,[]));          %将x1的训练数据集和x2的测试数据集的第三个维度进行拼接组成新的数据集,并且转为single型减少内存  
  165. dataMean = mean(data(:,:,:,set == 1), 4);                   %求出训练数据集中所有的图像的均值  
  166. data = bsxfun(@minus, data, dataMean) ;                     %利用bsxfun函数将数据集中的每个元素逐个减去均值  
  167.   
  168. %将数据存入imdb结构中  
  169. imdb.images.data = data ;                                   %data的大小为[28 28 1 70000]。 (60000+10000)  
  170. imdb.images.data_mean = dataMean;                           %dataMean的大小为[28 28]  
  171. imdb.images.labels = cat(2, y1, y2) ;                       %拼接训练数据集和测试数据集的标签,拼接后的大小为[1 70000]  
  172. imdb.images.set = set ;                                     %set的大小为[1 70000],unique(set) = [1 3]  
  173. imdb.meta.sets = {'train', 'val', 'test'} ;                 %imdb.meta.sets=1用于训练,imdb.meta.sets=2用于验证,imdb.meta.sets=3用于测试  
  174.   
  175. %arrayfun函数通过应用sprintf函数得到array中从0到9的元素并且将其数字标签转化为char文字型  
  176. imdb.meta.classes = arrayfun(@(x)sprintf('%d',x),0:9,'uniformoutput',false) ;  

后面会持续更新MatConvNet的其他代码的注释。

 

 

本文为原创文章转载必须注明本文出处以及附上 本文地址超链接  以及 博主博客地址:http://blog.csdn.net/qq_20259459  和 作者邮箱( jinweizhi93@gmai.com )。

(如果喜欢本文,欢迎大家关注我的博客或者动手点个赞,有需要可以邮件联系我)

版权声明:本文为博主原创文章,转载必须注明是转载文章和原文超链接以及作者信息。 https://blog.csdn.net/qq_20259459/article/details/54411178

 

posted @ 2018-03-31 10:58  菜鸡一枚  阅读(1207)  评论(0)    收藏  举报