使用MATLAB加载训练好的caffe模型进行识别分类

在进行下面的实验前,需要先对数据进行训练得到caffemodel,然后再进行分类识别

 

c_demo.m

function [scores, maxlabel] = c_demo(im, use_gpu)

% Add caffe/matlab to you Matlab search PATH to use matcaffe
if exist('/home/k40/caffe-master/matlab/+caffe', 'dir')
  addpath('/home/k40/caffe-master/matlab');
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/wcg/wcg/data/';
model_dir = '/home/k40/caffe-master/models/wcg/wcg/';
net_model = [model_dir 'deploy.prototxt'];
net_weights = [model_dir 'caffe_alexnet_train_iter_5000.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 = {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

[scores, maxlabel] = max(scores);

% call caffe.reset_all() to reset caffe
caffe.reset_all();

% ------------------------------------------------------------------------
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');
%d = load('home/k40/caffe-master/matlab/demo/ilsvrc_2012_mean.mat');
%mean_data = d.mean_data;
mean_data = caffe.io.read_mean('/home/k40/caffe-master/models/wcg/wcg/data/imagenet_mean.binaryproto');
IMAGE_DIM = 256;
CROPPED_DIM = 227;

% 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');
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);


 

shibie.m

M=8;
N=8;
%f = fullfile('/home/k40/caffe-master/matlab/demo/1');
for n = 1 : 1024
    a = strcat('/home/k40/caffe-master/matlab/demo/tupian/2/4/',num2str(n),'.png');
    I=imread(a);
%I = imread('10.png');
    [scores, maxlabel] = c_demo(I, 1);%获得最可能的类别及对应的概率
%caffe.io.read_mean('imagenet_mean.binaryproto');
I = im2double(I);
if maxlabel == 1
    I(1:M,1:N,:) = 0.5 * 0.5 * 0.5;
else I(1:M,1:N,:) = 0;
end
w=strcat('/home/k40/caffe-master/matlab/demo/tupian/2/2_4/',num2str(n),'.png');
imwrite(double(I),w);
end


注意:

1.需要将路径加载到/../..caffe-master/matlab/demo/下运行c_demo.m,否则可能报错。

2.模型准确率直接影响了获得的最大类标与最高得分,因此需要获得准确率较高的模型。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT远征军

谢谢各位鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值