# Caffe in Matlab (Matcaffe) simple tutorial

If you have Caffe compiled for Matlab (which you can do using `make matcaffe`

) then you can start following this simple tutorial.

First you have to make sure Matlab can see `caffe/matlab`

folder which would be something like `/home/yourusername/caffe/matlab`

you can do that from your Matlab script using `addpath('path/to/cafffe/matlab');`

Then, you need to set Caffe mode to either CPU (defualt if not set) or GPU mode. If you have Caffe compiled for GPU use it , it would be faster (unless you have a small GPU with limited memory then you would choose CPU for large models that will not git in the GPU)

caffe.set_mode_gpu();

gpu_id = 0; % we will use the first gpu

caffe.set_device(gpu_id);

% or you can use

caffe.set_mode_cpu();

If you have a trained model and you would like to test it, first you need to define your network like:

net_weights = [‘path/to/yourmodel.caffemodel’];

net_model = [‘path/to/your_deploy.prototxt’];

net = caffe.Net(net_model, net_weights, ‘test’);

make sure your deploy file has the same layer names as the actual model , Matlab will not give you an error ! it will just ignore the weights of that layer. next step is to prepare your image. you will need the prepare_image function which is available inside the caffehome/matlab/demo/classification_demo.m. Please go there to get the most updated version or if you can not get it from the source , here it is

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_data = 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, 10, ‘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

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

Be aware that this code uses the imagenet mean file also it performs 10 crops . check your model to see if you need to modify that. next you should prepare your image and pass it to the network.

input_data = {prepare_image(im)};

scores = net.forward(input_data);

scores = scores{1};

scores = mean(scores, 2); % take average scores over 10 crops

If you need to check the weights or the outputs of certain layers you can always do that in Matlab. Be ware that weights are the network weights and they are independent of the input, while the output is the network activation for this particular input you have just passed to the network.

weights_FC6 = net.params(‘fc6’,1).get_data();

output_FC6 = net.blobs(‘fc6’).get_data();

If you think there is something missing in this tutorial please comment with your request and i will add it to the tutorial as soon as possible.

I have a problem using Caffe with Matlab. I had initially installed Caffe-Master, but later realized that there is no matcaffe.cpp file, thus I searched internet for alternatives, then found that I had to install Caffe-Dev. Now I am able to successfully run ‘make matcaffe’ during building the Caffe, and it successfully generates the file caffe.mexa64 for my Linux 14.04 64-bit machine.

Now I want to use this mex file in matlab in a way as shown ‘caffe.reset_all();’, but it always throws error like Undefined variable “caffe” or class “caffe.reset_all”.

I am suspecting that there could be some path setting problem, I have even set the PATH of the mex file with ‘export PATH=$PATH:~/csb/linux_packages/caffe-dev/matlab/caffe

Any help in resolving this issue of using Caffe in Matlab would be very much appreciable.

Thanks and regards,

Chandra Shaker Balure

Matlab should be able to see caffe/matlab folder which would be something like /home/yourusername/caffe/matlab

you can do that from your Matlab script using addpath(‘path/to/cafffe/matlab’);

it seems against intuition because this folder does not contain the MEX file , but that is how it works.

I updated the article to include this information.

thanks for your comment and please let me know if this does not work

Hello,

I have want to use Caffe with Matlab for Windows. So I have the windows version from here

https://github.com/BVLC/caffe/tree/windows

I have followed the steps you have mentioned above. But I am getting error

Undefined function or variable ‘caffe_’.

Error in caffe.set_mode_gpu (line 5)

caffe_(‘set_mode_gpu’);

Please help me to solve this.

Thanks

Regards

Adnan

I got the same problem, anyone solved it?

Unfortunately, I do not have much experience with windows. Sorry about that. Just make sure you had the Mex files in place and the appropriate folder in path.

You should build the source code using script/build_win.bat. Then go to build and copy all file to matlab/+caffe/private. It works for me on x64 bit Window machine

This tutorial has a classic flaw, it starts really simply “first, turn on the computer using the big red button”, and then lulls in the reader that it’s a gentle ride “with the mouse move the icon around the screen”, then hits you “after you have recompiled your kernel…”.

If you’re just starting out how are you going to have a trained model, or know what a deploy file is?

Hi Alan, sorry about the confusion. When I wrote this tutorial I was thinking of someone who is familiar with Caffe but new to the Matlab interface of caffe , someone like that would know what is a trained model and deploy file. I agree with you that it would be more useful if it assumed that the reader is not familiar with Caffe , I will try to work on that by adding more details to this article or write a new article that introduce Caffe and refer it in this one. Many thanks

Thanks! Very helpful!

Spot on with this write-up, I truly suppose this web site wants way more consideration. I’ll in all probability be once more to read far more, thanks for that info.