【问题标题】:TensorFlow load image with image_dataset_from_directoryTensorFlow 使用 image_dataset_from_directory 加载图像
【发布时间】:2021-08-21 13:34:02
【问题描述】:

我在使用image_dataset_from_directory 创建tf.data.Dataset 进行一对一任务时遇到了一些麻烦。这意味着我会给模型一个输入图像,输出将是另一个图像。

我的数据集目录是这样的:

Dataset/
...input/
......a_image_1.jpg
......a_image_2.jpg
...output/
......a_image_1.jpg
......a_image_2.jpg

在数据集目录中对应的输入图像和目标图像具有相同的名称。我正在尝试通过以下方式加载数据集:

dataset_url = "Project/Dataset"
input_size= 300
batch_size = 8

train_ds = image_dataset_from_directory(
    dataset_url,
    labels='inferred',
    batch_size=batch_size,
    image_size=(input_size, input_size),
    validation_split=0.2,
    subset="training",
    seed=1337,
    label_mode='int',
)

valid_ds = image_dataset_from_directory(
    dataset_url,
    labels='inferred',
    batch_size=batch_size,
    image_size=(input_size, input_size),
    validation_split=0.2,
    subset="validation",
    seed=1337,
    label_mode='int',
)

此过程将两个文件夹中的所有图像作为类 1 和 2 加载。现在如何将两个类映射为 inputtarget?我在正确的轨道上吗?有没有其他办法?

【问题讨论】:

  • 你只需要这个:model.fit(train_ds, epochs=2, validation_data=valid_ds).
  • 这不是分类任务。我有点困惑。
  • 我没有找到任何文档声称 image_dataset_from_directory 支持任何东西,而不是 intcategoricalbinary 用于自动编码器。但是,如果您打算用于自动编码器,则可以改用 ImageDataGenerator.flow_from_directory() 并设置 class_mode='input'。如果你的标签是其他一些图片,你可以设置labels=None或者label_mode=None,定义2个image_dataset_from_directory。 1 代表 x,1 代表 y,然后是 model.fit(x,y)

标签: tensorflow machine-learning keras computer-vision


【解决方案1】:

创建目录数据。在数据中创建两个子目录图像,目标。在图像目录中放置您的图像。在目标目录中放置您的目标图像。确保您的图像和目标图像具有完全相同的文件名。这是必需的,以便在获取一批图像时,以相同的顺序获取其对应的目标图像。我使用 ImageDataGenerator.flow_from_directory 如下:

image_dir=r'c:\data\image'
target_dirr'c:\data\target'
target_size=(224,224) # set this to the target size you want
channels=3 # for color images
color_mode='rgb'
shuffle=True,
seed=123
class_mode=None
batch_size=10 # set this to desired batch size
vsplit=.2 # set this to the validation split you want
gen=ImageDataGenerator(rescale=1/255, validation_split=vsplit)
image_gen=gen.flow_from_directory(img_dir, target_size=target_size, color_mode=color_mode,class_mode=class_mode,seed=seed,
                                batch_size=batch_size, subset='training')
valid_image_gen=gen.flow_from_directory(img_dir, target_size=target_size, color_mode=color_mode,class_mode=class_mode,seed=seed,
                                batch_size=batch_size, subset='validation')
target_gen=gen.flow_from_directory(target_dir,target_size=target_size, color_mode=color_mode,class_mode=class_mode,seed=seed,
                                batch_size=batch_size, subset='training') 
valid_target_gen=gen.flow_from_directory(target_dir,target_size=target_size, color_mode=color_mode,class_mode=class_mode,seed=seed,
                                batch_size=batch_size, subset='validation') 
composite_gen=zip(image_gen, target_gen)
valid_gen=zip(valid_image_gen, valid_target_gen)

composite_gen 将产生 (image, target image) 的元组。测试一下

images, targets=next(composite_gen)
print (images.shape, targets.shape)
img1= images[0]
target1=targets[0]
# show these two images to ensure the image and targets are matched as required
plt.subplot(1,2,1)
plt.imshow(img1)
plt.subplot(1,2,2)
plt.imshow(target1)

您可以在 valid_gen 上运行相同的测试。然后使用composite_gen 和valid_gen 作为model.fit 的输入

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多