【问题标题】:Getting a dictionary key error while run a caffe model with python codes使用 python 代码运行 caffe 模型时出现字典键错误
【发布时间】: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


【解决方案1】:

我对 caffe 不够熟悉,无法提供帮助,但您可以先使用 out.items() 检查字典的值。我不知道预期的数据是什么,但既然你这样做了,你也许可以利用这些知识更好地理解你的问题。

感谢 Octavel 在我使用 out.items() 之后,我看到我的字典键是“softmax”。我用'softmax'改变了所有'prob'并解决了问题。

【讨论】:

  • 很高兴能帮上忙!记得将其标记为已接受的答案以关闭线程
猜你喜欢
  • 2021-08-05
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 2012-10-27
相关资源
最近更新 更多