What Is IoU (Intersection over Union)? Object Detection Metrics Explained

What is an IoU in computer vision: the overlap of predicted and true boxes over their union, how to calculate it, and how it drives mAP and mIoU.

What Is IoU (Intersection over Union)? Object Detection Metrics Explained
Written by TechnoLynx Published on 30 Aug 2026

IoU (intersection over union) is the area where a predicted bounding box and a ground-truth box overlap, divided by the total area the two boxes cover together. It runs from 0, when the boxes do not overlap at all, to 1, when they match exactly. Mathematically it is the Jaccard index, the standard set-similarity measure, applied to image regions. So in computer vision IoU is one number that says how well a detector located an object, and the mAP figures YOLO reports use it as their true-positive threshold.

What Is an IoU Score, and How Do You Calculate It for Two Boxes?

Write the ground-truth box as B_gt and the prediction as B. Then IoU = |B ∩ B_gt| / |B ∪ B_gt|, which is the form used in the detection literature. With boxes given as corner coordinates (x1, y1, x2, y2), the calculation runs in this order:

  1. Find the intersection rectangle: its left and top edges are the larger of the two boxes’ x1 and y1, and its right and bottom edges are the smaller of their x2 and y2.
  2. If the right edge is left of the left edge (or the bottom is above the top), the boxes do not overlap, so clamp the intersection width or height to zero.
  3. Multiply width by height to get the intersection area.
  4. Compute the union as area(A) + area(B) − intersection, so the shared region is counted only once.
  5. Divide intersection by union.

Adding the two areas without subtracting the overlap gives a denominator that is too large and an IoU that is too low.

Worked Example: A Box That Looks Right and Still Fails At 0.5

Assume a ground-truth box at (10, 10, 50, 50) and a prediction at (20, 15, 60, 55), in pixels. Both boxes are 40 × 40, so each covers 1,600 pixels.

Quantity Working Value
Intersection x-range max(10, 20) to min(50, 60) 20 to 50, width 30
Intersection y-range max(10, 15) to min(50, 55) 15 to 50, height 35
Intersection area 30 × 35 1,050
Union area 1,600 + 1,600 − 1,050 2,150
IoU 1,050 / 2,150 ≈ 0.488

The prediction is off by 10 pixels horizontally and 5 vertically. By eye, we would call it a hit. Under the 0.5 threshold of the PASCAL VOC metric, which COCO reports as AP50, it is not one.

Because IoU is relative to object size, the same pixel error moves it more on a small object than on a large one. COCO also reports AP separately for small, medium and large objects (under 32² pixels, 32² to 96², over 96²). For inspection work on small defects, read the small-object figure (APS) before the headline mAP50-95 inspection accuracy figure.

Computing IoU in Code

You rarely need to write this yourself. In PyTorch, torchvision.ops.box_iou returns the IoU between two sets of boxes as an N × M matrix, one value for every pair. Its documentation describes corner-coordinate (x1, y1, x2, y2) boxes along with the (x1, y1, width, height) and centre-based (cx, cy, width, height) formats. Check the box format before you check the model, since the function accepts several formats.

import torch
from torchvision.ops import box_iou

gt   = torch.tensor([[10., 10., 50., 50.]])   # x1, y1, x2, y2
pred = torch.tensor([[20., 15., 60., 55.]])
print(box_iou(pred, gt))  # tensor([[0.4884]])

How IoU Feeds Precision, Recall and mAP

On its own, IoU only scores the overlap of two boxes. To evaluate a detector you choose an IoU threshold t, and every detection is then classified as a true positive or a false positive depending on whether it overlaps a ground-truth object by at least t. A ground-truth object that no detection matches is a false negative. Cityscapes also counts a second prediction of an object that has already been matched as a false positive in its instance task.

With those counts, precision is TP / (TP + FP) and recall is TP / (TP + FN). Sweeping the confidence threshold gives a series of precision-recall points, and average precision (AP) is the area under the resulting curve. Mean average precision (mAP) averages AP across object classes. COCO goes a step further: it makes no distinction between AP and mAP in its naming and averages over all 80 categories and over ten IoU thresholds.

The worked example above shows why the threshold matters so much. At t = 0.5, the 0.488 box is a false positive, and its ground-truth object, now unmatched, is also a false negative. One slightly loose box therefore lowers precision and recall together.

The IoU Thresholds Behind the Common Headline Numbers

Reported metric IoU threshold(s) Who defines it
AP50 / [email protected] 0.50 PASCAL VOC metric, also reported by COCO
AP75 0.75 COCO’s “strict” metric
AP (COCO primary) 10 thresholds, 0.50 to 0.95 in steps of 0.05 COCO primary challenge metric
mAP50-95 averaged over 0.50 to 0.95 Ultralytics YOLO reports
Instance-level AP 10 overlaps, 0.5 to 0.95 in steps of 0.05 Cityscapes

COCO describes averaging over thresholds (mAP50-95 rather than mAP50) as a deliberate break from the single-threshold tradition, because it rewards detectors that localise better. Ultralytics documents 0.50 as the threshold typically used to define true positives in mAP.

What Is IoU in YOLO? Two Thresholds That Do Different Jobs

