Stack Exchange
ROC Curve area is nan CNN model
I have implemented a CNN based Classification of image datasets but the problem is it provides a nan value of the ROC_Curve's area.
Here is the coding part,
#Package Initilize
import numpy as np
from sklearn import metrics
import matplotlib.pyplot as plt
import tensorflow as tf
import keras
from keras.preprocessing import image
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

train_datagen = image.ImageDataGenerator(
    rescale=1/255,
    shear_range = 0.3,
    zoom_range = 0.3,
    horizontal_flip = True,
)
validation_datagen = image.ImageDataGenerator(
    rescale = 1/255
)

target_size = (100,100,3)

train = train_datagen.flow_from_directory(
    'Dataset/Train',
    target_size = target_size[:-1],
    batch_size = 32,
    class_mode = 'categorical'
)

validation = validation_datagen.flow_from_directory(
    'Dataset/Validation',
    target_size = target_size[:-1],
    batch_size = 32,
    class_mode = 'categorical'
)

test = validation_datagen.flow_from_directory(
    'Dataset/Test',
    target_size = target_size[:-1],
    batch_size = 32,
    shuffle = False,
    class_mode = 'categorical'
)

input_layer = keras.layers.Input(shape=target_size)

#Model Define
conv2d_1 = keras.layers.Conv2D(filters=64, kernel_size=(3,3), strides=1, padding='same', 
activation='relu', kernel_initializer='he_normal')(input_layer)
batchnorm_1 = keras.layers.BatchNormalization()(conv2d_1)
maxpool1=keras.layers.MaxPool2D(pool_size=(2,2))(batchnorm_1)

conv2d_2 = keras.layers.Conv2D(filters=32, kernel_size=(3,3), strides=1, padding='same', 
activation='relu',kernel_initializer='he_normal')(maxpool1)
batchnorm_2 = keras.layers.BatchNormalization()(conv2d_2)
maxpool2=keras.layers.MaxPool2D(pool_size=(2,2))(batchnorm_2)

flatten = keras.layers.Flatten()(maxpool2)
dense_1 = keras.layers.Dense(256, activation='relu')(flatten)

dense_2 = keras.layers.Dense(n_classes, activation='softmax')(dense_1)
dense_3 = keras.layers.Dense(n_classes, activation='softmax')(dense_2)

model = keras.models.Model(input_layer, dense_3)

#Compile Define
model.compile(optimizer=keras.optimizers.Adam(0.001),
      loss='categorical_crossentropy',
      metrics=['acc'])
model.summary()

#Fit the model
history = model.fit_generator(generator=train, validation_data=validation,
            epochs=2)

#ROC Curve Define
x, y = validation.next()
prediction = model.predict(x)

predict_label1 = np.argmax(prediction, axis=-1)
true_label1 = np.argmax(y, axis=-1)

y = np.array(true_label1)

scores = np.array(predict_label1)
fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=9)
roc_auc = metrics.auc(fpr, tpr)

plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange',
 lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic (ROC)')
plt.legend(loc="lower right")
plt.show()

The problem of ROC_Curve is given in the attached file, please check it.


The doc of sklearn.metrics.roc_curve() states, right at the top:

Note: this implementation is restricted to the binary classification task.

But it seems you have a multi-class model.
You can check this site for options regarding multi-class ROC with sklearn.
