pythontensorflowmachine-learningkerasconv-neural-network

Training a CNN using ImageDataGenerator and training fails after the 2nd epoch


I was training a CNN using ImageDataGenerator and encountered this problem where after the Second Epoch an Attribute error is raised.

The model is as follows

Model

import tensorflow as tf
from tensorflow.keras.optimizers import RMSprop

def create_model():
  '''Creates a CNN with 4 convolutional layers'''
  model = tf.keras.models.Sequential([
      tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),
      tf.keras.layers.MaxPooling2D(2, 2),
      tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
      tf.keras.layers.MaxPooling2D(2,2),
      tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
      tf.keras.layers.MaxPooling2D(2,2),
      tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
      tf.keras.layers.MaxPooling2D(2,2),
      tf.keras.layers.Flatten(),
      tf.keras.layers.Dense(512, activation='relu'),
      tf.keras.layers.Dense(1, activation='sigmoid')
  ])

  model.compile(loss='binary_crossentropy',
                optimizer=RMSprop(learning_rate=1e-4),
                metrics=['accuracy'])
  
  return model


from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        train_dir,  # This is the source directory for training images
        target_size=(150, 150),  # All images will be resized to 150x150
        batch_size=20,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
        validation_dir,
        target_size=(150, 150),
        batch_size=20,
        class_mode='binary',
        shuffle= False)


EPOCHS = 20

model = create_model()

history = model.fit(
      train_generator,
      steps_per_epoch=100,  # 2000 images = batch_size * steps
      epochs=EPOCHS,
      validation_data=validation_generator,
      validation_steps=50,  # 1000 images = batch_size * steps
      verbose=2)

Output

AttributeError                            Traceback (most recent call last)
Cell In[15], line 8
      5 model = create_model()
      7 # Train the model
----> 8 history = model.fit(
      9       train_generator,
     10       steps_per_epoch=100,  # 2000 images = batch_size * steps
     11       epochs=EPOCHS,
     12       validation_data=validation_generator,
     13       validation_steps=50,  # 1000 images = batch_size * steps
     14       verbose=2)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\utils\traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119     filtered_tb = _process_traceback_frames(e.__traceback__)
    120     # To get the full stack trace, call:
    121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
    123 finally:
    124     del filtered_tb

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\backend\tensorflow\trainer.py:354, in TensorFlowTrainer.fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq)
    333         self._eval_epoch_iterator = TFEpochIterator(
    334             x=val_x,
    335             y=val_y,
...
    355     }
    356     epoch_logs.update(val_logs)
    358 callbacks.on_epoch_end(epoch, epoch_logs)

AttributeError: 'NoneType' object has no attribute 'items'
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

I have tried the following debugging steps:

  1. upgrading Tensorflow and Keras
  2. trying a simpler Neural Network to see if that has the same issue but it worked fine.
  3. Instead of passing validation_generator to the model.fit(), doing it manually using numpy but that also didn't work out since for it the accuracy and error on training data was coming 0, for the even epochs only.

Have also checked the validation data it is properly loaded.

Python Version: 3.11.9 Tensorflow Version: 2.17.0 Keras Version: 3.4.1


Solution

  • I've successfully replicated your code using the versions that you specified, and it's functioning . The model is training with defined epochs. It looks like the problem might be related to how the image data generator is processing the data from the directory. Please check the provided path and verify that your main directory contains sub-folders, each representing a different class, matching the total number of classes in your dataset.

    Please refer to this gist