总是获得AUC = 0.5

时间:2019-01-06 04:45:28

标签: python tensorflow keras roc auc

我正在尝试使用卷积神经网络(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?也许我做错了什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

在这种情况下,可能是因为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