【问题标题】:How to use tf.keras.utils.image_dataset_from_directory to load test dataset?如何使用 tf.keras.utils.image_dataset_from_directory 加载测试数据集?
【发布时间】:2022-08-03 16:24:04
【问题描述】:

我在我的二进制分类 Mobilenet V2 模型中使用 tf.keras.utils.image_dataset_from_directory 通过定义训练和验证子集来拆分数据集,如下所示:

train_dataset = tf.keras.utils.image_dataset_from_directory(directory,
                                             shuffle=True,
                                             batch_size=BATCH_SIZE,
                                             image_size=IMG_SIZE,
                                             validation_split=0.2,
                                             subset=\'training\',
                                             seed=42)
validation_dataset = tf.keras.utils.image_dataset_from_directory(directory,
                                             shuffle=True,
                                             batch_size=BATCH_SIZE,
                                             image_size=IMG_SIZE,
                                             validation_split=0.2,
                                             subset=\'validation\',
                                             seed=42)

现在,我想在一组图像上使用model.predict() 来查看预测。我如何使用image_dataset_from_directory 考虑到不会有两个不同的文件夹包含各自的类,而只有一个我想要预测的文件夹?另外,现在image_dataset_from_directory函数的参数应该是什么?

  • 您需要有一个包含测试图像的单独目录。然后对 train/val 数据集执行相同的操作,但使用 shuffle=False 和不使用 validation_split

标签: python tensorflow keras tf.keras


【解决方案1】:

如前所述@Djinn, 你可以用同样的方法来做。

例如, 假设我在dogs_cats/binary_data 文件夹中有一个binary_data 文件夹,我在其中存储了多个类图像(每只猫和狗5-5 个图像),那么您可以提供直到dogs_cats 的路径。

这将通过声明类 1 自动获取 binary_data 文件夹中的所有图像,您可以在其中存储多个类图像(二进制 - 根据模型)。

现在您可以在model.predict() 中传递此数据集,并可以检查每个图像的预测。

请检查以下代码:

test_dataset = tf.keras.utils.image_dataset_from_directory(
          "/content/GoogleDrive/My Drive/MY WORK/dataset/dogs_cats/",
           shuffle=True,        #or False
           batch_size=BATCH_SIZE,
           image_size=IMG_SIZE)

输出:

Found 10 files belonging to 1 classes.

和预测部分:

predictions = model.predict(test_dataset)
predictions = tf.where(predictions < 0.5,0, 1)
print('Predictions:\n', predictions.numpy())

输出:

Predictions:
 [[0]
 [1]
 [0]
 [0]
 [1]
 [1]
 [1]
 [1]
 [1]
 [1]]

注意:预测的准确性可能取决于模型性能。

【讨论】:

    猜你喜欢
    • 2013-03-18
    • 1970-01-01
    • 2013-05-24
    • 2018-11-12
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2018-04-27
    相关资源
    最近更新 更多