ValueError: Shapes (None, 775, 770, 1) and (None, 776, 776, 8) are incompatible

I trying to replicate the semantic segmentation example

but train on my own data. There are 8 labels (7 features + background). My images are 775 by 770 pixels.

I changed num_classes to 8, img_size to (775,770) (and changed the input and target directories), otherwise I run the example code exactly. When I fit the model, I get the following error:

ValueError Traceback (most recent call last)
Cell In[32], line 14
12 # Train the model, doing validation at the end of each epoch.
13 epochs = 50
—> 14 model.fit(
15 train_dataset,
16 epochs=epochs,
17 validation_data=valid_dataset,
18 callbacks=callbacks,
19 verbose=2,
20 )
ValueError: in user code: [traceback]
ValueError: Shapes (None, 775, 770, 1) and (None, 784, 784, 8) are incompatible

The model’s final output shape is (None, 784, 784, 8).

Where am I going wrong? What else do I need to change in the model to match the size of my inputs and the number of classes?

Hi @Bryan_Brad,

Sorry for the delay in response.
This error is due to shape mismatch in input and output layer.If you are using pre-trained model for your specific needs regarding image segmentation consider this following things:

  1. Change the input layer to Input(shape=(775, 770, 1)) to match your image size.
  2. Ensure the final layer outputs the correct size by adding a Resizing(775, 770) layer before the output layer.
  3. Use padding='same' in all convolution and pooling layers to preserve spatial dimensions.
  4. Verify that the output shape matches (775, 770, 8) for the segmentation labels.

Hope this helps.Thank You.