How to implement trained LeNet MNIST classification model in python

4,405 views
Skip to first unread message

Niko Gamulin

unread,
Feb 25, 2015, 5:53:35 PM2/25/15
to caffe...@googlegroups.com
Hi,

Has anyone successfully Implemented trained LeNet MNIST model from tutorial to classify the written digits in python?

I have written the following script which unfortunately doesn't work:

import cPickle, gzip
import numpy as np
import matplotlib.pyplot as plt
import timeit
import random

import Image

caffe_root = '/usr/local/caffe/'  # this file is expected to be in {caffe_root}/examples
import sys
sys.path.insert(0, caffe_root + '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 = '/usr/local/caffe/examples/mnist/lenet.prototxt'
PRETRAINED = '/usr/local/caffe/examples/mnist/lenet_iter_10000.caffemodel'

f = gzip.open('/home/niko/Pictures/mnist.pkl.gz', 'rb') # downloaded from http://deeplearning.net/tutorial/gettingstarted.html
train_set, valid_set, test_set = cPickle.load(f)
f.close()

randomIndex = random.randint(1, len(test_set[0]))
selectedNumber = test_set[0][randomIndex]

caffe.set_phase_test()
#caffe.set_mode_cpu()
caffe.set_mode_gpu()
mean = mean=np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1)

net = caffe.Classifier(MODEL_FILE, PRETRAINED,
                       channel_swap=(2,1,0),
                       raw_scale=255,
                       image_dims=(28, 28))



input_image = selectedNumber.reshape(28, 28)
I8 = (((input_image - input_image.min()) / (input_image.max() - input_image.min())) * 255.9).astype(np.uint8)
img = Image.fromarray(I8)
img.save("file_tmp.jpg")
    
input_image_caffe = caffe.io.load_image("file_tmp.jpg", color=False)


prediction = net.predict([input_image_caffe])  # predict takes any number of images, and formats them for the Caffe net automatically
print 'prediction shape:', prediction[0].shape
plt.plot(prediction[0])
plt.show()
print 'predicted class:', prediction[0].argmax()

 When running the above script the line prediction = net.predict([input_image_caffe]) throws an error: index 2 is out of bounds for axis 2 with size 1.

I have included this io.py script but the above script nevertheless didn't work.

Actually I am not sure whether the error is caused due to possible incorrect caffe.Classifier:
net = caffe.Classifier(MODEL_FILE, PRETRAINED,
                       channel_swap=(2,1,0),
                       raw_scale=255,
                       image_dims=(28, 28))

which is supposed to classify grayscale images or there is some other issue. Also, the above script first saves array as grayscale image and then loads the file - I did this dirty workaround because didn't know whether the image file has to be loaded using function caffe.io.load_image() or it is possible to work with numpy array directly.

If anyone has managed to use the LeNet model from tutorial to classify digits in python script I would appreciate any suggestion for implementation. Also, in general, if anyone implemented some other classification model for grayscale images in python it would be also very helpful to get any insights about how to do it.

Thank you and best regards,

Niko

Nitin Iyer

unread,
Feb 25, 2015, 7:07:21 PM2/25/15
to caffe...@googlegroups.com
Here is some code I have used to get the MNIST LeNet working. Feel free to make use of it, hopefully its easy to follow along.

import sys
import caffe
import cv2
import Image
import matplotlib
matplotlib
.rcParams['backend'] = "Qt4Agg"
import numpy as np
import lmdb
caffe_root
= '../'

MODEL_FILE
= './examples/mnist/lenet.prototxt'
PRETRAINED
= './examples/mnist/lenet_iter_10000.caffemodel'

net
= caffe.Net(MODEL_FILE, PRETRAINED,caffe.TEST)
caffe
.set_mode_cpu()
# Test self-made image
"""
img = caffe.io.load_image('./examples/images/two_g.jpg', color=False)
img = img.astype(np.uint8)
out = net.forward_all(data=np.asarray([img.transpose(2,0,1)]))
print out['prob'][0]
"""

db_path
= '../ndsb_competition/ndsb_trial_train_lmdb'
lmdb_env
= lmdb.open(db_path)
lmdb_txn
= lmdb_env.begin()
lmdb_cursor
= lmdb_txn.cursor()
count
= 0
correct
= 0
for key, value in lmdb_cursor:
   
