【问题标题】:imagedatagen.flow_from_directory() - random test set predictions?imagedatagen.flow_from_directory() - 随机测试集预测?
【发布时间】:2021-06-02 14:32:13
【问题描述】:

我通过ImageDataGenerator() 为 CNN 二元分类问题加载我的训练集和测试集,如下所示:

datagen_train = ImageDataGenerator(validation_split=0.2, zoom_range = 0.2, width_shift_range=0.1, height_shift_range=0.1, horizontal_flip = True,
datagen_test = ImageDataGenerator()

train_it = cnn2_datagen.flow_from_directory(TRAIN_FOLDER, class_mode='binary', batch_size=32, target_size=(150,150), subset='training')
val_it = cnn2_datagen.flow_from_directory(TRAIN_FOLDER, class_mode='binary', batch_size=32, target_size=(150,150), subset='validation')
test_it = cnn2_datagen_test.flow_from_directory(TEST_FOLDER, class_mode='binary', batch_size=32, target_size=(150,150))

然后我创建我的模型并在其上拟合 train 和 val 数据集。之后,使用model.evaluate(test_it) 返回的准确率为 88%。然而,问题出现在使用model.predict(test_it) 时即使我使用完全相同的测试集,输出预测总是不同的顺序!例如:

y_pred = model.predict(test_it)

print(y_pred)

[0, 0, 1, 1]

然后我再次运行相同的代码块,model.predict(test_it) 的结果是 [1, 0, 0, 1] 。这种情况每次都会发生,而不会更改我的代码中的任何内容,并阻止我创建混淆矩阵,因为我无法将测试集中每个数据点的真实标签与 y_pred 进行比较,因为它是不同的预测顺序。

任何关于为什么会发生这种情况的建议将不胜感激。

【问题讨论】:

  • 在 test_it 代码的 flow_from_directory 中设置 shuffle=False。你有多少张测试图片?
  • @GerryP 成功了,谢谢!这是否意味着每次我运行预测时都会重新初始化测试集?因为它最初只是在开始时才被初始化,因此无论我理解什么,它都不应该改变
  • ipredict 不会随机播放,但 flow-from-directory 会。满意请采纳答案
  • 不能接受,因为您发表了评论,您还没有将其发布为答案

标签: python tensorflow keras artificial-intelligence


【解决方案1】:

在 test_it 代码的 flow_from_directory 中设置 suffle=False

【讨论】:

    猜你喜欢
    • 2019-07-21
    • 2014-08-07
    • 2019-05-12
    • 2021-03-21
    • 2019-05-04
    • 2015-12-22
    • 2020-02-13
    • 1970-01-01
    相关资源
    最近更新 更多