Foundations to Frontier
Tests
G. Computer Vision and Remote Sensing › G2 Core Vision Tasks

Instance and panoptic segmentation, Segment Anything

Instance segmentation gives every countable object its own mask; panoptic segmentation also labels the uncountable background ('stuff' such as sky, road, water), so every pixel gets a class and, for things, an instance id. Segment Anything (SAM, 2023) and SAM 2 (2024) added promptable, class-agnostic masks that now drive most annotation pipelines. Architectures are in Section E10 (Mask R-CNN, Panoptic FPN, Mask2Former, SAM); this page covers the task. It sets out the problem definitions, the datasets (COCO, Cityscapes, LVIS, SA-1B, iSAID), the metrics (mask AP, PQ = SQ × RQ), SAM-based labelling and inference pipelines, and the central failure mode: touching objects. In the exercises you implement the IoU matrix, PQ with its SQ and RQ parts, and SAM's prompt grid. You then turn a perfect foreground mask into instances in sparse and dense scenes. Connected components scored PQ 0.869 when sparse but 0.171 when dense, where 79% of objects merged with a neighbour. Erosion seeds reached 0.481, and per-object centres with 1-pixel noise and 5% misses reached 0.820.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Not just ‘building pixels’ but ‘building 1, building 2, …’

Semantic segmentation colours all buildings the same; instance segmentation gives each building its own colour, so you can count them and measure each one. Panoptic segmentation does both: every pixel gets a class, and pixels of countable things also say which object they belong to. Segment Anything is a model you can click on: point at something and it returns that object’s mask, even for objects it was never trained on. The hard case is objects that touch. The demo shows two disks: once they touch, a naive method sees one object.

Vision task lab
Connected components find 1 object (PQ 0.33); eroding 0 steps to find seeds and growing them back finds 1 (PQ 0.33). Touching objects (gap ≤ 0) merge unless something separates them.
Why engineers care

Counting and measuring individual buildings for disaster damage assessment (xBD), trees for forest inventories, boulders for landing-site hazard maps and parts on a conveyor for robotic picking all need instance masks.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Instance segmentation distinguishes…
  2. 2. Panoptic segmentation labels…
  3. 3. SAM returns a mask for…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Things and stuff. Things are countable (cars, people, buildings); stuff is amorphous (road, sky, vegetation, water). Instance segmentation covers things only; panoptic covers both, with no overlaps: each pixel has exactly one (class, id).
  • Methods (E10). Detect-then-segment: Mask R-CNN (He et al. 2017), with a 28 × 28 mask per box. Single-stage: YOLACT (2019), SOLOv2 (2020), YOLOv8/YOLO11-seg. Bottom-up: semantic map plus grouping by centres and offsets, as in Panoptic-DeepLab (Cheng et al. 2020). Query-based: Mask2Former (Cheng et al. 2022) and OneFormer (2023) handle semantic, instance and panoptic with one model.
  • Promptable models. SAM (Kirillov et al. 2023) takes points, boxes or masks; SA-1B has 11 million images and 1.1 billion masks. SAM 2 (Ravi et al. 2024) adds video with a memory. Grounded-SAM (2024) chains a text-prompted detector (Grounding DINO) to SAM; SAM 3 (Meta, 2025) accepts short text concepts directly.
  • Datasets. COCO (80 thing classes with masks; COCO panoptic has 133 classes); Cityscapes; LVIS (1203 classes); iSAID (Zamir et al. 2019; 15 classes, about 655000 instances in aerial images); SpaceNet and xBD for buildings.
  • Metrics. Mask AP (COCO AP with mask IoU); panoptic quality PQ = SQ × RQ (Kirillov et al. 2019), where segments match if IoU exceeds 0.5 (unique by construction), SQ is the mean IoU of matches and RQ = TP/(TP + FP/2 + FN/2).
📝 Quick check (Level 1)
0/3 answered
  1. 1. Road and sky are…
  2. 2. PQ factors into…
  3. 3. PQ matches are unique because…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Instance masks in practice, with and without SAM

Exercise 1.

  • The IoU matrix between two instance label maps.
  • PQ split into SQ and RQ.
  • Mask-to-box and SAM’s grid of prompt points.

Practice.

  • Labelling. Use SAM or SAM 2 in the annotation tool (CVAT, Label Studio): click or box an object, correct the mask; for video, propagate with SAM 2 and fix drift.
  • Detector + SAM. For a fixed class list, a fine-tuned detector’s boxes as SAM prompts often give better masks than a small instance-segmentation head; for open vocabulary, Grounding DINO + SAM.
  • Dense, touching objects (buildings, cells, parked cars): predict something that separates instances, such as centres and offsets, a boundary class or distance maps; connected components of a semantic map are not enough.
  • Post-processing. Remove tiny fragments by area, resolve overlaps by score (as panoptic merging does), fill holes.
  • SAM caveats. It is class-agnostic (add a classifier), ambiguous about granularity (part or whole; it returns three candidates), and heavy (ViT-H encoder); EfficientSAM, MobileSAM and SAM 2’s small variants run faster.
  • Evaluate with mask AP for instances and PQ for panoptic, reporting things and stuff separately.
from ultralytics import YOLO, SAM
det = YOLO("buildings_yolo11s.pt")                       # a detector fine-tuned on building boxes
sam = SAM("sam2.1_b.pt")
boxes = det("tile.png")[0].boxes.xyxy                     # detector boxes as prompts
masks = sam("tile.png", bboxes=boxes)[0].masks.data       # one mask per box
# automatic mode: a 32 x 32 grid of point prompts, then filtering by predicted IoU and stability score
from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator
🐍 Exercise 1: IoU matrix, PQ and SAM prompts
Compute the IoU matrix between instance maps, PQ with SQ and RQ, a mask's box and SAM's prompt grid.
📝 Quick check (Level 2)
0/3 answered
  1. 1. SAM's masks need, for a class list,…
  2. 2. For touching buildings, predict…
  3. 3. SAM 2 extends SAM to…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why touching objects merge. A semantic map says only ‘thing or not’. Two objects whose masks share an edge form one connected region, so any method that only groups connected foreground pixels must merge them; with random placement the share of objects touching a neighbour rises quickly with density, roughly like the fraction of area covered.

