Skip to main content
 首页 » 编程设计

python之在考虑深度学习方法时,我的 ROC 曲线有什么问题

2024年12月31日7me-sa

我有一个使用 CNN 对图像进行分类的应用程序。我正在考虑输入大小为 64 x 64 图像 block 的 Mobilenet、Resnet 和 Densenet。为了对图像进行分类,我将其类别定义为对其 block 进行分类时出现最多的类别。这个问题非常不平衡,我的正样本多于负样本。我正在考虑三个数据集。

为了解决这个问题,我首先计算了f-measure、normalized accuracy等指标。以下是一些数据集考虑三个 CNN 的归一化精度结果:

为了构建 ROC 曲线,我决定将图像的分数定义为其 block 的平均分数,所以这就是我的问题开始的地方。考虑到以下三个 CNN,请查看这些数据集的一些 ROC 曲线:

看到获得 50% 归一化准确度的方法也获得 0.85、0.90 甚至 0.97 AUC,我觉得很奇怪。这最后一个 AUC 似乎来自一个近乎完美的分类器,但如果它的归一化准确度是 50%,这怎么可能呢?

那么,这是什么原因呢?是因为:

1- 我的问题是不平衡的。那么,主要在我的数据集中发现的阳性样本是否也是如此?我的 ROC 中感兴趣的类别是否会影响结果?

2-我使用 block 的平均分数作为图像的分数。有什么方法可以解决这个问题吗?

这是我用来生成标签和分数的代码 (PYTHON)

 base_model=MobileNet(input_shape (64,64,3),weights=None,include_top=False) 
    x = base_model.output 
    x = GlobalAveragePooling2D()(x) 
    x = Dense(64, activation='relu')(x) 
    predictions = Dense(2, activation='softmax')(x) 
    model = Model(inputs=base_model.input, outputs=predictions) 
    model.load_weights(model_path) 
 
    intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer("dense_2").output) 
    print("Loaded model from disk") 
    intermediate_layer_model.compile(loss='categorical_crossentropy', optimizer=algorithm, metrics=['accuracy']) 
 
    #read images, divide them into blocks, predict images and define the mean scores as the score for an image 
    with open(test_images_path) as f: 
            images_list = f.readlines() 
            images_name = [a.strip() for a in images_list] 
            predicted_image_vector = [] 
            groundtruth_image_vector = [] 
 
            for line in images_name: 
                x_test=[] 
                y_test=[] 
                print(line) 
                image = cv2.imread(line,1) 
                #divide into blocks 
                windows = view_as_windows(image, (64,64,3), step=64) 
 
                #prepare blocks to be tested later  
                for i in range(windows.shape[0]): 
                    for j in range(windows.shape[1]): 
                            block=np.squeeze(windows[i,j]) 
                            x_test.append(block) 
                            label = du.define_class(line) 
                            y_test.append(label) 
 
            #predict scores for all blocks in the current test image 
            intermediate_output = intermediate_layer_model.predict(np.asarray(x_test), batch_size=32, verbose=0) 
            #the score for an image is the mean score of its blocks 
            prediction_current_image=np.mean(intermediate_output, axis=0) 
            predicted_image_vector.append(prediction_current_image) 
 groundtruth_image_vector.append(np.argmax(np.bincount(np.asarray(y_test)))) 
 
    predicted_image_vector=np.array(predicted_image_vector) 
    groundtruth_image_vector=np.array(groundtruth_image_vector) 
    print("saving scores and labels to plot ROC curves") 
 
    np.savetxt(dataset_name+ '-scores.txt', predicted_image_vector, delimiter=',')  
    np.savetxt(dataset_name+ '-labels.txt', groundtruth_image_vector, delimiter=',')  

这是我用来生成ROC曲线的代码(MATLAB)

function plot_roc(labels_file, scores_file, name_file, dataset_name) 
 
    format longG 
    label=dlmread(labels_file); 
    scores=dlmread(scores_file); 
    [X,Y,T,AUC] = perfcurve(label,scores(:,2),1);    
 
    f=figure() 
    plot(X,Y); 
    title(['ROC Curves for Mobilenet in ' dataset_name]) 
    xlabel('False positive rate');  
    ylabel('True positive rate'); 
    txt = {'Area Under the Curve:', AUC}; 
    text(0.5,0.5,txt) 
    saveas(f, name_file); 
    disp("ok") 
 
 
 
end 

请您参考如下方法:

根据我对您的方法的了解 - 输入图像被分成单独的 block ,这些 block 由 CNN 模型独立处理。每个补丁都有自己的分类(或分数,取决于它是在 softmax 之后还是之前)。然后图像的类别是根据补丁类别的投票来确定的。

但与构建 ROC 曲线相比,您是在使用各个补丁的平均分数来确定图像的分类。

这两种不同的方法是 AUC 与归一化精度之间分离的原因。

例如:

假设您在图像中有 3 个具有以下概率的补丁(对于 2 个类别):

[cls a, cls b]

[0.51, 0.49]

[0.51, 0.49]

[0.01, 0.99]

通过投票,a 类是预测(2 个补丁对 1),平均得分 b 类是预测(0.657 对 0.343)。

我个人认为投票不是基于补丁对图像进行分类的正确方法,因为它没有考虑模型对不同补丁的确定性,如示例所示。但是您更熟悉您的数据集,所以也许我错了。

关于如何解决您的问题,我认为有关数据集和任务的性质的更多信息会有所帮助(不平衡程度如何,最终目标是什么,等等)