尝试理解附加的python代码行并根据您的需要进行调整。这不是我的代码,但我写了一篇类似的文章来测试我的模型。
来源是:
<a href="https://www.cc.gatech.edu/~zk15/deep_learning/classify_test.py" rel="nofollow noreferrer">
https://www.cc.gatech.edu/~zk15/deep_learning/classify_test.py
</A>
如果您不想微调预先训练过的模型,很明显您不需要求解器。求解器是优化模型的原因。如果你想预测图像的类概率,你实际上只需要进行正向传递。请记住,deploy.prototxt必须具有适当的最后一层,该层使用softmax或sigmoid函数(取决于体系结构)。您不能使用train_val.prototxt中的loss函数。
import numpy as np
import matplotlib.pyplot as plt
Make sure that caffe is on the python path:
caffe_root = ‘../‘ # this file is expected to be in {caffe_root}/examples
import sys
sys.path.insert(0, caffe_root + ‘python’)
import caffe
Set the right path to your model definition file, pretrained model weights,
and the image you would like to classify.
MODEL_FILE = ‘../models/bvlc_reference_caffenet/deploy.prototxt’
PRETRAINED = ‘../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel’
IMAGE_FILE = ‘images/cat.jpg’
caffe.set_mode_cpu()
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)
prediction = net.predict([input_image]) # predict takes any number of images, and formats them for the Caffe net automatically
print ‘prediction shape:’, prediction[0].shape
plt.plot(prediction[0])
print ‘predicted class:’, prediction[0].argmax()
plt.show()
</code>