The pooling layer 'pool2' is not compatible with MATLAB. Caffe computes the output size as 13 13 256 but MATLAB computes it as 12 12 256. Caffe: Berkeley Vision and Learning Center 2013 BSD: Yes Linux, macOS, Windows: C: Python, MATLAB, C: Yes Under development: Yes Yes Yes: Yes Yes No? No: Chainer: Preferred Networks 2015 BSD: Yes Linux, macOS: Python: Python: No No Yes Yes Yes Yes Yes No Yes No: Deeplearning4j: Skymind engineering team; Deeplearning4j community; originally.
This article was originally posted here: Deep-Learning (CNN) with Scilab – Using Caffe Model by our partner Tan Chin Luh.
You can download the Image Processing & Computer Vision toolbox IPCV here: https://atoms.scilab.org/toolboxes/IPCV
In the previous post on Convolutional Neural Network (CNN), I have been using only Scilab code to build a simple CNN for MNIST data set for handwriting recognition. In this post, I am going to share how to load a Caffe model into Scilab and use it for objects recognition.
This example is going to use the Scilab Python Toolbox together with IPCV module to load the image, pre-process, and feed it into Caffe model to recognition. I will start from the point with the assumption that you already have the Python setup with caffe module working, and Scilab will call the caffe model from its’ environment. On top of that, I will just use the CPU only option for this demo.
Let’s see how it works in video first if you wanted to:
Let’s start to look into the codes.
The codes above will import the python libraries and set the caffe to CPU mode.
This will load the caffe model, the labels, and also the means values for the training dataset which will be subtracted from each layers later on.
Initially the data would be reshape to 3*227*227 for the convenient to assign data from the new image. (This likely is the limitation of Scipython module in copying the data for numpy ndarray, or I’ve find out the proper way yet)
This part is doing the “transformer” job in Python. I personally feel that this part is easier to be understand by using Scilab. First, we read in the image and convert it to 227 by 227 RGB image. This is followed by subtracting means RGB value from the training set from the image RGB value resulting the data from -128 to 127. (A lot of sites mentioned that the range is 0-255, which I disagreed).
This is followed by transposing the image using permute command, and convert from RGB to BGR. (this is how the network sees the image).
In this 3 lines, we will reshape the input blob to 1 x 154587, assign input to it, and then reshape it to 1 x 3 x 227 x 227 so that we could run the network.
Finally, we compute the forward propagation and get the result and show it on the image with detected answer.
A few results shown as below:
Caffe has command line, Python, and MATLAB interfaces for day-to-day usage, interfacing with research code, and rapid prototyping. While Caffe is a C++ library at heart and it exposes a modular interface for development, not every occasion calls for custom compilation. The cmdcaffe, pycaffe, and matcaffe interfaces are here for you.
Command Line
The command line interface – cmdcaffe – is the
caffe tool for model training, scoring, and diagnostics. Run caffe without any arguments for help. This tool and others are found in caffe/build/tools. (The following example calls require completing the LeNet / MNIST example first.)
Caffe Matlab
Training:
caffe train learns models from scratch, resumes learning from saved snapshots, and fine-tunes models to new data and tasks:
For example, you can run:
For a full example of fine-tuning, see examples/finetuning_on_flickr_style, but the training call alone is
Testing:
caffe test scores models by running them in the test phase and reports the net output as its score. The net architecture must be properly defined to output an accuracy measure or loss as its output. The per-batch score is reported and then the grand average is reported last.
Benchmarking:
caffe time benchmarks model execution layer-by-layer through timing and synchronization. This is useful to check system performance and measure relative execution times for models.
Diagnostics:
caffe device_query reports GPU details for reference and checking device ordinals for running on a given device in multi-GPU machines.
Parallelism: the
-gpu flag to the caffe tool can take a comma separated list of IDs to run on multiple GPUs. A solver and net will be instantiated for each GPU so the batch size is effectively multiplied by the number of GPUs. To reproduce single GPU training, reduce the batch size in the network definition accordingly.
Python
The Python interface – pycaffe – is the
caffe module and its scripts in caffe/python. import caffe to load models, do forward and backward, handle IO, visualize networks, and even instrument model solving. All model data, derivatives, and parameters are exposed for reading and writing.
Tutorial IPython notebooks are found in caffe/examples: do
ipython notebook caffe/examples to try them. For developer reference docstrings can be found throughout the code.
Caffe Matlab Download
Compile pycaffe by
make pycaffe .Add the module directory to your $PYTHONPATH by export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH or the like for import caffe .
MATLAB
The MATLAB interface – matcaffe – is the
caffe package in caffe/matlab in which you can integrate Caffe in your Matlab code.
In MatCaffe, you can
An ILSVRC image classification demo is in caffe/matlab/demo/classification_demo.m (you need to download BAIR CaffeNet from Model Zoo to run it).
Build MatCaffe
Build MatCaffe with
make all matcaffe . After that, you may test it using make mattest .
Common issue: if you run into error messages like
libstdc++.so.6:version 'GLIBCXX_3.4.15' not found during make mattest , then it usually means that your Matlab’s runtime libraries do not match your compile-time libraries. You may need to do the following before you start Matlab:
Or the equivalent based on where things are installed on your system, and do
make mattest again to see if the issue is fixed. Note: this issue is sometimes more complicated since during its startup Matlab may overwrite your LD_LIBRARY_PATH environment variable. You can run !ldd ./matlab/+caffe/private/caffe_.mexa64 (the mex extension may differ on your system) in Matlab to see its runtime libraries, and preload your compile-time libraries by exporting them to your LD_PRELOAD environment variable.
After successful building and testing, add this package to Matlab search PATH by starting
matlab from caffe root folder and running the following commands in Matlab command window.
You can save your Matlab search PATH by running
savepath so that you don’t have to run the command above again every time you use MatCaffe.
Use MatCaffe
MatCaffe is very similar to PyCaffe in usage.
Examples below shows detailed usages and assumes you have downloaded BAIR CaffeNet from Model Zoo and started
matlab from caffe root folder.
Set mode and device
Mode and device should always be set BEFORE you create a net or a solver.
![]()
Use CPU:
Use GPU and specify its gpu_id:
Create a network and access its layers and blobs
Create a network:
Or
which creates
net object as
The two
containers.Map objects are useful to find the index of a layer or a blob by its name.
You have access to every blob in this network. To fill blob ‘data’ with all ones:
To multiply all values in blob ‘data’ by 10:
Be aware that since Matlab is 1-indexed and column-major, the usual 4 blob dimensions in Matlab are
[width, height, channels, num] , and width is the fastest dimension. Also be aware that images are in BGR channels. Also, Caffe uses single-precision float data. If your data is not single, set_data will automatically convert it to single.
You also have access to every layer, so you can do network surgery. For example, to multiply conv1 parameters by 10:
Alternatively, you can use
To save the network you just modified:
To get a layer’s type (string):
Forward and backward
Forward pass can be done using
net.forward or net.forward_prefilled . Function net.forward takes in a cell array of N-D arrays containing data of input blob(s) and outputs a cell array containing data from output blob(s). Function net.forward_prefilled uses existing data in input blob(s) during forward pass, takes no input and produces no output. After creating some data for input blobs like data = rand(net.blobs('data').shape); you can run
Or
Backward is similar using
net.backward or net.backward_prefilled and replacing get_data and set_data with get_diff and set_diff . After creating some gradients for output blobs like prob_diff = rand(net.blobs('prob').shape); you can run
Or
However, the backward computation above doesn’t get correct results, because Caffe decides that the network does not need backward computation. To get correct backward results, you need to set
'force_backward: true' in your network prototxt.
After performing forward or backward pass, you can also get the data or diff in internal blobs. For example, to extract pool5 features after forward pass:
Reshape
Assume you want to run 1 image at a time instead of 10:
Then the whole network is reshaped, and now
net.blobs('prob').shape should be [1000 1] ;
![]() TrainingMatlab Caffe Install
Assume you have created training and validation lmdbs following our ImageNET Tutorial, to create a solver and train on ILSVRC 2012 classification dataset:
which creates
solver object as
To train:
Or train for only 1000 iterations (so that you can do something to its net before training more iterations)
To get iteration number:
To get its network:
To resume from a snapshot “your_snapshot.solverstate”:
Input and outputcaffe.io class provides basic input functions load_image and read_mean . For example, to read ILSVRC 2012 mean file (assume you have downloaded imagenet example auxiliary files by running ./data/ilsvrc12/get_ilsvrc_aux.sh ):
To read Caffe’s example image and resize to
[width, height] and suppose we want width = 256; height = 256;
Keep in mind that
width is the fastest dimension and channels are BGR, which is different from the usual way that Matlab stores an image. If you don’t want to use caffe.io.load_image and prefer to load an image by yourself, you can do
Caffe Matlab Install Windows
Also, you may take a look at caffe/matlab/demo/classification_demo.m to see how to prepare input by taking crops from an image.
We show in caffe/matlab/hdf5creation how to read and write HDF5 data with Matlab. We do not provide extra functions for data output as Matlab itself is already quite powerful in output.
Caffe Matlab TutorialClear nets and solversCaffe Matlab Windows
Call
caffe.reset_all() to clear all solvers and stand-alone nets you have created.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |