DeepLearning(基于caffe)实战项目(4)--Matlab测试训练好的model

       好了,现在我们已经训练好自己的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了,特别注意的是:大部分错误在于路径问题,所以一定要头脑清楚,写对路径。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值