Sub topic 13
Merging Semantic and Instance Predictions into Panoptic Output
Assume you know basic Python and NumPy, semantic and instance segmentation, and object-detection scores. This chapter combines two model outputs into one panoptic result.
Panoptic segmentation assigns every pixel a class and, for each thing object, an instance number. A thing is countable, such as a person or car. Stuff is an uncountable region, such as road, sky, or grass. A panoptic output usually uses:
- A class map with shape
(H, W). - An instance-ID map with shape
(H, W). - A convention such as instance ID
0for stuff and positive IDs for thing instances. - A void value, often
-1, for pixels that cannot be assigned reliably.
The semantic model provides a class for every pixel. The instance model provides masks, classes, and confidence scores for detected things. Merging must make the masks non-overlapping and must resolve conflicts.
A practical merging procedure
A simple procedure is:
- Sort detected instances by descending confidence.
- Reject masks with low confidence or very small area.
- Let each remaining instance claim only pixels that no earlier instance claimed.
- Check that an instance's predicted class is a valid thing class.
- Keep semantic labels for stuff pixels.
- Mark thing pixels that no accepted instance claims as void or as a configured fallback.
The order matters. If two masks overlap, giving pixels to the higher-scoring instance makes the result deterministic. More advanced systems use mask quality, overlap thresholds, and class compatibility rules.
import numpy as np
def merge_to_panoptic(
semantic, inst_masks, inst_classes, inst_scores, thing_classes,
score_threshold=0.5
):
semantic = np.asarray(semantic)
inst_masks = np.asarray(inst_masks, dtype=bool)
inst_classes = np.asarray(inst_classes)
inst_scores = np.asarray(inst_scores)
output_class = semantic.copy()
output_instance = np.zeros(semantic.shape, dtype=np.int32)
taken = np.zeros(semantic.shape, dtype=bool)
thing_classes = np.asarray(thing_classes)
order = np.argsort(-inst_scores)
next_id = 1
for i in order:
if inst_scores[i] < score_threshold:
continue
if not np.isin(inst_classes[i], thing_classes):
continue
free_pixels = inst_masks[i] & ~taken
if not free_pixels.any():
continue
output_class[free_pixels] = inst_classes[i]
output_instance[free_pixels] = next_id
taken[free_pixels] = True
next_id += 1
is_thing = np.isin(semantic, thing_classes)
output_class[is_thing & ~taken] = -1
return output_class, output_instanceThe class and instance maps must have the same height and width. The instance IDs are local to one image. Do not compare ID 1 in two different images as though it represented the same object.
Data flow
Practical checks
Inspect the final maps visually and numerically:
- Every instance ID should correspond to one class.
- Instance masks should not overlap.
- Stuff pixels should normally have instance ID
0. - Ignored or uncertain pixels should use the dataset's required void label.
- Use the same class IDs and void convention during training, merging, and evaluation.
Exercise
Two instance masks overlap, and instance A has score 0.82 while instance B has score 0.76. Which mask receives the overlapping pixels in the procedure above?
Show answer
Instance A receives them because instances are processed in descending confidence order. Instance B can claim only the remaining unclaimed pixels.