print "Count:"
   
print count
    count
= count + 1
    datum
= caffe.proto.caffe_pb2.Datum()
    datum
.ParseFromString(value)
    label
= int(datum.label)

    image
= image.transpose()
   
out = net.forward_all(data=np.asarray([image]))
    predicted_label
= out['prob'][0].argmax(axis=0)
   
print out['prob']
   
if label == predicted_label[0][0]:
        correct
= correct + 1
   
print("Label is class " + str(label) + ", predicted class is " + str(predicted_label[0][0]))

print(str(correct) + " out of " + str(count) + " were classified correctly")

Niko Gamulin

unread,
Feb 26, 2015, 3:48:25 AM2/26/15
to caffe...@googlegroups.com
Works fine. Thank you very much!

Cuong Duc

unread,
Aug 9, 2015, 12:46:21 AM8/9/15
to Caffe Users
Where should I put this code?

I tried to put it inside python folder of Caffe, but when I run the code it said that "No module named cv2"

Umar Spa

unread,
Aug 10, 2015, 4:28:59 AM8/10/15
to Caffe Users
Can you point out the lines which need to be modified in order to make it work on my computer ?
ps: I am newbie, so don't get angry on my stupid question, please...

Cristian Ruz

unread,
Aug 13, 2015, 10:16:54 PM8/13/15
to Caffe Users
Hi Coung,
That cv2 error is unrelated to caffe. It's because you need to install pyopencv (the python bindings for the OpenCV library). 

Thomas Burns

unread,
Aug 22, 2015, 1:40:04 AM8/22/15
to Caffe Users
Hi All,

I am new to Caffe and doing my best to pick this up.
I have tried running the above code but it won't work.
First, where can I find "../ndsb_competition/ndsb_trial_train_lmdb"? Can I substitute 'examples/mnist/mnist_train_lmdb'' instead?
Second,when I substitute the above and run the code I get:
Traceback (most recent call last):
  File "mnist.py", line 36, in <module>
    image = image.transpose()
NameError: name 'image' is not defined
Replacing it with Image doesn't help.

Any help is appreciated.
Thanks in advance.


Adarsh Chauhan

unread,
Aug 29, 2015, 11:54:59 AM8/29/15
to Caffe Users
Thomas,

Hey. I still am not sure how you can obtain that NDSB Competition database. Also, when I searched for this dataset, it turns out to be a dataset of biological images on species or something like that. I wonder why the code provided above is using that dataset for testing a model train on MNIST dataset.

Regarding substitution of MNIST test dataset already provided, I tried that and it worked i.e. the following line I updated in my testing code:
db_path = './examples/mnist/mnist_test_lmdb'

Regarding the 'image', you have to obtain it from the 'datum' as follows:
image = caffe.io.datum_to_array(datum)
image
= image.astype(np.uint8)

I hope it helps. :)

Hemanth Pidaparthy

unread,
Sep 14, 2015, 7:06:27 AM9/14/15
to Caffe Users
Hi,

  This is the code on the for loop that I am using. 

  for key, value in lmdb_cursor:
    print "Count:"
    print count
    count = count + 1
    datum = caffe.proto.caffe_pb2.Datum()
    datum.ParseFromString(value)
    label = int(datum.label)

    image = caffe.io.datum_to_array(datum)
    image = image.astype(np.uint8)
    image = image.transpose()
    out = net.forward_all(data=np.asarray([image]))
    predicted_label = out['prob'][0].argmax(axis=0)
    print out['prob']
    if label == predicted_label[0][0]:
        correct = correct + 1
    print("Label is class " + str(label) + ", predicted class is " + str(predicted_label[0][0]))



  I am getting the following error:


 Traceback (most recent call last):
  File "run.py", line 40, in <module>
    out = net.forward_all(data=np.asarray([image]))
  File "/home/hemanth2090/caffe/python/caffe/pycaffe.py", line 177, in _Net_forward_all
    outs = self.forward(blobs=blobs, **batch)
  File "/home/hemanth2090/caffe/python/caffe/pycaffe.py", line 102, in _Net_forward
    self.blobs[in_].data[...] = blob
