Debugging Caffe__caffe in MATLAB

Source: Internet
Author: User
debugging Caffe in MATLABCaffe itself is written in C + +, Cuda language. When debugging the model and parameters, it is difficult to get real-time feedback on the weights of the current training according to the running log and snapshot, and it is difficult to catch the bugs in the algorithm.
MATLAB is very suitable for the algorithm design, rapid iteration of the tool, only need to do a small amount of work can be compiled to write complex algorithms, debugging is very convenient, located in the workspace variables can be printed at any time, whether it is one-dimensional, two-dimensional or three-dimensional data, can be visually displayed, thus conducive to positioning algorithm design problems, Reduce debugging time.

There are two kinds of wrapper:python and Matlab in Caffe. Python is an open source tool that users can use without paying, and the disadvantage is that the syntax is not flexible enough, especially the algorithm description, compared with commercial software. MATLAB supports almost everything you know about matrix transformations, numerical computations, stochastic processes, probability theory, optimization, adaptive filtering, image processing, and neural network algorithms.

Below describes how to use the MATLAB debugging Caffe. This article assumes that the operating system is Ubuntu 14.04.1 64bit.

1. Install MATLAB r2014a

Can download here (Http://yunpan.taobao.com/s/ZFLGQjNABU, extract code: DXBXMJ)

Installation steps are similar to Windows, not tables. Install to ~/MATLAB/,~/.BASHRC Add export path=~/matlab/bin: $PATH

2. Install Caffe

Reference step: http://caffe.berkeleyvision.org/install_apt.html. Other OS please refer to http://caffe.berkeleyvision.org/installation.html.

If you want to compile your own dependencies, you can download Caffe all dependent packages (HTTP://YUNPAN.TAOBAO.COM/S/1I1TXCPYSK3, extract code: YUQZM1)

3. Compile Matcaffe

Modify Makefile.config, add this sentence:

Matlab_dir: = ~/matlab

After

Make Matcaffe

Generated matlab/+caffe/private/caffe_.mex64, can be directly called by MATLAB.

4. Running MATLAB Example

In the command line, configure the Caffe to run the necessary environment variables (otherwise Matcaffe will fail to run), input matlab&amp, so that the Matlab window started.

In the MATLAB command window, perform the following steps.

>> CD caffe_root_directory/

Switch to the Caffe root directory.

>> Addpath ('./matlab/+caffe/private ');

Add Matcaffe module location path to the MATLAB search path, easy to load.

>> CD matlab/demo/

Cut to the demo directory.

>> im = Imread ('.. /.. /examples/images/cat.jpg ');

Read a test picture.

>> figure;imshow (IM);

Pop up a window showing the cat's Test picture as follows:



>> [Scores, Maxlabel] = Classification_demo (IM, 1);

Elapsed is 0.533388 seconds.

Elapsed is 0.511420 seconds.

Cleared 0 Solvers and 1 stand-alone Nets

Run the category demo program. The result of the taxonomy is returned to the Scores,maxlabel two workspace variables.

>> Maxlabel

Maxlabel =

282

Note that the maximum classification probability of the tag number is 282, look for imagenet tags, corresponding to the n02123045 tabby, tabby cat (Data/ilsvrc2012/synset_words.txt)

>> Figure;plot (scores);

>> axis ([0, 999,-0.1, 0.5]);

>> grid on

Print scores, one-dimensional image is as follows:



The probability of this picture being divided into category No. 282 is 0.2985.

Here we just run a simple demo, and then analyze the contents of the CLASSIFICATION_DEMO.M file.

function [Scores, Maxlabel] = Classification_demo (IM, USE_GPU)% [scores, Maxlabel] = Classification_demo (IM, USE_GPU)% make
An example of image classification with BVLC Caffenet is important: Before running, you should first download BVLC caffenet training weights from Model Zoo (http://caffe.berkeleyvision.org/model_zoo.html) %% ****************************************************************************% for detailed documentation and Usage on Caffe ' s Matlab interface, as per% refer to Caffe interface at% Tutorial   L/interfaces.html#matlab% ****************************************************************************% input% Im color image as uint8 HxWx3% Use_gpu 1 To use the GPU, 0 to use the CPU% output% scores 1000-dimension Al ILSVRC score vector% maxlabel the label of the highest score% you could need to did the following you start M Atlab:% $ export ld_library_path=/opt/intel/mkl/lib/intel64:/usr/local/cuda-5.5/lib64% $ export ld_preload=/usr/lib/ X86_64-linux-gnu/libstdc++.so.6% Or the EQUivalent based on where things are installed on your system% Usage:% im = Imread ('. /..
/examples/images/cat.jpg ');
% scores = Classification_demo (IM, 1);
% [Score, class] = max (scores); % Five things to is aware of:% Caffe uses row-major order% MATLAB uses Column-major order% Caffe uses BGR color C Hannel order% MATLAB uses RGB color channel order% images need to have the data mean subtracted% data coming in FR
Om matlab needs to the "order%" [width, height, channels, images]% where width is the fastest dimension. % here are the rough matlab for putting image data to the correct% format in W x H x C with BGR channels:% Permute
Channels from RGB to BGR% Im_data = IM (:,:, [3, 2, 1]);
% flip width and height to make width the fastest dimension% Im_data = Permute (Im_data, [2, 1, 3]);
% convert from Uint8 to single% Im_data = single (Im_data);
% reshape to a fixed size (e.g., 227x227). % Im_data = imresize (Im_data, [Image_dim IMAGE_dim], ' bilinear ');

%% subtract mean_data (already in W x H x C with BGR channels)% Im_data = Im_data-mean_data;

% If You are have multiple images, cat them with cat (4, ...) % Add Caffe/matlab to your MATLAB search PATH to use Matcaffe 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'll use the the ' the ' the ' the ' the ' the ' the ' the ' 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 download
Ed from Model Zoo. Model_dir = '.. /..    /models/bvlc_reference_caffenet/';              % Model Directory Net_model = [model_dir ' Deploy.prototxt '];   % Model description file, note is Deploy.prototxt, does not contain data layers net_weights = [model_dir ' Bvlc_reference_caffenet.caffemodel ']; % model weights files that need to be downloaded beforehand to here phase = ' Test '; % run with phase test (so, dropout isN ' t applied)% is only classified, do not do training if ~exist (net_weights, ' file ') error (' Please download caffenet from Model Zoo before your run
This demo ');   End% Initialize a network net = Caffe.net (Net_model, net_weights, phase); % Initialize Network if Nargin < 1 for demo purposes we'll use the cat image fprintf (' using Caffe/examples/images/cat.jpg a
  s input image\n '); im = Imread ('.. /..    /examples/images/cat.jpg ');
% get input image end% prepare oversampled input% input_data is Height x Width x Channel x Num tic;         Input_data = {prepare_image (IM)};

% image redundancy processing TOC;
% do forward 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 C      ontaining data from output blob (s) scores = Net.forward (Input_data);

% classification, get scores TOC;
scores = Scores{1};  Scores = mean (scores, 2);  % takes the average value of all classification results [~, Maxlabel] = max (scores); % find the maximum probability corresponding to the labelNumber% call Caffe.reset_all () to 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 ');
Mean_data = D.mean_data;
Image_dim = 256;

Cropped_dim = 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 (:,:, [3, 2, 1]);  % Permute channels from RGB to BGR im_data = Permute (Im_data, [2, 1, 3]);  % Flip width and Height im_data = single (Im_data);  % convert from uint8 to single im_data = Imresize (Im_data, [Image_dim Image_dim], ' bilinear ');  % Resize Im_data im_data = im_data-mean_data; % subtract Mean_data (already in W x H x C, BGR)% oversample (4 corners, center, and their x-aXIs flips) Crops_data = zeros (Cropped_dim, Cropped_dim, 3, ' single ');
indices = [0 Image_dim-cropped_dim] + 1;
n = 1;
    For i = indices for j = Indices Crops_data (:,:,:, N) = Im_data (i:i+cropped_dim-1, j:j+cropped_dim-1,:);
    Crops_data (:,:,:, n+5) = Crops_data (End:-1:1,:,:, N);
  n = n + 1;
End center = floor (Indices (2)/2) + 1; Crops_data (:,:,:, 5) = ... im_data (center:center+cropped_dim-1,center:center+cropped_dim-1,:); Crops_data (:,:,:, 10) = Crops_data (End:-1:1,:,:, 5);

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.