How to test trained MNIST model with example digital images?

4,434 views
Skip to first unread message

Yili Zhao

unread,
Apr 11, 2015, 10:34:09 PM4/11/15
to caffe...@googlegroups.com
Hi,
  I have trained MNIST data set with Caffe, and generated "lenet_iter_10000.caffmodel"  model file.
  How can I use this trained model to classify example digital images? Is there any tutorial on this?
  Thanks! 

Antonio Paes

unread,
Apr 13, 2015, 11:57:43 PM4/13/15
to caffe...@googlegroups.com
I'm with the same problem, any progress?

Steven

unread,
Apr 14, 2015, 1:28:08 AM4/14/15
to caffe...@googlegroups.com
I've started trying to do this, but without success. If anyone can spot my error, I'd really appreciate it:

I started by creating the following deploy file, mainly by looking at the imagnet deploy file and imitating it:

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"
  convolution_param
{
    num_output
: 20
    kernel_size
: 5
    stride
: 1
 
}
}
layer
{
  name
: "pool1"
  type
: "Pooling"
  bottom
: "conv1"
  top
: "pool1"
  pooling_param
{
    pool
: MAX
    kernel_size
: 2
    stride
: 2
 
}
}
layer
{
  name
: "conv2"
  type
: "Convolution"
  bottom
: "pool1"
  top
: "conv2"
  convolution_param
{
    num_output
: 50
    kernel_size
: 5
    stride
: 1
 
}
}
layer
{
  name
: "pool2"
  type
: "Pooling"
  bottom
: "conv2"
  top
: "pool2"
  pooling_param
{
    pool
: MAX
    kernel_size
: 2
    stride
: 2
 
}
}
layer
{
  name
: "ip1"
  type
: "InnerProduct"
  bottom
: "pool2"
  top
: "ip1"
  inner_product_param
{
    num_output
: 500
 
}
}
layer
{
  name
: "relu1"
  type
: "ReLU"
  bottom
: "ip1"
  top
: "ip1"
}
layer
{
  name
: "ip2"
  type
: "InnerProduct"
  bottom
: "ip1"
  top
: "ip2"
  inner_product_param
{
    num_output
: 10
 
}
}



However, when I try to classify images from the lmdb (or, png versions of MNIST) data, the resulting prediction vector is not normalized and doesn't seem to have its maximum value where it should:

I started by recovering my trained net:

import os
import sys

caffe_home
='/home/me/Caffe/'
sys
.path.insert(0, caffe_home + 'caffe/python')
import caffe

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
%matplotlib inline


# Set the right path to your model definition file, pretrained model weights,
# and the image you would like to classify.
MODEL_FILE
= os.path.join(caffe_home,'caffe/examples/mnist/lenet_deploy_SG.prototxt')
PRETRAINED
= os.path.join(caffe_home,'caffe/examples/mnist/lenet_iter_10000.caffemodel')

net
= caffe.Classifier(MODEL_FILE, PRETRAINED,
                       raw_scale
=1,
                       image_dims
=(28, 28))

caffe
.set_mode_cpu()

Then, I tried to load one of the images that was saved in the lmdb:

#Note: This code was cribbed from examples
import lmdb
db_path
= os.path.join(caffe_home,'caffe/examples/mnist/mnist_test_lmdb')
lmdb_env
= lmdb.open(db_path)
lmdb_txn
= lmdb_env.begin()
lmdb_cursor
= lmdb_txn.cursor()
lmdb_cursor
.next()
key
=lmdb_cursor.key()
value
=lmdb_cursor.value()

datum
= caffe.proto.caffe_pb2.Datum()
datum
.ParseFromString(lmdb_cursor.value())
label
= int(datum.label)
lmdb_image
= caffe.io.datum_to_array(datum)
lmdb_image
= lmdb_image.astype(np.uint8)
lmdb_image2
= lmdb_image.transpose((1,2,0)) #Necessary to fit net's expectation of data dimensions

prediction
= net.predict([lmdb_image2])
print prediction

When I viewed the image, it was clearly a '7' (though not as vertically centered as I anticipated). However, the prediction vector was

