问题场景:
想使用Matlab进行神经网络的建立,下载MatConvNet工具箱后,运行官网的Quick Start程序出现标题所示load错误。
用load加载训练好的.mat模型时出现错误。
问题描述
matconvnet官网上的程序:
% setup MatConvNet
run matlab/vl_setupnn
% download a pre-trained CNN from the web (needed once)
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)) ;
---------运行结果-----------------------------------------
>>>错误使用 load
无法读取 MAT 文件 C:\Users\Focus\Desktop\NN\imagenet-googlenet-dag.mat。不是二进制 MAT 文件。请尝试执行 load -ASCII 以便以文本形式读取。
出错 scratch1 (line 10)
net = dagnn.DagNN.loadobj(load('imagenet-googlenet-dag.mat')) ;
原因分析:
urlwrite函数并没有将.mat模型下载到本地,而是将网页的HTML信息保存到了imagenet-googlenet-dag.mat文件中
查看imagenet-googlenet-dag.mat的大小可以看出文件太小了。
或者用在matlab交互界面用edit(‘imagenet-googlenet-dag.mat’)查看文件情况也可以看出:
解决方案:
到MatConvNet官网直接下载模型放入工作目录中使用
如直接在官网下载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)) ;