0

I want to generate a ROC curve of my trained model, but I do no know how to do this using a ImageDataGenerator().

I saw this link How can I plot AUC and ROC while using fit_generator and evaluate_generator to train my network?, but this only answered the question of how to get the AUC.

I also tried it in the following way:

y_pred =  model.predict_generator(test_generator, steps= step_size_test)
fpr, tpr, tresholds = roc_curve(y_pred, test_generator.classes)

This gave me an error

This is a part of my code


model.compile(loss="binary_crossentropy", optimizer= 'Adam', metrics=['accuracy', auc])
  

train_datagen = ImageDataGenerator(rescale=1.0 / 255.0)
train_generator = train_datagen.flow_from_directory(
    directory=f'./data/train/',
    target_size=(Preprocess.image_resolution, Preprocess.image_resolution),
    color_mode="grayscale",
    batch_size=64,
    classes=['a', 'b'],
    class_mode="binary",
    shuffle=True,
    seed=42
)

valid_datagen = ImageDataGenerator(rescale=1.0 / 255.0)
valid_generator = valid_datagen.flow_from_directory(
    directory=f'./data/valid/',
    target_size=(Preprocess.image_resolution, Preprocess.image_resolution),
    color_mode="grayscale",
    batch_size=8,
    classes=['a', 'b'],
    class_mode="binary",
    shuffle=True,
    seed=42
)

test_datagen = ImageDataGenerator()
test_generator = test_datagen.flow_from_directory(
    directory=f'./data/test/',
    target_size=(Preprocess.image_resolution, Preprocess.image_resolution),
    color_mode="grayscale",
    batch_size=1,
    classes=['a', 'b'],
    class_mode='binary',
    shuffle=False,
    seed=42
)

step_size_train = train_generator.n // train_generator.batch_size
step_size_valid = valid_generator.n // valid_generator.batch_size
step_size_test = test_generator.n // test_generator.batch_size

model = build_three_layer_cnn_model()

history = model.fit_generator(generator=train_generator, 
                    steps_per_epoch=step_size_train,
                    validation_data=valid_generator,
                    validation_steps=step_size_valid,
                    epochs=10)
Timbus Calin
  • 13,809
  • 5
  • 41
  • 59
Leslie
  • 65
  • 1
  • 8

1 Answers1

1

The problem with your code is here :

roc_curve(y_pred, test_generator.classes)

According to the documentation of scikit-learn, you will need to pass the scores(probabilities), instead of classes as a second parameter.

Also, please note that your first parameter is y_pred instead of y_true.

Try by calling roc_curve(y_true,y_scores), where y_true is your ground truth and y_scores are the output probabilities by your model(i.e. model.predict(X_test))

Documentation for ROC-Curve: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html#sklearn.metrics.roc_curve

Timbus Calin
  • 13,809
  • 5
  • 41
  • 59