"Caffe-windows" Caffe-master CLASSFICATION_DEMO.M ultra-detailed analysis

Source: Internet
Author: User

CLASSIFICATION_DEMO.M is a good learning material, understand this code, you can use the training model in MATLAB to classify the input image, and I also learned in the Oversample instance, finally understand the data enhancement is a what.

CAFFE-MASTER\MATLAB\DEMO\CLASSIFICATION_DEMO.M This demo is for the Imagenet image classification (1000 classes), mainly call the training model, the input image classification, output classification results.

To run this demo, the things you need are:

1. Model description File: Deploy.prototxt (this file is original, no tube)

2. Model itself: Bvlc_reference_caffenet.caffemodel

3. Tag file: Used to describe what the 1000 classes were

4. Mean file: That is, the training sample of the mean file (not read the relevant information, temporarily considered to be a training sample of the mean file), Ilsvrc_2012_mean.mat, the file already exists in Caffe-master\matlab\+caffe\imagenet

From the above we can know that when we want to apply classification to their own data set, we need to change the corresponding 4 points.

Matalb the configuration of the interface and the operation of the demo, please see the previous blog: "caffe-windows" Caffe-master Matlab interface configuration (http://blog.csdn.net/u011995719/article/ details/53994570)

Now analyze the CLASSIFICATION_DEMO.M and then apply it to your project.

First, let's look at the input and output:

Input

Im color image as Uint8hxwx3

Use_gpu 1 To use the GPU, 0 touse the CPU

Output

Scores 1000-dimensional Ilsvrcscore vector

Maxlabel the label of the highest score

Input has two parameters, one is a picture, for a single graph (to test multiple, with for resolution), the other is the CPU and GPU selection.

The output also has two parameters, one is the score, that is, the picture corresponds to a probability distribution of all categories, the other is the highest score corresponding to the category, which is related to the label file.

The following directly paste the source code for analysis (deletion of the original comments, recommended against the original note):

% 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 = '. /.. /models/bvlc_reference_caffenet/';% Model folder path
Net_model = [Model_dir ' Deploy.prototxt ']; The path of the% model description file
net_weights = [Model_dir ' Bvlc_reference_caffenet.caffemodel ']; The path of the% model
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


% if classification_demo the input parameter of this function is less than 1, that is, there is no input parameter, then the caffe/examples/images/cat.jpg image is used by default.
If Nargin < 1
fprintf (' using caffe/examples/images/cat.jpg as input image\n ');
im = Imread ('.. /.. /examples/images/cat.jpg ');
End




The% focus comes! Since the data in Caffe is in the order of BGR, and MATLAB is the order of RGB, the input image needs to be transformed
% here, a prepare_image function is used to convert RGB to BGR, and the input image is resize, crop operation and the reduction of the mean value
% run to the bottom to see the Prepare_image function
Tic
Input_data = {prepare_image (IM)};
ToC


Tic


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


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 ();


% ------------------------------------------------------------------------
function crops_data = Prepare_image (IM)
% ------------------------------------------------------------------------
% Caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that
% is already in W x H x C with BGR channels
d = Load ('..    /+caffe/imagenet/ilsvrc_2012_mean.mat '); % read mean file Channel order is already BGR
Mean_data = D.mean_data;
Image_dim = 256; The size of the% resize
Cropped_dim = 227; % crop size, because the model input is 227*227, so eventually to get a 227*227


% 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]); % RGB turned into BGR, Im_data channel sequence is already BGR
Im_data = Permute (Im_data, [2, 1, 3]); % transpose of the input image, long and wide swap
Im_data = single (Im_data); % data format converted to UINT8 type
Im_data = Imresize (Im_data, [Image_dim Image_dim], ' bilinear '); % resize to image_dim size of the input image using bilinear interpolation
Im_data = Im_data-mean_data; % and subtract the mean value


% Oversample (4 corners, center, and their x-axis flips)
% oversample is the data enhancement 2012 Alex and others put forward a technology, here is in the picture (here 256*256) 4 Corners and the center of the Intercept 5
% 227*227 image, then mirror the 5 images on the x-axis, and get 10 227*227 pictures as input to the model
Crops_data = Zeros (Cropped_dim, Cropped_dim, 3, ' single ');
indices = [0 Image_dim-cropped_dim] + 1;
n = 1;
For i = The indices% for loop only intercepts 4 corners of the
For j = Indices
Crops_data (:,:,:, N) = Im_data (i:i+cropped_dim-1, j:j+cropped_dim-1,:); % intercept a picture of a corner of the 227*227
Crops_data (:,:,:, n+5) = Crops_data (End:-1:1,:,:, N); % The image is mirrored on the x-axis, +5 is the result of a total interception of 5 227*227
n = n + 1; % intercept 5, and then mirror, a picture into 10 pictures to enter
End
End
Center = Floor (Indices (2)/2) + 1; % Capture Center picture, then mirror
Crops_data (:,:,:, 5) = ...
Im_data (center:center+cropped_dim-1,center:center+cropped_dim-1,:);
Crops_data (:,:,:, ten) = Crops_data (End:-1:1,:,:, 5);

Ps:

1. You can see the following model description file, Deploy.prototxt, in this line "Input_param {shape: {dim:10 dim:3 dim:227 dim:227}}" Why the input is 10*3*22 7*227. At first I thought this 10 is batch number, but after reading the Classification_demo finally understand this 10 is how to come!

Interested students can open caffe-master\examples\mnist under the Lenet.prototxt, see this line Input_param {shape: {dim:64 dim:1 dim:28 dim:28}}, for What is 64?? Ask for answers

For reference, the Cifar10_quick.prototxt under Caffe-master\examples\cifar10 is Input_param {shape: {dim:1 dim:3 dim:32 dim:32}}

2. Find scores when there is a 1000*10 matrix, that is, each crop picture is a separate input, input is not a 10*3*227*227 data, but 10 3*227*227 data, and then get 10 groups of scores, and then ask for the average! (How old feel a kind of cheating suspicion!! So to use this code to your own data set, remember to keep an eye on scores!!

"Caffe-windows" Caffe-master CLASSFICATION_DEMO.M ultra-detailed analysis

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.