matconvnet加载模型错误:MATLAB错误使用 load 无法读取 MAT 文件

问题场景:

想使用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)) ;
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值