python 使用新训练好的模型进行分类

6、在python中使用已经训练好的模型。

Caffe只提供封装好的imagenet模型,给定一副图像,直接计算出图像的特征和进行预测。首先需要下载模型文件

Python代码如下:

from caffe import imagenet
from matplotlib import pyplot
# Set the right path to your model file, pretrained model
# and the image you would like to classify.
MODEL_FILE = 'examples/imagenet_deploy.prototxt'
PRETRAINED = '/home/jiayq/Downloads/caffe_reference_imagenet_model’
IMAGE_FILE = '/home/jiayq/lena.png'
 
net = imagenet.ImageNetClassifier(MODEL_FILE, PRETRAINED) 
#预测
prediction = net.predict(IMAGE_FILE)
#绘制预测图像
print 'prediction shape:', prediction.shape
pyplot.plot(prediction)
prediction shape: (1000,)
[<matplotlib.lines.Line2D at 0x8faf4d0>] #结果如图所示


图上的横轴表示的label,纵轴表示在该类别上的概率,有图我们看到,lena.jpg被分到了”sombrero”这组,结果还算准确。

原文地址:https://www.cnblogs.com/airlove/p/4705603.html