如何在Resnet 50分类中输出置信度? [英] How do I output confidence level in Resnet 50 classification?

查看:957
本文介绍了如何在Resnet 50分类中输出置信度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我训练了Resnet-50分类网络以对对象进行分类,然后使用以下代码评估网络。

I trained Resnet-50 classification network to classify my objects and I use the following code to evaluate the network.

from tensorflow.keras.models import load_model
import cv2
import numpy as np
import os

class_names = ["x", "y", "b","g", "xx", "yy", "bb","gg", "xyz","xzy","yy"]


model = load_model('transfer_resnet.h5')


model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

imgg = cv2.imread('/path to image/a1.jpg')


img = cv2.resize(imgg,(224,224))
img = np.reshape(img,[1,224,224,3])


classes = np.argmax(model.predict(img), axis = -1)

print(classes)

for i in classes:
    names = class_names[i]
print(names)



cv2.imshow("id",imgg)
key = cv2.waitKey(0)

处理后系统的输出为仅对象的类而不显示任何置信度百分比,我的问题是如何在测试期间也显示置信度百分比?

The output of the system after processing is only the class of the object without showing any confidence percentage, my question is how do I also show confidence percentage during the test?

推荐答案

model.predict 为您提供每个班级的信心。除此之外,使用 np.argmax 只会使您具有最高的置信度。

model.predict gives you the confidences for each class. Using np.argmax on top of this gives you only the class with the highest confidence.

因此,只需:

confidences = np.squeeze(model.predict(img))

我添加了 np.squeeze 删除任何单例尺寸,因为我们只是在查看单个图像,因此批量大小为1。因此,第一个尺寸仅具有大小为1,因此我要放入 np.squeeze 来删除单例尺寸。
此外,您可以通过执行以下操作获得此图像的最佳分类以及信心:

I've added np.squeeze to remove any singleton dimensions as we're just looking at a single image, so the batch size is 1. The first dimension therefore will only have a size 1, so I'm putting in np.squeeze to remove the singleton dimension. Further, you can get the best class for this image as well as the confidence by doing:

class = np.argmax(confidences)
name = class_names[class]
top_conf = confidences[class]

如果您想走得更远,并说出预测中的前5个类别,则可以执行 np.argsort ,对预测进行排序,然后找到相应类别的索引并显示这些置信度。请注意,我将按负数进行排序,因此我们以降序获得可信度,因此排序的第一个索引对应于具有最高可信度的类别。我还将概率按100缩放,以根据您的要求为您提供百分比的置信度:

If you want to go further and show say the top 5 classes in the prediction, you can do a np.argsort, sort the predictions then find the indices of the corresponding classes and show those confidences. Take note that I'm going to sort by the negative so we get the confidences in descending order so the first index of the sort corresponds to the class with the highest confidence. I'll also scale the probability by 100 to give you a percentage confidence as you requested:

k = 5
confidences = np.squeeze(model.predict(img))
inds = np.argsort(-confidences)
top_k = inds[:k]
top_confidences = confidences[inds]

for i, (conf, ind) in enumerate(zip(top_confidences, top_k)):
    print(f'Class #{i + 1} - {class_names[ind]} - Confidence: {100 * conf}%')

您可以修改代码以显示所需数量。我已经简化了这个过程,因此,如果您只想选择最自信的课程,请设置 k = 1

You can adapt the code to display how many you'd like. I've made this easy for you to play with so if you just want the most confident class only, set k = 1.

这篇关于如何在Resnet 50分类中输出置信度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