Matconvnet学习笔记

来源:互联网 发布:php 重复的数据相加 编辑:程序博客网 时间:2024/05/18 04:01

作为一个CNN新手,分享一下matconvnet中例程的学习心得。

matconvnet的安装编译在此就不再赘述了,不知道的同学可以到官网上去看,上面写的已经很全面了。matconvnet

我跑的第一个例程是cnn_mnist,

  跑matlab相信大家都会,这个例程大概要跑20个epoch,得到如下图所示的20个mat,其中最后一个net-epoch-20就是我们最后训练出来的网络model

接下来的验证,我们就是要用到这个mat。


但是这个生成的网络并没有有关测试的参数,一个是averageimage,二是description,于是我在cnn_mnist()的代码段中加了这么几句话用于验证,放的位置在训练代码前就可以了。

net.meta.normalization.averageImage =imdb.images.data_mean ;%我后加的net.meta.normalization.description = imdb.meta.classes;%我后加的

因为该例程没有给出测试代码,所以我参考cnn_vgg_faces()的测试样例,稍微修改了一下,大体如下所示;

run(fullfile(fileparts(mfilename('fullpath')),...  '..', '..', 'matlab', 'vl_setupnn.m')) ;net = load('net-epoch-20.mat') ;net = net.net;net.layers{end}.type = 'softmax';im = imread('image1.jpg') ;im_ = single(im) ; % note: 255 rangeres = vl_simplenn(net, im_) ;% show the class% show the classification resultscores = squeeze(gather(res(end).x)) ;[bestScore, best] = max(scores) ;figure(1) ; clf ; imagesc(im) ; axis equal off ;title(sprintf('%s (%d), score %.3f',...              net.meta.classes.description{best}, best, bestScore), ...      'Interpreter', 'none') ;
其中测试图像是我从mnist数据集中分离出来的,如果是自己的图片还要做调整大小,减去均值,单精度转换这些操作。

还有在原始的网络中,最后一层是softmaxloss相当于softmax+logistic loss。如果我们直接用最后一层产生得分,程序会报错failed assertion。根据网上大牛的解决方案,推荐在测试代码(这里指的是测试代码,训练代码还是不变),最后一层的类型改为softmax。这样我们就可以成功通过网络得到分数,结果如下。







0 0