Segment Anything In-Context via Visual Demonstrations

Yuzhu Wang, Manni Duan, Hongliang Ye, Shu KongarXiv 2026

Imagine you’re trying to explain to a friend exactly which weirdly shaped cell in a microscope slide you’re interested in. You could try to describe it—"the one that looks a bit like a squashed grape with a jagged edge"—but it’s much easier to just point at a few examples and say, "Like these."

This is the core idea behind SAYD (Segment Anything via Visual Demonstrations), a new foundation model that masters the art of "show, don't tell" for computer vision.

TL;DR

SAYD is a foundation model for In-Context Segmentation (ICS): instead of using text prompts, it learns to segment objects in a new image by looking at a few visual examples (image + mask) provided by the user. It excels in specialized domains like medicine and astronomy where words often fail, and it does so with significantly better speed and mask quality than previous approaches.

What Problem Does This Paper Address?

We’ve become accustomed to models like SAM (Segment Anything Model) that can find almost anything if you click on it or give it a box. However, there is a "knowledge acquisition bottleneck" in specialized fields. If you are a pathologist looking for a specific type of carcinoma or an astronomer hunting for a rare galaxy merger, a general-purpose text prompt like "segment the cell" isn't specific enough.

Existing "In-Context" models—which try to learn from visual examples—often struggle with two things:

  1. Mask Quality: They often try to find the object and segment it in one messy step, leading to blurry or inaccurate boundaries.
  2. Efficiency: Many models have to re-process the "demonstration" images every single time they look at a new query image, making them slow for high-throughput scientific work.
Figure 1: Our foundation model SAYD solves in-context segmentation. We train it on public datasets originally curated for panoptic segmentation (ref. the first four columns showing test images from these datasets). SAYD generalizes to out-of-domain benchmarks spanning natural, biological, ecological, cosmological, medical, pathological, and satellite imagery (ref. examples shown in the last eight columns). SAYD effectively segments the visual targets, particularly those that are often identified by domain experts through tacit visual knowledge, indicated by the red arrow in the fifth and sixth columns.

Methods

The authors introduce several clever architectural shifts to make SAYD both faster and more accurate:

  • Class-Agnostic Mask Generation: Unlike previous models that try to find "the specific thing" requested by the prompt immediately, SAYD’s decoder first identifies all coherent "things" in the query image regardless of what they are. This "class-agnostic" approach allows the model to focus purely on producing high-quality, sharp masks.
  • Prototype Matching: Once the masks are generated, the model compares them to "prototypes" extracted from your visual demonstrations. It’s essentially a high-speed game of "match the feature."
  • Handling the Unknown (ignota): Real-world images often contain objects that weren't in your examples. SAYD explicitly accounts for these novel classes (which the authors call ignota) during training. This prevents the model from getting confused and trying to force-fit every object into one of your provided categories.
  • Finetuning the Encoder: While many models use a "frozen" (unchanging) image encoder, the authors found that finetuning the encoder specifically for this matching task was crucial for success.
Figure 4: Left: In SAYD, an image encoder extracts feature maps for the query and prompt images. A mask decoder processes the query’s feature map and outputs two sets of tokens (with transformations by MLPs): Qmap for mask generation, and Qcls for mask classification. Meanwhile, the feature maps of prompt images are averaged into class prototypes P based on the prompt masks. SAYD uses the prototypes to classify the tokens Qcls. Combining the masks and their predicted classes produces segmentation results at the instance, semantic, and panoptic levels. Right: Our decoder design (a) takes as input only the query’s feature map that generates class-agnostic masks. Differently, the design (b), widely adopted in prior work [11, 55], additionally requires prompts to generate class-aware masks, which overemphasize prompt-query semantic alignment at the expense of mask quality. Our design not only reduces computational overhead but also improves ICS performance (Tables 3&4).

