How to find wrong prediction cases in test set CNNs using Keras
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
A CNN that reports good accuracy can still fail in ways that matter to users. The practical way to improve a classifier is to inspect the samples it gets wrong, measure where the errors cluster, and then feed that information back into training. In Keras, that workflow is mostly array comparison plus a few plots, but the details matter if you want reproducible analysis instead of ad hoc debugging.
Start with Deterministic Predictions
Before you look for mistakes, make sure your test data and predicted labels are aligned. That sounds obvious, but shuffled generators and reordered file paths are a common source of fake error analysis.
If your test set is already loaded as arrays, the basic prediction flow is simple:
For binary classification with a sigmoid output, do not use argmax. Apply an explicit threshold:
Keep the original sample identifiers if you have them. If your test images came from files, store file_paths alongside x_test and y_test so every wrong prediction can be traced back to the original asset.
Collect the Wrong Cases with Useful Metadata
The most useful output is not just an index list. You usually want true label, predicted label, model confidence, and optional path or external id.
When the model is wrong and very confident, that sample deserves attention. High-confidence failures often indicate one of three issues:
- the label is wrong
- preprocessing differs between train and test
- the model learned a shortcut feature instead of the real concept
It is also worth sorting the wrong cases by confidence:
This quickly surfaces the failures that most damage trust in the model.
Visualize the Misclassified Samples
A confusion matrix tells you which classes are confused, but raw images tell you why. Build a small grid first and inspect it visually.
After that, inspect special slices rather than a random wall of images:
- all mistakes for one confused class pair
- only high-confidence mistakes
- only low-light or low-resolution samples
- only mislabeled files suspected by humans
This is where model improvement usually starts. You may find annotation errors faster than you find architecture errors.
Add Class-Level Error Analysis
Per-sample review is useful, but you also need a class-level summary. A confusion matrix and classification report give you that.
If one class is consistently predicted as another, that is usually more actionable than a global accuracy drop. For example, if cats are often predicted as dogs, you might need more training samples for small cats, better cropping, or augmentation that reduces background bias.
Exporting the wrong cases to a table also helps when someone else needs to review them:
In a real project, include file path, dataset split version, and model checkpoint name. Without that context, a CSV of wrong predictions becomes much less useful a week later.
Turn Wrong Cases into a Training Improvement Loop
Error analysis only matters if it changes the next training run. After reviewing wrong cases, write down concrete decisions:
- relabel suspicious samples
- add targeted augmentation for confused classes
- rebalance the training set
- normalize preprocessing between training and inference
- collect more examples of edge cases
For harder cases, add model explanations such as Grad-CAM to the wrong predictions. That can show whether the CNN is focusing on the object or on irrelevant background texture. Even a small batch of reviewed explanations can reveal a shortcut-learning problem quickly.
The key is consistency. Run the same wrong-case analysis after every retrain so you can tell whether the new model fixed the actual failures or merely changed the aggregate score.
Common Pitfalls
- Evaluating on shuffled generator output without preserving sample order, which makes true labels and predictions stop matching.
- Using
argmaxfor a sigmoid binary classifier and getting misleading predicted labels. - Looking only at total accuracy instead of class confusion and high-confidence errors.
- Reviewing images visually but not exporting reproducible metadata such as sample id and confidence.
- Treating every wrong prediction as model weakness when some cases are actually label or data-quality issues.
Summary
- Wrong-case analysis is one of the fastest ways to improve a CNN after initial training.
- In Keras, the core steps are prediction, label comparison, and indexed inspection of failures.
- High-confidence mistakes are usually more informative than low-confidence borderline cases.
- Use both visual review and class-level metrics such as confusion matrices.
- Save wrong-case metadata so the same failures can drive relabeling, retraining, and regression checks.
Related reading
- How to Fine-tuning a Pretrained Network in Tensorflow?
- How to fit list of numpy array into LSTM Neural Network?
- How to fix low volatile GPU-Util with Tensorflow-GPU and Keras?
- How to fix 'Object arrays cannot be loaded when allow_pickleFalse' in the sketch_rnn algorithm
- How to Fine tune existing Tensorflow Object Detection model to recognize additional classes?
- How to Fine tune existing Tensorflow Object Detection model to recognize additional classes?
- How to fit a polynomial curve to data using scikit-learn?
- How to fit more than one line to data points
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.