Skip to content

Commit

Permalink
Added matcaffe_init to easy reuse of caffe initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
sguada committed Mar 31, 2014
1 parent 3b7e148 commit 38e3d7f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 55 deletions.
28 changes: 4 additions & 24 deletions matlab/caffe/matcaffe_batch.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
%
% Usage:
% scores = matcaffe_batch({'peppers.png','onion.png'}, 0);
% scores = matcaffe_batch('list_images.txt', 0);
% scores = matcaffe_batch('list_images.txt', 1);
if ischar(list_im)
%Assume it is a file contaning the list of images
filename = list_im;
Expand All @@ -30,33 +30,13 @@
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');
if exist('use_gpu', 'var')
matcaffe_init(use_gpu);
else
caffe('set_mode_cpu');
matcaffe_init();
end

% put into test mode
caffe('set_phase_test');

d = load('ilsvrc_2012_mean');
IMAGE_MEAN = d.image_mean;

Expand Down
37 changes: 6 additions & 31 deletions matlab/caffe/matcaffe_demo.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,19 @@
% The actual forward function. It takes in a cell array of 4-D arrays as
% input and outputs a cell array.

% init caffe network (spews logging info)

% init caffe network (spews logging info)
if exist('use_gpu', 'var')
matcaffe_init(use_gpu);
else
matcaffe_init();
end

if nargin < 1
% For demo purposes we will use the peppers image
im = imread('peppers.png');
end

if caffe('is_initialized') == 0
model_def_file = '../../examples/imagenet/imagenet_deploy.prototxt';
model_file = '../../examples/imagenet/caffe_reference_imagenet_model';
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

fprintf('Done with init\n');

% set to use GPU or CPU
if exist('use_gpu', 'var') && use_gpu
fprintf('Using GPU Mode\n');
caffe('set_mode_gpu');
else
fprintf('Using CPU Mode\n');
caffe('set_mode_cpu');
end

fprintf('Done with set_mode\n');
% put into test mode
caffe('set_phase_test');
fprintf('Done with set_phase_test\n');

% prepare oversampled input
% input_data is Height x Width x Channel x Num
tic;
Expand Down
44 changes: 44 additions & 0 deletions matlab/caffe/matcaffe_init.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function matcaffe_init(use_gpu, model_def_file, model_file)
% matcaffe_init(model_def_file, model_file, use_gpu)
% Initilize matcaffe wrapper

if nargin < 1
% By default use CPU
use_gpu = 0;
end
if nargin < 2 || isempty(model_def_file)
% By default use imagenet_deploy
model_def_file = '../../examples/imagenet/imagenet_deploy.prototxt';
end
if nargin < 3 || isempty(model_file)
% By default use caffe reference model
model_file = '../../examples/imagenet/caffe_reference_imagenet_model';
end


if caffe('is_initialized') == 0
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
fprintf('Done with init\n');

% set to use GPU or CPU
if use_gpu
fprintf('Using GPU Mode\n');
caffe('set_mode_gpu');
else
fprintf('Using CPU Mode\n');
caffe('set_mode_cpu');
end
fprintf('Done with set_mode\n');

% put into test mode
caffe('set_phase_test');
fprintf('Done with set_phase_test\n');

0 comments on commit 38e3d7f

Please sign in to comment.