MatConvNet卷积神经网络(二)

本篇内容:

1. 尝试DAG模块运行更复杂的图片识别算法。

2. 使用DAG模块,识别任意图片。


一、使用DAG模块

上一篇的例子是使用SimpleNN的典范。更复杂的模型则是使用DagNN。例如,要运行GoogLeNet使用:

首先试运行一下是否可以识别示例,将下面的代码复制后全部粘贴到MATLAB命令行中运行即可。(注意:运行目录应为MATCONVNET文件夹下)


% setup MatConvNet

run matlab/vl_setupnn


% download a pre-trained CNN from the web (只需要第一次运行即可)

urlwrite(

... 'http://www.vlfeat.org/matconvnet/models/imagenet-googlenet-dag.mat', ...

'imagenet-googlenet-dag.mat') ;


% load the pre-trained CNN

net = dagnn.DagNN.loadobj(load('imagenet-googlenet-dag.mat')) ;

net.mode = 'test' ;


% load and preprocess an image

im = imread('peppers.png') ;

im_ = single(im) ;


% note: 0-255 range

im_ = imresize(im_, net.meta.normalization.imageSize(1:2)) ;

im_ = bsxfun(@minus, im_, net.meta.normalization.averageImage) ;


% run the CNN

net.eval({'data', im_}) ;


% obtain the CNN otuput

scores = net.vars(net.getVarIndex('prob')).value ;

scores = squeeze(gather(scores)) ;


% show the classification results

[bestScore, best] = max(scores) ;figure(1) ; clf ; imagesc(im) ;

title(sprintf('%s (%d), score %.3f',

...net.meta.classes.description{best}, best, bestScore)) ;



二、使用已经训练好的神经网络测试自己的图片

上面的例子运行无误后,就可以尝试来测试自己的图片了。

事实上,只需要将PNG格式的图片保存到运行目录下,然后将代码中的图片名改为要测试的名字即可。


我写了一个简单的脚本,可以参考一下:

%ex.m


run  matlab/vl_setupnn

net = dagnn.DagNN.loadobj(load('imagenet-googlenet-dag.mat')) ;
net.mode = 'test' ;
a = zeros(1,1);
while(1)
    a = input('Please input the pngs name.\n','s');
    a = ['photos/',a];
    im = imread(a);
    im_ = single(im) ; % note: 0-255 range
    im_ = imresize(im_, net.meta.normalization.imageSize(1:2)) ;
    im_ = bsxfun(@minus, im_, net.meta.normalization.averageImage) ;
    net.eval({'data', im_}) ;
    scores = net.vars(net.getVarIndex('prob')).value ;
    scores = squeeze(gather(scores)) ;
    [bestScore, best] = max(scores) ;
    figure(1) ; clf ; imagesc(im) ;
    title(sprintf('%s (%d), score %.3f',...
    net.meta.classes.description{best}, best, bestScore)) ;
end


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值