我有一个名为TRAIN的目录,它有37个子目录,其中包含不同大小的图像,我想用TensorFlow的ImageDataGenerator加载这些图像,但它需要相同大小的图像。我想为这些图像添加填充以调整它们的大小。
tf.image.resize_with_crop_or_pad(
image, target_height, target_width
)
我在网上找到了上面的代码,但我不知道如何使用它。
使用tf.keras.preprocessing.image_dataset_from_directory
调整目录中的图像大小。在image_size=(img_height, img_width)
中提及所需的图像大小。默认image_size为(256256(查找以下示例代码
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)