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