matlab + mnist 调用训练好的caffe模型进行手写体识别

最近在看深度学习的一些东西,本意是要用网上下到的caffe模型做人群密度检测的,可是苦于度caffe一点都不熟悉,纵使有再好的模型和现成的网络也不知道怎么用,没办法先从怎么用caffe模型进行手写体识别入手吧。可是!!!!!网上找到的竟然绝大多数都是怎么训练mnist的caffe模型的,至于怎么调用,居然少之又少(难道模型的调用就那么显然吗?苦逼小白真是。。。。痛苦)。陆陆续续弄了两天终于搞定了matlab下的调用,在此分享一下。
声明:初级中的初级教程,高手绕道。
所用软件:matlab2014a
参考:
1、http://blog.csdn.net/zb1165048017/article/details/52447109#reply【【caffe-Windows】mnist实例编译之model的使用-matlab】
2、
http://m.blog.csdn.net/article/details?id=51702686【【caffe-Windows】微软官方caffe之 matlab接口配置】

一、
首先要确定你的caffe对matlab接口进行了配置,如果没有配置好matlab接口,运行时会出现这样的错误
这里写图片描述
好像是说caffe_这个函数没有定义,原因就是caffe编译时没有配置好matlab接口,详细过程可以参考http://m.blog.csdn.net/article/details?id=51702686

二、
接下来的详细步骤可以参考http://blog.csdn.net/zb1165048017/article/details/52447109#reply这篇博文,作者讲解的非常详细,所需要的资源文件,博主也都上传分享,非常感谢这位博主的分享。下面仅对一些对于初学者不太清楚的地方做些简单说明。

博文第四步中:修改classification_demo.m部分


