如何在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
型
我该怎么解决?
1条答案
按热度按时间bxjv4tth1#
我稍微猜测一下,但假设它是多个类
result
的典型softmax输出,那么每个类的概率-对于单个类预测,人们想要最高条目的索引。class_predictions = result.argmax(axis=-1)
个这会变成
字符串
转换为
[1,0]
,可用于索引类