Tuesday, May 11, 2021

 What is the difference between image_dataset_from_directory and ImageDataSet.flow_from_directory ?


keras provides two different methods to generate Image datasets : 

1. tf.keras.preprocessing.image_dataset_from_directory

2. tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory


The main difference between these two methods is that image_dataset_from_directory (first method above) does not have any provisions for data augmentation. Where as ImageDataGenerator.flow_from_directory has the provision for on-the-fly data augmentation. 

In fact ImageDataGenerator is the class provided by keras library that has the capability of on the fly data augmentation and it can augment existing keras datasets also.  It provides three methods, 

  • flow
  • flow_from_dataframe
  • flow_from_directory
The flow method geneate batches of real time augmented data from (data, label) arrays, which can be existing built-in datasets provided by keras like fashion_mnist/cifar10. 

In contrast, image_dataset_from_directory works only on directories and lacks the functionality of real time data augmentation. 

Apart from this logical difference, we can notice following minor syntactical differences also: 

1. The first method is contained in the module tf.keras.preprocessing, where as the second method ( and the ImageDataGenerator class itself) is contained in tf.keras.preprocessing.image module. 

2. Another very minor difference is to specify image size, we have to use image_size parameter in image_dataset_from_directory. Where as in ImageDatasetGenerator.flow_from_directory, we have to use the parameter target_size. 


3. Validation_split: image_dataset_from_directory has a parameter "validation_split". But in case of ImageDatasetGenerator, it is a property of the class itself. You have to specify this parameter while creating the class instance and then pass the subset parameter in flow_from_directory. 

If you specify validation_split as a parameter to flow_from_directory instead, you will get following error: 

TypeError: flow_from_directory() got an unexpected keyword argument 'validation_split'

No comments:

Post a Comment

 using Microsoft.AspNetCore.Mvc; using System.Xml.Linq; using System.Xml.XPath; //<table class="common-table medium js-table js-stre...