在 Tensorflow 中为输入和输出保留相同的数据集扩充

Keep same dataset augmentation for input and output in Tensorflow

我有一个包含图像作为输入和输出的批处理数据集。代码是这样的:

os.chdir(r'E:/trainTest')

def process_img(file_path):
    img = tf.io.read_file(file_path)
    img = tf.image.decode_png(img, channels=3)
    img = tf.image.convert_image_dtype(img, tf.float32)
    img = tf.image.resize(img, size=(img_height, img_width))
    return img

x_files = glob('input/*.png')
y_files = glob('output/*.png')

files_ds = tf.data.Dataset.from_tensor_slices((x_files, y_files))


#Dataset which gives me input-output 
files_ds = files_ds.map(lambda x, y: (process_img(x), process_img(y))).batch(batch_size)

#model init etc
#----

model.fit(files_ds,epochs=25)

问题是我的模型没有足够的图像。所以我的问题是,如何从 files_ds 创建增强图像(如翻转、旋转、缩放等)?因为必须以与输入图像相同的方式来增强输出图像。

这个问题实际上来自以下问题,我想在它自己的部分提出这个问题:

可以使用tf.keras.preprocessing.image.ImageDataGenerator进行预处理。 This 是完整选项的文档页面。我已经分享了一个关于如何将其与 flow_from_directory 一起使用的小示例。您没有事先阅读图像并用完您的 RAM。图像从目录加载,预处理并在需要时输入模型。

# we create two instances with the same arguments
data_gen_args = dict(rescale=1./255,
                     shear_range=0.2,
                     horizontal_flip=True,
                     rotation_range=90,
                     width_shift_range=0.1,
                     height_shift_range=0.1,
                     zoom_range=0.2)

image_datagen = ImageDataGenerator(**data_gen_args)
mask_datagen = ImageDataGenerator(**data_gen_args)

# Provide the same seed and keyword arguments to the fit and flow methods
seed = 1

image_generator = image_datagen.flow_from_directory(
    'data/images',
    class_mode=None,
    seed=seed)

mask_generator = mask_datagen.flow_from_directory(
    'data/masks',
    class_mode=None,
    seed=seed)

# combine generators into one which yields image and masks
train_generator = zip(image_generator, mask_generator)

model.fit(
    train_generator,
    steps_per_epoch=2000,
    epochs=50)

tf.image 有一堆你可以使用的随机变换。例如:

这是一个完全随机的猫图像示例。

from skimage import data
import matplotlib.pyplot as plt
import tensorflow as tf

cat = data.chelsea()

plt.imshow(cat)
plt.show()

经过变换的图像:

from skimage import data
import matplotlib.pyplot as plt
import tensorflow as tf

cat = data.chelsea()

plt.imshow(tf.image.random_hue(cat, .2, .5))
plt.show()

您可以像这样在 tf.data.Dataset 中实现它:

def process_img(file_path):
    img = tf.io.read_file(file_path)
    img = tf.image.decode_png(img, channels=3)
    img = tf.image.convert_image_dtype(img, tf.float32)
    img = tf.image.resize(img, size=(img_height, img_width))
    img = tf.image.random_hue(img, 0., .5) # Here
    return img

我找到了一种在图形模式下保持相同转换的方法。基本上是在同一次调用中将两个图像传递给变形金刚。

import os
import tensorflow as tf
os.chdir(r'c:/users/user/Pictures')
from glob2 import glob
import matplotlib.pyplot as plt

x_files = glob('inputs/*.jpg')
y_files = glob('targets/*.jpg')

files_ds = tf.data.Dataset.from_tensor_slices((x_files, y_files))

def load(file_path):
    img = tf.io.read_file(file_path)
    img = tf.image.decode_jpeg(img, channels=3)
    img = tf.image.convert_image_dtype(img, tf.float32)
    img = tf.image.resize(img, size=(28, 28))
    return img

def process_img(file_path1, file_path2):
    img = tf.stack([load(file_path1), load(file_path2)])
    img = tf.image.random_hue(img, max_delta=.5)
    return img[0], img[1]

files_ds = files_ds.map(lambda x, y: process_img(x, y)).batch(1)

a, b = next(iter(files_ds))

plt.imshow(a[0, ...])
plt.imshow(b[0, ...])