Search code examples
matlabdeep-learningcaffemnistmatcaffe

Deploy and use LeNet on Matlab using MatCaffe


I am having issue with MatCaffe. I trained LeNet using my own dataset (2 classification, 0 or 1) in python successfully and trying to deploy it on Matlab now. Net architecture is from caffe/examples/mnist/lenet.prototxt. All the input images I fed into the net always return 1. (i tried using both positive and negative images from training).

Below is my code:

deployNet = 'lenet_deploy.prototxt';
caffeModel = 'weight.caffemodel';
caffe.set_mode_cpu();
net = caffe.Net(deployNet, caffeModel, 'test');
net.blobs('data').reshape([28 28 1 1]);
net.reshape();

patch_data = imread('cropped.jpg'); % already in greyscale
patch_data = imresize(patch_data, [28 28],'bilinear');
imshow(patch_data)

input_data = {patch_data};
scores = net.forward(input_data);

highest = max(scores{1});
disp(i);
disp(highest);

The highest always return 1 even for negative image. I tried deploying it on python and it works great. I am guessing issue with the way I pre-process the input.


Solution

  • Found out the issue. I forgotten to multiply the image with the training scale and transpose the width and height since Matlab is 1-indexed and column-major, the usual 4 blob dimensions in Matlab are [width, height, channels, num], and width is the fastest dimension. So just add in 2 more line of codes:

    deployNet = 'lenet_deploy.prototxt';
    caffeModel = 'weight.caffemodel';
    caffe.set_mode_cpu();
    net = caffe.Net(deployNet, caffeModel, 'test');
    net.blobs('data').reshape([28 28 1 1]);
    net.reshape();
    
    patch = imread('cropped.jpg'); % already in greyscale
    patch = single(patch) * 0.00390625; % multiply with scale
    patch = permute(patch, [2,1,3]); %permute width and height
    input_data = {patch};
    scores = net.forward(input_data);
    
    highest = scores{1};