Instance segmentation
Quick answer
Instance segmentation distinguishes individual objects even when they share a class. Each detection includes its own mask, class and confidence. A semantic map may merge two touching objects into a single foreground region. Examples are static Python snippets tested locally on CPU; Torchvision is required for selected examples. ByteMind execution is unverified.
Same box, different masks
a={(0,0),(1,1)};b={(0,1),(1,0)}print(len(a&b),len(a|b))print("box IoU = 1; mask IoU = 0")Expected output (locally tested): 0 4 box IoU = 1; mask IoU = 0. Both masks span the same 2x2 box but share no pixels.
How it works
Represent a binary mask as a set of foreground pixel indices. Mask IoU uses the same intersection-over-union definition as boxes, but counts pixels instead of rectangle area. Overlapping boxes can contain disjoint masks. Match predicted masks by class and image using confidence-ranked one-to-one matching with a mask-IoU threshold; duplicates are false positives. Instance IDs are arbitrary identifiers, not semantic class IDs. Different instance masks may overlap in a prediction; a panoptic map instead assigns a single segment per pixel and covers stuff as well as things. Report empty-mask handling and whether crowd/ignore regions are supported. The simple exercise treats a pair of empty masks as zero union and returns counts, leaving IoU undefined. Evaluate mask AP separately from box AP.
Merged instances
truth=[{0,1},{2,3}]pred=[{0,1,2,3}]print(set.union(*truth)==set.union(*pred))print([len(pred[0]&m)/len(pred[0]|m) for m in truth])Expected output (locally tested): True [0.5, 0.5]. Semantic foreground is identical, but one prediction cannot match both instances.
Common mistakes
Do not match by arbitrary instance ID equality. Do not collapse all same-class masks into one before instance evaluation. Do not infer mask overlap from box overlap.
Practice and next steps
Construct two disjoint diagonal pixel masks with the same enclosing box and compare box IoU with mask IoU. Inspect why merging instances can preserve semantic accuracy.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Instance segmentation — knowledge check
Apply instance segmentation to small examples and debugging scenarios.
Two touching circles of same class: which task separates them?
Can identical boxes have zero mask IoU?
Should predicted instance ID equal annotated ID?
A merged mask overlaps two objects. Maximum matches per prediction?
What does empty union imply?