ValueError: could not broadcast input array from shape (64,28,28,1) into shape (64,1,28,28)



 If I pass the image array as it is without transposing, I get the following error.

Traceback (most recent call last):
  File "run.py", line 43, in <module>
    if label == predicted_label[0][0]:
IndexError: invalid index to scalar variable.



   I am using the code mentioned above and made the changes you suggested. I dont know why the code is not working. Can you please help me? I am pretty sure it is a very silly mistake.

Adarsh Chauhan

unread,
Sep 14, 2015, 7:32:23 AM9/14/15
to Caffe Users
Hemanth,

Reading the first error, I can make out that you do not need to transpose the input image read from datum.

Regarding the second error, just use the following (the corresponding part from the code that you shared):

if label == predicted_label:

        correct
= correct + 1
print("Label is class " + str(label) + ", predicted class is " + str(predicted_label))

This is because predicted_label is a scalar and not a vector. So, you cannot access its first element as it doesn't have any.

Hemanth Pidaparthy

unread,
Sep 14, 2015, 7:48:02 AM9/14/15
to Caffe Users
Hey,

  Thanks. Finally got it working!

Fradaric Joseph

unread,
Feb 19, 2016, 8:14:57 AM2/19/16
to Caffe Users
Hello, I used your code for running MNIST and it worked properly. Then I tried it for CIFAR QUICK but accuracy is only 51. Do you think its not straight forwardly compatible for  cifar?
code below shows how I used it for cifar quick

#####################################
import sys
import caffe
import cv2
import Image
import matplotlib
import numpy as np
import lmdb
caffe_root = '/home/fred/CIFAR_QUICK/caffe'

MODEL_FILE = '/home/fred/CIFAR_QUICK/caffe/examples/cifar10/cifar10_quick.prototxt'
PRETRAINED = '/home/fred/CIFAR_QUICK/caffe/examples/cifar10/cifar10_quick_iter_5000.caffemodel.h5'

net = caffe.Net(MODEL_FILE, PRETRAINED,caffe.TEST)
caffe.set_mode_cpu()
db_path = '/home/fred/CIFAR_QUICK/caffe/examples/cifar10/cifar10_test_lmdb'
lmdb_env = lmdb.open(db_path)
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()
count = 0
correct = 0
for key, value in lmdb_cursor:
    print "Count:"
    print count
    count = count + 1
    datum = caffe.proto.caffe_pb2.Datum()
    datum.ParseFromString(value)
    label = int(datum.label)
    image = caffe.io.datum_to_array(datum)
    image = image.astype(np.uint8)
    out = net.forward_all(data=np.asarray([image]))
    predicted_label = out['prob'][0].argmax(axis=0)
    print out['prob']
    if label == predicted_label:
        correct = correct + 1
    print("Label is class " + str(label) + ", predicted class is " + str(predicted_label))

print(str(correct) + " out of " + str(count) + " were classified correctly")
######################################################################


On Thursday, February 26, 2015 at 1:07:21 AM UTC+1, Nitin Iyer wrote:
Message has been deleted

shruti sneha

unread,
Nov 8, 2016, 2:13:11 AM11/8/16
to Caffe Users
hey, can you plz suggest the location where to store this file. while running this python file, i'm getting an error msg like :

WARNING: Logging before InitGoogleLogging() is written to STDERR
W1108 12:40:20.843312 27819 _caffe.cpp:122] DEPRECATION WARNING - deprecated use of Python interface
W1108 12:40:20.843449 27819 _caffe.cpp:123] Use this instead (with the named "weights" parameter):
W1108 12:40:20.843487 27819 _caffe.cpp:125] Net('./examples/mnist/lenet.prototxt', 1, weights='./examples/mnist/lenet_iter_10000.caffemodel')

Traceback (most recent call last):
  File "mnist_python.py", line 14, in <module>
    net = caffe.Net(MODEL_FILE, PRETRAINED,caffe.TEST)
RuntimeError: Could not open file ./examples/mnist/lenet.prototxt

plz help me out.
Reply all
Reply to author
Forward
0 new messages