Skip to content

Commit

Permalink
Move demo to demo/ and check weights file existence
Browse files Browse the repository at this point in the history
Move all Matlab demo to caffe/matlab/demo. Since we want the user to add
caffe/matlab to Matlab search PATH, we don't want to mess it up with too
many files

Check if CaffeNet is already downloaded in classification demo.
  • Loading branch information
ronghanghu committed May 29, 2015
1 parent d3d7d07 commit 0f13fee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
10 changes: 6 additions & 4 deletions docs/tutorial/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ In MatCaffe, you can
* Run for a certain number of iterations and give back control to Matlab
* Intermingle arbitrary Matlab code to with gradient steps

An ILSVRC image classification demo is in caffe/matlab/classification_demo.m
An ILSVRC image classification demo is in caffe/matlab/demo/classification_demo.m

### 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 `libstdc++.so.6:version 'GLIBCXX_3.4.15' not found` during `make mattest`, then it means that your Matlab's runtime libraries does not match your compile-time libraries. You may need to do the following before you start matlab:
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:

export LD_LIBRARY_PATH=/opt/intel/mkl/lib/intel64:/usr/local/cuda/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, 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` in Matlab to see its runtime libraries, and preload your compile-time libraries by exporting them to your `LD_PRELOAD` environment variable.
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.

Expand Down Expand Up @@ -270,7 +270,9 @@ To read Caffe's example image and resize to `[width, height]` and suppose we wan
im_data = permute(im_data, [2, 1, 3]); % permute width and height
im_data = single(im_data); % convert to single precision

We do not provide extra functions for data output as Matlab itself is already quite powerful in output.
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.

#### Clear nets and solvers

Expand Down
27 changes: 20 additions & 7 deletions matlab/classification_demo.m → matlab/demo/classification_demo.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function [scores, maxlabel] = classification_demo(im, use_gpu)
% scores = classification_demo(im, use_gpu)
% [scores, maxlabel] = classification_demo(im, use_gpu)
%
% Image classification demo using BVLC CaffeNet.
%
Expand All @@ -18,14 +18,15 @@
%
% output
% scores 1000-dimensional ILSVRC score vector
% maxlabel the label of the highest score
%
% You may need to do the following before you start matlab:
% $ 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');
% im = imread('../../examples/images/cat.jpg');
% scores = classification_demo(im, 1);
% [score, class] = max(scores);
% Five things to be aware of:
Expand All @@ -51,6 +52,13 @@

% If you have multiple images, cat them with cat(4, ...)

% Add caffe/matlab to you 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();
Expand All @@ -62,16 +70,21 @@

% Initialize the network using BVLC CaffeNet for image classification
% Weights (parameter) file needs to be downloaded from Model Zoo.
model_dir = '../models/bvlc_reference_caffenet/';
model_dir = '../../models/bvlc_reference_caffenet/';
net_model = [model_dir 'deploy.prototxt'];
net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel'];
phase = 'test';
phase = 'test'; % run with phase test (so that dropout isn't applied)
if ~exist(net_weights, 'file')
error('Please download CaffeNet from Model Zoo before you run this demo');
end

% Initialize a network
net = caffe.Net(net_model, net_weights, phase);

if nargin < 1
% For demo purposes we will use the cat image
fprintf('using ../examples/images/cat.jpg as input image\n');
im = imread('../examples/images/cat.jpg');
fprintf('using caffe/examples/images/cat.jpg as input image\n');
im = imread('../../examples/images/cat.jpg');
end

% prepare oversampled input
Expand Down Expand Up @@ -102,7 +115,7 @@
% ------------------------------------------------------------------------
function images = prepare_image(im)
% ------------------------------------------------------------------------
d = load('+caffe/imagenet/ilsvrc_2012_mean.mat');
d = load('../+caffe/imagenet/ilsvrc_2012_mean.mat');
IMAGE_MEAN = d.image_mean;
IMAGE_DIM = 256;
CROPPED_DIM = 227;
Expand Down

0 comments on commit 0f13fee

Please sign in to comment.