In a YOLO pipeline, IoU appears in two places. One is the evaluation threshold described above. The other is the IoU threshold in non-maximum suppression (NMS), the final step in most detectors, which removes a box whenever its overlap with a higher-scoring box exceeds the threshold.

In their 2023 RT-DETR paper, Zhao and colleagues explain that a higher NMS IoU threshold filters out fewer overlapping boxes in each round, so false positives increase, while a lower threshold deletes true positives when objects overlap or occlude each other. They also measured that NMS execution time rises as the IoU threshold increases or the confidence threshold falls. In their tests, YOLOv8 achieved its best AP with a confidence threshold of 0.001 and an NMS IoU threshold of 0.7, at the price of comparatively long NMS time.

On a smart camera or gateway, NMS time comes out of the same latency budget as the network, so the threshold used for a leaderboard is not automatically the right one on IoT and embedded devices. Classic NMS also judges overlap alone. The AAAI-20 paper that introduced DIoU argues this causes false suppression under occlusion and proposes DIoU-NMS, which also considers the distance between box centres. In that paper’s experiments with YOLOv3 and SSD trained using CIoU loss, DIoU-NMS beat original NMS at every threshold between 0.43 and 0.48.

Why a YOLO Box Can Have High Confidence but Low IoU

A detection’s confidence is the model’s own estimate, while IoU is measured against a ground truth the model never sees at inference time. A model can be certain of the class and still place the box a few pixels off; on a small object, as the worked example shows, that alone can push IoU below 0.5. If high-confidence boxes keep failing the IoU match, check localisation first: annotation consistency, box format and input resolution.

IoU as a Training Loss, and Why GIoU, DIoU and CIoU Exist

Because IoU is the measure you are judged on, it is natural to optimise it directly, using the IoU loss L = 1 − IoU. Rezatofighi and colleagues (CVPR 2019) pointed out the catch: when the predicted and ground-truth boxes do not overlap, IoU stays flat at zero however far apart they are, so it provides no signal to move the box closer. Their fix, generalised IoU (GIoU), works as both a loss and a metric, and they report consistent improvements on PASCAL VOC and MS COCO.

The AAAI-20 paper frames all of these as L = 1 − IoU + R(B, B_gt), where R is a penalty term. DIoU adds a penalty on the distance between box centres, and CIoU adds a further term for consistency of aspect ratio, so overlap, centre distance and aspect ratio are all covered. The authors report faster convergence and better accuracy for CIoU than for IoU and GIoU losses. Those are the paper’s own results on its own benchmarks, not a guarantee on your data.

How IoU Works for Segmentation Masks, and What Mean IoU (mIoU) Is

For masks, the regions are sets of pixels instead of rectangles, and the formula becomes IoU = TP / (TP + FP + FN), counting true positive, false positive and false negative pixels. PASCAL VOC defined segmentation accuracy this way: correctly labelled pixels of a class divided by the pixels labelled as that class in either the ground truth or the prediction. COCO’s box and mask evaluations are identical except that IoU is computed over masks rather than boxes.

Mean IoU (mIoU) computes this IoU separately for each class and then averages the per-class scores. Voxel51 calls it the standard metric for semantic segmentation, and the class averaging is the point: common classes cannot dominate, so a model also has to segment rare classes well. Pixel accuracy, by contrast, can look high simply because the model labels large background areas correctly.

TensorFlow’s tf.keras.metrics.MeanIoU accumulates predictions in a confusion matrix, computes IoU for each class, then returns their mean. Cityscapes counts pixels over the whole test set before dividing. So two mIoU values from different scripts are not comparable until you know how each accumulated its counts. Background handling is the other choice: TorchMetrics’ MeanIoU has an include_background flag and a per_class option that returns one IoU per class instead of a scalar.

from torchmetrics.segmentation import MeanIoU

miou = MeanIoU(num_classes=3, per_class=True, include_background=False)

Standard pixel IoU counts every pixel equally, whatever the size of the instance it belongs to. Cityscapes addresses this with an instance-level iIoU, which weights each pixel by the ratio of the class’s average instance size to the size of its ground-truth instance.

Checklist Before Comparing Two IoU-Based Numbers

Compare IoU-derived figures only with their settings attached. Before putting two of them side by side, confirm:

  • IoU threshold or range: a single 0.50, a strict 0.75, or the 0.50 to 0.95 average.
  • Box or mask IoU: the same AP definition is computed over boxes or over masks.
  • Box format: corner, corner-plus-size or centre coordinates.
  • Class averaging: per-class mean, and whether background is included.
  • Accumulation: whether counts are accumulated across the whole dataset.
  • NMS settings during evaluation: the IoU and confidence thresholds used.
  • Object size mix: small errors move IoU more on small objects.

FAQ

What Is the Dice Score vs IoU?

Both measure overlap between a prediction and the ground truth. They rise and fall together but weight the intersection differently, and Dice tends to give the higher value for the same prediction. A Dice score therefore cannot be compared directly with an IoU score.

Is This the Same as an IOU, the “I owe you” Note?

No. An IOU in finance is an informal written acknowledgement of a debt and has nothing to do with intersection over union, which is a geometric overlap measure used in computer vision.