The classification and application of model in "Caffe-windows" Caffe-master matlab

Source: Internet
Author: User

This article describes how to use the well-trained model for image classification in MATLAB. Will take mnist as an example, the main use of Caffe-master\matlab\demo under the CLASSIFICATION_DEMO.M, can refer to my previous blog "Caffe-windows" Caffe-master CLASSFICATION_DEMO.M Ultra-Detailed analysis (http://blog.csdn.net/u011995719/article/details/54135189)

First post the Great God's blog: http://blog.csdn.net/zb1165048017/article/details/52447109

My experiment was to refer to his steps, compared to the previous mnist classification, where the reduced-mean operation was added, so the configuration file needed to be changed, and you could learn how to find the mean file for the training set and subtract the mean value operation.

Pre-Preparation: Download test picture (link: http://pan.baidu.com/s/1o7NySMu Password: bead from: http://blog.csdn.net/zb1165048017/article/details/ 52217772), put under folder Caffe-master\matlab\demo

First step: Calculate the mean value, get the mean file Mean.binaryproto

Create a text document under Caffe-master\examples\mnist, and change the suffix to. bat to copy the following code:

.. \.. \build\x64\release\compute_image_mean.exe-backend=lmdb. /.. /examples\mnist\mnist_train_lmdb Mean.binaryproto

Pause

Double-click to run, you will get Mean.binaryproto under Caffe-master\examples\mnist

(PS: As for mnist data acquisition is not repeated here, please refer to my previous blog: http://blog.csdn.net/u011995719/article/details/54023085)

It is important to note that when converting mnist data to Leveldb, the corresponding changes need to be made to the above code (IMDB to LEVELDB)

Step Two: Training model, need to change 3 files Lenet.prototxt, Lenet_solver.prototxt, Lenet_train_test.prototxt

Because we want to do the minus-mean operation, we want to make changes to Lenet_solver.prototxt and lenet_train_test.prototxt, in order to avoid the previous conflict, copy it here, and then rename it to Lenet_solver_, respectively. Mean.prototxt and Lenet_train_test_mean.prototxt

Lenet_solver_mean.prototxt more lenet_solver.prototxt changes are as follows:

NET: ". /.. /examples/mnist/lenet_train_test_mean.prototxt "

Lenet_train_test_mean.prototxt more lenet_train_test.prototxt changes are as follows:

Phase:train

}

Transform_param {

Mean_file: ". /.. /examples/mnist/mean.binaryproto "

scale:0.00390625

}

Phase:test

}

Transform_param {

Mean_file: ". /.. /examples/mnist/mean.binaryproto "

scale:0.00390625

}

Please note the format of your data, Lmdb or LEVELDB.

Change the lenet.prototxt at the end, change the following:

Input_param {shape: {dim:1 dim:1 dim:28 dim:28}}

Here's a question of why 64 is a bug? Or what? No, I don't understand.

After you have changed the above three files, you can create a. bat file under Caffe-master\examples\mnist training, specifically

After training, there will be Lenet_iter_10000.caffemodel, this is the back in MATLAB to use the model

Step three: Create a label file Mnist_synset_words.txt

Create a text document under Caffe-master\matlab\demo to copy the following code:

0

1

2

3

4

5

6

7

8

9

Save, in order to distinguish the different label files, here changed the file name is Mnist_synset_words, so in the later MATLAB program read time need to make corresponding changes

Fourth step: Edit two matlab files, one is CLASSIFICATION_DEMO.M modification, one is to call Classification_demo's main function

Create M file under Caffe-master\matlab\demo, save as MNIST_TEST.M specific code as follows:

Clear
Clc
Close all
Im=imread ([' binarybmp/5.bmp ']); % Read Picture
Figure;imshow (IM); % Show Pictures
[Scores, Maxlabel] = MNIST_CF (im ', 1); % get score The second parameter 0 is cpu,1 for the GPU
Scores;
Maxlabel;

Figure;plot (scores); % draw a score situation
Axis ([0, 10,-0.1, 0.5]); % Axis Range
Grid on% with mesh

FID = fopen (' mnist_synset_words.txt ', ' R ');
i=0;
While ~feof (FID)% while ~feof indicates that the loop continues if the end of the file is not read
i=i+1;
Lin = Fgetl (FID); % Fgetl reads a line from a file that is already open, and discards the end of the newline character
Lin = Strtrim (Lin); % Strtrim remove spaces from the beginning of a string or cell
if (I==maxlabel)
fprintf (' The Maxlabel of%d in label txt is%s\n ', I,lin)
Break
End
End

Create M file under Caffe-master\matlab\demo, save as MNIST_CF.M

The specific code is as follows:

function [Scores, Maxlabel] = MNIST_CF (IM, USE_GPU)
% here is the add path to ensure that Caffe-master\matlab\+caffe can be found
if exist ('.. /+caffe ', ' dir ')
Addpath ('.. /..‘);
Else
Error (' Please run the demo from Caffe/matlab/demo ');
End
% Set CPU or GPU
If exist (' Use_gpu ', ' var ') && Use_gpu
Caffe.set_mode_gpu ();
gpu_id = 0; % We'll use the first GPU on this demo
Caffe.set_device (gpu_id);
Else
Caffe.set_mode_cpu ();
End


Model_dir = '. /.. /examples/mnist/';
Net_model = [Model_dir ' Lenet.prototxt '];
net_weights = [Model_dir ' Lenet_iter_10000.caffemodel '];
Phase = ' Test '; % indicates that the network status is test and prevents the use of dropout
If ~exist (net_weights, ' file ')% detects whether the model exists, does not exist then an error alert
Error (' Download caffenet from the Model Zoo before you run this demo ');
End



NET = Caffe.net (Net_model, net_weights, phase); % Initialize Network
% Oversample
Mean_data = Caffe.io.read_mean ('.. /.. /examples/mnist/mean.binaryproto '); %
scale = 0.00390625;
im = double (IM);
im = (im-mean_data) *scale;
Input_data = {IM};


Scores = Net.forward (Input_data); % input data into the network, forward propagation, score, scores is a cell tuple


scores = Scores{1}; % scores is the 1000*10 matrix 10 is the corresponding 10 crop picture
Scores = mean (scores, 2); % averaging of 10 crop
[~, Maxlabel] = max (scores); % again find the largest one


% Reset Caffe
Caffe.reset_all ();

Fifth step: Run MNIST_TEST.M to

PS: It is easy to get the problem of incorrect path, the error first to see if the path is incorrect. Next is here I changed a lot of filenames, so the corresponding file inside also to make changes, it is best to use CTRL + C Ctrl + V method, lest the writing error.

The classification and application of model in "Caffe-windows" Caffe-master matlab

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.