dz整站网站建设,棋牌推广如何精准引流,园林景观设计公司需要什么资质,做网站实训报告总结前言
针对上一个caffe文章留下的matlab手写数字识别的问题#xff0c;感谢caffe中文社区的 ghgzh 的提示#xff0c;原文请看#xff1a;caffe中文社区
第一步
手写图片的制作方法我就不说了#xff0c;直接把我自己画的几个数字放到云盘先#xff1a;
三通道图像以及…前言
针对上一个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
imimread(./binarybmp/5.bmp);
figure;imshow(im);%显示图片
[scores, maxlabel] classification_demo(im, 0);%获取得分第二个参数0为CPU1为GPU
scores
maxlabelfigure;plot(scores);%画出得分情况
axis([0, 10, -0.1, 0.5]);%坐标轴范围
grid on %有网格fid fopen(synset_words.txt, r);
i0;
while ~feof(fid)ii1;lin fgetl(fid);lin strtrim(lin);if(imaxlabel)fprintf(the maxlabel of %d in label txt is %s\n,i,lin)breakend
end 第四步 修改classification_demo如下 function [scores, maxlabel] classification_demo(im, use_gpu)if exist(../../caffe, dir)addpath(../..);
elseerror(Please run this demo from caffe/matlab/demo);
end% Set caffe mode
if exist(use_gpu, var) use_gpucaffe.set_mode_gpu();gpu_id 0; % we will use the first gpu in this democaffe.set_device(gpu_id);
elsecaffe.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 isnt 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);scale0.00390625;imdouble(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
第五步
差不多结束了运行程序吧。我的测试结果如下