0

I'm using Caffe (http://caffe.berkeleyvision.org/) for image classification. I'm using it on Windows and everything seems to be compiling just fine.

To start learning I followed the MNIST tutorial (http://caffe.berkeleyvision.org/gathered/examples/mnist.html). I downloaded the data and ran ..\caffe.exe train --solver=...examples\mnist\lenet_solver.prototxt. It ran 10.000 iterations, printed that the accuracy was 98.5, and generated two files: lenet_iter_10000.solverstate, and lenet_iter_10000.caffemodel.

So, I though it would be funny to try to classify my own image, it should be easy right?.

I can find resources such as: https://software.intel.com/en-us/articles/training-and-deploying-deep-learning-networks-with-caffe-optimized-for-intel-architecture#Examples telling how to prepare, train and time my model. But each time a tutorial/article comes to actually putting a single instance into the CNN, they skip to the next point and tell to download some new model. Some resources tell to use the classifier.bin/.exe, but this file takes a imagenet_mean.binaryproto or similar for mnist. I have no idea where to find or generated this file.

So in short: When I have trained a CNN using Caffe, how to I input a single image and get the output using the files I already have?

Update: Based on the help, I got the Net to recognize an image but the recognition is not correct even if the network had an accuracy of 99.0%. I used the following python code to recognice an image:

NET_FILE = 'deploy.prototxt'
MODEL_FILE = 'lenet_iter_10000.caffemodel'
net = caffe.Net(NET_FILE, MODEL_FILE, caffe.TEST)

im = Image.open("img4.jpg")
in_ = np.array(im, dtype=np.float32)
net.blobs['data'].data[...] = in_
out = net.forward() # Run the network for the given input image
print out;

I'm not sure if I format the image correctly for the MNIST example. The image is a 28x28 grayscale image with a basic 4. Do I have to do more transformations on the image?

The network (deploy) looks like this (start and end):

input: "data"
input_shape {
  dim: 1 # batchsize
  dim: 1 # number of colour channels - rgb
  dim: 28 # width
  dim: 28 # height
}

....

layer {
  name: "loss"
  type: "Softmax"
  bottom: "ip2"
  top: "loss"
}
MortenGR
  • 803
  • 1
  • 8
  • 22

2 Answers2

0

If I understand the question correctly, you have a trained model and you want to test the model using your own input images. There are many ways to do this.

One method I commonly use is to run a python script similar to what I have here.

Just keep in mind that you have to build python in caffe using make pycaffe and point to the folder by editing the line sys.path.append('../../../python')

Also edit the following lines to your model filenames.

NET_FILE = 'deploy.prototxt'
MODEL_FILE = 'fcn8s-heavy-pascal.caffemodel'

Edit the following line. Instead of score you should use the last layer of your network to get the output.

out = net.blobs['score'].data
malreddysid
  • 1,342
  • 1
  • 9
  • 12
0

You need to create a deploy.prototxt file from your original network.prototxt file. The data layer has to look like this:

input: "data"
input_shape {
  dim: 1
  dim: [channles]
  dim: [width]
  dim: [height]
}

where you replace [channels], [width], and [height] with the correct values of your image.

You also need to remove any layers which get the "label" as its bottom input (this would usually be only your loss layer).

Then you can use this deploy.prototxt file to test your inputs using MATLAB or PYTHON.

Amir
  • 2,259
  • 1
  • 19
  • 29
  • See the update, do you have any ideas why it's not working? – MortenGR Oct 14 '16 at 11:35
  • Read image with caffe.io.load_image. I believe caffe transposes the image when loading it. Or try transposing your image before feeding into the network. – Amir Oct 14 '16 at 16:50