DeepLearning(基於caffe)實戰專案(4)--Matlab測試訓練好的model
阿新 • • 發佈:2019-02-03
好了,現在我們已經訓練好自己的model,如何用這個model去預測我們待測樣本的標籤,就成了一個需要解決的問題。
這裡我們主要說的是Matlab程式呼叫訓練好的model來預測標籤。但是在說這個之前,有必要說說如何用caffe檔案中的classification.exe
程式碼如下:
>>e: >>cd E:\Python\Caffe\Build\x64\Release >>compute_image_mean.exe ../../../examples/mnist/Deal_Data/mnist_train_lmdb ../../../examples/mnist/mean.binaryproto --backend=lmdb >>classification.exe ../../../examples/mnist/lenet_deploy.prototxt ../../../examples/mnist/lenet_iter_10000.caffemodel ../../../examples/mnist/mean.binaryproto ../../../examples/mnist/MatlabCode/label.txt ../../../examples/mnist/MatlabCode/testimage/12.png >>pause
現在我們開始說如何用matlab呼叫訓練好的model來測試(還是以mnist為例,)
Step1:準備階段
1、這裡我們需要將編譯好的“+caffe檔案”複製到你的matlab程式所在的資料夾下
2、將待測圖片也複製到該資料夾下
3、新建一個TXT檔案(名為label.txt),寫0,1,2,3,4,5,6,7,8,9(為了將輸出的值對映到標籤時用的)
4、根據lenet_train_test.prototxt,改寫所需要的lenet_deploy。
name: "LeNet"
layer {
name: "data"
type: "Input"
top: "data"
input_param { shape: { dim: 1 dim: 1 dim: 28 dim: 28 } }
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "pool2"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "prob"
type: "Softmax"
bottom: "ip2"
top: "prob"
}
Step2:核心函式(ClassificateFuction.m)
function [scores, maxlabel] = ClassificateFuction(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 = '../';
net_model = [model_dir 'lenet_deploy.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('../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();
Step3:主函式(mnist_test.m)
%該程式為使用caffe中mnist訓練出來的model,來預測待測圖片的標籤
% ---------------------------------------自寫程式--------------------------------------
clc;clear all;close all;
im=imread('/testimage/14.png');
figure;imshow(im);%輸入im要進行轉置[scores,maxlabel]=classification_demo(im',0); %獲取得分 第二個引數0:cpu 1:gpu
figure;plot(scores); %畫出得分情況
axis([0,10,-0.1,1.5]);%座標軸範圍
grid on; %有網格
fid=fopen('label.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 labelof %d in label txt is %c\n',i,lin);
break;
end
end
Step4:run起來(mnist_test.m)
bingo!結果出來了,效果還不錯,基本上預測正確了
這麼跑一通,可以呼叫caffemodel了,特別注意的是:大部分錯誤在於路徑問題,所以一定要頭腦清楚,寫對路徑。