Visual In-Context Prompting
- Publication
- CVPR 2024Conference
- First submitted to arXiv
- arXiv:2311.13601
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{objectdetectiondinov2023,
title = {{Visual In-Context Prompting}},
author = {Feng Li and Qing Jiang and Hao Zhang and Tianhe Ren and Shilong Liu and Xueyan Zou and Huaizhe Xu and Hongyang Li and Chunyuan Li and Jianwei Yang and Lei Zhang and Jianfeng Gao},
year = {2023},
eprint = {2311.13601},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2311.13601}
}
Grounding DINO lets a detector understand text, but some things are hard to put into words.
Take a particular screw on a production line, a particular kind of defect, the leaf of a particular cultivar. You know what it looks like, but you can't come up with a name a model would recognize. The most direct approach then is to point at a picture, say "this one," and ask the model to find everything in a new photo that looks the same.
Large language models have long been used this way. Put a few examples in the prompt and the model follows them, without retraining; this is called in-context learning. This paper wants to bring the same thing to image segmentation, where the examples are no longer text but "an image plus a circled region."
The first author is Feng Li of the Hong Kong University of Science and Technology, who was also first author of Mask DINO and Semantic-SAM; the co-authors come from Microsoft Research, IDEA, Tsinghua and other institutions, and the list includes Shilong Liu of Grounding DINO and Xueyan Zou of SEEM.

The figure above shows three things DINOv can do. The first row is generic segmentation, where a few reference images are enough to find everything of the same kind in a new image; the second row is referring segmentation, where one stroke on a reference image finds the corresponding object in another image; the third row is video, where you mark the first frame and every frame after it gets segmented along with it.
Defining the Problem

This figure places the methods of the time in a grid. The vertical axis is the form of the prompt; from top to bottom, there are in-context prompts made of several examples, visual prompts that click or draw a box on a single image, a cropped piece of an image used as the prompt, and text. The horizontal axis is the task:
- Referring: find the one object the prompt points to.
- Generic: find all the objects of the same concept as the prompt.
The example below the figure makes this clearer. The input image holds Doraemon and Dorami. The referring task returns a single result, either both characters as one region or Doraemon alone; the generic task separates the two characters and gives each its own box.
Where each method sits reveals a gap. SAM takes points and boxes but only does referring; one click returns one object. SegGPT can look at several examples. It paints the examples' masks straight onto the pixels and asks the model to "color in" accordingly, and it likewise tends to find the single most relevant object; the authors argue that painting masks into pixels is hard to generalize to unseen concepts.
Text-prompted models such as Grounding DINO and GLIP can do generic detection; a single "zebra" boxes every zebra. But text can't take a mask as input.
The top-right cell is generic segmentation from several visual examples. Only SegGPT reaches halfway into it, and in practice it leans toward finding a single object. That is the spot DINOv sets out to fill.
Solving the Problem
Overall Architecture

DINOv is built on Mask DINO. Mask DINO is a detection-plus-segmentation model by the same group of authors; its structure is DINO's Transformer detector with a mask branch attached, and each query outputs both a box and a mask.
DINOv adds a prompt encoder alongside it. We'll walk through panel (a).
There are two sets of inputs. One is reference images , each carrying visual prompts from the user, which can be masks, boxes, scribbles or points; the other is the target image to be segmented. The reference image can be the target image itself, in which case this reduces to interactive segmentation on a single image.
First, both sets of images pass through the same image encoder (a Swin Transformer):
Second, the prompt encoder extracts the reference prompt features from the reference image features and the prompts , and sampling them gives the prompt queries that go into the decoder:
Third, the decoder handles two kinds of queries at once. One is the segmentation queries that Mask DINO already has, which propose candidate objects in the target image; the other is the prompt queries from the previous step. Both share the same decoder and cross-attend to the target image features :
goes to the mask head, which outputs masks and boxes. is "the prompt after it has read the target image," which the authors call the target visual prompt. A zebra in the reference image and a zebra in the target image differ in angle and lighting; having the prompt read the target image too is meant to adjust it to how things look in the target image.
The last step is matching, which works out which candidate object corresponds to which prompt.
The Prompt Encoder

