Reputation: 353
I have images of cars and my goal is to extend my training dataset, I would like to combine the newly generated images and original images and train a deep neural network based on them to perform a classification task.
From this blog post: https://www.pyimagesearch.com/2019/07/08/keras-imagedatagenerator-and-data-augmentation/
"the Keras ImageDataGenerator class is not an “additive” operation. It’s not taking the original data, randomly transforming it, and then returning both the original data and transformed data."
What method would you suggest for extending the dataset?
Upvotes: 3
Views: 1202
Reputation: 2079
Although Keras ImageDataGenerator is not an additive operation as you said, it can still help you if you want to train your model on augmented images. Here's how ImageDataGenerator works: you specify images' directory and augmention parameters, and than on each epoch of training the gererator takes take image and transform it. So, that means that if you have 300 images total, with ImageDataGenerator you'll get 300 different transformed images each epoch.
If it's not the way you are looking for, you can try another thing. Use the OpenCV library to read images and save their copies with some transformation such as zooming, shearing, etc. to a one directory. This way you can make as many images per epoch as you want. And when you'he made them, use ImageDataGenerator without any parameters. One problem here - images always are the same. In the first case images should be different on every epoch.
To save new augmented images you can use:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from PIL import Image
import numpy as np
image = Image.open('asd.png')
image = np.array(image)
image = np.expand_dims(image, 0)
data_generator = ImageDataGenerator(
rotation_range=30,
)
for _, _ in zip(data_generator.flow(
image,
save_to_dir=<DIR_NAME>,
save_prefix=<PREFIX>,
save_format='png'
), range(N)):
pass
Where N is number of augmented images you want to create using 1 base image
Upvotes: 1