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”這組,結果還算准確。