如何找到此CNN模型(keras)的ROC曲线和AUC分数 [英] How to find the ROC curve and AUC score of this CNN model (keras)
问题描述
我在keras中的CNN代码如下:
My CNN code in keras is as follows:
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
classifier = Sequential()
#1st Conv layer
classifier.add(Convolution2D(64, (9, 9), input_shape=(64, 64, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(4,4)))
#2nd Conv layer
classifier.add(Convolution2D(32, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
#Flattening
classifier.add(Flatten())
# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout(0.1))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout(0.2))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 2, activation = 'softmax'))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
#Fitting dataset
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('dataset/training_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical')
test_set = test_datagen.flow_from_directory('dataset/test_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical')
classifier.fit_generator(
training_set,
steps_per_epoch=(1341+3875)/32,
epochs=15,
validation_data=test_set,
validation_steps=(234+390)/32)
无论我在sklearn.metrics中看到roc_curve的用法如何,它都带有x_train,y_train,x_test,y_test之类的参数,我知道它们可以是pandas DataFrames,但就我而言并非如此.我如何绘制ROC曲线并获得AUC分数,用于像此处这样的CNN模型训练?
Wherever I see the use of roc_curve from sklearn.metrics, it takes parameters like x_train, y_train, x_test, y_test which I know can be pandas DataFrames but in my case it is not the case. How do I plot the ROC curve and get AUC score for model training for CNNs like here?
推荐答案
我知道了.我要做的就是将从preds = classifier.predict(test_set)
获得的preds的数据类型与从labels = test_set
获得的true_labels匹配. Preds基本上是一个numpy.ndarray,包含具有np.float32值的单个元素列表.将标签转换为相同的格式和形状后,即可完成roc_curve的工作.
I got it working. All I had to do was match the datatype of preds obtained from preds = classifier.predict(test_set)
with the true_labels I got from labels = test_set
. Preds is basically a numpy.ndarray containing single element lists which have np.float32 values. Conversion of labels to that same format and shape got the roc_curve working.
此外,我必须在fpr, tpr, threshold = roc_curve(true_labels, preds)
中添加第三个变量阈值,以免出现ValueError:弹出太多无法解包错误的值.
Also, I had to add a third variable threshold in fpr, tpr, threshold = roc_curve(true_labels, preds)
so no ValueError: too many values to unpack error popped up.
这篇关于如何找到此CNN模型(keras)的ROC曲线和AUC分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!