Datasets

  • Training: SAYD was trained on 12 public datasets originally designed for panoptic segmentation (like COCO, ADE20K, and Cityscapes). These provide a rich variety of natural scenes.
  • Evaluation: To prove it’s a true "foundation model," the authors tested it on 24 out-of-domain benchmarks. These aren't just more pictures of cats and cars; they include:
  • Biological: Ant anatomy, plant diseases, and cell microscopy.
  • Cosmological: Galaxy Zoo images.
  • Medical: Chest X-rays and pathological slides.
  • Satellite: Remote sensing of urban and rural areas.

Key Insights and Ideas

  • Visual Knowledge is Tacit: Some things are just hard to put into words. Figure 3 shows that even powerful models like SAM3 fail when given complex text descriptions of ant anatomy, whereas SAYD nails it because it can "see" the visual pattern.
  • The "Thing-ness" First Approach: By separating where an object is from what it is, SAYD achieves much cleaner boundaries. This is a departure from models that try to do both at once, which often results in "leaky" segmentations.
  • Efficiency Scales: Because the model can pre-compute the "prototypes" from your examples, its runtime stays nearly constant even if you ask it to find dozens of different types of objects at once.
Figure 3: Qualitative comparison of segmentation models through three examples. SAM3 [11], a prominent vision foundation model (VFM) for open-vocabulary segmentation, fails to segment the targets using text prompts. DINOv [55] and INSID3 [23], representative ICS models, also struggle to accurately segment the targets. In contrast, our SAYD, a VFM specifically designed for ICS, consistently produces the most accurate and visually coherent segmentation results.

Key Results

  • Scientific Superiority: On specialized datasets like "Ant" (micro-CT scans), SAYD achieved an mIoU (a standard accuracy metric) of 32.04, compared to just 9.36 for SAM3.
  • Out-of-Domain Generalization: Across nearly all 24 test sets, SAYD significantly outperformed existing in-context models like DINOv and INSID3.
  • Speed: SAYD maintains a low computational cost (0.3 TFLOPs) that doesn't explode as you add more classes to your prompt, unlike competitors whose runtime scales linearly with the number of classes.

Limitations and Future Work

Despite its prowess, the authors note a few hurdles:

  • Irregular Shapes: The model can still struggle with extremely irregular or rare shapes (like certain metallic microstructures) because its mask generator is trained primarily on more "standard" objects.
  • Single Prototype Limitation: Currently, each visual concept is represented by a single "average" feature. In the future, using a "bag of features" might better capture objects that look very different from different angles.
  • The Science Gap: While SAYD is a leap forward, its performance on scientific imagery is still lower than its performance on everyday objects. There is still work to be done in closing that domain gap.

Summary updated

Abstract

We present the Segment Anything Model via Visual Demonstrations (SAYD), a foundation model for in-context segmentation. Given visual demos comprising prompt images and their segmentation masks, SAYD segments coherent regions (e.g., objects or their parts) in a query image by establishing visual correspondences between the prompts and the query. SAYD generalizes to previously-unseen segmentation tasks, including those requiring tacit visual knowledge or involving concepts that are difficult to articulate unambiguously in language. We train SAYD on 12 public datasets originally curated for panoptic segmentation using our proposed techniques, including a lightweight mask decoder for prompt-agnostic mask generation and a learning mechanism for recognizing novel query classes absent in the prompts. These innovations enhance both computational efficiency and out-of-domain generalization. Consequently, SAYD significantly outperforms existing methods of in-context segmentation and open-vocabulary segmentation across 24 out-of-domain benchmarks, spanning natural, biological, ecological, cosmological, medical, pathological, and satellite imagery.
BibTeX
@misc{wang2026segment,
  author = {Yuzhu Wang and Manni Duan and Hongliang Ye and Shu Kong},
  title = {{Segment Anything In-Context via Visual Demonstrations}},
  year = {2026},
  eprint = {2611.29896},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.29896},
}
Figure