Johnny
Johnny

Reputation: 139

Input 0 of layer max_pooling2d_5 is incompatible with the layer

When trying to train my model, I get

 ValueError: Input 0 of layer max_pooling2d_5 is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: (None, None, 180, 180, 16)

My image shape is (32, 180, 180, 3), which is 4 dimensional. I am also shuffling and batching my training dataset: train_ds = train_ds.shuffle(100).batch(4)

Network

num_classes = 101

model = Sequential([
  layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(64, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(128, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(256, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dense(num_classes, activation='softmax')
])

What is causing this traceback?

Upvotes: 0

Views: 652

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36714

Your original images seem to be already batched. If you batch a 4D tensor it will output a tensor of shape:

(4, 32, 180, 180, 3)

I suggest you do not batch the images a second time. Since you're using image_dataset_from_directory with batch_size=32, you don't need to use .batch(4).

Upvotes: 2

Related Questions