使用ImageDataGenerator时如何产生ROC曲线 [英] How to produce an ROC curve when using a ImageDataGenerator

查看:143
本文介绍了使用ImageDataGenerator时如何产生ROC曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成训练有素的模型的ROC曲线,但是我不知道如何使用ImageDataGenerator进行此操作.

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)

这给了我一个错误

这是我的代码的一部分


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)

推荐答案

您的代码存在问题:

roc_curve(y_pred, test_generator.classes)

根据scikit-learn的文档,您将需要传递分数(概率),而不是将类作为第二个参数.

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

另外,请注意,您的第一个参数是y_pred而不是y_true.

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

尝试调用roc_curve(y_true,y_scores),其中y_true是您的基本事实,y_scores是模型的输出概率(即model.predict(X_test))

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))

ROC曲线的文档: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html#sklearn.metrics.roc_curve

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

这篇关于使用ImageDataGenerator时如何产生ROC曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