I trained a CNN to classify multiple object categories from images.
The training completes without errors and the accuracy looks decent.
But when I run inference, every image gets the same label.
Even very different images are predicted as the same class.
This happens when the model has collapsed to predicting the most dominant class in the dataset.
If one class appears much more often than others, the CNN can minimize loss simply by always predicting it. This gives decent training accuracy but useless predictions.
Check your class distribution. If it is skewed, use class weighting or balanced sampling:
loss = nn.CrossEntropyLoss(weight=class_weights)
Also verify that your labels are correctly aligned with your images.
Common mistakes:
Highly imbalanced datasets
Shuffled images but not labels
Incorrect label encoding
The practical takeaway is that class imbalance silently trains your CNN to cheat.