To turn "this region of the reference image" into a vector, the easiest way is to crop that piece out and feed it to CLIP. The authors didn't do this, for two reasons:
- CLIP would be taking a small cropped image, which is very different from the whole images it saw in training, especially for small objects.
- CLIP's features lean toward semantics. Saying "this is a car" is no problem, but video tracking needs to remember the appearance details of "this car."
So DINOv reuses its own image encoder directly. The prompt encoder is just a learnable query that does masked cross-attention once on each of the feature maps at three scales, from small to large. The mask is the region the prompt marks, so the query reads only the features inside that region.
Points and boxes can be handled the same way, by converting them into the corresponding regions.
Matching: Who Is Who
With both and in hand, the next step is to compute their similarity. Both tasks use the same computation and differ only in one linear layer.
Generic segmentation treats the prompts as categories. Suppose a training batch contains three categories, zebra, giraffe and elephant; then there are three prompt vectors, the decoder proposes candidate objects, and each object has to be assigned to one of the three categories:
This is almost the same as the classification head of an ordinary segmentation model, except that the category weights are not a learned, fixed matrix but are computed on the spot from the prompts. The matrix in panel (b) has 3 rows, with each column one instance's one-hot category label, and the loss is computed as "each query against all prompts."
Referring segmentation works the other way round; each prompt has to find, in the target image, the one instance it refers to:
Panel (c) has six instances, each prompt is assigned to one of them, and the loss is computed as "each prompt against all candidates."
Apart from the two linear layers and , the whole model is shared.
How the Prompts Are Sampled
The two tasks get their prompts from different sources, so they sample them differently too.
Referring segmentation trains by self-referring. During training the reference image is the target image; a prompt is taken from some instance, and the model is asked to find that same instance again. This needs no category labels at all, so data like SA-1B, which has masks but no categories, can be used for training. Training only ever pairs an image with itself, but swapping in a different target image at inference gives cross-image referring.
Generic segmentation needs positives and negatives. The authors treat the images of the whole batch as a pool of examples, in three steps:
- Group all the prompts in the batch by category.
- For each category, randomly decide how many examples to use, a number between 1 and an upper limit .
- Draw prompts from that category and average them into one token that represents the category.
There are as many prompt queries as there are categories in the batch, and the prompts of other categories naturally become negatives. This also explains why the batch has to be large enough; with too small a batch, there are too few examples and negatives to draw from.
At inference, taking COCO as the example, prompt features are first computed from the annotated masks of each category in the training set and stored, and evaluation draws 16 per category at random. So even in zero-shot evaluation, each category still needs examples with masks first; zero-shot here means the model was not trained on that dataset.
Two Kinds of Queries
The two tasks also use different segmentation queries:
- Generic segmentation keeps Mask DINO's 300 learnable queries, each with a learnable box as its position.
- Referring segmentation uses Semantic-SAM's point queries. The prompt's point or box is converted into an anchor box for the position and then copied six times, each copy paired with a content query of a different granularity; the same point may refer to a part, an object or a whole group of things, and each of the six queries handles one granularity.
When training on SA-1B, 10 points inside each object are taken as positives and 40 points from other regions as negatives; at inference, a uniform 20×20 grid of points is laid over the image.
The training data is COCO 2017 panoptic segmentation (about 110K images, with categories) plus 20% of SA-1B (about 2 million images, masks only).
Discussion
One Set of Weights, Three Scenarios
| Method | Prompt | COCO PQ | ADE20K PQ | ADE20K mIoU | SegInW mean AP |
|---|---|---|---|---|---|
| Mask DINO-L | Closed-set | 58.3 | – | – | – |
| OpenSeeD-L | Text | 59.5 | 19.7 | 23.4 | 36.1 |
| SegGPT-L | Visual | 43.4 | – | – | – |
| FC-CLIP-L | Text | 54.4 | 26.8 | 34.1 | – |
| FC-CLIP-L (CLIP visual features as prompts) | Visual | – | 2.3 | 7.8 | – |
| DINOv-T | Visual | 49.0 | 19.4 | 21.9 | 39.5 |
| DINOv-L | Visual | 57.7 | 23.2 | 25.3 | 40.6 |
Compiled from Table 1 of the paper. COCO is an evaluation within the training data; ADE20K and SegInW (25 instance segmentation datasets) are evaluations outside it.
On COCO, DINOv-L's 57.7 PQ is only 0.6 below its base, Mask DINO-L, a closed-set model that is trained on category labels and classifies directly. In other words, swapping the classifier for "16 examples per category" costs almost nothing on seen categories. SegGPT, which also relies on visual prompts, gets only 43.4.
ADE20K has unseen scenes and categories. DINOv-L's 23.2 PQ is higher than the text-prompted OpenSeeD-L (19.7), but the text-prompted FC-CLIP-L in this table has 26.8, which is better still. On SegInW mean AP, DINOv-L is also 4.5 above OpenSeeD-L.
The FC-CLIP row with CLIP visual features as prompts is an extra experiment the authors added. FC-CLIP uses a frozen CLIP for text-prompted open-set segmentation, and since CLIP's image and text features are already aligned, the authors tried replacing the text prompts with CLIP's visual features. ADE20K PQ fell from 26.8 to 2.3. The text and visual features live in the same space, yet swapping one for the other barely works at all, which is why the authors insist on training a visual-prompt model of their own.
For detection, the mean AP over ODinW's 35 datasets is 15.7 (DINOv-L), higher than GLIP-T trained on Objects365 only (11.4); but the median is only 4.8, and many of the small datasets score close to zero.
Video Segmentation Without Video Data
DINOv was not trained on any video, and its way of doing video object segmentation is straightforward. The user's prompt on the first frame serves as the reference; when processing frame , the masks predicted for the previous few frames are also encoded as prompts and averaged with the first frame's prompt to serve as the reference for this frame. The memory length defaults to 8 frames; too short isn't stable enough, and too long actually makes things worse:
| Memory length | 1 | 2 | 4 | 8 | 16 |
|---|---|---|---|---|---|
| DAVIS17 J&F | 62.1 | 69.6 | 71.5 | 72.3 | 68.0 |
Compiled from Table 8 of the paper, DINOv-SwinL.
On DAVIS 2017, DINOv-T has a J&F of 73.3, against 60.4 for SEEM-T, which also uses no video data. SegGPT-L, however, reaches 75.6, and on YouTube-VOS 2018 it is 74.7 vs. DINOv-T's 60.9, so DINOv doesn't lead on this one.
Ablations
Both kinds of queries are needed. Tested with Swin-T, keeping only the point queries drops COCO mask AP from 42.7 to 31.0, and keeping only the generic queries drops DAVIS17 J&F from 73.3 to 68.9. Each task relies on its own kind of query.
CLIP makes a poor prompt encoder. Replacing the prompt encoder with "crop the region and feed it to CLIP" costs only 1.1 PQ on COCO (49.6 vs. 48.5), but ADE20K falls from 19.4 to 12.6, and mask AP from 11.4 to 1.4. No difference shows on seen categories, but it falls apart as soon as the scenes are unseen.
The two datasets complement each other. Trained on COCO alone, DAVIS17 is 63.3; on SA-1B alone, 68.4; on both, 73.3. COCO PQ also rises from 48.9 to 49.6.
The batch has to be large.
| Sampling batch size | 1 | 4 | 8 | 32 | 64 |
|---|---|---|---|---|---|
| COCO PQ | 28.9 | 45.1 | 47.3 | 47.8 | 49.0 |
Compiled from Table 7 of the paper, DINOv-SwinT.
With a batch of just 1 image, the only things available as examples are on that same image, and PQ is only 28.9; raising the batch to 64 brings it up to 49.0. The authors' explanation is that the larger the batch, the closer training gets to the inference situation of "drawing examples at random from many images."
How many examples?