Merging costs PQ twice. A merged segment usually has IoU at most about 0.5 with each of its objects, so it matches none: one false positive and two false negatives instead of two true positives. RQ falls and SQ is unaffected, so PQ falls in proportion.

Separating cues. Erosion shrinks the mask until thin necks break, finds seeds, and grows them back (the marker-controlled watershed idea); it fails when objects overlap more than the erosion depth or are smaller than it. Centre-based methods predict one point per object and assign pixels to the nearest or offset-indicated centre; they separate objects regardless of contact, and their errors come from missed or duplicated centres. Query-based models (Mask2Former) and prompt-based ones (SAM with one prompt per object) are the learned versions of the same idea: one query or prompt per instance.

PQ details. PQ = Σ IoU over matches / (TP + FP/2 + FN/2) = SQ × RQ. Small, unmatched fragments count as false positives in full, which is why area filtering helps.

📝 Quick check (Level 3)
0/3 answered
  1. 1. A merged segment covering two equal objects typically…
  2. 2. Erosion seeds fail when…
  3. 3. One prompt per object in SAM plays the role of…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How well do connected components, erosion seeds and object centres turn a foreground mask into instances as objects crowd together?

History.

  • K. He, G. Gkioxari, P. Dollár and R. Girshick, “Mask R-CNN” (2017).
  • A. Kirillov, K. He, R. Girshick, C. Rother and P. Dollár, “Panoptic segmentation” (2019): the task and PQ.
  • B. Cheng et al., “Panoptic-DeepLab” (2020): centres and offsets.
  • A. Kirillov et al., “Segment anything” (2023).

Exercise 2. 64 × 64 scenes of disks with radius 3–6 pixels (tanks, trees, buildings), 10 scenes per density.

  • Placement: sparse (10 objects) or dense (40 objects); a new disk is rejected if its centre is closer than 0.8 × the sum of radii to an earlier one, so objects touch and overlap slightly but do not pile up.
  • Input: a perfect foreground mask, which isolates the grouping step.
  • Methods: connected components; erosion by 2 pixels, components as seeds, grown back over the mask; nearest-centre grouping with predicted centres jittered by 1 pixel and 5% missed.
  • Measures: PQ and the mean error in the number of instances.
method sparse PQ sparse count error dense PQ dense count error
connected components 0.869 −1.3 0.171 −31.6
erosion seeds 0.919 −0.6 0.481 −15.9
noisy centres 0.962 −0.1 0.820 −1.7
  • Connected components collapsed with density: 13% of objects merged with a neighbour in sparse scenes but 79% in dense ones, and 40 objects became about 8 regions.
  • Erosion seeds helped but only partly: necks wider than the erosion depth survived, leaving about 16 objects uncounted.
  • Centres separated objects regardless of contact (PQ 0.820); their errors are missed centres (two objects share one) and boundary pixels assigned to the wrong neighbour.
  • Seeds 1 and 2: dense PQ 0.286 and 0.190 (components), 0.586 and 0.484 (erosion), 0.823 and 0.785 (centres). In sparse scenes erosion seeds beat noisy centres on both seeds (0.921 against 0.897, 0.959 against 0.930): when objects barely touch, 1-pixel centre noise costs more than it gains.
  • Caveat: foreground masks were perfect and centres simulated; real systems also err on the foreground, and their centre quality is what a network learns.
🐍 Exercise 2 (research): how well do connected components, erosion seeds and object centres turn a foreground mask into instances as objects crowd together?
Group a perfect foreground mask into instances with connected components, erosion seeds and noisy centres, in sparse and dense scenes, and score PQ and count error.

Instance and panoptic segmentation in AI, robotics and aerospace

  • Disaster response: per-building damage from xBD (2019) imagery, which needs building instances before damage classification.
  • Planetary science: crater and boulder instance segmentation for landing-site hazard maps and surface dating.
  • Robotic manipulation: instance masks of parts or debris for grasp planning; SAM with depth for unseen objects.
  • Annotation at scale: in SAM’s own data engine, model-assisted annotation took about 14 seconds per mask, which the authors put at 6.5 times faster than COCO’s polygon labelling.

References

  1. K. He, G. Gkioxari, P. Dollár and R. Girshick (2017). “Mask R-CNN.” ICCV.
  2. A. Kirillov, K. He, R. Girshick, C. Rother and P. Dollár (2019). “Panoptic segmentation.” CVPR.
  3. B. Cheng et al. (2020). “Panoptic-DeepLab: a simple, strong, and fast baseline for bottom-up panoptic segmentation.” CVPR.
  4. B. Cheng, I. Misra, A. Schwing, A. Kirillov and R. Girdhar (2022). “Masked-attention mask transformer for universal image segmentation.” CVPR.
  5. A. Kirillov et al. (2023). “Segment anything.” ICCV.
  6. N. Ravi et al. (2024). “SAM 2: segment anything in images and videos.” arXiv (ICLR 2025).
  7. S. W. Zamir et al. (2019). “iSAID: a large-scale dataset for instance segmentation in aerial images.” CVPR Workshops.
  8. R. Gupta et al. (2019). “xBD: a dataset for assessing building damage from satellite imagery.” CVPR Workshops.