【问题标题】:Tensorflow TypeError: 'numpy.ndarray' object is not callableTensorflow TypeError:“numpy.ndarray”对象不可调用
【发布时间】:2019-06-26 13:02:48
【问题描述】:

在尝试预测模型时,我得到了这个 numpy.ndarray 错误。它可能是准备函数的返回语句。可以做些什么来摆脱这个错误。 导入简历2 将张量流导入为 tf

CATEGORIES = ["Dog", "Cat"]


def prepare(filepath):
    IMG_SIZE = 50  # 50 in txt-based
    img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)


model = tf.keras.models.load_model("64x3-CNN.model")

prediction = model.predict([prepare('dog.jpg')])
print(prediction)  # will be a list in a list.

试图给出完整路径仍然存在相同的错误。

TypeError                                 Traceback (most recent call last)
<ipython-input-45-f9de27e9ff1e> in <module>
     15 
     16 prediction = model.predict([prepare('dog.jpg')])
---> 17 print(prediction)  # will be a list in a list.
     18 print(CATEGORIES[int(prediction[0][0])])

TypeError: 'numpy.ndarray' object is not callable

【问题讨论】:

    标签: python-3.x numpy tensorflow machine-learning deep-learning


    【解决方案1】:

    不确定您的其余代码是什么样的。但是如果你在 Python 3 中使用 'print' 作为变量,你会得到这个错误:

    import numpy as np
    x = np.zeros((2,2))
    print = np.ones((2,2))
    print(x)
    

    输出:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'numpy.ndarray' object is not callable
    

    【讨论】:

    • 我可以做些什么来获得结果?这是我使用 load_model 调用的 tensorflow 模型。我从博客中复制了这段代码,它似乎对他有用,但不适合我
    【解决方案2】:

    这种类型的错误主要发生在尝试打印数组而不是简单字符串或单个变量数字时,因此我建议您更改:

    17 print(prediction)  # will be a list in a list.
     18 print(CATEGORIES[int(prediction[0][0])])
    

    然后你会得到:

    17 print(str(prediction))  # will be a list in a list.
     18 print(str(CATEGORIES[int(prediction[0][0])]))
    

    【讨论】:

      猜你喜欢
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 2021-08-18
      • 2019-05-09
      • 2020-11-13
      相关资源
      最近更新 更多