← All answersEvaluation & deployment

What is a confusion matrix, and how do precision, recall, and F1-score evaluate a vision model?

Short answer

A confusion matrix is a table of predictions against ground truth, splitting results into true positives, false positives, true negatives, and false negatives. From it: precision = TP / (TP + FP), 'of what I flagged, how much was right'; recall = TP / (TP + FN), 'of what was really there, how much I caught'; and F1 is their harmonic mean, a single balanced score. Which one matters most depends on whether false alarms or misses are costlier.

Accuracy alone is misleading, especially with imbalance. The confusion matrix and the metrics derived from it tell you what kind of mistakes a model makes, not just how many.

  • Precision - TP / (TP + FP): when the model says 'defect', how often is it right? Low precision = too many false alarms.
  • Recall - TP / (TP + FN): of all real defects, how many did it find? Low recall = too many misses.
  • F1 - 2 x (precision x recall) / (precision + recall): one number that balances both; drops if either is poor.

Which to optimize

It's a business decision. For medical screening or safety, misses are unacceptable, so you favor recall. For a spam-like filter where false alarms annoy users, you favor precision. F1 is the go-to when you need a single balanced number. Tuning the confidence threshold slides you along this trade-off.

from ultralytics import YOLO

model = YOLO("runs/detect/train/weights/best.pt")
metrics = model.val()                 # also writes confusion_matrix.png
print("precision", metrics.box.mp)    # mean precision
print("recall", metrics.box.mr)       # mean recall

Stuck on this in a real project?

Book a free call and I’ll give you a straight answer on your specific case.

Book a consultation