Tensorflow TypeError:'numpy.ndarray'对象不可调用

时间:2019-06-26 13:02:48

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

在尝试预测模型时,我遇到此numpy.ndarray错误。它可能是prepare函数的返回语句。可以采取什么措施来消除此错误。     导入cv2     将tensorflow导入为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

2 个答案:

答案 0 :(得分:2)

不确定其余代码的外观。但是,如果在Python 3中使用“ print”作为变量,则会出现此错误:

count = 0
flag = True
i = len(q) - 1
for x in range(len(q)):
    temp = max(q) - x
    index = q.index(temp)
    if index < i - x:
        if i - x - index > 2:
            print("Too chaotic")
            flag = False
            break
        q.remove(temp)
        q.insert(i - x, temp)
        count += i - x - index
if flag:
    print(count)

输出:

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

答案 1 :(得分:1)

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

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])]))