function [scores, maxlabel] = classification_demo(im, use_gpu)
% [scores, maxlabel] = classification_demo(im, use_gpu)
%
% Image classification demo using BVLC CaffeNet.
%
% IMPORTANT: before you run this demo, you should download BVLC CaffeNet
% from Model Zoo (http://caffe.berkeleyvision.org/model_zoo.html)
%
% ****************************************************************************
% For detailed documentation and usage on Caffe's Matlab interface, please
% refer to Caffe Interface Tutorial at
% http://caffe.berkeleyvision.org/tutorial/interfaces.html#matlab
% ****************************************************************************
%
% input
%   im       color image as uint8 HxWx3
%   use_gpu  1 to use the GPU, 0 to use the CPU
%
% output
%   scores   1000-dimensional ILSVRC score vector
%   maxlabel the label of the highest score
%
% You may need to do the following before you start matlab:
%  $ export LD_LIBRARY_PATH=/opt/intel/mkl/lib/intel64:/usr/local/cuda-5.5/lib64
%  $ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6
% Or the equivalent based on where things are installed on your system
%
% Usage:
%  im = imread('../../examples/images/cat.jpg');
%  scores = classification_demo(im, 1);
%  [score, class] = max(scores);
% Five things to be aware of:
%   caffe uses row-major order
%   matlab uses column-major order
%   caffe uses BGR color channel order
%   matlab uses RGB color channel order
%   images need to have the data mean subtracted

% Data coming in from matlab needs to be in the order
%   [width, height, channels, images]
% where width is the fastest dimension.
% Here is the rough matlab for putting image data into the correct
% format in W x H x C with BGR channels:
%   % permute channels from RGB to BGR
%   im_data = im(:, :, [3, 2, 1]);
%   % flip width and height to make width the fastest dimension
%   im_data = permute(im_data, [2, 1, 3]);
%   % convert from uint8 to single
%   im_data = single(im_data);
%   % reshape to a fixed size (e.g., 227x227).
%   im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear');
%   % subtract mean_data (already in W x H x C with BGR channels)
%   im_data = im_data - mean_data;

% If you have multiple images, cat them with cat(4, ...)

% Add caffe/matlab to you Matlab search PATH to use matcaffe
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 = '../../models/bvlc_reference_caffenet/';
% net_model = [model_dir 'deploy.prototxt'];
% net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel'];
model_dir = '../../examples/mnist/';
net_model = [model_dir 'lenet1.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);

% if nargin < 1
%   % For demo purposes we will use the cat image
%   fprintf('using caffe/examples/images/cat.jpg as input image\n');
%   im = imread('../../examples/images/cat.jpg');
% end

% prepare oversampled input
% input_data is Height x Width x Channel x Num
tic;

%运行成功之前老是出现这样的错误:input data/diff size does not match target blob shape, input data/diff size: [ 28 28 1 1 ] vs target blob shape: [ 28 28 1 64 ],
%从错误信息来看应该是输入参数不匹配,可是要怎么改呢?单步调试了半天也不知道怎么搞,后来把这句话给注释掉:im=(im-mean_data)*scale;  竟然就好了,具体什么原因呢?由于我对caffe还了解的很少,具体还不清楚粗,可是蛋疼的是,我运行成功后,把这句话取消注释,居然没问题了,关闭matlab重新打开运行,依然没问题,一万只草泥马在心理个感觉,你懂吗?


    mean_data = caffe.io.read_mean('./../../examples/mnist/mean.binaryproto');  
    scale=0.00390625;  %1/256
    im=double(im); 
    im=(im-mean_data)*scale;  
    input_data = {im};  
  % input_data = {prepare_image(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();

% % ------------------------------------------------------------------------
%后面的这些可以注释掉,不过不注释掉也没有关系,前面input_data部分做了修改,不会调用该函数了
% function crops_data = prepare_image(im)
% % ------------------------------------------------------------------------
% % caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that
% % is already in W x H x C with BGR channels
% d = load('../+caffe/imagenet/ilsvrc_2012_mean.mat');
% mean_data = d.mean_data;
% IMAGE_DIM = 256;
% % CROPPED_DIM = 227;
% CROPPED_DIM = 28;
% 
% % Convert an image returned by Matlab's imread to im_data in caffe's data
% % format: W x H x C with BGR channels
% im_data = im(:, :, [3, 2, 1]);  % permute channels from RGB to BGR
% im_data = permute(im_data, [2, 1, 3]);  % flip width and height
% im_data = single(im_data);  % convert from uint8 to single
% im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear');  % resize im_data
% im_data = im_data - mean_data;  % subtract mean_data (already in W x H x C, BGR)
% 
% % oversample (4 corners, center, and their x-axis flips)
% % crops_data = zeros(CROPPED_DIM, CROPPED_DIM, 3, 10, 'single');
% crops_data = zeros(CROPPED_DIM, CROPPED_DIM, 1, 1, 'single');
% indices = [0 IMAGE_DIM-CROPPED_DIM] + 1;
% n = 1;
% for i = indices
%   for j = indices
%     crops_data(:, :, :, n) = im_data(i:i+CROPPED_DIM-1, j:j+CROPPED_DIM-1, :);
%     crops_data(:, :, :, n+5) = crops_data(end:-1:1, :, :, n);
%     n = n + 1;
%   end
% end
% center = floor(indices(2) / 2) + 1;
% crops_data(:,:,:,5) = ...
%   im_data(center:center+CROPPED_DIM-1,center:center+CROPPED_DIM-1,:);
% crops_data(:,:,:,10) = crops_data(end:-1:1, :, :, 5);

三、
主程序及注释:

clear;clc;close;
im=imread('./binarybmp/4.bmp');

figure;imshow(im);
%输入im要进行转置
[scores,maxlabel]=classification_demo(im',0);%获取得分 第二个参数0:cpu 1:gpu
scores
maxlabel

figure;plot(scores);%画出得分情况
axis([0,10,-0.1,1.5]);%坐标轴范围
grid on;%有网格

fid=fopen('synset_words.txt','r');
i=0;
while ~feof(fid)%test for end-of-file
    i=i+1;
    lin=fgetl(fid);%read line from file
    lin=strtrim(lin);%remove the leading and trailing white-space from string
    if(i==maxlabel)
        fprintf('the maxlabel of %d in label txt is %c\n',i,lin);
        break;
    end
en

四、
运行结果
这里写图片描述
这里写图片描述

除了把0误识别成6之外,其他9个数字均能正确识别。

五、
经验总结:
1.图片务必黑底白字
2.输入im要进行转置[scores,maxlabel]=classification_demo(im’,0),原因是caffe读入图片的格式与matlab读入图片的格式不一样,详细见classification_demo.m中的注释说明:
% Usage:
% im = imread(‘../../examples/images/cat.jpg’);
% scores = classification_demo(im, 1);
% [score, class] = max(scores);
% Five things to be aware of:
% caffe uses row-major order
% matlab uses column-major order
% caffe uses BGR color channel order
% matlab uses RGB color channel order
% images need to have the data mean subtracted

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值