使用python代码运行caffe模型时出现字典键错误



我已经通过nvidia的数字训练了一个caffe模型。现在我正试图用python程序初始化我的模型。我试过一些样本,但我遇到了字典键错误,这告诉我没有"prob"键。我是经营深度模型的新手。我修改了";dersmon";github上的预测.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的预期数据是什么,但既然知道了,您可能可以使用这些知识更好地理解您的问题。

感谢Octavel用完.items((后,我发现我的字典关键字是softmax。我用softmax改变了所有的prob,问题解决了。

最新更新