Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras2 ImageDataGenerator or TensorFlow tf.data?

With Keras2 being implemented into TensorFlow and TensorFlow 2.0 on the horizon, should you use Keras ImageDataGenerator with e.g, flow_from_directory or tf.data from TensorFlow which also can be used with fit_genearator of Keras now?

Will both methods will have their place by serving a different purpose or will tf.data be the new way to go and Keras generators deprecated in the future?

Thanks, I would like to take the path which keeps me up to date a bit longer in this fast moving field.

like image 253
Florida Man Avatar asked Apr 11 '19 08:04

Florida Man


1 Answers

Alongside custom defined Python generators, you can wrap the ImageDataGenerator from Keras inside tf.data.

The following snippets are taken from the TensorFlow 2.0 documentation.

img_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, rotation_range=20)
ds = tf.data.Dataset.from_generator(
    img_gen.flow_from_directory, args=[flowers], 
    output_types=(tf.float32, tf.float32), 
    output_shapes = ([32,256,256,3],[32,5])
)

Therefore, one can still use the typical Keras ImageDataGenerator, you just need to wrap it into a tf.data.Dataset like above.

like image 70
Timbus Calin Avatar answered Oct 14 '22 17:10

Timbus Calin