Move demo to demo/ and check weights file existence
authorRonghang Hu <huronghang@hotmail.com>
Thu, 28 May 2015 10:33:54 +0000 (18:33 +0800)
committerRonghang Hu <huronghang@hotmail.com>
Fri, 29 May 2015 05:21:26 +0000 (13:21 +0800)
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.

docs/tutorial/interfaces.md
matlab/demo/classification_demo.m [moved from matlab/classification_demo.m with 84% similarity]

index a57e173..a59a410 100644 (file)
@@ -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.
 
@@ -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
 
similarity index 84%
rename from matlab/classification_demo.m
rename to matlab/demo/classification_demo.m
index 43a7bf6..4535824 100644 (file)
@@ -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.
 %
@@ -18,6 +18,7 @@ function [scores, maxlabel] = classification_demo(im, use_gpu)
 %
 % 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
@@ -25,7 +26,7 @@ function [scores, maxlabel] = classification_demo(im, use_gpu)
 % 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:
@@ -51,6 +52,13 @@ function [scores, maxlabel] = classification_demo(im, use_gpu)
 
 % 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();
@@ -62,16 +70,21 @@ end
 
 % 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
@@ -102,7 +115,7 @@ caffe.reset_all();
 % ------------------------------------------------------------------------
 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;