2

I successfully trained my Caffe net on the mnist database following http://caffe.berkeleyvision.org/gathered/examples/mnist.html

Now I want to test the network with my own images using the Matlab wrapper.

Therefore in "matcaffe.m" im loading the file "lenet.prototxt" which is not used for training but which seems to be suited for testing. It is referencing a input size of 28 x 28 pixels:

name: "LeNet"
input: "data"
input_dim: 64
input_dim: 1
input_dim: 28
input_dim: 28
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"

Therefore I adapted the "prepare_image" function in "matcaffe.m" accordingly. It now looks like this:

% ------------------------------------------------------------------------
function images = prepare_image(im)
IMAGE_DIM = 28;
% resize to fixed input size
im = rgb2gray(im);
im = imresize(im, [IMAGE_DIM IMAGE_DIM], 'bilinear');
im = single(im);
images = zeros(1,1,IMAGE_DIM,IMAGE_DIM);
images(1,1,:,:) = im;
images = single(images);
%-------------------------------------------------------------

This converts the input image to a [1 x 1 x 28 x 28], 4dim, grayscale image. But still Matlab is complaining:

Error using caffe
MatCaffe input size does not match the input size of the
network
Error in matcaffe_myModel_mnist (line 76)
scores = caffe('forward', input_data);

Does somebody have experience with testing the trained mnist net on his own data?

mcExchange
  • 6,154
  • 12
  • 57
  • 103

2 Answers2

3

The reason you are having that error (input size does not match) is that the network prototxt is expecting a batch of 64 images. The lines

input_dim: 64
input_dim: 1
input_dim: 28
input_dim: 28

Mean that the network is expecting a batch of 64 grayscale, 28 by 28 images. If you keep all your MATLAB code the same and change that first line to

input_dim: 1

Your problem should go away.

mprat
  • 2,451
  • 15
  • 33
  • I now that tutorial. I already successfully trained on the mnist database. My problem is how to use the trained net in combination with my own image data and the matlab wrapper "matcaffe.m" – mcExchange May 19 '15 at 16:54
  • Ah, sorry, I misunderstood. I will update my answer. – mprat May 19 '15 at 18:03
  • However there still seems to be a bug in the way I input my data, because the accuracy on mnist test data is about 10 % (which is random guessing). Am I missing some more preprocessing? Does anyone have tried this? (I tried scaling the input data by 256 but it didn't help either – mcExchange May 20 '15 at 13:46
  • That was only true for the imagenet example. However in the case of mnist, there is no mean subtraction and the images are 1 channel only. As I said the data is only scaled (by 1/256) during training. But using that scaling didn't help – mcExchange May 20 '15 at 14:32
  • Yes. the training was quite fast (about 3 min). But test accuracy was 0.9907 (97%) on the validation data. Did you train mnist yourself? – mcExchange May 20 '15 at 22:17
  • 1
    Got it. I forgot to transpose the input image. Now everything is working fine. I will write the solution in a separate answer for a better overview. Still thanks a lot for your help – mcExchange May 21 '15 at 13:29
3

Finally I found the full solution: This how to predict a digit of your own input image using the matcaffe.m (Matlab wrapper) for Caffe

  1. In "matcaffe.m": One has to reference the file "caffe-master/examples/mnist/lenet.prototxt"
  2. Adapt the file "lenet.prototxt" as pointed out by mprat: Change the entry input_dim to input_dim: 1
  3. Use the follwing adaptation to the subfunction "prepare_image" in matcaffe.m:

(Input can be an rgb image of any size)

function image = prepare_image(im)

IMAGE_DIM = 28;

% If input image is too big , is rgb and of type uint8:
% -> resize to fixed input size, single channel, type float

im = rgb2gray(im);
im = imresize(im, [IMAGE_DIM IMAGE_DIM], 'bilinear');
im = single(im);

% Caffe needs a 4D input matrix which has single precision
% Data has to be scaled by 1/256 = 0.00390625 (like during training)
% In the second last line the image is beeing transposed!
images = zeros(1,1,IMAGE_DIM,IMAGE_DIM);
images(1,1,:,:) = 0.00390625*im';
images = single(images);
mcExchange
  • 6,154
  • 12
  • 57
  • 103
  • You need to correct "images" to "image" and change option for "im = rgb2gray(im)" since the curren code is applied for color image only. – Atena Nguyen Jun 21 '17 at 05:43
  • It does run in my computer I have to change the format image(1,1,:, :) to image(:, :, 1, 1) in order to make it run. However, I get very poor results with the real data set (i collected in Internet). Did you face this problem? – Atena Nguyen Jun 21 '17 at 05:45