Sub topic 14
Scoring Panoptic Results with PQ, SQ, and RQ
This chapter assumes you know IoU and mean IoU, the idea of things versus stuff classes, and how a panoptic ID map stores one segment per pixel. Mean IoU suits semantic segmentation and mask AP suits instance segmentation. Neither scores a panoptic result, which mixes both kinds of class in one output. Panoptic quality (PQ) fills that gap with a single number that treats things and stuff the same way.
The matching rule
PQ first decides which predicted segments count as correct. A predicted segment and a ground-truth segment are a match when:
- They have the same class.
- Their IoU is strictly greater than 0.5.
The threshold gives a guarantee. Two predicted segments cannot overlap, because a panoptic output assigns each pixel to one segment. A ground-truth segment can therefore have at most one prediction with IoU above 0.5. The matching is unique, and no assignment algorithm is needed.
The matches split every segment into three groups:
- True positives (TP): matched pairs of predicted and ground-truth segments.
- False positives (FP): predicted segments with no match.
- False negatives (FN): ground-truth segments with no match.
The formulas
PQ is computed separately for each class and then averaged:
- SQ = (sum of IoU over TP) / |TP|
- RQ = |TP| / (|TP| + 0.5 |FP| + 0.5 |FN|)
- PQ = SQ × RQ = (sum of IoU over TP) / (|TP| + 0.5 |FP| + 0.5 |FN|)
| Metric | Name | What it measures | Analogy |
|---|---|---|---|
| SQ | Segmentation quality | How well matched segments overlap | Average IoU of the matches |
| RQ | Recognition quality | How many segments were found, with few extras | F1 score at the segment level |
| PQ | Panoptic quality | Both together | Overlap-weighted F1 |
RQ is an F1 score. With F1 = TP / (TP + 0.5 FP + 0.5 FN), PQ is that F1 with each TP weighted by its IoU. The split into SQ and RQ tells you where a model fails. A low RQ with a high SQ means the masks are sharp but the model misses or invents segments. A high RQ with a low SQ means the model finds the right segments but draws their boundaries poorly.
Things, stuff, and void
Reports usually give PQ, PQ^Th (averaged over thing classes), and PQ^St (averaged over stuff classes). A stuff class such as "sky" has at most one segment per image, so for stuff classes PQ mostly measures region quality. For things, PQ also measures whether the model separates individual objects.
Some pixels in the ground truth are void. These are unlabeled, ambiguous, or in crowd regions. The standard evaluation handles them as follows:
- Void ground-truth pixels do not count against the IoU of a prediction. Prediction pixels that fall on void are removed from the union.
- A predicted segment is not a false positive if more than 50% of its pixels fall on void ground truth.
- Predicted pixels without any segment (void in the prediction) are not scored as an error by themselves. They show up only as lower IoU or as missed segments.
Implementation
The code below computes TP, FP, FN, and IoU sums for one image from two ID maps. Segment ID 0 means void or unlabeled. Each dictionary maps a segment ID to its class.
import numpy as np
from collections import defaultdict
def panoptic_stats(gt_ids, gt_cls, pred_ids, pred_cls, void_id=0):
stats = defaultdict(lambda: {"tp": 0, "fp": 0, "fn": 0, "iou": 0.0})
pairs, counts = np.unique(
np.stack([gt_ids.ravel(), pred_ids.ravel()]), axis=1, return_counts=True
)
inter = {(int(g), int(p)): int(n) for (g, p), n in zip(pairs.T, counts)}
gt_area = {g: int((gt_ids == g).sum()) for g in gt_cls}
pred_area = {p: int((pred_ids == p).sum()) for p in pred_cls}
matched_gt, matched_pred = set(), set()
for (g, p), n in inter.items():
if g not in gt_cls or p not in pred_cls or gt_cls[g] != pred_cls[p]:
continue
# Prediction pixels that land on void ground truth leave the union.
union = gt_area[g] + pred_area[p] - n - inter.get((void_id, p), 0)
iou = n / union
if iou > 0.5:
s = stats[gt_cls[g]]
s["tp"] += 1
s["iou"] += iou
matched_gt.add(g)
matched_pred.add(p)
for g, cls in gt_cls.items():
if g not in matched_gt:
stats[cls]["fn"] += 1
for p, cls in pred_cls.items():
if p in matched_pred:
continue
if inter.get((void_id, p), 0) / max(pred_area[p], 1) > 0.5:
continue # mostly on void: ignored, not a false positive
stats[cls]["fp"] += 1
return stats
def panoptic_quality(stats):
result = {}
for cls, s in stats.items():
denom = s["tp"] + 0.5 * s["fp"] + 0.5 * s["fn"]
if denom == 0:
continue # class absent everywhere: leave it out of the mean
sq = s["iou"] / s["tp"] if s["tp"] else 0.0
rq = s["tp"] / denom
result[cls] = (sq * rq, sq, rq)
return result
gt = np.array([[1, 1, 1, 2, 2, 2],
[1, 1, 1, 2, 2, 2],
[4, 4, 4, 4, 4, 4]])
pred = np.array([[1, 1, 1, 1, 3, 3],
[1, 1, 1, 1, 3, 3],
[0, 0, 0, 0, 0, 0]])
gt_cls = {1: 1, 2: 1, 4: 2} # two "car" instances (class 1), one "road" (class 2)
pred_cls = {1: 1, 3: 1}
res = panoptic_quality(panoptic_stats(gt, gt_cls, pred, pred_cls))
for cls, (pq, sq, rq) in sorted(res.items()):
print(f"class {cls}: PQ={pq:.3f} SQ={sq:.3f} RQ={rq:.3f}")
print("mean PQ:", round(sum(v[0] for v in res.values()) / len(res), 3))Class 1 has two true positives with IoU 0.75 and about 0.667, so SQ is about 0.708 and RQ is 1. The model missed the road, so class 2 gets PQ 0. The mean PQ is about 0.354.
Practical details
- Accumulate before dividing. Sum TP, FP, FN, and IoU over the whole dataset, then compute PQ per class. Averaging per-image PQ values gives a different and misleading number, because images with few segments would weigh as much as crowded ones.
- Average only over classes that occur. The code skips a class with TP = FP = FN = 0. Counting it as 0 or 1 would distort the mean.
- No confidence ranking. PQ scores one fixed output, so it has no score threshold sweep as in AP. Low-confidence segments that you keep add false positives. The score threshold you choose and the merging rules decide the result.
- Class IDs must align. Predictions and ground truth must use the same class IDs after any label remapping.
Exercise
Exercise
For one class, a model gets 3 true positives with IoUs 0.9, 0.8, and 0.7, plus 1 false positive and 2 false negatives. Compute SQ, RQ, and PQ.
Show answer
The IoU sum is 2.4, so SQ = 2.4 / 3 = 0.8. RQ = 3 / (3 + 0.5 × 1 + 0.5 × 2) = 3 / 4.5 ≈ 0.667. PQ = SQ × RQ ≈ 0.533, which equals 2.4 / 4.5.
Exercise
Why does the IoU > 0.5 threshold make the matching unique without needing a matching algorithm?
Show answer
Segments in a panoptic output do not overlap. If a ground-truth segment had IoU above 0.5 with two predicted segments, those two predictions would have to share more than half of its pixels in total, which is impossible. So at most one prediction can match each ground-truth segment.