summaryrefslogtreecommitdiff
path: root/matlab
diff options
context:
space:
mode:
authorSergio Guadarrama <sguada@gmail.com>2014-03-31 14:37:58 -0700
committerSergio Guadarrama <sguada@gmail.com>2014-03-31 14:37:58 -0700
commit0edff71a0117ae93cb0708f2d1eefa76debc542d (patch)
tree58b5d77e0c71d02cf4d02520bc5cefeaba880e52 /matlab
parent6ebf2f1ffafd0b5eb51eaec1fd6a267c4b29781a (diff)
downloadcaffe-0edff71a0117ae93cb0708f2d1eefa76debc542d.tar.gz
caffe-0edff71a0117ae93cb0708f2d1eefa76debc542d.tar.bz2
caffe-0edff71a0117ae93cb0708f2d1eefa76debc542d.zip
Resolved merge conflicts
Diffstat (limited to 'matlab')
-rw-r--r--matlab/caffe/matcaffe.cpp22
-rw-r--r--matlab/caffe/matcaffe_demo.m52
2 files changed, 66 insertions, 8 deletions
diff --git a/matlab/caffe/matcaffe.cpp b/matlab/caffe/matcaffe.cpp
index 7dca6412..c3482eda 100644
--- a/matlab/caffe/matcaffe.cpp
+++ b/matlab/caffe/matcaffe.cpp
@@ -1,3 +1,4 @@
+<<<<<<< HEAD
// Copyright 2014 BVLC and contributors.
//
// matcaffe.cpp provides a wrapper of the caffe::Net class as well as some
@@ -42,7 +43,9 @@ static int init_key = -2;
// If you have multiple images, cat them with cat(4, ...)
//
// The actual forward function. It takes in a cell array of 4-D arrays as
-// input and outputs a cell array.
+
+// input and outputs a cell array.
+
static mxArray* do_forward(const mxArray* const bottom) {
vector<Blob<float>*>& input_blobs = net_->input_blobs();
CHECK_EQ(static_cast<unsigned int>(mxGetDimensions(bottom)[0]),
@@ -67,8 +70,11 @@ static mxArray* do_forward(const mxArray* const bottom) {
const vector<Blob<float>*>& output_blobs = net_->ForwardPrefilled();
mxArray* mx_out = mxCreateCellMatrix(output_blobs.size(), 1);
for (unsigned int i = 0; i < output_blobs.size(); ++i) {
- mxArray* mx_blob = mxCreateNumericMatrix(output_blobs[i]->count(),
- 1, mxSINGLE_CLASS, mxREAL);
+ // internally data is stored as (width, height, channels, num)
+ // where width is the fastest dimension
+ mwSize dims[4] = {output_blobs[i]->width(), output_blobs[i]->height(),
+ output_blobs[i]->channels(), output_blobs[i]->num()};
+ mxArray* mx_blob = mxCreateNumericArray(4, dims, mxSINGLE_CLASS, mxREAL);
mxSetCell(mx_out, i, mx_blob);
float* data_ptr = reinterpret_cast<float*>(mxGetPr(mx_blob));
switch (Caffe::mode()) {
@@ -227,6 +233,14 @@ static void init(MEX_ARGS) {
}
}
+static void end(MEX_ARGS){
+ if (net_) {
+ net_.reset();
+ init_key = -2;
+ LOG(INFO) << "Network deleted, now it needs to init before used again";
+ }
+
+}
static void forward(MEX_ARGS) {
if (nrhs != 1) {
LOG(ERROR) << "Only given " << nrhs << " arguments";
@@ -265,7 +279,7 @@ static handler_registry handlers[] = {
{ "get_weights", get_weights },
{ "get_init_key", get_init_key },
// The end.
- { "END", NULL },
+ { "END", end },
};
diff --git a/matlab/caffe/matcaffe_demo.m b/matlab/caffe/matcaffe_demo.m
index ff27f970..1faeb4be 100644
--- a/matlab/caffe/matcaffe_demo.m
+++ b/matlab/caffe/matcaffe_demo.m
@@ -19,8 +19,42 @@ function [scores, layers] = matcaffe_demo(im, use_gpu)
% im = imread('../../examples/images/cat.jpg');
% scores = matcaffe_demo(im, 1);
% [score, class] = max(scores);
+% Five things to be aware of:
+% caffe uses row-major order
+% matlab uses column-major order
+% caffe uses BGR color channel order
+% matlab uses RGB color channel order
+% images need to have the data mean subtracted
+
+% Data coming in from matlab needs to be in the order
+% [width, height, channels, images]
+% where width is the fastest dimension.
+% Data coming out from matcaffe is
+% [height, width, channels, images]
+% Here is the rough matlab for putting image data into the correct
+% format:
+% % convert from uint8 to single
+% im = single(im);
+% % reshape to a fixed size (e.g., 227x227)
+% im = imresize(im, [IMAGE_DIM IMAGE_DIM], 'bilinear');
+% % permute from RGB to BGR and subtract the data mean (already in BGR)
+% im = im(:,:,[3 2 1]) - data_mean;
+% % flip width and height to make width the fastest dimension
+% im = permute(im, [2 1 3]);
+
+% If you have multiple images, cat them with cat(4, ...)
+
+% 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 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';
@@ -28,6 +62,10 @@ if caffe('is_initialized') == 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
@@ -42,21 +80,27 @@ end
caffe('set_phase_test');
% prepare oversampled input
+% input_data is Height x Width x Channel x Num
tic;
input_data = {prepare_image(im)};
toc;
% do forward pass to get scores
+% scores are now Width x Height x Channels x Num
tic;
scores = caffe('forward', input_data);
toc;
-% average output scores
-scores = reshape(scores{1}, [1000 10]);
-scores = mean(scores, 2);
+% One can permute back Width and Height.
+scores = scores{1};
+size(scores)
+scores = squeeze(scores);
+scores = mean(squeeze,2);
% you can also get network weights by calling
-layers = caffe('get_weights');
+if nargout == 2
+ weights = caffe('get_weights');
+end
% ------------------------------------------------------------------------
function images = prepare_image(im)