【caffe-Windows】mnist实例编译之model的使用-matlab
前言
針對(duì)上一個(gè)caffe文章留下的matlab手寫數(shù)字識(shí)別的問題,感謝caffe中文社區(qū)的 @ghgzh 的提示,原文請(qǐng)看:caffe中文社區(qū)
第一步
手寫圖片的制作方法我就不說了,直接把我自己畫的幾個(gè)數(shù)字放到云盤先:
三通道圖像以及轉(zhuǎn)換所需代碼:鏈接:http://pan.baidu.com/s/1gfqeCAR 密碼:88kk
轉(zhuǎn)換后的灰度圖像:鏈接:http://pan.baidu.com/s/1eSyohsY 密碼:zwum
【注意】如果你的手寫數(shù)字是黑字白底,最好反轉(zhuǎn)成白字黑底,畢竟mnist的數(shù)據(jù)集就是白字黑底
第二步
創(chuàng)建標(biāo)簽,從零開始,synset_words.txt如下:
0 1 2 3 4 5 6 7 8 9第三步
書寫調(diào)用classfication demo的test文件:
【重點(diǎn)】上一篇文章所留問題就是在這里解決的,在圖片輸入網(wǎng)絡(luò)之前也就是net.forwarad之前,必須經(jīng)過轉(zhuǎn)置,原理未知,目前是感覺跟matlab和opencv讀取圖片的方法有關(guān),使用caffe.io.loadimage讀取是按照BGR讀取,并且進(jìn)行了旋轉(zhuǎn),所以使用matlab時(shí)候必須進(jìn)行同等處理,詳細(xì)原因在后面分析代碼再說~~~
clear clc close all im=imread('./binarybmp/5.bmp'); figure;imshow(im);%顯示圖片 [scores, maxlabel] = classification_demo(im', 0);%獲取得分第二個(gè)參數(shù)0為CPU,1為GPU scores maxlabelfigure;plot(scores);%畫出得分情況 axis([0, 10, -0.1, 0.5]);%坐標(biāo)軸范圍 grid on %有網(wǎng)格fid = fopen('synset_words.txt', 'r'); i=0; while ~feof(fid)i=i+1;lin = fgetl(fid);lin = strtrim(lin);if(i==maxlabel)fprintf('the maxlabel of %d in label txt is %s\n',i,lin)breakend end第四步
修改classification_demo如下:
function [scores, maxlabel] = classification_demo(im, use_gpu)if exist('../../+caffe', 'dir')addpath('../..'); elseerror('Please run this demo from caffe/matlab/demo'); end% Set caffe mode if exist('use_gpu', 'var') && use_gpucaffe.set_mode_gpu();gpu_id = 0; % we will use the first gpu in this democaffe.set_device(gpu_id); elsecaffe.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 = '../../../examples/mnist/'; net_model = [model_dir 'lenet.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('../../../examples/mnist/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();說明一下修改的部分,①模型文件:調(diào)用的模型依舊是要注意deploy.prototxt與train_test .prototxt的區(qū)別,在mnist實(shí)例中,deploy.prototxt就是lenet.prototxt,后者就是lenet_train_test.prototxt,它倆分別是前者在識(shí)別期間調(diào)用,一個(gè)是在訓(xùn)練及測(cè)試階段調(diào)用。區(qū)別在于,deploy文件前兩層并無指定輸入數(shù)據(jù)和測(cè)試數(shù)據(jù)的層,最后一層中deploy使用的softmax的“pro”,輸出的是可能標(biāo)簽的概率,而train_test使用的是softmax的“l(fā)oss”,用于指示每次訓(xùn)練的損失。
②去掉了在cat 的那個(gè)classification_demo中的圖片預(yù)處理中需要進(jìn)行crops_data處理對(duì)圖片進(jìn)行分塊的部分。在手寫數(shù)字中的預(yù)處理,處理在第三步中比較重要的一步圖像轉(zhuǎn)置外,在classification_demo中需要進(jìn)行零均值以及縮放兩步驟。(注,按照上一篇的訓(xùn)練文件,我們已經(jīng)更改了預(yù)處理步驟,添加了均值計(jì)算這一過程,在lenet_train_test1第一二層的?transform_param中可以看到)
這里附帶一下我的deploy和train_test文件
deploy.prototxt文件:鏈接:http://pan.baidu.com/s/1pLPGf03 密碼:cqjy
train_test.prototxt文件:鏈接:http://pan.baidu.com/s/1kVM03DP 密碼:l83w
第五步
差不多結(jié)束了,運(yùn)行程序吧。我的測(cè)試結(jié)果如下:
總結(jié)
以上是生活随笔為你收集整理的【caffe-Windows】mnist实例编译之model的使用-matlab的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 卡卡贷代还信用卡财务处理中稳吗?多久到账
- 下一篇: 【caffe-Windows】训练自己数