【发布时间】:2021-04-09 09:38:05
【问题描述】:
我已经通过 nvidia 的数字训练了一个 caffe 模型。现在我正在尝试使用 python 程序初始化我的模型。我已经尝试了一些示例,但我遇到了一个字典键错误,它告诉我没有“prob”键。我是运行深度模型的新手。 我在 github 上修改了“dersmon”的 prediction.py 代码: https://gist.github.com/dersmon/8b701a41a3a1d6b45098
我找到了一些解决方案,我在下面提供了链接。错误似乎相同,但对我不起作用。 https://groups.google.com/g/caffe-users/c/gv90MUHshrM?pli=1
这是我的代码;
import caffe
import cv2
from PIL import Image
import numpy as np
caffe_root = "/home/kenan/caffe/"
MODEL_FILE = caffe_root + 'models/tez_test/deploy.prototxt'
PRETRAINED = caffe_root + 'models/tez_test/snapshot_iter_13749140.caffemodel'
net = caffe.Net(MODEL_FILE, PRETRAINED, caffe.TEST)
caffe.set_mode_cpu()
blob = caffe.proto.caffe_pb2.BlobProto()
data = open(caffe_root + 'models/tez_test/mean.binaryproto' , 'rb' ).read()
blob.ParseFromString(data)
meanArray = np.array( caffe.io.blobproto_to_array(blob) ).transpose(3,2,1,0)
meanArray = meanArray[:,:,:,0]
img = caffe.io.load_image(caffe_root + '/models/tez_test/sample_img/res.jpg')
img = caffe.io.resize(img, (224, 224))
meanArray = caffe.io.resize(meanArray,(224,224))
img = img - meanArray
img = img.astype(np.uint8)
imageData = np.asarray([img.transpose(2, 1, 0)])
imageData = np.divide(imageData, 255.0)
out = net.forward(data=imageData)
print(format(out['prob'][0].argmax()))
imagenet_labels_filename = caffe_root + 'models/tez_test/labels.txt'
labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\s')
top_k = net.blobs['prob'].data[0].flatten().argsort()[-1: -6: -1]
print (out['prob'][0])
print (top_k)
print (labels[top_k])
...这是我的错误;
Traceback (most recent call last):
File "deep-test.py", line 37, in <module>
print(format(out['prob'][0].argmax()))
KeyError: 'prob'
感谢您的帮助。
【问题讨论】:
-
我对 caffe 不够熟悉,无法提供帮助,但您可以先使用
out.items()检查字典的值。我不知道out的预期数据是什么,但既然您这样做了,您或许可以利用这些知识更好地了解您的问题。
标签: python numpy deep-learning caffe