Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tf.data.Dataset from tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory?

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) where x is a numpy array containing a batch of images with shape (batch_size, *target_size, channels) and y is a numpy array of corresponding labels.

like image 750
A T Avatar asked Feb 09 '19 12:02

A T


People also ask

What is TF keras preprocessing image ImageDataGenerator?

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!

What is ImageDataGenerator in keras used for?

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.

Does ImageDataGenerator add more images to my dataset?

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.


Video Answer


1 Answers

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))
like image 52
Dmytro Prylipko Avatar answered Nov 08 '22 21:11

Dmytro Prylipko