如何将预测模型的结果与类标签进行比较(多类问题)[重复]

原文标题How to compare results from a Predicted Model to Class Labels (Multiclass Problem) [duplicate]

我有3类课程Tree, Stump, Ground。我为这些类别制作了一个列表:

CATEGORIES = ["Tree", "Stump", "Ground"]

当我打印我的预测时,它给了我输出

[[0. 1. 0.]]

我已经阅读了有关 numpy 的 Argmax 的信息,但我不完全确定在这种情况下如何使用它。

我试过使用

print(np.argmax(prediction))

但这给了我1的输出。这很好,但我想找出1的索引是什么,然后打印出类别而不是最高值。

import cv2
import tensorflow as tf
import numpy as np

CATEGORIES = ["Tree", "Stump", "Ground"]


def prepare(filepath):
    IMG_SIZE = 150 # This value must be the same as the value in Part1
    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)

# Able to load a .model, .h3, .chibai and even .dog
model = tf.keras.models.load_model("models/test.model")

prediction = model.predict([prepare('image.jpg')])
print("Predictions:")
print(prediction)
print(np.argmax(prediction))

我希望我的预测能告诉我:

Predictions:
[[0. 1. 0.]]
Stump

感谢您阅读 :) 我非常感谢任何帮助。

原文链接:https://stackoverflow.com//questions/71987807/how-to-compare-results-from-a-predicted-model-to-class-labels-multiclass-proble

回复

我来回复
  • Dr. Snoopy的头像
    Dr. Snoopy 评论

    你只需要用np.argmax的结果来索引类别:

    pred_name = CATEGORIES[np.argmax(prediction)]
    print(pred_name)
    
    1年前 0条评论