Sub topic 6
Panoptic segmentation: a unified output
This chapter assumes you know the semantic and instance segmentation outputs from the earlier chapters. You should also be comfortable with NumPy arrays and with the difference between things and stuff.
Why a unified output is needed
Semantic segmentation labels every pixel, but it cannot tell two adjacent cars apart. Instance segmentation separates individual objects, but it usually ignores background regions such as road and sky. It also lets masks overlap. A downstream system, such as a self-driving stack or a photo editor, often needs both. It needs to know what every pixel is and which object each pixel belongs to.
Panoptic segmentation, introduced by Kirillov et al. in 2019, defines one output that covers both needs. Every pixel receives exactly one semantic class. Pixels of thing classes also receive an instance identity. Nothing overlaps, and nothing is silently dropped.
The rules of a panoptic result
A valid panoptic prediction is a partition of the image into segments. Each segment has a class and an ID.
- Every pixel belongs to at most one segment. Overlap is not allowed.
- Each segment has one class label.
- A stuff class (road, sky, grass) produces at most one segment per image, even when its pixels form disconnected regions. Stuff has no instances.
- A thing class (car, person) produces one segment per object.
- Pixels that belong to no segment are void. Void is also used for ground-truth regions that annotators could not label.
Rule 3 follows from the definition of stuff. Two patches of sky on either side of a tree are still "the sky". Rule 1 is the real departure from instance segmentation, where two masks can claim the same pixel. A panoptic system must resolve each conflict.
| Property | Semantic | Instance | Panoptic |
|---|---|---|---|
| Covers stuff classes | Yes | Usually no | Yes |
| Separates objects | No | Yes | Yes (things only) |
| Overlap allowed | No | Yes | No |
| Confidence scores needed | No | Yes, for ranking | Usually used internally to resolve conflicts |
| Void pixels | Ignore label | Not applicable | Explicit void |
How the result is stored
There are three common encodings. You will meet all of them.
- Two maps. One
(H, W)map holds the class per pixel and another holds the instance index. Stuff pixels get instance 0. - A single segment-ID map. Each segment gets a unique integer. A common convention, used in Cityscapes label files, is
class_id * 1000 + instance_id. Decoding is integer division and remainder. - COCO panoptic format. A PNG stores the segment ID in the RGB channels as
id = R + 256 * G + 256² * B. A JSON file lists each segment with itsid,category_id,area,bbox, andiscrowdflag. The PNG alone tells you which pixels belong together, and the JSON tells you what each segment is.
The code below builds a small panoptic result by hand. It encodes the two maps into one ID map, then recovers each segment's class, instance, and area. It also checks the thing rule: every thing pixel must have an instance ID.
import numpy as np
DIVISOR = 1000
VOID = -1
CLASS_NAMES = {0: "road", 1: "sky", 2: "car"}
THING_CLASSES = [2]
class_map = np.array([
[1, 1, 1, 1, 1, 1],
[0, 2, 2, 0, 2, 2],
[0, 2, 2, 0, 2, VOID],
])
instance_map = np.array([
[0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 2, 2],
[0, 1, 1, 0, 2, 0],
])
def encode(class_map, instance_map, divisor=DIVISOR):
# Void pixels keep the value -1; all others become class * divisor + instance.
return np.where(class_map == VOID, VOID, class_map * divisor + instance_map)
def check_things_have_instances(class_map, instance_map, thing_classes):
is_thing = np.isin(class_map, thing_classes)
return bool(np.all(instance_map[is_thing] > 0))
segment_ids = encode(class_map, instance_map)
print("things have instances:",
check_things_have_instances(class_map, instance_map, THING_CLASSES))
ids, counts = np.unique(segment_ids, return_counts=True)
for seg_id, area in zip(ids, counts):
if seg_id == VOID:
print(f"void: {area} px")
continue
cls, inst = divmod(int(seg_id), DIVISOR)
print(f"id {seg_id}: {CLASS_NAMES[cls]} instance {inst}, {area} px")The output lists one void pixel, one road segment (4 px), one sky segment (6 px), and two car segments (4 px and 3 px). The areas add up to the 18 pixels in the image. That sum check is a useful sanity test for any panoptic result.
How models produce panoptic output
Two design families exist. Later chapters cover each in detail, so this section gives only the shape of each.
Merge-based systems run a semantic branch and an instance branch, often on a shared backbone, as in Panoptic FPN. A post-processing step combines the two outputs. It lets high-confidence instances claim pixels first, fills the remaining pixels from the semantic map, and discards instances that are mostly covered. The "Merging semantic and instance predictions" chapter implements this step.
Mask classification systems such as MaskFormer and Mask2Former predict a set of masks, each with a class. They treat stuff and things the same way. A stuff class is a mask that happens to appear once. These models are covered in the "Unified mask classification" chapter. The final step still resolves overlaps, usually by assigning each pixel to the mask with the highest combined score.
Both families end in the same place: a partition with classes and IDs.
Edge cases and failure modes
- Overlap conflicts. Two instance masks disagree about a pixel. The merge rule decides who wins, and a poor rule can erase a correctly detected object.
- Semantic and instance disagreement. The semantic branch says "road" where an instance mask says "car". Merge-based systems usually trust the instance for thing classes, but the semantic map decides what fills the leftover pixels.
- Fragmented stuff. A stuff region split by an occluding object is still one segment. If your code treats connected components as separate segments, your output will not match the ground truth.
- Crowd regions. Dense groups, such as a crowd of people, may be annotated as one segment with
iscrowd=1rather than as individuals. Evaluation code treats these segments specially. - Void handling. Void in the ground truth is excluded from scoring. Void in a prediction counts as a miss for any ground-truth pixel it covers.
- ID instability. Instance IDs only distinguish segments within one image. IDs have no meaning across images, so never compare them across frames or files.
Summary
Panoptic segmentation gives a single, non-overlapping labeling of the image in which stuff is labeled by class and each thing is labeled by class and instance. Its main practical costs are overlap resolution and the extra bookkeeping of IDs, void regions, and crowd regions. The next chapters turn to how overlap and quality are measured: IoU, then panoptic quality.
Exercise
A panoptic ID map uses the convention class_id * 1000 + instance_id. A segment has ID 12003. What are its class and instance, and can this segment belong to a stuff class? Also, a model reports two segments with ID 5000 in the same image. What is wrong?
Show answer
Dividing 12003 by 1000 gives class 12 and instance 3. Since the instance is not 0, it is a thing segment, so it cannot be a stuff class (stuff segments have instance 0). Two segments with ID 5000 would be class 5 with instance 0, which is a stuff segment. A stuff class may produce only one segment per image, so the duplicated ID means disconnected stuff regions were wrongly split instead of being merged into a single segment.