qubvel / segmentation_models

Segmentation models with pretrained backbones. Keras and TensorFlow Keras.
MIT License
4.67k stars 1.03k forks source link

incompatible shapes in model.fit_generator #517

Open elliestath opened 2 years ago

elliestath commented 2 years ago

Hey, I know it has been asked before but I couldn't figure out a solution to my problem.

I have 5 classes and I define them like that :

CLASSES = ['facade', 'window', 'obstacle', 'sky', 'door']

but this line n_classes = 1 if len(CLASSES) == 1 else (len(CLASSES) + 1) adds one more class.

If I keep the settings as default in the notebook, I have n_classes=6 but then during training, I get randomly other shape mismatch errors. In this case, I got this

Incompatible shapes: [1,384,480,6] vs. [1,48,1]
     [[{{node loss/softmax_loss/binary_focal_loss_plus_dice_loss/mul_6}}]] 

at the end of the first epoch but this error happens at a different times when I change the validation_steps

I use:

BACKBONE = 'efficientnetb0'
BATCH_SIZE = 8
CLASSES = ['facade', 'window', 'obstacle', 'sky', 'door']
LR = 0.0001
EPOCHS = 50

preprocess_input = sm.get_preprocessing(BACKBONE)

And my loss

# define optimizer
optim = keras.optimizers.Adam(LR)

# actulally total_loss can be imported directly from library, above example just show you how to manipulate with losses
total_loss = sm.losses.categorical_focal_dice_loss

metrics = [sm.metrics.IOUScore(threshold=0.5), sm.metrics.FScore(threshold=0.5)]

# compile keras model with defined optimozer, loss and metrics
model.compile(optim, total_loss, metrics)

My shapes:

print('train_dataloader length:', len(train_dataloader))
print('valid_dataloader length:', len(valid_dataloader))
print('train_dataloader shape:', train_dataloader[0][0].shape)
print('valid_dataloader shape:', valid_dataloader[0][0].shape)

train_dataloader length: 20
valid_dataloader length: 46
train_dataloader shape: (8, 320, 320, 3)
valid_dataloader shape: (1, 384, 480, 3)

Any help would be appreciated!