我想生成训练有素的模型的ROC曲线,但是我不知道如何使用ImageDataGenerator进行此操作。
我看到了该链接How can I plot AUC and ROC while using fit_generator and evaluate_generator to train my network?,但这仅回答了如何获取AUC的问题。
我还通过以下方式进行了尝试:
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)
答案 0 :(得分:0)
您的代码存在问题:
roc_curve(y_pred, test_generator.classes)
根据scikit-learn的文档,您需要传递分数(概率),而不是将类作为第二个参数。
另外,请注意,您的第一个参数是y_pred而不是y_true。
尝试调用roc_curve(y_true,y_scores),其中y_true是您的基本事实,y_scores是模型的输出概率(即model.predict(X_test))