Visual Foundation Models (VFMs) such as the Segment Anything Model (SAM) have significantly advanced broad use of image segmentation. However, SAM and its variants necessitate substantial manual effort for prompt generation and additional training for specific applications. Recent approaches address these limitations by integrating SAM into in-context (one/few shot) segmentation, enabling auto-prompting through semantic alignment between query and support images. Despite these efforts, they still generate sub-optimal prompts that degrade segmentation quality due to visual inconsistencies between support and query images. To tackle this limitation, we introduce PR-MaGIC (Prompt Refinement via Mask Decoder Gradient Flow for In-Context Segmentation), a training-free test-time framework that refines prompts via gradient flow derived from SAM's mask decoder. PR-MaGIC seamlessly integrates into in-context segmentation frameworks, being theoretically grounded yet practically stabilized through a simple top-1 selection strategy that ensures robust performance across samples. Extensive evaluations demonstrate that PR-MaGIC consistently improves segmentation quality across various benchmarks, effectively mitigating inadequate prompts without requiring additional training or architectural modifications.
Figures & tables
Figure 1: Illustration of PR-MaGIC. PR-MaGIC iteratively refines prompts for segmentation by updating the embedding vector distribution ρt with mask decoder gradient flow. This process minimizes the KL divergence between ρt and the target embedding vector distribution μ from the ground truth mask. At each iteration t , given an initial set of prompts and their corresponding segmentation masks, the embedding vector zt is updated along the gradient flow derived from the mask decoder, which in turn updates ρt .
Figure 2: Example of prompts (green dots) and segmentation results (red masks). (a) Support Image (elephant), (b) and (c) Misaligned prompts and segmentation results from PerSAM-F and Matcher, (d) Result from Matcher refined by PR-MaGIC, (e) Ground Truth.
Figure 3: Overview of PR-MaGIC . Encoder box denotes image encoder ( Eθ ), sim box denotes similarity computing module, and Mask Decoder box denotes SAM’s mask decoder. SAM’s prompt encoder is omitted here to show the clear flow of our method, and green boxes denote our core method. 1) Image Embedding : Encoder maps the support and query images to embeddings z0s and z0q , with z0s selected using the given support mask. 2) Prompt Refinement via Gradient Flow : Using Mask Decoder ’s logit dϕ , we update the query embeddings by the mask decoder driven gradient flow, resample prompts Pt+1 from St+1 , and iterate for t=0…T−1 to form a candidate set of masks {m^t}t=1T . 3) Segmentation Mask Selection (Top-1 similarity selection) : Among the generated masks, we select the one whose mask-aware query embedding zt′q exhibits the highest similarity to the support embedding z′s .
Figure 4: Refinement process of PR-MaGIC. Curated illustrations of refinement process with PerSAM-F as the baseline. a) Target objects in the support images with blue masks (bear (top) and airplane (bottom)), b) Segmentation results from PerSAM-F with point prompts in green dots. c), d) Refined prompt and segmentation after one and five iterations, e) Ground truth (G.T.).
Figure 5: Qualitative results for semantic and part segmentation. Baselines from top to bottom: PerSAM-F, Matcher. a) : Semantic segmentation. Target objects in support images are highlighted with blue masks, and point prompts are denoted by green dots. b) : Part segmentation. Here, target objects are highlighted with blue masks in green boxes, and point prompts are similarly marked and emphasized. Refinement with PR-MaGIC (3 rd columns) improves the segmentation over the baselines (2 nd columns).
(a) Semantic
(b) Part / Fine-grained
FSS
COCO
LVIS
PACO
Pascal
DIS
Method
B
T
O ∗
B
T
O ∗
B
T
O ∗
B
T
O ∗
B
T
O ∗
B
T
O ∗
PerSAM-F
85.85
87.26
91.88
53.36
55.96
63.89
47.62
50.76
56.09
45.21
48.17
54.20
48.43
51.69
57.08
60.90
64.66
71.98
Matcher(1-shot)
92.08
92.06
93.55
69.53
71.23
76.14
59.39
61.52
64.75
50.27
54.08
56.71
54.76
58.28
61.13
46.65
55.08
58.10
Matcher(5-shot)
93.26
93.41
94.32
67.69
70.74
74.88
57.14
60.79
63.88
48.66
53.15
55.30
54.54
59.27
61.55
–
–
–
Table 1: mIoU (%) on six datasets. B = baseline, O = PR-MaGIC with Oracle selection ( ∗ ), T = PR-MaGIC with Top-1 selection. Bold indicates T values that improve over baseline.
Model
FSS
COCO
LVIS
PACO-Part
Pascal-Part
DIS
Oracle
Top-1
Oracle
Top-1
Oracle
Top-1
Oracle
Top-1
Oracle
Top-1
Oracle
Top-1
PerSAM
2.53 ± 1.71
2.61 ± 1.71
2.41 ± 1.72
2.51 ± 1.73
2.50 ± 1.71
2.48 ± 1.71
2.50 ± 1.72
2.48 ± 1.72
2.49 ± 1.72
2.48 ± 1.72
2.45 ± 1.72
2.49 ± 1.69
Matcher (1shot)
2.45 ± 1.72
2.55 ± 1.71
2.47 ± 1.74
2.47 ± 1.72
2.05 ± 1.80
2.05 ± 1.81
2.44 ± 1.73
2.46 ± 1.73
2.44 ± 1.73
2.42 ± 1.72
2.51 ± 1.73
2.52 ± 1.70
Matcher (5shot)
2.50 ± 1.72
2.50 ± 1.73
2.41 ± 1.71
2.45 ± 1.74
2.29 ± 1.77
2.29 ± 1.77
2.49 ± 1.71
2.48 ± 1.71
2.48 ± 1.72
2.47 ± 1.72
–
–
Table 2: Mean ± std of the optimal iterations in PR-MaGIC under Oracle and Top-1 selection across various datasets and baselines.
Figure 6: Sensitivity analysis on the step size η and iterations T shows that the gradient flow can substantially improve segmentation performance but may exhibit instability under certain conditions, which motivates top-1 mask selection.
Figure 7: Convergence failure cases of PR-MaGIC. Each column represents: (a) Support Image, (b) Baseline output, (c) PR-MaGIC ( t=1 ), (d) PR-MaGIC ( t=5 ), and (e) Ground Truth. The top two rows compare PR-MaGIC with PerSAM-F as the baseline, while the bottom two rows compare PR-MaGIC with Matcher. In this case, PR-MaGIC selects the top-1 mask from the iterations.
Appendix figures & tables13 assets
Supplementary material from the paper’s appendix.
Appendix
Dataset
Baselines (#-shot)
Baseline mIoU
Iterations (PR-MaGIC Gain)
Oracle Gain
Top-1 Gain
1
2
3
4
5
COCO-20i
Matcher (1shot)
69.53
-0.15
0.21
0.07
0.31
0.66
+6.61
+1.70
Matcher (5shot)
67.69
0.17
-0.20
-0.05
0.50
0.88
+7.19
+3.05
PerSAM-F (1shot)
53.38
+0.06
-0.11
-0.04
-0.27
-0.43
+10.53
+2.60
FSS-1000
Matcher (1shot)
92.08
-0.19
-0.10
0.02
-0.05
-0.05
+1.47
-0.02
Matcher (5shot)
93.26
0.11
0.01
0.09
0.00
-0.01
+1.06
+0.15
Appendix
Table 3: Average mIoU progression and Oracle/Top-1 improvements across datasets. Gains computed as (Metric - Baseline).
FSS
COCO
LVIS
PACO-Part
Pascal-Part
DIS5K
Baseline
85.85 ± 0.12
53.36 ± 0.14
47.62 ± 0.07
45.21 ± 0.09
48.43 ± 0.05
60.90 ± 0.35
Top-1
87.26±0.09
55.96±0.25
50.76±0.06
48.17±0.09
51.69±0.06
64.66±0.25
Oracle
91.88 ± 0.07
63.89 ± 0.12
56.09 ± 0.04
54.20 ± 0.02
57.08 ± 0.11
71.98 ± 0.13
Appendix
Table 4: Variance of mIoU for PerSAM-F + PR-MaGIC across datasets.
Baseline
Top-1
Oracle
PerSAM-F + PR-MaGIC
60.90 ± 0.35
64.66±0.25
71.98 ± 0.13
Matcher + PR-MaGIC
47.82 ± 0.85
55.82±0.64
59.28 ± 0.86
Appendix
Table 5: Variance of mIoU for DIS5K dataset (1-shot segmentation)
Figure 8: Iterative refinement behavior of PR-MaGIC for Matcher. Each example shows the support image, refinement outputs from iterations 0(baseline) – 5, and the ground truth. The blue-boxed output denotes the step selected by our top-1 selection module. Although the selection is not guaranteed to be globally optimal, it prevents degradation by choosing the final step when refinement converges and an earlier, more stable step when it becomes unstable.
Figure 9: Iterative refinement behavior of PR-MaGIC for PerSAM. Each example shows the support image, refinement outputs from iterations 0(baseline) – 5, and the ground truth. The blue-boxed output denotes the step selected by our top-1 selection module. Although the selection is not guaranteed to be globally optimal, it prevents degradation by choosing the final step when refinement converges and an earlier, more stable step when it becomes unstable.
Figure 10: Qualitative result of PR-MaGIC with PerSAM-F in semantic segmentation.
Figure 11: Qualitative result of PR-MaGIC with Matcher in semantic segmentation.
Figure 12: Qualitative result of PR-MaGIC with PerSAM-F in part segmentation.
Figure 13: Qualitative result of PR-MaGIC with Matcher in part segmentation.
Figure 14: Qualitative result of five shot semantic segmentation (Matcher). The test samples are separated by dividers. For each sample, the upper row shows the support images, while the lower row displays, from left to right, the baseline result, the result of PR-MaGIC across iterations, and the ground truth.
Figure 15: Qualitative result of five shot part segmentation (Matcher). The test samples are separated by dividers. For each sample, the upper row shows the support images, while the lower row displays, from left to right, the baseline result, the result of PR-MaGIC across iterations, and the ground truth.
Figure 16: Illustration of prompt refinement of PR-MaGIC
η1=1e−2
η2=1e−3
η3=1e−4
ci
Metric
Base ∗
γ1
γ2
γ3
γ1
γ2
γ3
γ1
γ2
γ3
c1
Mean
55.02
58.64
70.32
64.18
65.20
62.88
63.77
62.90
63.77
58.48
Std
(30.65)
(24.37)
(27.82)
(28.14)
(26.94)
(27.90)
(27.89)
(27.87)
(27.89)
(27.51)
c2
Mean
40.34
52.95
56.67
50.19
50.13
46.96
45.62
46.96
48.54
48.64
Std
(38.56)
(33.21)
(37.06)
(37.36)
(34.19)
(36.87)
(37.56)
(36.87)
(38.20)
(38.26)
c3
Mean
45.89
79.55
70.07
57.45
69.37
56.56
54.41
54.47
54.41
53.90
Appendix
Table 6: Overall mIoU(%) results of PR-MaGIC across η , γ and classes ( ci ) configurations. Base ∗ : PerSAM-F. Each ci includes 10 samples per class: {gas_pump,indian_cobra,cocktail_shaker,golfcart,teddy,garbage_truck,stapler,children_slide,studio_couch} . γ1=1 , γ2=10−1 , γ3=10−2 .
Promptable foundation models such as the Segment Anything Model (SAM) produce high-quality masks but remain semantically blind, relying on external prompts to specify categories. Existing vision-language approaches address this limitation by using external prompt coupling, where a vision-language model generates spatial prompts for SAM as a separate stage. We propose CLIP-Guided SAM, a parameter-efficient segmentation framework built on internal semantic conditioning. Instead of using semantic signals only to generate prompts, we inject CLIP-derived text, vision, and similarity features directly into SAM's image encoder through lightweight multi-modal semantic adapters. These adapters condition SAM's internal feature representations, allowing semantic information to influence mask prediction while preserving SAM's original promptable interface. Our framework is designed for low labeled-data settings and applies to both general-domain benchmarks and specialized downstream tasks. It supports two operating modes: Manual mode, for interactive segmentation with both text and spatial prompts, and Semi-Automatic text-only mode, for applications that require concept-specific segmentation using only textual input. We show that robustness depends on aligning training with the type of prompts used at inference, making train-test prompt consistency an important design principle. Through extensive experiments and ablations, we evaluate our method against SAM+PEFT baselines without semantic conditioning, vision-language + SAM pipelines, SAM 3, and strong semi-supervised segmentation methods that rely on large amounts of unlabeled data. Across these settings, CLIP-Guided SAM consistently achieves superior or competitive performance while remaining parameter-efficient in both training and deployment.
Prompt-conditioned foundation segmenters have emerged as a dominant paradigm for image segmentation, where explicit spatial prompts(e.g., points, boxes, masks) guide mask decoding. However, many real-world deployments require fully automatic segmentation, creating a structural mismatch: the decoder expects prompts that are unavailable at inference. Existing adaptations typically modify intermediate features, inadvertently bypassing the model's native prompt interface and weakening prompt-conditioned decoding. We propose IP-SAM, which revisits adaptation from a prompt-space perspective through prompt-space conditioning. Specifically, a Self-Prompt Generator (SPG) distills image context into complementary intrinsic prompts that serve as coarse regional anchors. These cues are projected through SAM2's frozen prompt encoder, restoring prompt-guided decoding without external intervention. To suppress background-induced false positives, Prompt-Space Gating (PSG) leverages the intrinsic background prompt as an asymmetric suppressive constraint prior to decoding. Under a deterministic no-external-prompt protocol, IP-SAM achieves state-of-the-art performance across four camouflaged object detection benchmarks with only 21.26M trainable parameters. Furthermore, the proposed conditioning strategy generalizes beyond COD to medical polyp segmentation.
Huiyao Zhang, Jin Bai, Rui Guo +3
University of Chinese Academy of Sciences · Technology and Engineering Center for Space Utilization, Chinese Academy of Sciences
Referring Camouflaged Object Detection (Ref-COD) requires segmenting hidden targets guided by reference cues. While supervised methods are annotation-heavy and training-free approaches via sparse point-prompting are sensitive to localization errors, we propose GFR-SAM, a robust three-stage training-free framework. GFR-SAM shifts the paradigm from fragile point-matching to a "Generate-Filter-Refine" pipeline. First, we introduce In-Context Exemplar-guided Segmentation, empowering SAM3 with cross-image inference to generate candidate masks via holistic visual exemplars, bypassing its native intra-image constraints. Second, a Region-Global Contrastive Filtering module ranks candidates through DINOv3-based prototypical alignment, effectively suppressing background distractors. Finally, a Geometric-Semantic Refinement module synergizes bounding box and text prompts to recover fine-grained boundaries and enhance instance recall. Evaluated on the R2C7K benchmark, GFR-SAM outperforms existing training-free methods by 8.7% in weighted F-measure (Fβw) and competes with supervised state-of-the-art counterparts. Ultimately, this work underscores the potential of unlocking SAM3's latent capability for cross-image In-Context prompting, establishing a robust, training-free paradigm that effectively bridges the gap between general-purpose foundation models and specialized, label-intensive perception tasks without the need for task-specific fine-tuning.
Yilong Yang, Jianxin Tian, Shengchuan Zhang +1
Key Laboratory of Multimedia Trusted Perception and Efficient Computing, Ministry of Education of China, Xiamen University