import numpy as np
import sys
caffe_root = '/home/tyd/caffe/' #the file should be run from (caffe_root)/examples
sys.path.insert(0,caffe_root + 'python')
import caffe
caffe.set_mode_cpu()
model_def = '/home/tyd/caffe/models/bvlc_reference_caffenet/deploy.prototxt'
model_weights = '/home/tyd/caffe_case/bvlc_reference_caffenet.caffemodel'
net = caffe.Net(model_def,model_weights,caffe.TEST)
mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')
mu = mu.mean(1).mean(1)
print 'mean-subtracted values:',zip('BGR',mu)
#create transform for the input called 'data'
tranformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
tranformer.set_transpose('data':(2,0,1)) #move image channels to ermost dimension
tranformer.set_mean('data':mu) #subtract the dataset mean value in each channels
tranformer.set_raw_scale('data':255) #rescale from [0,1] to [0,255]
tranformer.set_channel_swap('data':(2,1,0)) #swap channels from RGB TO BGR
net.blobs['data'].reshape(1, # batch size
3, #3-channel BGR image
277,277) #image size is 277x277
image = caffe.io.load_image(caffe_root+'examples/images/cat.jpg)
#cope the image data into the memory allocated for the net
net.blobs['data'].data[...] = transformed_image
###perform classification
output = net.forward()
output_prob= output['prob'][0]
print 'predicted class is :',output_prob.argmax()