@hanxiaoyang
2016-10-10T05:08:45.000000Z
字数 1005
阅读 3039
未分类
example code by @寒小阳
#example code by @Hanxiaoyangimport numpy as npimport sysimport matplotlib.pyplot as plt# 这一步一定要保证,从caffe路径importcaffe_root = '../' # 你的caffe编译文件夹路径sys.path.insert(0, caffe_root + 'python')import caffe# 我们如果已经训练好模型了,只是想做个图像识别的demo# 模型网络结构MODEL_FILE = '../models/bvlc_reference_caffenet/deploy.prototxt'# 预训练好的模型PRETRAINED = '../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'# 要识别的图像文件IMAGE_FILE = 'images/dog.jpg'# 选择使用cpu还是gpucaffe.set_mode_cpu()#使用gpu#caffe.set_device(0)#caffe.set_mode_gpu()# 用网络结构定义文件,预训练模型,训练集均值文件初始化分类器net = caffe.Classifier(MODEL_FILE, PRETRAINED,mean=np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1),channel_swap=(2,1,0),raw_scale=255,image_dims=(256, 256))# 载入待预测图片input_image = caffe.io.load_image(IMAGE_FILE)# 展示一下图片plt.imshow(input_image)# predict阶段,其实就是做前向运算,softmax输出概率prediction = net.predict([input_image])print 'prediction shape:', prediction[0].shapeplt.plot(prediction[0])print 'predicted class:', prediction[0].argmax()plt.show()
