在Tensorflow中为输入和输出保留相同的数据集扩充 [英] Keep same dataset augmentation for input and output in Tensorflow

查看:261
本文介绍了在Tensorflow中为输入和输出保留相同的数据集扩充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a batch dataset which contains image as input and output. Code is like this:

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创建增强图像(如翻转,旋转,缩放等)?因为必须以相同的方式增强输出图像,所以必须增强输入图像.

Problem is I don't have enough images for my model. So my question is, how can i create augmented images (like flipped, rotated, zoomed etc) from files_ds? Because the output image has to be augmented the same way the input image is augmented.

这个问题实际上来自以下问题,我想在其自己的部分中问这个问题:
Tensorflow image_dataset_from_directory用于输入数据集和输出数据集

This question actually arrived from the following question and i wanted to ask this in its own section:
Tensorflow image_dataset_from_directory for input dataset and output dataset

推荐答案

有很多可以使用的随机转换.例如:

tf.image has a bunch of random transformations you can use. For instance:

这是一个关于猫的完全随机图像的例子.

Here's an example on a completely random image of a cat.

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中实现此目标:

You can implement this in you tf.data.Dataset like this:

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

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

I found a way to keep the same transformation in graph mode. It's basically to pass two images in the same call to the transformers.

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, ...])

这篇关于在Tensorflow中为输入和输出保留相同的数据集扩充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