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: