Overfitting
Overfitting happens when a model memorizes its training data, noise included, and fails on new data. See how to spot it and the methods that prevent it.
Overfitting happens when a machine learning model fits its training data so closely that it learns the noise and quirks of those specific examples along with the real patterns. An overfit model scores well on the data it trained on and noticeably worse on new data, and the size of that gap measures how badly it overfits. In bias-variance terms, overfitting is high variance: the model's predictions depend too much on the particular samples it happened to see.
Recognizing Overfitting#
The telltale sign is a growing gap between training and validation data performance. On learning curves plotted per epoch, training loss keeps falling while validation loss flattens and then rises, and validation metrics such as mean average precision (mAP) stall or decline. The classic small-scale demonstration fits polynomials of two different degrees to 12 noisy points sampled from a sine wave:
import numpy as np
from numpy.polynomial import Polynomial
rng = np.random.default_rng(0)
# 12 noisy training points from a sine wave, plus clean test points
x_train = rng.uniform(0, 1, 12)
y_train = np.sin(2 * np.pi * x_train) + rng.normal(0, 0.2, 12)
x_test = np.linspace(0, 1, 200)
y_test = np.sin(2 * np.pi * x_test)
for degree in (3, 9):
fit = Polynomial.fit(x_train, y_train, degree)
train_mse = np.mean((fit(x_train) - y_train) ** 2)
test_mse = np.mean((fit(x_test) - y_test) ** 2)
print(f"Degree {degree:2d}: train MSE {train_mse:.4f}, test MSE {test_mse:.4f}")The degree-9 polynomial has enough flexibility to bend toward the noise, which lowers its training error from 0.0356 to 0.0148 but raises its test error tenfold, from 0.0182 to 0.1859.
Overfitting vs. Underfitting#
Underfitting is the opposite failure: a model too simple, or trained too briefly, to capture the patterns in the data, so it performs poorly on training and validation data alike. Comparing the two errors tells the cases apart and points to different fixes.
Common Causes#
- Too much capacity for the data: A neural network with millions of parameters can memorize a few hundred images outright.
- Noisy or mislabeled data: Missing boxes, wrong classes, and outliers give the model errors to fit that do not exist in the real world.
- Training too long: After the useful patterns are learned, additional epochs mostly fit the noise that remains.
- Narrow training data: A detector trained only on daytime images from one camera can latch onto that camera's background and lighting and miss objects at a new site.
Preventing Overfitting#
- More and more varied data: Additional real examples are the most direct fix, and data augmentation multiplies variety by flipping, scaling, recoloring, and mixing existing images.
- Regularization: Weight decay penalizes large weights, and dropout randomly disables units during training so no single feature becomes indispensable.
- Smaller or pretrained models: A smaller model has less room to memorize, and transfer learning with frozen early layers cuts the number of weights that must be learned from a small dataset.
- Early stopping: Evaluating on validation data after every epoch and stopping once the score stops improving keeps the model from training into the noise.
- Honest evaluation: Cross-validation gives a more reliable estimate of generalization error on small datasets, and a separate test set catches overfitting to the validation data during hyperparameter tuning.
Ultralytics YOLO training applies early stopping through the patience argument, which ends a run after that many epochs without improvement in validation fitness (mAP50-95 for detection), and it saves the weights from the best epoch as best.pt even when training runs to the end.
from ultralytics import YOLO
# Load a pretrained YOLO26n model
model = YOLO("yolo26n.pt")
# Allow up to 100 epochs, but stop after 20 epochs without validation improvement
results = model.train(data="coco8.yaml", epochs=100, patience=20)The default is patience=100, and for datasets under 1,000 images the YOLO26 fine-tuning guidance suggests epochs=50 with patience=20.
Overfitting vs. Related Terms#
- Data leakage: Overlap between training and evaluation data, which hides overfitting. When near-duplicate frames from the same video land in both the training and validation splits, a model that memorized the training set still scores well on validation, and the problem only appears after deployment. Splitting by video or scene prevents it.
- Shortcut learning: The model relies on a spurious cue, such as a watermark or background, that is present in both training and validation data. Validation scores look healthy, and the model fails on new data where the cue is missing.










