1. 程式人生 > >Windows下用Matlab載入caffemodel做影象分類

Windows下用Matlab載入caffemodel做影象分類

1.編譯caffe的matlab介面

用到了happynear提供的caffe-windows-master,編譯caffe和matlab介面的過程看這裡。編譯好之後,caffe-windows-master\matlab\+caffe\private內的檔案如下:
如果嫌麻煩,可以直接跳過1,下載我編譯好的matlab介面,以及happynear提供的第三方庫的dll(在bin資料夾):
下載地址: 注意:記得將該bin資料夾加入系環境變數;

2.修改matlab\demo\classification_demo.m

將bin資料夾加入環境變數後,修改matlab\demo\classification_demo.m,可以參考我修改的程式碼:
function classification_demo()
close all;
clc;
clear mex;
clear is_valid_handle;
caffe.reset_all();
if isdir('log')
     rmdir('log','s');
end
net_model =  'deploy.prototxt';
net_weights = 'Net_iter_150000.caffemodel';
label_file = 'car_words.txt';
im_path = 'car\';
phase = 'test'; % run with phase test (so that dropout isn't applied)
use_gpu=true;
gpu_id=0;
if exist('../+caffe', 'dir')
  addpath('..')
else
  error('error');
end
% Set caffe mode

if  use_gpu
  caffe.set_mode_gpu();
  caffe.set_device(gpu_id);
else
  caffe.set_mode_cpu();
end

if ~exist(net_weights, 'file')
  error('模型檔案不存在!');
end

% Initialize a network
net = caffe.Net(net_model, net_weights, phase);


Files = dir(im_path);
im_names = cell(1,length(Files)-2);
for j = 3:length(Files)
    im_names{j-2} = Files(j).name;
end

for j = 1:length(im_names)
    im = imread([im_path, im_names{j}]);
   
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

[maxscores, maxlabel] = max(scores);

fid=fopen(label_file,'r','n','UTF-8');
for ii = 1:maxlabel
    tline=fgetl(fid);
end
fclose(fid);

label=tline;
label=strcat(label,':',num2str(maxscores));
imshow(im);
text(50,50,label,'fontsize',20,'color','r');
pause();

% call caffe.reset_all() to reset caffe
 if j == length(im_names)
     caffe.reset_all();
 end
end
% ------------------------------------------------------------------------
function crops_data = prepare_image(im)
d = load('+caffe/imagenet/ilsvrc_2012_mean.mat');
mean_data = d.mean_data;
IMAGE_DIM = 256;
CROPPED_DIM = 224;

% 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);
其中,net_model是模型定義prototxt檔案;net_weights是訓練的到的caffemodel;label_file是標籤檔案。
CROPPED_DIM = 224;
修改成你的模型輸入大小。 (因為不知道怎麼用matlab讀取binaryproto檔案,所以這裡均值檔案用的是ilsvrc2012的均值檔案。之前試過用c++讀取binaryproto檔案並儲存為jpg,但是感覺效果不是很好。)

原來它是測試一張圖片的,這裡改為測試整個資料夾的圖片;

3.結果