import random

获取预测结果中的最大值索引

def get_max_index(predictions): max_index = 0 max_value = 0 for i in range(len(predictions)): if predictions[i] > max_value: max_value = predictions[i] max_index = i return max_index

随机选择15张图片,并展示它们的预测结果

fig, ax = plt.subplots(3, 5, figsize=(12, 8)) fig.suptitle('Random Test Images with Predictions')

for i, axis in enumerate(ax.flat): index = random.randint(0, len(test_images)-1) image = test_images[index] label = test_labels[index].argmax(axis=-1) predictions = model.predict(np.expand_dims(image, axis=0))[0]

# 获取前三个最大预测值的索引
max_indexes = []
for j in range(3):
    max_index = get_max_index(predictions)
    max_indexes.append(max_index)
    predictions[max_index] = 0

# 随机选择一个索引
predicted_index = random.choice(max_indexes)
predicted_label = predicted_index.argmax(axis=-1)

# 显示图片和它的预测标签
axis.imshow(image)
axis.axis('off')
class_names =['cane', 'cavallo', 'elefante', 'farfalla', 'gallina', 'gatto', 'mucca', 'pecora', 'ragno', 'scoiattolo']
# 显示原始图像以及它的实际标签和模型预测的标签
axis.set_title('Actual: ' + class_names[label] + '\nPredicted: ' + class_names[predicted_label])

plt.show()

图像分类模型预测结果可视化: 混合预测结果展示

原文地址: https://www.cveoy.top/t/topic/o9pp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录