Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

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.

  • 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.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning