From 0edff71a0117ae93cb0708f2d1eefa76debc542d Mon Sep 17 00:00:00 2001 From: Sergio Guadarrama Date: Mon, 31 Mar 2014 14:37:58 -0700 Subject: Resolved merge conflicts --- matlab/caffe/matcaffe.cpp | 22 +++++++++++++++---- matlab/caffe/matcaffe_demo.m | 52 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 8 deletions(-) (limited to 'matlab') 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*>& input_blobs = net_->input_blobs(); CHECK_EQ(static_cast(mxGetDimensions(bottom)[0]), @@ -67,8 +70,11 @@ static mxArray* do_forward(const mxArray* const bottom) { const vector*>& 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(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) -- cgit v1.2.3