How do I create a tf.data.Dataset
from tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory
?
I'm considering tf.data.Dataset.from_generator
, but it's unclear how to acquire the output_types
keyword argument for it, given the return type:
A
DirectoryIterator
yielding tuples of(x, y)
wherex
is a numpy array containing a batch of images with shape(batch_size, *target_size, channels)
andy
is a numpy array of corresponding labels.
Keras ImageDataGenerator is a gem! It lets you augment your images in real-time while your model is still training! You can apply any random transformations on each training image as it is passed to the model. This will not only make your model robust but will also save up on the overhead memory!
Introduction. In this Keras tutorial, we will talk about the Image Data Generator class of Keras i.e. ImageDataGenerator which is used for generating images using Image Augmentation techniques dynamically during training.
If you do not mention steps_per_epoch in the training generator, the Imagedatagenerator generates different random augmented images for every batch in each epoch.
Both batch_x and batch_y in ImageDataGenerator
are of type K.floatx()
, so must be tf.float32
by default.
Similar question was discussed already at How to use Keras generator with tf.data API. Let me copy-paste the answer from there:
def make_generator():
train_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator =
train_datagen.flow_from_directory(train_dataset_folder,target_size=(224, 224), class_mode='categorical', batch_size=32)
return train_generator
train_dataset = tf.data.Dataset.from_generator(make_generator,(tf.float32, tf.float32))
The author faced another issue with the graph scope, but I guess it is unrelated to your question.
Or as a one liner:
tf.data.Dataset.from_generator(lambda:
ImageDataGenerator().flow_from_directory('folder_path'),(tf.float32, tf.float32))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With