简体   繁体   English

有没有办法在自定义 Tensorflow 模型中找到每个类的平均精度和召回率?

[英]Is there a way to find the average precision and recall of each class in the custom Tensorflow model?

I have trained a model using TensorFlow and SSD MobileNet.我已经使用 TensorFlow 和 SSD MobileNet 训练了一个模型。 I was able to find the mean average precision of the model.我能够找到模型的平均平均精度。 Is their a way to find the average precision of each class in the models.他们是一种找到模型中每个类的平均精度的方法。 I am using Tensorflow 2.5 version.我正在使用 TensorFlow 2.5 版本。 Thanks in advance提前致谢

You can use sklearn per the code below.您可以按照下面的代码使用 sklearn。 For both the confusion matrix and the classification report you need to provide y_predict and y_true.对于混淆矩阵和分类报告,您需要提供 y_predict 和 y_true。 After you train you model then do predictions on the test set.训练模型后,对测试集进行预测。 Somewhere I assume you have y_true in your code as the label for the classes.我假设您的代码中有 y_true 作为类的标签。 I will assume they are present in a list called y_true and are in the SAME order as your inputs to model.predict.我将假设它们存在于一个名为 y_true 的列表中,并且与您对 model.predict 的输入的顺序相同。 I will also assume you have a list called classes which are the names of your classes in order.我还将假设您有一个名为 classes 的列表,它们是按顺序排列的类的名称。 For example if cats is label 0 and dogs is label 1 then classes=[cats, dogs]例如,如果猫是标签 0,狗是标签 1,那么 classes=[cats, dogs]

from sklearn.metrics import confusion_matrix, classification_report
preds=model.predict ---etc
ypredict=[]
for p in preds:
    index=np.argmax(p)
    y_predict.append(index)
y_true= np.array(y_true)        
y_predict=np.array(y_predict)    
# create a confusion matrix 
cm = confusion_matrix(y_true, y_predict ) 
# code below formats the confusion matrix plot       
length=len(classes)
if length<8:
    fig_width=8
    fig_height=8
 else:
    fig_width= int(length * .5)
    fig_height= int(length * .5)
plt.figure(figsize=(fig_width, fig_height))
sns.heatmap(cm, annot=True, vmin=0, fmt='g', cmap='Blues', cbar=False)       
plt.xticks(np.arange(length)+.5, classes, rotation= 90)
plt.yticks(np.arange(length)+.5, classes, rotation=0)
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix")
plt.show()
clr = classification_report(y_true, y_pred, target_names=classes)
print("Classification Report:\n----------------------\n", clr)

Below is an example of a classification report以下是分类报告的示例

Classification Report:
----------------------
               precision    recall  f1-score   support

      Banana       1.00      1.00      1.00        15
       Bread       1.00      1.00      1.00        15
        Eggs       1.00      1.00      1.00        15
        Milk       1.00      1.00      1.00        15
       Mixed       1.00      1.00      1.00        12
      Potato       1.00      1.00      1.00        15
     Spinach       1.00      1.00      1.00        15
      Tomato       1.00      1.00      1.00        15

    accuracy                           1.00       117
   macro avg       1.00      1.00      1.00       117
weighted avg       1.00      1.00      1.00       117

I am facing this same problem.我面临同样的问题。 Can't find where this y_pred or y_test come from.找不到这个 y_pred 或 y_test 来自哪里。 There is no y_pred or y_test defined during the SSD MobileNetV2 training. SSD MobileNetV2 训练期间没有定义 y_pred 或 y_test。 If anyone have an answer please share如果有人有答案请分享

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 张量流计算召回率和精度 - computing recall and precision with tensorflow Tensorflow 中多类分类的分类精度和召回率? - Class wise precision and recall for multi class classification in Tensorflow? 为什么我的TensorFlow模型-1.000的平均准确率? - Why is the Average Precision of my TensorFlow model -1.000? 为什么使用tensorflow做物体检测时平均准确率和平均召回率很低? - Why are the average precision and average recall very low when using tensorflow to do object detection? 通过 Class 计算精度和召回率 - Calculating Precision and Recall by Class 训练模型并支持召回率/精度的最佳方法是什么? - What is the best way to train your model and favoring recall/precision? 如何使sklearn模型达到预定的精度或召回某个类? - How to make sklearn model reach a predefine precision or recall on some class? 在 Tensorflow 2 中的每个 epoch 之后计算每个类的召回率 - Calculate recall for each class after each epoch in Tensorflow 2 model 中的精度和召回率相同 - Precision and recall are the same within a model 任何 sklearn 模块都可以在 k 折交叉验证中返回负类的平均精度和召回分数吗? - Can any sklearn module return average precision and recall scores for negative class in k-fold cross validation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM