Re: How can I test an image using pretrained model in matlab?

290 views
Skip to first unread message
Message has been deleted

Ahmed Ibrahim

unread,
Mar 18, 2016, 11:49:32 AM3/18/16
to Caffe Users
try this 
inputimg=imread('your image file name');
% you should resize your image id needed by your model

net
= caffe.Net('network_deploy.prototxt', 'trainedmodel.caffemodel', 'test');
input_data
= {prepare_image(inputimg)};
scores
= net.forward(input_data);

I copied the definition of prepare_image from inside the file "classification_demo.m" in the matlab demo folder then modify it to fit my problem
you have to modify that to fit your problem

function crops_data = prepare_image(im)
% ------------------------------------------------------------------------
% caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that
% is already in W x H x C with BGR channels
d
= load('../+caffe/imagenet/ilsvrc_2012_mean.mat');
mean_data
= d.mean_data;
IMAGE_DIM
= 256;
CROPPED_DIM
= 227;


% Convert an image returned by Matlab's imread to im_data in caffe's data
% format: W x H x C with BGR channels
im_data
= im(:, :, [3, 2, 1]);  % permute channels from RGB to BGR
im_data
= permute(im_data, [2, 1, 3]);  % flip width and height
im_data
= single(im_data);  % convert from uint8 to single
im_data
= imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear');  % resize im_data
im_data
= im_data - mean_data;  % subtract mean_data (already in W x H x C, BGR)


% oversample (4 corners, center, and their x-axis flips)
crops_data
= zeros(CROPPED_DIM, CROPPED_DIM, 3, 10, 'single');
indices
= [0 IMAGE_DIM-CROPPED_DIM] + 1;
n
= 1;
for i = indices
 
for j = indices
    crops_data
(:, :, :, n) = im_data(i:i+CROPPED_DIM-1, j:j+CROPPED_DIM-1, :);
    crops_data
(:, :, :, n+5) = crops_data(end:-1:1, :, :, n);
    n
= n + 1;
 
end
end
center
= floor(indices(2) / 2) + 1;
crops_data
(:,:,:,5) = ...
  im_data
(center:center+CROPPED_DIM-1,center:center+CROPPED_DIM-1,:);
crops_data
(:,:,:,10) = crops_data(end:-1:1, :, :, 5);





On Thursday, March 17, 2016 at 5:19:32 AM UTC-4, monjoy saha wrote:
Hi please tell me how can I test an image using pretrained model in matlab.
Message has been deleted

Nam Vo

unread,
Mar 18, 2016, 2:06:23 PM3/18/16
to Caffe Users
build caffe matlab interface (how? look it up on caffe website) and add that to matlab path

On Friday, March 18, 2016 at 11:56:35 AM UTC-4, monjoy saha wrote:
When I am running "net = caffe.Net('network_deploy.prototxt', 'trainedmodel.caffemodel', 'test');" in matlab command windows I am getting Undefined variable "caffe" or class "caffe.Net". error.


Please suggest.

Christos Apostolopoulos

unread,
Mar 18, 2016, 6:34:35 PM3/18/16
to Caffe Users
1) after you make caffe run "make matcaffe" and "make mattest". then open matlab from the caffe root and run "addpath ./matlab" in matlab.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages