import numpy as np
import cv2
import caffe
caffe_root = "./"
caffe.set_mode_gpu()
net = caffe.Net(caffe_root + 'examples/mnist/lenet.prototxt',
caffe_root + "examples/mnist/lenet_iter_10000.caffemodel",
caffe.TEST)
transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
transformer.set_transpose('data',(2,0,1))
net.blobs['data'].reshape(1,1,28,28)
images = cv2.imread(caffe_root + "examples/mnist/lenet/image/7_5.jpg",cv2.CV_8UC1)
images = np.reshape(images,(images.shape[0],images.shape[1],1))
#white background
images = 255 - images
net.blobs['data'].data[...] = transformer.preprocess('data',images)
out = net.forward()
print("Predicted class is #{}.".format(out['prob'].argmax()))