欲练此功,必先自攻《Windows玩转Caffe(一):Win10+VS2013+Caffe配置详解【附:多图】》http://blog.csdn.net/u012958854/article/details/78123610
1. 下载Matlab,并安装
本人安装的Matlab2015b,下载地址:http://jingyan.baidu.com/article/eae07827a466a31fed548563.html
2. 修改配置,编译matcaffe
打开CommonSettings.props,编辑要点:
<!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be
set to the root of your Matlab installation. -->
<MatlabSupport>true</MatlabSupport>
<PropertyGroup Condition="'$(MatlabSupport)'=='true'">
<MatlabDir>D:\Program Files\MATLAB\R2015b</MatlabDir>
<LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>
<IncludePath>$(MatlabDir)\extern\include;$(MatlabDir)\toolbox\distcomp\gpu\extern\include;$(IncludePath)</IncludePath>
</PropertyGroup>
(详见http://blog.csdn.net/u012958854/article/details/78123610)
编译matcaffe工程,在caffe-master\Build\x64\Release下将生成matcaffe文件夹。
3. 修改classification_demo.m
打开caffe-master\matlab\democlassification_demo.m文件,编辑要点:
% Add caffe/matlab to you Matlab search PATH to use matcaffe
if exist('../+caffe', 'dir')
addpath('../../Build/x64/Release/matcaffe');
else
error('Please run this demo from caffe/matlab/demo');
end
上面红色字体的路径很眼熟吧,即上步刚生成的,此处相对路径写法而已。
4. 打开Matlab,运行指令
前提,把Matlab当前文件夹设置为caffe-master\matlab\demo目录。
4.1 加载测试图片并显示
>> im =imread('../../examples/images/cat.jpg');%读取图片cat.jpg
>>figure;imshow(im);%显示图片
4.2 调用分类demo,查看最大标签
>>[scores, maxlabel] = classification_demo(im, 1);%获取得分第二个参数0为CPU,1为GPU
时间已过 0.022875 秒。
时间已过 0.190251 秒。
Cleared 0solvers and 1 stand-alone nets
>>maxlabel %查看最大标签
maxlabel =
282
4.3 画出分类得分,即可视化
>>figure;plot(scores);%画出得分情况
axis([0, 1000,0, 0.5]);%坐标轴范围
grid on %显示网格
4.4 根据最大标签解析出图片类别
>> fid =fopen('../../models/bvlc_reference_caffenet/synset_words.txt', 'r');
i=0;
while~feof(fid)
i=i+1;
lin = fgetl(fid);
lin = strtrim(lin);
if(i==maxlabel)
fprintf('the label of %d is%s\n',i,lin)
break
end
end
the label of 282is n02123045 tabby, tabby cat
分类结果tabby,tabby cat正确,至此可视化初探基本完成。
上述四步命令可以写成一个.m文件:
clear
clc
im = imread('../../examples/images/cat.jpg');%读取图片cat.jpg
figure;imshow(im);%显示图片
[scores, maxlabel] = classification_demo(im, 1);%获取得分第二个参数0为CPU,1为GPU
maxlabel %查看最大标签 //y: fprintf('maxlabel = %d\n', maxlabel);
figure;plot(scores);%画出得分情况
axis([0, 1000, 0, 0.5]);%坐标轴范围
grid on %显示网格
fid = fopen('../../models/bvlc_reference_caffenet/synset_words.txt', 'r');
i=0;
while ~feof(fid)
i=i+1;
lin = fgetl(fid);
lin = strtrim(lin);
if(i==maxlabel)
fprintf('the label of %d is %s\n',i,lin)
break
end
end
运行结果:
ps: 标签文件synset_words.txt在我的上传资源中有下载,不知为何资源分不能选择0,起步2,5,8,。。。