array([[ 1.46435475,  3.83749199, -2.24171877, -2.16914892, -1.05741477,
         
0.06117836, -0.63742262,  1.62853026, -2.24497342, -1.52514422]], dtype=float32)

This is not normalized, and its max does not correspond to the '7' class. I've gotten similar results using PNG images from the MNIST dataset.

If anyone can point out my mistake(s), it would be a big help.

Thanks,

Steven

unread,
Apr 19, 2015, 11:15:17 PM4/19/15
to caffe...@googlegroups.com
For what it's worth, here is how I did it (using an iPython Notebook):
#Initial imports and defs for net


import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

import os
import sys

caffe_home='/home/me/Caffe/'
sys.path.insert(0, caffe_home + 'caffe/python')
import caffe

# Set the right path to your model definition file, pretrained model weights,
# and the image you would like to classify.
MODEL_FILE = os.path.join(caffe_home,'caffe/examples/mnist/lenet.prototxt')
PRETRAINED = os.path.join(caffe_home,'caffe/examples/mnist/lenet_iter_10000.caffemodel')

#Load data for images
caffe_image_dir = os.path.join(caffe_home,'images/MNIST_TestIms/pgmProcImages')

#Load database with images (really want to replace this with actual image files,
#but until things work, use the images the net acually trained on)
import lmdb
db_path = os.path.join(caffe_home,'caffe/examples/mnist/mnist_train_lmdb')

lmdb_env = lmdb.open(db_path)
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()

#Initialize net
caffe.set_mode_gpu()

net1 = caffe.Classifier(MODEL_FILE, PRETRAINED,
                        raw_scale=255,
                        image_dims=(28,28))

#Get Info from lmdb
lmdb_cursor.next()
value=lmdb_cursor.value()

#Get Image
datum = caffe.proto.caffe_pb2.Datum()
datum.ParseFromString(value)

#Get Ground Truth Label (only possible, since using data set
#the net trained with
label = int(datum.label)
lmdb_image0 = caffe.io.datum_to_array(datum)
lmdb_image = lmdb_image0.transpose((1,2,0))
lmdb_image2=lmdb_image.astype(float32)

#Show image, Net's predictions (& confidences) for Image & Ground Truth
#plt.imshow(lmdb_image2[:,:,0], cmap =cm.Greys_r)
print lmdb_image2.shape,lmdb_image2.dtype
prediction = net1.predict([lmdb_image2], oversample=False)
print "Confidences: ", prediction, "\nPredicted Label: ", np.argmax(prediction), "\nTrue Label: ", label
print

-- Or, if you want to classify actual image files (just be certain you are using a 28x28x1 black&white image of float32's, whose values range from 0-255):

IMAGE = '8_0.png'     #Example of a particular MNIST image in PNG format
IMAGE_FILE = os.path.join(caffe_image_dir, IMAGE)
input_image = caffe.io.load_image(IMAGE_FILE)
input_image = input_image[:,:,0]
plt.imshow(input_image, cmap =cm.Greys_r)
input_image = np.expand_dims(input_image,2)
#input_image=input_image.astype(np.uint8)
print input_image.shape, input_image.dtype
prediction = net1.predict([input_image],oversample=False) 
print "Confidences: ", prediction, "\nPredicted Label: ", np.argmax(prediction), "\nTrue Label: ", IMAGE[0]


On Saturday, April 11, 2015 at 10:34:09 PM UTC-4, Yili Zhao wrote:

tong wang

unread,
Dec 1, 2015, 10:06:40 AM12/1/15
to Caffe Users

hi steve , thanks for your post .I just finished set up the caffe , same question , did you succedd in your previous work ?

tong wang

unread,
Dec 1, 2015, 10:07:22 AM12/1/15
to Caffe Users
how is the progress?


Le dimanche 12 avril 2015 10:34:09 UTC+8, Yili Zhao a écrit :

zho...@ihoment.com

unread,
Mar 22, 2017, 5:15:43 AM3/22/17
to Caffe Users
我搞定了,参考这个吧 

https://github.com/9crk/caffe-mnist-test

I finished it .check out this. 
Reply all
Reply to author
Forward
0 new messages