【caffe-Windows】mnist实例编译之model的使用-matlab
2016-09-06 10:12
441 查看
前言
针对上一个caffe文章留下的matlab手写数字识别的问题,感谢caffe中文社区的 @ghgzh 的提示,原文请看:caffe中文社区第一步
手写图片的制作方法我就不说了,直接把我自己画的几个数字放到云盘先:三通道图像以及转换所需代码:链接:http://pan.baidu.com/s/1gfqeCAR 密码:88kk
转换后的灰度图像:链接:http://pan.baidu.com/s/1eSyohsY 密码:zwum
【注意】如果你的手写数字是黑字白底,最好反转成白字黑底,毕竟mnist的数据集就是白字黑底
第二步
创建标签,从零开始,synset_words.txt如下:0 1 2 3 4 5 6 7 8 9
第三步
书写调用classfication demo的test文件:【重点】上一篇文章所留问题就是在这里解决的,在图片输入网络之前也就是net.forwarad之前,必须经过转置,原理未知,目前是感觉跟matlab和opencv读取图片的方法有关,使用caffe.io.loadimage读取是按照BGR读取,并且进行了旋转,所以使用matlab时候必须进行同等处理,详细原因在后面分析代码再说~~~
clear clc close all im=imread('./binarybmp/5.bmp'); figure;imshow(im);%显示图片 [scores, maxlabel] = classification_demo(im', 0);%获取得分第二个参数0为CPU,1为GPU scores maxlabel figure;plot(scores);%画出得分情况 axis([0, 10, -0.1, 0.5]);%坐标轴范围 grid on %有网格 fid = fopen('synset_words.txt', 'r'); i=0; while ~feof(fid) i=i+1; lin = fgetl(fid); lin = strtrim(lin); if(i==maxlabel) fprintf('the maxlabel of %d in label txt is %s\n',i,lin) break end end
第四步
修改classification_demo如下:function [scores, maxlabel] = classification_demo(im, use_gpu) if exist('../../+caffe', 'dir') addpath('../..'); else error('Please run this demo from caffe/matlab/demo'); end % Set caffe mode if exist('use_gpu', 'var') && use_gpu caffe.set_mode_gpu(); gpu_id = 0; % we will use the first gpu in this demo caffe.set_device(gpu_id); else caffe.set_mode_cpu(); end % Initialize the network using BVLC CaffeNet for image classification % Weights (parameter) file needs to be downloaded from Model Zoo. model_dir = '../../../examples/mnist/'; net_model = [model_dir 'lenet.prototxt']; net_weights = [model_dir 'lenet_iter_10000.caffemodel']; phase = 'test'; % run with phase test (so that dropout isn't applied) if ~exist(net_weights, 'file') error('Please download CaffeNet from Model Zoo before you run this demo'); end % Initialize a network net = caffe.Net(net_model, net_weights, phase); % prepare oversampled input % input_data is Height x Width x Channel x Num tic; mean_data = caffe.io.read_mean('../../../examples/mnist/mean.binaryproto'); scale=0.00390625; im=double(im); im=(im-mean_data)*scale; input_data = {im}; toc; % do forward pass to get scores % scores are now Channels x Num, where Channels == 1000 tic; % The net forward function. It takes in a cell array of N-D arrays % (where N == 4 here) containing data of input blob(s) and outputs a cell % array containing data from output blob(s) scores = net.forward(input_data); toc; scores = scores{1}; scores = mean(scores, 2); % take average scores over 10 crops [~, maxlabel] = max(scores); % call caffe.reset_all() to reset caffe caffe.reset_all();说明一下修改的部分,
①模型文件:调用的模型依旧是要注意deploy.prototxt与train_test .prototxt的区别,在mnist实例中,deploy.prototxt就是lenet.prototxt,后者就是lenet_train_test.prototxt,它俩分别是前者在识别期间调用,一个是在训练及测试阶段调用。区别在于,deploy文件前两层并无指定输入数据和测试数据的层,最后一层中deploy使用的softmax的“pro”,输出的是可能标签的概率,而train_test使用的是softmax的“loss”,用于指示每次训练的损失。
②去掉了在cat 的那个classification_demo中的图片预处理中需要进行crops_data处理对图片进行分块的部分。在手写数字中的预处理,处理在第三步中比较重要的一步图像转置外,在classification_demo中需要进行零均值以及缩放两步骤。(注,按照上一篇的训练文件,我们已经更改了预处理步骤,添加了均值计算这一过程,在lenet_train_test1第一二层的 transform_param中可以看到)
这里附带一下我的deploy和train_test文件
deploy.prototxt文件:链接:http://pan.baidu.com/s/1pLPGf03 密码:cqjy
train_test.prototxt文件:链接:http://pan.baidu.com/s/1kVM03DP 密码:l83w
第五步
差不多结束了,运行程序吧。我的测试结果如下:相关文章推荐
- 【caffe-Windows】mnist实例编译之model的使用-classification
- 【caffe-Windows】mnist实例编译之model的使用-classification
- 接上篇 【caffe-Windows】mnist实例编译之model的使用-classification(2016-08-16)
- 【caffe-Windows】mnist实例编译之model的生成
- 3【caffe-Windows】cifar实例编译之model的使用
- 【caffe-Windows】mnist实例编译之model的生成
- 【caffe-Windows】cifar实例编译之model的使用
- 【caffe-Windows_3】cifar实例编译之model的使用
- Microsoft caffe(caffe-windows) cifar实例编译之model的使用
- 【caffe-Windows】cifar实例编译之model的使用
- 【caffe-Windows】mnist实例编译之model的生成(2016-06-27)(原文评论可参考)
- 【caffe-Windows】cifar实例编译之model的使用
- 【caffe-Windows】cifar实例编译之model的使用
- mnist实例编译之model的使用-matlab
- 如何快糙好猛地在Windows下编译CAFFE并使用其matlab和python接口
- 2.【caffe-Windows】cifar实例编译之model的生成
- 如何快糙好猛地在Windows下编译CAFFE并使用其matlab和python接口
- 如何快糙好猛地在Windows下编译CAFFE并使用其matlab和python接口
- 【caffe-Windows】cifar实例编译之model的生成
- 如何快糙好猛地在Windows下编译CAFFE并使用其matlab和python接口