summaryrefslogtreecommitdiff
path: root/matlab
diff options
context:
space:
mode:
authorSergio Guadarrama <sguada@gmail.com>2014-03-31 14:40:38 -0700
committerSergio Guadarrama <sguada@gmail.com>2014-03-31 14:40:38 -0700
commit8f0580b836d8054837729a6995dbaa72205da267 (patch)
tree1b88fc5fed534b0d2d9eac421a3429ba8a2a96ae /matlab
parentc8249e7f880297976ff3faade106620b4e968206 (diff)
downloadcaffe-8f0580b836d8054837729a6995dbaa72205da267.tar.gz
caffe-8f0580b836d8054837729a6995dbaa72205da267.tar.bz2
caffe-8f0580b836d8054837729a6995dbaa72205da267.zip
Changed matcaffe_demo to return maxlabel
Diffstat (limited to 'matlab')
-rw-r--r--matlab/caffe/matcaffe_batch.m90
-rw-r--r--matlab/caffe/matcaffe_demo.m8
-rw-r--r--matlab/caffe/prepare_batch.m41
-rw-r--r--matlab/caffe/print_cell.m42
-rw-r--r--matlab/caffe/read_cell.m21
5 files changed, 196 insertions, 6 deletions
diff --git a/matlab/caffe/matcaffe_batch.m b/matlab/caffe/matcaffe_batch.m
new file mode 100644
index 00000000..2e0e07f1
--- /dev/null
+++ b/matlab/caffe/matcaffe_batch.m
@@ -0,0 +1,90 @@
+function [scores,list_im] = matcaffe_batch(list_im, use_gpu)
+% scores = matcaffe_batch(list_im, use_gpu)
+%
+% Demo of the matlab wrapper using the ILSVRC network.
+%
+% input
+% list_im list of images files
+% use_gpu 1 to use the GPU, 0 to use the CPU
+%
+% output
+% scores 1000 x num_images ILSVRC output vector
+%
+% 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
+%
+% Usage:
+% scores = matcaffe_batch({'peppers.png','onion.png'}, 0);
+% scores = matcaffe_batch('list_images.txt', 0);
+if ischar(list_im)
+ %Assume it is a file contaning the list of images
+ filename = list_im;
+ list_im = read_cell(filename);
+end
+batch_size = 10;
+dim = 1000;
+disp(list_im)
+if mod(length(list_im),batch_size)
+ warning(['Assuming batches of ' num2str(batch_size) ' images rest will be filled with zeros'])
+end
+
+if caffe('is_initialized') == 0
+ model_def_file = '../../examples/imagenet_deploy.prototxt';
+ model_file = '../../models/alexnet_train_iter_470000';
+ if exist(model_file, 'file') == 0
+ % NOTE: you'll have to get the pre-trained ILSVRC network
+ error('You need a network model file');
+ end
+ if ~exist(model_def_file,'file')
+ % NOTE: you'll have to get network definition
+ error('You need the network prototxt definition');
+ end
+ caffe('init', model_def_file, model_file);
+end
+
+
+% init caffe network (spews logging info)
+
+% set to use GPU or CPU
+if exist('use_gpu', 'var') && use_gpu
+ caffe('set_mode_gpu');
+else
+ caffe('set_mode_cpu');
+end
+
+% put into test mode
+caffe('set_phase_test');
+
+d = load('ilsvrc_2012_mean');
+IMAGE_MEAN = d.image_mean;
+
+% prepare input
+
+num_images = length(list_im);
+scores = zeros(dim,num_images,'single');
+num_batches = ceil(length(list_im)/batch_size)
+initic=tic;
+for bb = 1 : num_batches
+ batchtic = tic;
+ range = 1+batch_size*(bb-1):min(num_images,batch_size * bb);
+ tic
+ input_data = prepare_batch(list_im(range),IMAGE_MEAN,batch_size);
+ toc, tic
+ fprintf('Batch %d out of %d %.2f%% Complete ETA %.2f seconds\n',...
+ bb,num_batches,bb/num_batches*100,toc(initic)/bb*(num_batches-bb));
+ output_data = caffe('forward', {input_data});
+ toc
+ output_data = squeeze(output_data{1});
+ scores(:,range) = output_data(:,mod(range-1,batch_size)+1);
+ toc(batchtic)
+end
+toc(initic);
+
+if exist('filename', 'var')
+ save([filename '.probs.mat'],'list_im','scores','-v7.3');
+end
+
+
+
diff --git a/matlab/caffe/matcaffe_demo.m b/matlab/caffe/matcaffe_demo.m
index 7d4e81b9..7296eb1b 100644
--- a/matlab/caffe/matcaffe_demo.m
+++ b/matlab/caffe/matcaffe_demo.m
@@ -1,4 +1,4 @@
-function [scores, layers] = matcaffe_demo(im, use_gpu)
+function [scores, maxlabel] = matcaffe_demo(im, use_gpu)
% scores = matcaffe_demo(im, use_gpu)
%
% Demo of the matlab wrapper using the ILSVRC network.
@@ -89,16 +89,12 @@ tic;
scores = caffe('forward', input_data);
toc;
-% One can permute back Width and Height.
scores = scores{1};
size(scores)
scores = squeeze(scores);
scores = mean(scores,2);
-% you can also get network weights by calling
-if nargout == 2
- weights = caffe('get_weights');
-end
+[~,maxlabel] = max(scores);
% ------------------------------------------------------------------------
function images = prepare_image(im)
diff --git a/matlab/caffe/prepare_batch.m b/matlab/caffe/prepare_batch.m
new file mode 100644
index 00000000..345c8eb5
--- /dev/null
+++ b/matlab/caffe/prepare_batch.m
@@ -0,0 +1,41 @@
+% ------------------------------------------------------------------------
+function images = prepare_batch(image_files,IMAGE_MEAN,batch_size)
+% ------------------------------------------------------------------------
+if nargin < 2
+ d = load('ilsvrc_2012_mean');
+ IMAGE_MEAN = d.image_mean;
+end
+num_images = length(image_files);
+if nargin < 3
+ batch_size = num_images;
+end
+
+IMAGE_DIM = 256;
+CROPPED_DIM = 227;
+indices = [0 IMAGE_DIM-CROPPED_DIM] + 1;
+center = floor(indices(2) / 2)+1;
+
+num_images = length(image_files);
+images = zeros(CROPPED_DIM,CROPPED_DIM,3,batch_size,'single');
+
+parfor i=1:num_images
+ % read file
+ fprintf('%c Preparing %s\n',13,image_files{i});
+ try
+ im = imread(image_files{i});
+ % resize to fixed input size
+ im = single(im);
+ im = imresize(im, [IMAGE_DIM IMAGE_DIM], 'bilinear');
+ % Transform GRAY to RGB
+ if size(im,3) == 1
+ im = cat(3,im,im,im);
+ end
+ % permute from RGB to BGR (IMAGE_MEAN is already BGR)
+ im = im(:,:,[3 2 1]) - IMAGE_MEAN;
+ % Crop the center of the image
+ images(:,:,:,i) = permute(im(center:center+CROPPED_DIM-1,...
+ center:center+CROPPED_DIM-1,:),[2 1 3]);
+ catch
+ warning('Problems with file',image_files{i});
+ end
+end \ No newline at end of file
diff --git a/matlab/caffe/print_cell.m b/matlab/caffe/print_cell.m
new file mode 100644
index 00000000..864340d4
--- /dev/null
+++ b/matlab/caffe/print_cell.m
@@ -0,0 +1,42 @@
+function res=print_cell(input,file,linesep,cellsep)
+assert(iscell(input),'The input should be a cell')
+if nargin < 4
+ cellsep = '\t';
+end
+if nargin < 3
+ linesep = '\n';
+end
+if exist('file','var') && ~isempty(file)
+ %%
+ fid = fopen(file,'w');
+ for l=1:length(input)
+ if iscell(input{l})
+ for i=1:length(input{l})
+ fprintf(fid,['%s' cellsep],input{l}{i});
+ end
+ fprintf(fid,linesep);
+ else
+ if size(input,2) > 1
+ for i=1:size(input,2)
+ fprintf(fid,'%s ',input{l,i});
+ end
+ fprintf(fid,linesep);
+ else
+ fprintf(fid,['%s' linesep],input{l});
+ end
+ end
+ end
+ fclose(fid);
+else
+ res = '';
+ for l=1:length(input)
+ if iscell(input{l})
+ for i=1:length(input{l})
+ res = [res sprintf([cellsep{1} '%s' cellsep{2}],input{l}{i})];
+ end
+ res = [res sprintf(linesep)];
+ else
+ res = [res sprintf(['%s' linesep],input{l}(:))];
+ end
+ end
+end \ No newline at end of file
diff --git a/matlab/caffe/read_cell.m b/matlab/caffe/read_cell.m
new file mode 100644
index 00000000..19831167
--- /dev/null
+++ b/matlab/caffe/read_cell.m
@@ -0,0 +1,21 @@
+function res=read_cell(filename,linesep,cellsep)
+if nargin < 2, linesep='\n'; end
+if nargin < 3, cellsep = '\t'; end
+if exist(filename,'file')
+ fid = fopen(filename);
+else
+ % Assume that filename is either a file ide or a string
+ fid = filename;
+end
+
+fileLines = textscan(fid,'%s','delimiter',linesep,'BufSize',100000);
+
+fileLines = fileLines{1};
+
+if regexp(fileLines{1},cellsep,'once')
+ fileLines = regexprep(fileLines,['^' cellsep '|' cellsep '$'],'');
+ res = regexp(fileLines,cellsep,'split');
+ res = cell2matcell(res);
+else
+ res = fileLines;
+end