【问题标题】:TypeError: unhashable type: 'numpy.ndarray' Python3.9 image classification using tensorflow and kerasTypeError: unhashable type: 'numpy.ndarray' Python3.9 图像分类使用 tensorflow 和 keras
【发布时间】:2022-01-22 03:52:44
【问题描述】:

我尝试使用此示例代码进行图像分类

def show_classify_button(file_path):
    classify_btn = Button(top, text="Classify Image", command=lambda: classify(file_path), padx=10, pady=5)
    classify_btn.configure(background="#364156", foreground="white", font=('arial',10,'bold'))
    classify_btn.place(relx=0.79,rely=0.46)

def classify(file_path):
    image = Image.open(file_path)
    image = image.resize((32,32))
    image = numpy.expand_dims(image, axis=0)
    image = numpy.array(image)
    pred = model.predict([image])[0]
    sign = classes[pred]
    print(sign)
    label.configure(foreground='#011638')

终端弹出这个

Traceback (most recent call last):
  line 39, in <lambda>
    classify_btn = Button(top, text="Classify Image", command=lambda: classify(file_path), padx=10, pady=5)
  line 49, in classify
    sign = classes[pred]
TypeError: unhashable type: 'numpy.ndarray'

我尝试使用输出检查 pred 中的数据

[30990.06  46435.57  17636.973 16334.658 15860.342 16765.371 26879.748
 14579.97  41989.523 34359.215]

我不知道为什么,因为数据来自一组数组

我是新手,我使用 python3.9 可以帮助我

【问题讨论】:

  • 没有python 2.9 也许你的意思是3.9?
  • 是的,很抱歉

标签: python tensorflow keras conv-neural-network artificial-intelligence


【解决方案1】:

您正在尝试访问第 49 行的 classes 变量

sign = classes[pred]

类的类型为numpy.ndarray。 因此,您尝试访问索引为 pred 的数组,但因为 pred 不是数字,它会引发 unhashable type: 'numpy.ndarray' 错误。

通过使用键而不是索引访问类的值,您将类视为字典。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多