The horizontal axis is how many examples per category are given at inference, from 1 to 32. All four lines climb fastest over the first few examples; COCO tops out at around 4, and the other datasets level off after 8. Going from 1 example to 8 raises ADE20K PQ from around 14 to around 21, a big difference; going from 16 to 32 changes almost nothing. The default of 16 is plenty.
Limitations
The authors list only one limitation of their own, that COCO is the only data with category labels they used; it could be expanded, and text prompts could be added as well.
Two more things can be read from the experimental setup. First, generic segmentation needs examples and masks prepared for each category, 16 per category in evaluation, and in a new domain the user has to prepare these examples. Second, training isn't cheap. The official instructions recommend a total batch of 64, with one SA-1B image and one COCO image on each GPU, which comes to 64 GPUs in multi-node training.
Conclusion
This paper shows that "give a few circled examples and find everything of the same kind" can be done. On COCO it is only 0.6 PQ below the closed-set Mask DINO, and on ADE20K it beats some of the text-prompted models. What makes it work is encoding the prompts with the model's own features; CLIP's features stop working as soon as the scenes are unseen.
If you want to try it yourself, the official release includes two sets of weights, Swin-T and Swin-L, along with the training code. Don't shrink the batch too much in training; going from 64 down to 8 already costs 1.7 COCO PQ, and at 1 image it is down to 28.9.
The top of the official README also recommends a demo, suggesting that anyone who wants to see what visual prompts can do try the team's other tool, T-Rex.
That's the one we'll look at next.