DINO-KNN: Training-Free Multiclass In-Context Segmentation via Nearest Neighbors
Authors: Hussain Alasmawi, Numan Saeed, Mohammad Yaqub
Organizations: Division of Computing and Mathematical Sciences, Mohamed bin Zayed University of Artificial Intelligence (MBZUAI), Abu Dhabi, United Arab Emirates
In-context segmentation (ICS) segments a target image given a few annotated reference images. Recent training-free methods built on frozen foundation models segment one class at a time and gain little from additional references. We study the multiclass, multi-reference setting and ask how far a simple joint decision rule can go. We introduce DINO-KNN, which stores every labeled reference patch of a frozen DINOv3 encoder in a single memory and labels each target patch by a distance-weighted vote of its k nearest neighbors. All classes are predicted in a single pass, with one label per pixel, without training or a mask decoder. We evaluate on eight datasets, four natural and four medical, with R=1 to 15 reference images and three reference seeds. DINO-KNN achieves the best average mIoU at every R; it is the best method on six of the eight datasets from R=3 and on seven of the eight from R=7. Its accuracy continues to grow with the number of references, from 37.8 mIoU at R=1 to 56.2 at R=15, while the strongest baseline, INSID3, rises from 36.1 to 42.9. DINO-KNN is also 2.1x faster than INSID3. The code will be made public.
Figures & tables
Figure 2: Overview of DINO-KNN with R=3 references. References and target are encoded by a frozen DINOv3 ViT-L/16 and positionally debiased. All labeled reference patches form one memory. The memory panel shows the reference patches around the marked target patch: a dot closer to the center is more similar to it, and the circle encloses its k nearest patches. Their inverse-distance votes yield the patch’s class scores. The vote is repeated for every target patch, and the patch label map is resized to the image.
Natural images
Medical images
Average
R
Method
COCO
LVIS
SUIM
iSAID
CAMUS
FASS
PSFHS
EndoVis17
Natural (4)
Medical (4)
All (8)
1
PerSAM
6.7 ± 2.1
6.9 ± 0.9
20.9 ± 1.0
17.2 ± 4.9
27.1 ± 11.0
6.9 ± 4.2
20.7 ± 2.1
17.0 ± 0.7
12.9 ± 2.2
17.9 ± 2.4
15.4 ± 2.2
GF-SAM
41.2 ± 1.2
43.5 ± 1.0
58.9 ± 0.9
37.7 ± 1.3
30.1 ± 5.0
7.8 ± 1.0
22.3 ± 2.7
34.8 ± 1.7
45.3 ± 0.4
23.7 ± 1.9
34.5 ± 0.8
INSID3
41.3 ± 2.1
47.4 ± 0.9
55.6 ± 1.5
44.6 ± 3.3
32.3 ± 7.5
9.9 ± 1.4
23.4 ± 1.5
34.1 ± 0.3
47.2 ± 0.4
24.9 ± 2.0
36.1 ± 0.8
DINO-KNN (ours)
35.7 ± 0.8
40.7 ± 1.1
57.2 ± 0.4
43.5 ± 3.3
34.1 ± 9.7
15.2 ± 4.9
39.4 ± 5.6
36.5 ± 1.5
44.3 ± 1.0
31.3 ± 2.8
37.8 ± 1.7
3
GF-SAM
48.5 ± 1.6
51.4 ± 1.0
65.9 ± 1.0
43.8 ± 2.1
32.2 ± 4.5
9.3 ± 3.5
21.9 ± 0.6
38.4 ± 0.5
52.4 ± 0.8
25.4 ± 0.7
38.9 ± 0.1
Table 1: Multiclass mIoU for R=1 to 15 reference images , mean ± std over three seeds. PerSAM accepts a single reference. Best mean per column and R in bold.
Figure 3: Qualitative results of DINO-KNN and INSID3 for R=1,5,15 , with per-episode mIoU.
R=1
R=5
Nat.
Med.
All
Nat.
Med.
All
Nearest prototype
24.7 ± 0.4
13.3 ± 2.6
19.0 ± 1.1
28.3 ± 1.0
14.5 ± 2.1
21.4 ± 0.7
1-NN
41.4 ± 1.0
30.3 ± 2.5
35.8 ± 1.5
52.1 ± 0.6
41.8 ± 1.8
47.0 ± 1.2
k -NN w/o debiasing
40.3 ± 1.1
30.1 ± 3.1
35.2 ± 1.7
53.2 ± 0.4
43.0 ± 1.7
48.1 ± 1.1
k -NN (ours)
44.3 ± 1.0
31.3 ± 2.8
37.8 ± 1.7
56.7 ± 0.7
43.7 ± 1.6
50.2 ± 1.1
Table 2: Reference representation and positional debiasing. Average mIoU, mean over three seeds.
Figure 4: Number of neighbors k . Average mIoU of DINO-KNN over LVIS, CAMUS and EndoVis17, mean over three seeds. Dashed line: R=1 ; solid line: R=5 . The dotted line marks k=15 . Shaded: standard deviation over seeds.
Figure 5: Encoder size. Average mIoU over three seeds with DINOv3 ViT-S/16, ViT-B/16 and ViT-L/16. Dashed lines: R=1 ; solid lines: R=5 . Shaded: standard deviation over seeds.
Figure 6: Failure cases : episodes on which DINO-KNN and INSID3 both have a low IoU at R=15 , with per-episode mIoU.
Figure 7: Input size. Average mIoU of DINO-KNN and INSID3 with 512×512 , 1024×1024 and 2048×2048 inputs, one seed, on the targets evaluated at all three sizes. Dashed lines: R=1 ; solid lines: R=5 .
R=1
R=3
R=5
Method
penalized
ignored
penalized
ignored
penalized
ignored
INSID3
35.9 ± 0.5
44.1 ± 1.2
42.2 ± 1.1
49.7 ± 1.2
44.2 ± 1.1
51.7 ± 0.8
DINO-KNN (ours)
35.2 ± 0.5
42.2 ± 0.8
43.1 ± 0.6
50.7 ± 0.7
45.5 ± 1.0
53.0 ± 0.8
Table 3: Classes absent from the target , mean ± std over three seeds, averaged over COCO, LVIS, SUIM, iSAID and EndoVis17. The references keep all their classes. Predictions of absent classes are either penalized as false positives or ignored.
Latency (ms)
GPU memory (GB)
Method
Input
R=1
R=3
R=5
R=1
R=3
R=5
PerSAM
10242
393
–
–
5.6
–
–
GF-SAM
10242
762
488
629
6.8
6.1
6.3
INSID3
10242
470
474
474
1.6
1.7
1.9
DINO-KNN (ours)
10242
217
221
225
1.5
1.6
1.7
Table 4: Latency per target image and peak GPU memory on COCO episodes with cached reference features, on one NVIDIA RTX 5000 Ada. Latency is the mean over episodes.
Natural images
Medical images
Average
R
Method
COCO
LVIS
SUIM
iSAID
CAMUS
FASS
PSFHS
EndoVis17
Natural (4)
Medical (4)
All (8)
1
PerSAM
9.1
7.9
22.0
22.7
39.7
2.2
22.6
16.6
15.4
20.3
17.9
Matcher
28.2
31.6
50.5
33.8
38.8
6.0
23.8
23.2
36.0
22.9
29.5
GF-SAM
39.9
44.7
58.1
38.2
34.0
7.4
24.1
36.7
45.2
25.6
35.4
INSID3
39.1
47.2
54.1
47.6
39.8
8.3
24.8
34.5
47.0
26.8
36.9
DINO-KNN (ours)
35.2
41.7
57.6
46.6
43.7
12.8
42.3
37.9
45.3
34.2
39.7
Table S1 : All methods, one seed. mIoU, R=1,3,5 .
Figure S1 : mIoU against the number of reference images R , per dataset (Tab. 1 of the main paper). Bands: ± standard deviation over three seeds.
Natural images
Medical images
Average
R
Method
COCO
LVIS
SUIM
iSAID
CAMUS
FASS
PSFHS
EndoVis17
Natural (4)
Medical (4)
All (8)
1
Nearest prototype
17.9 ± 1.1
13.6 ± 0.2
43.3 ± 1.7
23.9 ± 1.1
24.1 ± 7.0
3.6 ± 1.1
14.7 ± 4.2
10.9 ± 0.9
24.7 ± 0.4
13.3 ± 2.6
19.0 ± 1.1
1-NN
32.4 ± 0.4
38.2 ± 1.5
53.3 ± 0.6
41.5 ± 3.4
33.1 ± 9.5
15.9 ± 4.5
38.1 ± 4.9
34.3 ± 1.1
41.4 ± 1.0
30.3 ± 2.5
35.8 ± 1.5
k -NN w/o debiasing
33.1 ± 0.2
37.3 ± 0.5
52.7 ± 0.7
38.1 ± 3.9
34.4 ± 10.3
13.5 ± 5.1
36.7 ± 7.3
35.8 ± 1.0
40.3 ± 1.1
30.1 ± 3.1
35.2 ± 1.7
k -NN (ours)
35.7 ± 0.8
40.7 ± 1.1
57.2 ± 0.4
43.5 ± 3.3
34.1 ± 9.7
15.2 ± 4.9
39.4 ± 5.6
36.5 ± 1.5
44.3 ± 1.0
31.3 ± 2.8
37.8 ± 1.7
5
Nearest prototype
21.2 ± 1.1
14.9 ± 0.4
50.7 ± 1.7
26.4 ± 1.2
25.1 ± 5.2
5.8 ± 0.8
13.7 ± 3.0
13.5 ± 0.9
28.3 ± 1.0
14.5 ± 2.1
21.4 ± 0.7
Table S2 : Reference representation and positional debiasing in DINO-KNN , per dataset (Tab. 2 of the main paper). mIoU, mean ± std over three seeds.
Natural images
Medical images
Average
R
Method
COCO
LVIS
SUIM
iSAID
CAMUS
FASS
PSFHS
EndoVis17
Natural (4)
Medical (4)
All (8)
1
INSID3, ViT-S/16
29.3 ± 0.9
32.1 ± 0.4
49.0 ± 0.5
22.5 ± 1.4
32.3 ± 3.4
5.4 ± 1.8
21.3 ± 5.8
22.5 ± 0.7
33.2 ± 0.6
20.4 ± 2.3
26.8 ± 0.9
INSID3, ViT-B/16
34.2 ± 1.6
38.0 ± 0.6
56.1 ± 0.5
30.1 ± 3.3
31.7 ± 8.1
7.4 ± 1.2
19.8 ± 4.1
31.8 ± 2.6
39.6 ± 1.0
22.7 ± 3.4
31.1 ± 1.9
INSID3, ViT-L/16
41.3 ± 2.1
47.4 ± 0.9
55.6 ± 1.5
44.6 ± 3.3
32.3 ± 7.5
9.9 ± 1.4
23.4 ± 1.5
34.1 ± 0.3
47.2 ± 0.4
24.9 ± 2.0
36.1 ± 0.8
DINO-KNN, ViT-S/16
28.2 ± 0.8
30.9 ± 1.2
48.0 ± 0.9
27.0 ± 1.3
34.5 ± 9.5
12.5 ± 4.4
35.2 ± 2.3
32.2 ± 0.2
33.5 ± 0.4
28.6 ± 2.1
31.1 ± 1.0
DINO-KNN, ViT-B/16
33.5 ± 0.9
36.3 ± 1.0
54.6 ± 0.7
34.0 ± 1.6
36.2 ± 12.1
14.4 ± 4.8
34.9 ± 7.6
36.4 ± 0.9
39.6 ± 0.6
30.5 ± 3.7
35.0 ± 1.9
Table S3 : Encoder size (DINOv3), per dataset (Fig. 5 of the main paper). mIoU, mean ± std over three seeds.
Natural images
Medical images
Average
R
Method
COCO
LVIS
SUIM
iSAID
CAMUS
FASS
PSFHS
EndoVis17
Natural (4)
Medical (4)
All (8)
1
INSID3, 512
36.0
44.2
54.1
47.0
38.3
9.1
28.5
35.4
45.3
27.8
36.6
INSID3, 1024
39.1
47.2
54.1
47.6
39.7
8.3
24.8
34.5
47.0
26.8
36.9
INSID3, 2048
38.8
48.3
57.8
43.0
38.7
7.1
27.0
31.9
47.0
26.2
36.6
DINO-KNN, 512
30.8
36.2
53.8
42.7
43.5
5.9
44.6
35.2
40.9
32.3
36.6
DINO-KNN, 1024
35.2
41.7
57.6
46.6
43.3
12.8
42.3
37.9
45.3
34.1
39.7
Table S4 : Input size ( 512 , 1024 or 2048 pixels per side), per dataset (Fig. 7 of the main paper). mIoU, one seed.
Absent classes penalized
Absent classes ignored
R
Method
COCO
LVIS
SUIM
iSAID
EndoVis17
COCO
LVIS
SUIM
iSAID
EndoVis17
1
INSID3
22.1 ± 2.0
40.5 ± 1.3
50.4 ± 1.0
39.6 ± 3.3
27.0 ± 0.3
44.0 ± 1.7
40.5 ± 1.3
55.2 ± 1.8
47.0 ± 4.6
33.6 ± 0.1
DINO-KNN (ours)
19.3 ± 1.5
33.1 ± 0.1
52.2 ± 0.9
42.2 ± 4.7
29.2 ± 1.3
37.2 ± 2.3
33.1 ± 0.1
57.3 ± 0.5
47.1 ± 3.8
36.4 ± 1.5
3
INSID3
29.3 ± 1.1
45.9 ± 0.7
57.5 ± 1.8
47.2 ± 3.8
31.2 ± 0.7
49.1 ± 1.5
45.9 ± 0.7
61.6 ± 2.0
52.2 ± 3.0
39.5 ± 0.9
DINO-KNN (ours)
26.9 ± 0.9
44.2 ± 0.6
59.5 ± 1.0
51.0 ± 3.0
34.1 ± 1.0
46.0 ± 2.2
44.2 ± 0.6
64.3 ± 1.1
55.8 ± 2.1
42.9 ± 1.5
5
INSID3
32.1 ± 2.0
46.7 ± 0.3
60.2 ± 1.6
50.5 ± 4.3
31.7 ± 2.0
52.0 ± 1.5
46.7 ± 0.3
63.9 ± 1.5
55.1 ± 2.5
40.5 ± 2.9
Table S5 : Classes absent from the target , per dataset (Tab. 3 of the main paper). mIoU, mean ± std over three seeds. Predictions of absent classes are either penalized as false positives or ignored. LVIS has no episode with an absent class here, so both metrics are equal.
Figure S2 : Improvement of DINO-KNN with more references, natural images.
Figure S3 : Improvement of DINO-KNN with more references, medical images.
Figure S4 : Failure cases of DINO-KNN, natural images.
Figure S5 : Failure cases of DINO-KNN, medical images.
In-context segmentation (ICS) aims to segment arbitrary concepts, e.g., objects, parts, or personalized instances, given one annotated visual examples. Existing work relies on (i) fine-tuning vision foundation models (VFMs), which improves in-domain results but harms generalization, or (ii) combines multiple frozen VFMs, which preserves generalization but yields architectural complexity and fixed segmentation granularities. We revisit ICS from a minimalist perspective and ask: Can a single self-supervised backbone support both semantic matching and segmentation, without any supervision or auxiliary models? We show that scaled-up dense self-supervised features from DINOv3 exhibit strong spatial structure and semantic correspondence. We introduce INSID3, a training-free approach that segments concepts at varying granularities only from frozen DINOv3 features, given an in-context example. INSID3 achieves state-of-the-art results across one-shot semantic, part, and personalized segmentation, outperforming previous work by +7.5 % mIoU, while using 3x fewer parameters and without any mask or category-level supervision. Code is available at https://github.com/visinf/INSID3 .
Claudia Cuttano, Gabriele Trivigno, Christoph Reich +3
Politecnico di Torino · TU Darmstadt · TU Munich +2
In-context segmentation (ICS) uses one or a few annotated reference images to specify what to segment in a target image. Existing training-free ICS approaches typically rely on manually designed inference procedures, which may limit how flexibly reference information is combined. Learned in-context inference offers an adaptive alternative, but acquiring this capability through segmentation-specific training can compromise generalization to unseen domains. We therefore ask: can a pretrained in-context learner infer what to segment from annotated references without segmentation-specific training? We introduce SegPFN, a training-free framework that reformulates ICS as tabular classification over frozen visual features. A frozen tabular prior-data fitted network (PFN) uses labeled reference patches as context to classify patches in the target image. We comprehensively benchmark SegPFN against major existing ICS methods across 11 datasets spanning semantic, part, and personalized segmentation. SegPFN improves overall mean intersection-over-union (mIoU) by 4.7 points over the strongest overall baseline and achieves the highest average mIoU in four of five task-shot settings. Further analyses show that hard-negative reference patches improve fine-grained target discrimination, while joint conditioning enables SegPFN to use multiple references effectively. These results show that SegPFN offers a simple and effective paradigm for training-free in-context segmentation.
In-Context Segmentation (ICS) aims to precisely segment arbitrary semantic concepts, such as objects or parts, given one or a few annotated visual exemplars. In this paper, we revisit ICS from a more classical segmentation perspective, viewing it as a coarse-to-fine progressive refinement process. Rather than directly predicting the final mask through reference-query matching, we progressively refine the segmentation from coarse and ambiguous foreground responses to precise and complete foreground structures. Building upon this perspective, we propose a training-free in-context segmentation framework, termed FoRIS. Specifically, FoRIS consists of three key stages: Foreground Purification, Foreground Localization, and Foreground Consolidation, which progressively suppress background distractions, localize discriminative target regions, and recover complete foreground structures through semantic aggregation. Experimental results demonstrate that FoRIS achieves SOTA performance across semantic and part segmentation tasks, with average improvements of 4.5 and 4.8 mIoU points over existing approaches in the 1-shot and 5-shot settings, respectively. Code: https://github.com/Xi-Mu-Yu/FoRIS.
Ming Hu, Jianfu Yin, Mingyu Dou +5
Xi’an Institute of Optics and Precision Mechanics, Chinese Academy of Sciences · University of Chinese Academy of Sciences · Xi’an Jiaotong University +1