'Error in loading image_dataset_from_directory in tensorflow?

This is the code from https://keras.io/examples/vision/image_classification_from_scratch/

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# generate a dataset 
image_size = (180,180)
batch_size = 32

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "PetImages",
    validation_split = 0.2,
    subset = "training",
    seed = 1337,
    image_size = image_size,
    batch_size = batch_size,
)

Error is

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-bb7f2d14bf63> in <module>
      3 batch_size = 32
      4 
----> 5 train_ds = tf.keras.preprocessing.image_dataset_from_directory(
      6     "PetImages",
      7     validation_split = 0.2,

AttributeError: module 'tensorflow.keras.preprocessing' has no attribute 'image_dataset_from_directory'

Any smallest detail which I am overlooking now?



Solution 1:[1]

It has been addressed under this issue.

The specific function (tf.keras.preprocessing.image_dataset_from_directory) is not available under TensorFlow v2.1.x or v2.2.0 yet. It is only available with the tf-nightly builds and is existent in the source code of the master branch.

Too bad they didn't indicate it anywhere on site. Better to use flow_from_directory for now. Or switch to tf-nightly and carry on.

Solution 2:[2]

v2.5.0
I got the same error using that code:

tf.keras.utils.image_dataset_from_directory(...)

changing it to:

tf.keras.preprocessing.image_dataset_from_directory(...)

fix my problem

Solution 3:[3]

I also had the same problem. When I upgraded the TensorFlow version to 2.3.0, it worked.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 colt.exe
Solution 2
Solution 3 Satya Prakash