Search code examples
pythontensorflow

Cannot load dataset in with tensorflow


My code is this:

import tensorflow as tf
import tensorflow_datasets as tfds
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
"R:\Imagenes\DS_Graph\Train",
labels='inferred',  # Infer labels from subdirectory names
label_mode='binary',  # For binary classification (cats vs. dogs)
#image_size=(IMG_WIDTH, IMG_HEIGHT),
interpolation='nearest',
batch_size=32,
shuffle=True  # Shuffle training data
)

validation_ds = tf.keras.preprocessing.image_dataset_from_directory(
"R:\Imagenes\DS_Graph\Validation",
# ... (same parameters as train_ds)
)  
data, metadata = tfds.load('DS_Graph', as_supervised=True, with_info =  True)

I keep getting the error

module 'tensorflow_datasets' has no attribute 'load'

i upgraded tensorflow and data set, an the tensorflow_datasets but it didn't work.

Note: i'm running tensorflow locally, i use and previous version of python in order to install it using anaconda browser.


Solution

  • Since you are loading your dataset directly form the directories by using "image_dataset_from_directory", thus there is no need of using "tfds.load" for your specific dataset.

    Your revised code should look something like this:

    import tensorflow as tf
    
    train_ds = tf.keras.preprocessing.image_dataset_from_directory(
        r"R:\Imagenes\DS_Graph\Train",
        labels='inferred',
        label_mode='binary',
        interpolation='nearest',
        batch_size=32,
        shuffle=True
    )
    
    validation_ds = tf.keras.preprocessing.image_dataset_from_directory(
        r"R:\Imagenes\DS_Graph\Validation",
        labels='inferred',
        label_mode='binary',
        interpolation='nearest',
        batch_size=32,
        shuffle=False
    )