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

问题描述

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

os.chdir(r'E:/trainTest')def process_img(file_path):img = tf.io.read_file(file_path)img = tf.image.decode_png(img, 通道=3)img = tf.image.convert_image_dtype(img, tf.float32)img = tf.image.resize(img, size=(img_height, img_width))返回图像x_files = glob('输入/*.png')y_files = glob('输出/*.png')files_ds = tf.data.Dataset.from_tensor_slices((x_files, y_files))#Dataset 给我输入输出files_ds = files_ds.map(lambda x, y: (process_img(x), process_img(y))).batch(batch_size)#模型初始化等#----模型.fit(files_ds,epochs=25)

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

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

经过变换的图像:

from skimage 导入数据导入 matplotlib.pyplot 作为 plt将张量流导入为 tfcat = 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, 通道=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) # 这里返回图像

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

导入操作系统将张量流导入为 tfos.chdir(r'c:/users/user/Pictures')从 glob2 导入 glob导入 matplotlib.pyplot 作为 pltx_files = glob('inputs/*.jpg')y_files = glob('targets/*.jpg')files_ds = tf.data.Dataset.from_tensor_slices((x_files, y_files))定义加载(文件路径):img = tf.io.read_file(file_path)img = tf.image.decode_jpeg(img, 通道=3)img = tf.image.convert_image_dtype(img, tf.float32)img = tf.image.resize(img, size=(28, 28))返回图像def process_img(file_path1, file_path2):img = tf.stack([加载(file_path1), 加载(file_path2)])img = tf.image.random_hue(img, max_delta=.5)返回 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, ...])

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)

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.

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()

Image with transformation:

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()

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 中为输入和输出保持相同的数据集扩充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:15
查看更多