Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 548
TheExorcist Avatar asked Jun 16 '20 13:06

TheExorcist


People also ask

What is Image_dataset_from_directory?

image_dataset_from_directory allows to load your data in the tf. data. Dataset format. Hence, it enables also the use, and this is the best, of the Keras preprocessing layers, including data augmentation.

What does Image_dataset_from_directory return?

Used in the notebooks. Then calling image_dataset_from_directory(main_directory, labels='inferred') will return a tf. data. Dataset that yields batches of images from the subdirectories class_a and class_b , together with labels 0 and 1 (0 corresponding to class_a and 1 corresponding to class_b ).

What keras preprocessing?

Keras Preprocessing is the data preprocessing and data augmentation module of the Keras deep learning library. It provides utilities for working with image data, text data, and sequence data.


3 Answers

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.

like image 78
colt.exe Avatar answered Oct 16 '22 07:10

colt.exe


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

like image 30
Adi Shumely Avatar answered Oct 16 '22 07:10

Adi Shumely


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

like image 1
Satya Prakash Avatar answered Oct 16 '22 07:10

Satya Prakash