Back to paper library
arXiv 2023CVPR 2024Object detectionPaper notes

DINOv

Follow the Example

Original paper

Visual In-Context Prompting

Paper authors

Feng Li · Qing Jiang · Hao Zhang …

Show all 12 authors

Feng Li · Qing Jiang · Hao Zhang · Tianhe Ren · Shilong Liu · Xueyan Zou · Huaizhe Xu · Hongyang Li · Chunyuan Li · Jianwei Yang · Lei Zhang · Jianfeng Gao

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}
}
On this page

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.

Original Figure 1: three rows of demonstrations. The first row is generic segmentation with visual prompts: on the left are N reference images with zebras and giraffes marked, and DINOv colors all the giraffes and zebras in a new image, each kind separately; the second row is referring segmentation with visual prompts: with one stroke drawn on a reference image, DINOv finds the corresponding object in another image; the third row is zero-shot video object and part segmentation: the car body and wheels are marked in the first frame and segmented through the following frames.
Original Figure 1, Li et al., from PDF page 1, arXiv license, scholarly quotation.

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

Original Figure 2: the horizontal axis splits tasks into Referring and Generic, and the vertical axis lists four kinds of prompt from top to bottom: visual in-context prompts made of several examples, visual prompts on a single image, cropped image prompts and text prompts. SegGPT sits in the in-context prompt row, straddling the divide between referring and generic; SAM falls in the referring area for visual prompts, SEEM covers the visual, image and text prompts of the referring area, OVSeg sits in text referring, OWL-ViT/MQ-Det in the generic area for image and text, and Grounding DINO/GLIP in the generic area for text; DINOv (Ours) covers both the referring and the generic areas for in-context and visual prompts. Below is one input image with Doraemon and Dorami in it; the referring task outputs a single box, around both characters or around Doraemon alone, and the generic task boxes the two characters separately.
Original Figure 2, Li et al., from PDF page 2, arXiv license, scholarly quotation.

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

Original Figure 3: (a) the overall framework. The input image passes through the Vision Encoder to get vision tokens, and the user's visual prompts (masks, scribbles, boxes and so on) pass through the Prompt Encoder to become visual prompt tokens; the DINOv decoder on the left processes the segmentation queries, the weight-sharing DINOv decoder on the right processes the visual prompts, and the outputs of both go to Matching & Loss together. (b) Generic segmentation: six visual prompts from three categories are sampled and aggregated into three category tokens, forming a 3-row matching matrix with the segmentation queries, in which each column is one instance's one-hot category label, and the loss pivots on the queries. (c) Referring segmentation: each visual prompt is classified as one of six instances, and the loss pivots on the prompts.
Original Figure 3, Li et al., from PDF page 3, arXiv license, scholarly quotation.

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 NN reference images I\mathcal{I}, each carrying visual prompts P\mathcal{P} from the user, which can be masks, boxes, scribbles or points; the other is the target image It\mathbf{I}_t 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):

Z=Enc(I),Z=Enc(It)\mathcal{Z} = \text{Enc}(\mathcal{I}), \quad \mathbf{Z} = \text{Enc}(\mathbf{I}_t)

Second, the prompt encoder extracts the reference prompt features F\mathcal{F} from the reference image features Z\mathcal{Z} and the prompts P\mathcal{P}, and sampling them gives the prompt queries Qp\mathbf{Q}_p that go into the decoder:

F=PromptEncoder(P,Z)\mathcal{F} = \text{PromptEncoder}(\mathcal{P}, \mathcal{Z}) Qp=PromptSample(F)\mathbf{Q}_p = \text{PromptSample}(\mathcal{F})

Third, the decoder handles two kinds of queries at once. One is the segmentation queries Qs\mathbf{Q}_s that Mask DINO already has, which propose candidate objects in the target image; the other is the prompt queries Qp\mathbf{Q}_p from the previous step. Both share the same decoder and cross-attend to the target image features Z\mathbf{Z}:

Os=Decoder(Qs;Z)\mathbf{O}_s = \text{Decoder}(\mathbf{Q}_s; \mathbf{Z}) Op=Decoder(Qp;Z)\mathbf{O}_p = \text{Decoder}(\mathbf{Q}_p; \mathbf{Z})

Os\mathbf{O}_s goes to the mask head, which outputs masks and boxes. Op\mathbf{O}_p 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

Original Figure 5: a learnable visual prompt query moves from bottom to top, doing masked attention on three multi-scale feature maps in order from small to large; the human-shaped region on the feature maps is the location marked by the prompt.
Original Figure 5, Li et al., from PDF page 4, arXiv license, scholarly quotation.

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:

  1. CLIP would be taking a small cropped image, which is very different from the whole images it saw in training, especially for small objects.
  2. 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 Os\mathbf{O}_s and Op\mathbf{O}_p 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 NsN_s candidate objects, and each object has to be assigned to one of the three categories:

Cg=g(Op) g(Os)⊤,Cg∈RNp×Ns\mathbf{C}_g = g(\mathbf{O}_p)\,g(\mathbf{O}_s)^{\top}, \quad \mathbf{C}_g \in \mathbb{R}^{N_p \times N_s}

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:

Cr=h(Op) h(Os)⊤\mathbf{C}_r = h(\mathbf{O}_p)\,h(\mathbf{O}_s)^{\top}

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 gg and hh, 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:

  1. Group all the prompts in the batch by category.
  2. For each category, randomly decide how many examples to use, a number nn between 1 and an upper limit NN.
  3. Draw nn 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

MethodPromptCOCO PQADE20K PQADE20K mIoUSegInW mean AP
Mask DINO-LClosed-set58.3–––
OpenSeeD-LText59.519.723.436.1
SegGPT-LVisual43.4–––
FC-CLIP-LText54.426.834.1–
FC-CLIP-L (CLIP visual features as prompts)Visual–2.37.8–
DINOv-TVisual49.019.421.939.5
DINOv-LVisual57.723.225.340.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 tt, 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 length124816
DAVIS17 J&F62.169.671.572.368.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 size1483264
COCO PQ28.945.147.347.849.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?

Original Figure 7: four curves for 1, 2, 4, 8, 16 and 32 in-context examples at inference. COCO PQ rises from about 49 to about 57 and then levels off; SegInW mean AP rises from about 31 to about 44; ADE20K PQ from about 14 to about 23; ODinW mean AP from about 9 to about 15. All four curves flatten out after 8 examples.
Original Figure 7, Li et al., from PDF page 9, arXiv license, scholarly quotation.

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.