【问题标题】:Always getting AUC = 0.5总是得到 AUC = 0.5
【发布时间】:2019-06-01 04:36:33
【问题描述】:

我正在尝试使用卷积神经网络 (CNN) 来预测测试图像的类别,如下所示:

for root, dirs, files in os.walk(test_directory):
    for file in files:
        img = cv2.imread(root + '/' + file)
        img = cv2.resize(img,(512,512),interpolation=cv2.INTER_AREA)
        img = np.expand_dims(img, axis=0)
        img = img/255.0
        if os.path.basename(root) == 'nevus':
            label = 1
        elif os.path.basename(root) == 'melanoma':
            label = 0
        labels.append(label)
        img_class = model.predict_classes(img)
        img_class_probability = model.predict(img)
        prediction_probability = img_class_probability[0]
        prediction_probabilities.append(prediction_probability)
        prediction = img_class[0]
        if prediction == label:
            correct_classification = correct_classification + 1
        number_of_test_images = number_of_test_images + 1

fpr, tpr, thresholds = roc_curve(labels, prediction_probabilities)
auc_value = auc(fpr, tpr)

我想问一下为什么我的 AUC = 0.5?也许我做错了什么?

谢谢。

【问题讨论】:

  • 您是否计算过其他指标,例如准确性、敏感性、特异性以及您获得的分数?
  • @Suleiman 感谢您的友好回复。我计算了准确率,大约是 77%。但是,似乎预测的类别都是相同的(即总是“黑色素瘤”)。
  • 您的代码似乎不包含模型。请向我们展示构建和训练模型的代码(包括学习参数初始化器和优化器的选择)
  • 如果你的模型总是预测一个类别并且你有一个平衡的数据集,即相同数量的痣和黑色素瘤,那么这将解释为什么 AUC = 0.5

标签: python tensorflow keras roc auc


【解决方案1】:

在这种情况下,可能是 AUC 的阈值设置不正确,导致所有模型输出值(分数/概率)在所有阈值选择下始终是同一类,因此 AUC 将始终为 0.5

以下示例说明阈值对 AUC 的影响

import tensorflow as tf # tf 2.0+

y_true = [0, 0, 1, 1, 1, 1]
y_pred = [0.001, 0.002, 0.003, 0.004, 0.005, 0.006]

#--------------------------------------------------------------
m1 = tf.keras.metrics.AUC(thresholds=[0.0, 0.5, 1.0])
m1.update_state(y_true, y_pred)

# when threshold=0.0 , y_class always be 1
# when threshold=0.5 , y_class always be 0
# when threshold=1.0 , y_class always be 0 

print('AUC={}'.format(m1.result().numpy())) 
# output: AUC=0.5


#--------------------------------------------------------------
m2 = tf.keras.metrics.AUC(thresholds=[0.0, 0.0045, 1.0])
m2.update_state(y_true, y_pred)

# when threshold=0.0    , y_class always be 1 
# when threshold=0.0045 , y_class will   be [0, 0, 0, 0, 1, 1]
# when threshold=1.0    , y_class always be 0 

print('AUC={}'.format(m2.result().numpy())) 
# output: AUC=0.75


#--------------------------------------------------------------
m3 = tf.keras.metrics.AUC(num_thresholds=300) 
# print(m3.thresholds)
m3.update_state(y_true, y_pred)

print('AUC={}'.format(m3.result().numpy())) 
# output: AUC=0.875

【讨论】:

    猜你喜欢
    • 2019-09-21
    • 2020-12-30
    • 2018-05-14
    • 2018-09-27
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多