DataGenerator Output Dims


I am trying to read some Grey-scaled images using the DataGenerator. However, the image shape is coming out as (n, m, 3).

When I try to pass the input shape as (n,m,1) the geenerator renders images with (n,m,1,3) shape.

How can i enforce the (n,m,1)?

# coursera 
train_dir = '/content/data/train'
print("Train Directory --> ", os.listdir(train_dir))

train_datagen = ImageDataGenerator(rescale=1./255)

train_data_generator = train_datagen.flow_from_directory(
    target_size=(img_height, img_width),


Found 83484 images belonging to 4 classes.
(512, 496, 3)

ImageDataGenerator is deprecated in recent versions of tensorflow. Here’s an api you can use along with setting the color_mode property.

In [4]: train_generator = tf.keras.utils.image_dataset_from_directory(
…: directory=data_root,
…: color_mode=“grayscale”,
…: subset=“training”,
…: validation_split=.3,
…: image_size=(64,64),
…: seed=42,
…: shuffle=True)
Found 4 files belonging to 2 classes.
Using 3 files for training.

In [4]: train_generator.element_spec
(TensorSpec(shape=(None, 64, 64, 1), dtype=tf.float32, name=None),
TensorSpec(shape=(None,), dtype=tf.int32, name=None))

Will the future lectures reflect this update?

The next update to the course should hopefully fix usage of this api. I’ve informed the staff about this.

If you are planning to take the tensorflow developer certificate exam, the handbook asks you know the ImageDataGenerator API.

1 Like

One more question, it seems like this new API does not support augmentation, how do we go about that?

Please see this link.