'ImageDataGenerator' has no len()

Hi!
Could somebody point me to the correct direction?
In the Programming Assignemnt of C2W4, when I test my train_val_generators function, I get this error:

TypeError: object of type ‘ImageDataGenerator’ has no len()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-c93bf4854fbc> in <module>()
      1 # Test your generators
----> 2 train_generator, validation_generator = train_val_generators(training_images, training_labels, validation_images, validation_labels)
      3 
      4 print(f"Images of training generator have shape: {train_generator.x.shape}")
      5 print(f"Labels of training generator have shape: {train_generator.y.shape}")

3 frames
<ipython-input-19-115d113ddc15> in train_val_generators(training_images, training_labels, validation_images, validation_labels)
     39   train_generator = train_datagen.flow(x=train_datagen,
     40                                        y=training_labels,
---> 41                                        batch_size=32) #25 
     42 
     43 

/usr/local/lib/python3.7/dist-packages/keras/preprocessing/image.py in flow(self, x, y, batch_size, shuffle, sample_weight, seed, save_to_dir, save_prefix, save_format, subset)
    894         save_prefix=save_prefix,
    895         save_format=save_format,
--> 896         subset=subset)
    897 
    898   def flow_from_directory(self,

/usr/local/lib/python3.7/dist-packages/keras/preprocessing/image.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
    472         save_format=save_format,
    473         subset=subset,
--> 474         **kwargs)
    475 
    476 

/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/numpy_array_iterator.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
     83             x_misc = []
     84 
---> 85         if y is not None and len(x) != len(y):
     86             raise ValueError('`x` (images tensor) and `y` (labels) '
     87                              'should have the same length. '
TypeError: object of type 'ImageDataGenerator' has no len()

I’m following the same structure we used in the course. This is how I define the function:

# GRADED FUNCTION: train_val_generators

def train_val_generators(training_images, training_labels, validation_images, validation_labels):

  """

  Creates the training and validation data generators

  

  Args:

    training_images (array): parsed images from the train CSV file

    training_labels (array): parsed labels from the train CSV file

    validation_images (array): parsed images from the test CSV file

    validation_labels (array): parsed labels from the test CSV file

    

  Returns:

    train_generator, validation_generator - tuple containing the generators


  ### START CODE HERE

  # In this section you will have to add another dimension to the data

  # So, for example, if your array is (10000, 28, 28)

  # You will need to make it (10000, 28, 28, 1)

  # Hint: np.expand_dims

  training_images = np.expand_dims(training_images,3)

  validation_images = np.expand_dims(validation_images,3)

  # Instantiate the ImageDataGenerator class 

  # Don't forget to normalize pixel values 

  # and set arguments to augment the images (if desired)

  train_datagen = ImageDataGenerator(

      rescale = 1./255,

      rotation_range=40,

      width_shift_range=0.2,

      height_shift_range=0.2,

      shear_range=0.2,

      zoom_range=0.2,

      horizontal_flip=True,

      fill_mode='nearest')

  # Pass in the appropriate arguments to the flow method

  train_generator = train_datagen.flow(x=train_datagen,

                                       y=training_labels,

                                       batch_size=32) #25 

  

  # Instantiate the ImageDataGenerator class (don't forget to set the rescale argument)

  # Remember that validation data should not be augmented

  validation_datagen = ImageDataGenerator(rescale = 1./255)

  # Pass in the appropriate arguments to the flow method

  validation_generator = validation_datagen.flow(x=validation_datagen,

                                                 y=validation_labels,

                                                 batch_size=32) #25 

  ### END CODE HERE

  return train_generator, validation_generator

Thanks for your attention :slight_smile:

Hello @Rockdrigo, welcome to the community.

You’re not passing the right images to train_datagen.flow(),
Same thing with validation_datagen.flow()
PS: Labels are good.

1 Like

Thank you very much :slight_smile:

My pleasure, happy learning!