【问题标题】:Resize all images in a subdirectory调整子目录中所有图像的大小
【发布时间】:2021-08-23 02:05:12
【问题描述】:

我有一个名为 TRAIN 的目录,它有 37 个子目录,其中包含不同大小的图像,我想用 TensorFlow 的 ImageDataGenerator 加载这些图像,但它需要相同大小的图像。我想为这些图像添加填充以调整它们的大小。

  tf.image.resize_with_crop_or_pad(
    image, target_height, target_width
)

我在网上找到了上面的代码,但我不知道如何使用它。

【问题讨论】:

  • 没有。 ImageDataGenerator 不希望所有图像的大小相同。您可以在生成器中通过target_size=(width, height) 参数指定输出大小。
  • 所以,如果我的一张图片是(230, 300),如果我指定 target_size=(400, 400),它会将图片拉伸到 (400,400) 还是将添加填充? @Kaveh
  • 它将使用nearest方法,这是interpolation的默认值。如果您愿意,可以使用其他方法。检查这个:tensorflow.org/api_docs/python/tf/keras/preprocessing/image/…

标签: tensorflow image-processing


【解决方案1】:

使用tf.keras.preprocessing.image_dataset_from_directory 调整目录中的图像大小。 在image_size=(img_height, img_width) 中提及所需的图像尺寸。默认 image_size 将为 (256,256) 找到下面的示例代码

import tensorflow as tf
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 2010-11-06
    • 1970-01-01
    • 2014-08-22
    • 2018-12-12
    • 1970-01-01
    • 2013-11-20
    • 2014-05-05
    • 1970-01-01
    相关资源
    最近更新 更多