Deep Learning with Caffe & Matlab

Setup Caffe for use with Matlab

Install Caffe here

To use Caffe in Matlab, other than the installation steps in the above link, you need to compile Caffe for Matlab purpose:

  • First, uncomment the line containing MATLAB_DIR in the Makefile.config in the /caffe folder. Update the path to the bin folder of the Matlab installed in the machine. The path is usually in /usr/local/MATLAB/R2015b/.
  • Compile the caffe for Matlab again, by using the following command from the caffe folder in terminal:

$make matcaffe

Classifying images in Matlab using Caffe functions

Caffe is shipped with an Matlab example for classifying images in the /caffe/matlab/demo folder.This file is called: classification_demo.m.

Run Matlab. Sometime there is a error in linking the libstdc library, hence run the following from terminal before running Matlab:

$ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6

The example can be run by two commands in Matlab:

im = imread(‘../../examples/images/cat.jpg’);
scores = classification_demo(im, 0);  % Use 1 instead of 0 if you want to run on GPU

The code return scores for 1000 classes in the ImageNet dataset. You can run and debug the code to have a better insight into it.

 

 

 

Deep Learning with Caffe & Python: (T2) CNN 1/3

Convolutional Neural Network (CNN) is a special type of feed-forward neural network dealing with images. One fundamental advantage of CNNs is the use of shared weight in convolutional layers, which means that the same filter (weights bank) is used for each pixel in the layer; this both reduces memory footprint and improves performance.

In this post, I will show how to test, understand and train this type of network.

I. CLASSIFYING images using pre-trained models

Caffe has already been packed with a number of examples, including one classification ipython notebook using CNN. This is a great example to start with. From terminal, navigate to the caffe installed path (usually in ~/caffe), run ipython notebook by typing:

$ipython notebook

This command calls ipython server to run and open up the web browser for working with ipython files. In the web browser just opened up, navigate to /caffe/examples/ and open file 00-classification.ipynb. Note that a ipython file is divided into multiple blocks so that you can run through each to debug the code. Use Shift+Enter to run through current block and move to the next block when the current finishes. Other shortcut keys can be found here.