import cPickle, gzipimport numpy as npimport matplotlib.pyplot as pltimport timeitimport random
import Image
caffe_root = '/usr/local/caffe/' # this file is expected to be in {caffe_root}/examplesimport syssys.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.htmltrain_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 automaticallyprint 'prediction shape:', prediction[0].shapeplt.plot(prediction[0])plt.show()print 'predicted class:', prediction[0].argmax()
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")
../ndsb_competition/ndsb_trial_train_lmdb"?
Can I substitute 'examples/mnist/mnist_train_lmdb'' instead?db_path = './examples/mnist/mnist_test_lmdb'
image = caffe.io.datum_to_array(datum)
image = image.astype(np.uint8)
if label == predicted_label:
correct = correct + 1
print("Label is class " + str(label) + ", predicted class is " + str(predicted_label))