tensorflow TypeError:打印预测结果时,只有size-1数组可以转换为Python标量

mu0hgdu0  于 2023-08-06  发布在  Python
关注(0)|答案(1)|浏览(97)

如何在print(Classes[int(result)])上修复此错误?

import numpy as np
import matplotlib.pyplot as plt

# Pre-Processing test data same as train data.
img_width=256
img_height=256
#model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])

def prepare(img_path):
    img = tf.keras.utils.load_img(img_path, target_size=(256, 256))
    x = image.img_to_array(img)
    x = x/255
    return np.expand_dims(x, axis=0)
    
    
result = model.predict([prepare('/content/Soil_With_CNN/test/Clay/clay.test.100.jpg')])
disease=tf.keras.utils.load_img('/content/Soil_With_CNN/test/Clay/clay.test.100.jpg')
plt.imshow(disease)
#result.astype(int)
print(Classes[int(result)])

字符串
该代码给出:

TypeError: only size-1 arrays can be converted to Python scalars


我该怎么解决?

bxjv4tth

bxjv4tth1#

我稍微猜测一下,但假设它是多个类result的典型softmax输出,那么每个类的概率-对于单个类预测,人们想要最高条目的索引。
class_predictions = result.argmax(axis=-1)
这会变成

[[0.1,0.9]
[0.6,0.4]]

字符串
转换为[1,0],可用于索引类

相关问题