如何提高 CNN 分类中的 F1-score

How to improve the F1-score in CNN classification

我的项目是关于class大脑核磁共振扫描的化。 重要的是我的模型更多地针对误报而不是误报,因为我正在处理医疗数据。最好是假检测肿瘤然后不检测。 我的问题是如何提高 f1 分数。

The brain mri scan

我尝试了什么:1) 我对肿瘤进行了欠采样 class -> 这将我的 f1 分数提高了 1%

数据集:https://www.kaggle.com/mateuszbuda/lgg-mri-segmentation

Data balance

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.preprocessing import OneHotEncoder
from sklearn import preprocessing
from sklearn.utils import shuffle

import cv2


import matplotlib.image as mpimg
from skimage.io import imread, imshow
from skimage.color import rgb2gray
from skimage import data, color, io, filters, morphology,transform, exposure, feature, util
from scipy import ndimage

from sklearn.preprocessing import StandardScaler
import seaborn as sns




import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Input, Dense, Dropout, Flatten, BatchNormalization,concatenate
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Conv2DTranspose
from tensorflow.keras import backend as K
from tensorflow.python.keras.layers.core import Dropout, Lambda
from tensorflow.python.keras.layers.core import * 
from tensorflow.keras.layers import *
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing import image
from tensorflow.keras import regularizers

from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.applications.vgg19 import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing.text import Tokenizer

import glob


from tensorflow.keras.callbacks import *
# Neural network

# Model checkpoint
checkpoint_filepath = './Classification_brain_best.h5'

# Early stopping
mc = tf.keras.callbacks.ModelCheckpoint(filepath = checkpoint_filepath,save_weights_only=True, monitor='val_loss',mode='auto',save_best_only=True)
# Early stopping
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience = 50)

adam = tf.keras.optimizers.Adam(lr=0.001)

# Neural network parameters
#-------------------------
num_classes = 2
img_rows, img_cols = image_size, image_size
input_shape = (img_rows, img_cols,3) # 1 -> grijs waarden, 3 -> kleur
epochs = 500
batch_size = 64

#------------ layers ----------

OwnClassificationNN=Sequential()

OwnClassificationNN.add(Conv2D(64,(3,3),input_shape=input_shape))
OwnClassificationNN.add(Activation('relu'))
OwnClassificationNN.add(MaxPooling2D(pool_size=(2,2)))
#The first CNN layer followed by Relu and MaxPooling layers

OwnClassificationNN.add(BatchNormalization())
OwnClassificationNN.add(Dropout(0.1))

OwnClassificationNN.add(Conv2D(32,(3,3)))
OwnClassificationNN.add(Activation('relu'))
OwnClassificationNN.add(MaxPooling2D(pool_size=(2,2)))

OwnClassificationNN.add(BatchNormalization())

OwnClassificationNN.add(Dropout(0.2))

OwnClassificationNN.add(Conv2D(16,(3,3)))
OwnClassificationNN.add(Activation('relu'))
OwnClassificationNN.add(MaxPooling2D(pool_size=(2,2)))



#---------- Classification part ------------------------------
OwnClassificationNN.add(Flatten())
OwnClassificationNN.add(Dropout(0.5)) # ---->> to avoid overfitting 
#Flatten layer to stack the output convolutions from second convolution layer
OwnClassificationNN.add(Dense(8,activation='relu'))
#Dense layer of 64 neurons

OwnClassificationNN.add(Dense(num_classes,activation='softmax'))
#The Final layer with two outputs for two categories

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


**the Model for classification**

# Trainen van het CNN
history = OwnClassificationNN.fit(X_train, y_train,batch_size=batch_size, epochs=epochs, validation_data=(X_val,y_val) ,verbose=1,callbacks=[es,mc])

This is the metric of the prediction on test data

有几种方法可以解决漏报问题,最明显的方法是提高模型的准确性,这可以通过先前训练的模型(迁移学习/微调)来完成,减少漏报和错误积极的。您可以尝试一下,但正如测试指标所显示的那样,您已经有了一个非常好的模型,具有很高的准确性。

但是,在某些像您这样的假阴性非常敏感的特殊情况下,值得强制模型减少 return 显着增加假阳性的假阴性。这可以很容易地完成,因为你的神经网络的最后一层是一个具有 softmax 激活函数的密集层。这个函数作为 las 层激活函数非常有效,因为它计算多项式概率分布。

Softmax activation function

换句话说,你的最后一层是两个神经元层,其中每个神经元给你有或没有肿瘤的概率。因此,如果“具有肿瘤神经元”的概率高于 0.5,它将将该病例归类为阳性,反之亦然。您始终可以更改阈值以强制您的模型分类为概率高于 0.3 或 0.4 的情况,从而减少假阴性。