Sub topic 14
Analyzing Errors and Improving Your Data
Assume you know Python, PyTorch tensors and modules, and the basic training loop. You should also understand the difference between training, validation, and test data.
A validation score tells you how often the model is correct. It does not tell you why the model fails. Error analysis means examining incorrect predictions and looking for patterns that suggest a data or training problem.
Start with per-class results
Overall accuracy can hide weak classes. If 95% of your images belong to one class, a model that always predicts that class can appear successful.
Use a confusion matrix to compare the true class with the predicted class. Each row represents the actual class. Each column represents the predicted class. Large off-diagonal values show which classes the model confuses.
from sklearn.metrics import confusion_matrix, classification_report
class_names = ["cat", "dog", "rabbit"]
y_true = [0, 0, 0, 1, 1, 1, 2, 2, 2]
y_pred = [0, 1, 0, 1, 1, 0, 2, 1, 2]
matrix = confusion_matrix(y_true, y_pred)
print(matrix)
print(classification_report(
y_true,
y_pred,
target_names=class_names,
zero_division=0,
))The report includes precision, recall, and F1 score for each class.
- Precision is the fraction of a class's predictions that are correct.
- Recall is the fraction of that class's real examples that the model finds.
- F1 score balances precision and recall.
A low recall for rabbit means real rabbits are often assigned another label. A low precision for dog means many images predicted as dogs belong to other classes.
Inspect the images behind the numbers
For every incorrect prediction, record the image path, true label, predicted label, and prediction confidence. Display errors grouped by confusion pair, such as cat → dog.
Look for these patterns:
- The image is mislabeled or ambiguous.
- The object is too small, hidden, blurry, or badly lit.
- A background or watermark appears mostly in one class.
- Training images and test images come from different cameras or environments.
- The model learns a shortcut instead of the object itself.
Correct labels first. Adding more incorrectly labeled images usually makes the boundary between classes less clear.
Check leakage and splitting mistakes
Data leakage occurs when information from validation or test data influences training. Near-duplicate images are a common source. For example, frames from one video or several photos of the same product should not be randomly distributed across all splits.
Split by a meaningful group, such as subject, patient, product, video, or capture session. Keep every image from one group in the same split.
The test set should remain untouched until you have finished choosing preprocessing, model settings, and stopping time.
Choose a remedy based on the pattern
| Observed pattern | Likely cause | Useful remedy |
|---|---|---|
| Training score rises while validation score falls | Overfitting | More varied data, stronger augmentation, weight decay, or early stopping |
| One class has very low recall | Class imbalance or poor examples | Collect that class, use balanced sampling, or apply class-weighted loss |
| Two classes are often confused | Similar appearance or weak labels | Add clearer examples and inspect the labels |
| Validation is suspiciously high | Leakage or duplicates | Split by group and remove duplicates |
| Errors cluster in one lighting condition | Domain shift | Add images from that condition and use realistic augmentation |
Augmentation should imitate changes that can happen in real use. Horizontal flips may help for animals, but flipping text or asymmetric medical images can create invalid examples.
Improve the dataset iteratively
After each change, keep the same held-out test set and compare per-class results. Save the data version, model settings, and confusion matrix. This makes improvements reproducible and prevents a change that raises accuracy from hiding harm to a smaller class.
Exercise
Your validation accuracy is 92%, but one class has recall of 20%. What should you inspect first?
Show answer
Inspect the class counts, labels, confusion matrix, and representative images. Check for missing or ambiguous examples before changing the model.