T-Rex2: Towards Generic Object Detection via Text-Visual Prompt Synergy
- Publication
- ECCV 2024Conference
- First submitted to arXiv
- arXiv:2403.14610
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{objectdetectiontrex22024,
title = {{T-Rex2: Towards Generic Object Detection via Text-Visual Prompt Synergy}},
author = {Qing Jiang and Feng Li and Zhaoyang Zeng and Tianhe Ren and Shilong Liu and Lei Zhang},
year = {2024},
eprint = {2403.14610},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2403.14610}
}
The previous two papers each took one road. Grounding DINO tells the detector what to look for with text, and DINOv uses circled examples instead.
Each road has its blind spots. Ask a model to find "dog," and one word covers corgis, huskies and mutts; with pictures, you'd need a whole pile of breeds to make yourself clear. Conversely, ask a model to find "this microorganism under the microscope," and someone without a biology background can't even name it, while boxing it in the image is the quickest way.
This paper comes from IDEA, and the author list includes the first author Qing Jiang as well as Feng Li of DINOv and Shilong Liu of Grounding DINO. They had earlier built a model called T-Rex that counts things you box; this time, T-Rex2 puts both text and visual prompts into one detector, so users can choose whichever suits the situation, or use both together.

The figure above shows T-Rex2's three modes of use. The top section shows best what it is for. A plate of red fruit, pills, bacterial colonies in a Petri dish, a crowd in a cartoon are all cases of "I don't know what it's called, but I can point it out to you," and a single image often holds dozens or hundreds of them.
Defining the Problem

The authors explain the problem with a long-tail curve. Objects occur very unevenly; the further right you go, the more categories there are and the less data each one has.
The head at the far left is the 80 categories that closed-set datasets like COCO cover.
Moving right, you reach the range of text prompts. Models like Grounding DINO learn what a "Ferris wheel" looks like from large amounts of image-text data, so they can cover far more than 80 categories. But text prompts have two problems:
- Data shortage. Aligning text with images takes enough paired data, and the rarer the category, the worse it is learned.
- Hard to describe. A Ferris wheel can be named; "this microorganism under the microscope" can't, and someone without the expertise simply doesn't know what to type.
The tail of the curve goes to visual prompts. Giving examples requires neither knowing the name nor cross-modal alignment; it relies on visual similarity, so it can find things never seen in training.
But visual prompts have their own weak spot. They struggle to express abstract concepts. The word "dog" covers every breed, size and coat color; to express the same concept in pictures, you'd have to collect a whole pile of examples.
The strengths and weaknesses of the two kinds of prompt complement each other neatly, and T-Rex2 sets out to combine both in one model.
There's one more detail. Interactive segmentation like SAM is one-to-one, where one click returns one mask; detection is one-to-many, where boxing one pigeon means finding every pigeon in the image.
Solving the Problem
![Original Figure 3: the T-Rex2 architecture. The input image passes through the Image Encoder to get multi-scale features; the user's visual prompts (boxes or points) go through Position Embedding, then Deformable Cross Attention extracts prompt features from the image features, which an aggregator token gathers; text prompts (person, pigeon, tree) go through the Text Encoder, which takes [CLS]. Contrastive Alignment is applied between the visual and text prompt embeddings, with their similarity matrix in the bottom right; the detection queries output by the DETR Decoder are compared with the prompt embeddings by similarity to give the final boxes.](/content/papers/assets/176f140c0cc24862fb479679d66a110267aee76d72f8cd4da6ec4e9aec2cc13b.png)
T-Rex2 has four components, an image encoder, a visual prompt encoder, a text prompt encoder and a box decoder. Its skeleton is DINO's DETR-style detector.
The biggest difference from Grounding DINO is when the prompts come in. Grounding DINO already has text and image attend to each other in the encoder, whereas in T-Rex2 the image is encoded on its own first and the prompts join only at the end, which makes it late fusion. The benefit of this choice shows up later, in interactive speed.
Image Encoder
As in Deformable DETR, a Swin Transformer extracts multi-scale feature maps, which then go through a six-layer encoder with deformable self-attention. The output is levels of feature maps .
Visual Prompt Encoder
This is T-Rex2's main new component. We'll walk through the figure's example, supposing the user has boxed three pigeons in a photo.
Step one turns coordinates into vectors. The three boxes are normalized coordinates between 0 and 1; they first go through a fixed sine-cosine positional encoding and are then projected to dimensions by a linear layer:
Here . If the user gives points instead, a different linear layer is used. The authors deliberately handle points and boxes as two kinds of prompt, unlike Semantic-SAM, which treats a point as a tiny box.
Step two prepares the queries. Each box gets a learnable content vector, one shared by all three boxes and copied three times to give . An extra "global" token is added, whose position is the center and size of the whole image, . Content and position are concatenated along the channel dimension and passed through a linear layer:
The resulting is . There are now 4 queries; the first 3 correspond to the three pigeons, and the last one is the global token that does the aggregating.
Step three has the queries fetch features from the image. With multi-scale deformable cross-attention, each query uses its own box as the reference, samples only a few points near the box, and reads what this pigeon looks like from each level of feature maps. In the implementation, this step is stacked three layers deep.
Step four aggregates. The four queries go through one round of self-attention and an FFN, and in this step the global token reads the features of the three pigeons. Its output is taken as the final visual prompt embedding:
The three boxes end up as a single vector , which stands for "this kind of thing."
Text Encoder and Box Decoder
The text side is much simpler. A category name or short phrase goes into CLIP's text encoder, and the [CLS] output is taken as the embedding . Unlike Grounding DINO, which encodes word by word with BERT, here one category is one vector.
The box decoder follows DINO. The queries' initial positions are picked the Grounding DINO way, by computing the similarity between encoder features and prompt embeddings and taking the top 900; each decoder layer predicts anchor offsets .
Classification works the same way too, with the prompt embeddings used directly as the weights of the classification layer:
is the number of prompt categories and the number of detection queries. Visual and text prompts share the same image encoder and decoder; the only difference is that is replaced by .
Aligning the Two Kinds of Prompt
The same region has both a visual prompt embedding and a text embedding . The authors add an InfoNCE loss between the two, pulling together the two embeddings of the same region and pushing apart those of different regions:
The authors read this as mutual distillation. Text is the anchor of a concept; the motley visual examples gather around it and learn the abstract concept of "dog" from it. Conversely, having seen a great many visual examples, the text becomes more precise too.
Training
The training strategy for visual prompts is "this image sets the question, and this image answers it." For each category in each training image, a random number of annotated boxes, anywhere from one to all of them, are picked as prompts, and the model has to find every object of that category in the image; with probability one half, the boxes are replaced by their center points, which trains point prompts along the way.
Cross-image training (using examples from image A to detect in image B) looks more sensible, but the authors didn't do it. Different datasets define their categories differently, and assembling pairs of images of the same category is troublesome in an open-set setting.
Training for text prompts uses both detection data and grounding data. For detection data, the category names present in the image are the positives, and negatives are drawn from other categories; for grounding data, the phrase for each box is used, and negatives are drawn from a global dictionary holding the category names and phrases that appear more than 100 times in the training data.
The two kinds of prompt are trained in alternation, text in one iteration and visual in the next. The total loss is classification (sigmoid focal loss), L1, GIoU and DINO's denoising loss, plus the alignment loss from above.
Data Engine
Each kind of prompt has its own data sources:
- Text. Besides Objects365, OpenImages and GoldG, it relies heavily on pseudo-labeled data. Captions from Conceptual Captions and LAION go through spaCy to extract noun phrases, which are then handed to Grounding DINO to box their locations; for the classification dataset Bamboo, the images' category names are used directly to query Grounding DINO. In total there are 3.15 million annotated images plus 3.39 million pseudo-labeled ones.
- Visual. Besides Objects365, OpenImages, HierText (text) and CrowdHuman (crowds), more data is mined from SA-1B. SA-1B has plenty of boxes but no categories, so the authors first train an initial T-Rex2 that takes visual prompts only, then use TAP to name each box from 2,560 categories, and then use the initial model to add the boxes the annotations missed. In total there are 2.4 million annotated images plus 650K pseudo-labeled ones.
Four Ways to Use It
At inference there are four combinations:
- Text. The same as ordinary open-vocabulary detection, suited to common objects.
- Interactive visual. Box or click on the current image, and the model finds everything of the same kind; for anything missed or wrongly boxed, just add another prompt.
- Generic visual. Collect examples from several images, average their embeddings into a single , and store it for detecting in other images.
- Mixed. The text embedding and the visual embedding are simply averaged, .
Discussion
Three Evaluation Protocols
Before looking at the evaluation, get the rules straight, because the three kinds of prompt differ a lot in difficulty:
- Text: all category names in the dataset are used as prompts, the same as ordinary zero-shot evaluation.
- Visual-G (generic): for each category, 16 images containing it are drawn at random from the dataset's training set, and the 16 embeddings extracted with the annotated boxes are averaged.
- Visual-I (interactive): for each category present in each test image, one annotated box is picked at random as the prompt. The model in effect knows in advance which categories are in the image, and is handed one correct answer on top of that.
The authors themselves admit that comparing Visual-I with the other two isn't entirely fair.
Text for the Head, Visual for the Tail
| Model and prompt | COCO | LVIS | LVIS rare | ODinW | RF100 |
|---|---|---|---|---|---|
| Grounding DINO-T, text | 48.4 | 27.4 | – | 22.3 | – |
| Grounding DINO-L, text | 52.5 | 33.9 | – | 26.1 | – |
| DINOv-L, visual | – | – | – | 15.7 | – |
| T-Rex2-T, text | 45.8 | 42.8 | 29.0 | 18.0 | 8.2 |
| T-Rex2-T, visual | 38.8 | 37.4 | 32.4 | 23.6 | 17.4 |
| T-Rex2-L, text | 52.2 | 54.9 | 42.7 | 22.0 | 10.5 |
| T-Rex2-L, visual | 46.5 | 47.6 | 43.8 | 27.8 | 18.5 |
Compiled from Table 1 of the paper, all zero-shot AP; T and L are the Swin-T and Swin-L backbones, and visual means Visual-G. LVIS is the overall AP on minival, LVIS rare is the APr on the rare categories of val, ODinW is the mean over 35 datasets, and RF100 is Roboflow100.
Taking Swin-T as the example, text prompts beat visual ones by 7 AP on COCO and by 5.4 on LVIS minival; on LVIS's rare categories, visual turns the tables and wins by 3.4, and it also wins by 5.6 on ODinW and by 9.2 on Roboflow100. Roboflow100 collects 100 datasets of aerial imagery, game footage, underwater scenes, documents and the like, exactly the part of the long tail where things are "hard to name."
Compared with Grounding DINO, T-Rex2 is slightly lower on COCO (text 52.2 vs. 52.5) but far higher on LVIS (54.9 vs. 33.9). Their training data differ a lot, though; T-Rex2's text data alone adds up to more than 6 million images.

This figure breaks LVIS's 1,203 categories out one by one. Each bar is a category, going up where text wins and down where visual wins; the categories are sorted by how many times they appear in the text training data.
Among the high-frequency categories on the left, text wins 254 and visual 151; the middle segment starts to flip, and by the rare categories on the right, visual wins handily, 253 to 84. The division of labor sketched by the long-tail curve at the start is measured here for real.
Interactive Mode
In the interactive setting, with boxes as prompts, Swin-T gets 56.6 AP on COCO, 62.6 on LVIS val and 37.7 on ODinW, all far above the other two kinds of prompt. As noted earlier, though, this setting knows in advance which categories are in the image.
The authors also tested few-shot counting. Each image comes with three example boxes, and the model has to count how many objects of that kind there are, mostly in scenes crowded with small objects. On FSC147, T-Rex2's mean absolute error is 10.94, worse than the 8.72 of its predecessor T-Rex; but on the multi-category FSCD-LVIS, its detection AP is 43.35, above T-Rex's 40.32.
Does the Synergy Really Help?
| Training | Prompt at evaluation | COCO | LVIS val | LVIS val APr |
|---|---|---|---|---|
| Text only | Text | 46.4 | 32.8 | 32.1 |
| Visual only | Visual-G | 14.0 | 15.3 | 8.6 |
| Joint, no alignment | Text | 44.4 | 32.2 | 28.2 |
| Joint, no alignment | Visual-G | 38.7 | 30.2 | 29.4 |
| Joint + contrastive alignment | Text | 45.8 | 34.8 | 29.0 |
| Joint + contrastive alignment | Visual-G | 38.8 | 34.9 | 32.4 |
Compiled from Table 4 of the paper, Swin-T.
Start with the second row. Trained on visual prompts alone, generic visual detection gets only 14.0 AP on COCO, which amounts to not having learned it at all. The authors' explanation is that visual examples vary too much from one another; every chair looks different, and without a shared anchor the model has a hard time generalizing to the concept of "chair."
Once trained together with text, visual prompts jump to 38.7. But text prompts get worse, with COCO falling from 46.4 to 44.4.

The authors use t-SNE to find the cause. In the left plot, without alignment, the text embeddings of the ten categories (large symbols) are all crammed into a small clump in the middle, far from their respective visual embeddings. When the same region feature has to match two targets that point in different directions at once, learning gets harder.
With contrastive alignment added (right plot), the visual embeddings gather into ten clusters around the text embeddings. In the table, both sides improve, with visual prompts gaining 4.7 on LVIS and text prompts gaining 2.6.
Compared with the "text only" row, though, text prompts are still 0.6 lower on COCO. On LVIS overall AP they gain 2.0, but on rare categories they lose 3.1. The gains from synergy are mainly on the visual-prompt side.
How Many Examples?
Generic visual prompts need examples collected from the training set. More examples are better, but the gains saturate quickly:
| Examples per category | 1 | 4 | 16 | 32 | 64 |
|---|---|---|---|---|---|
| COCO AP | 29.2 | 32.9 | 38.8 | 41.3 | 41.4 |
| LVIS val AP | 26.2 | 32.9 | 34.9 | 35.1 | 35.2 |
Compiled from Table 5 of the paper, Visual-G, Swin-T.
With only 1 example, COCO is just 29.2; with 16 it is 38.8, and beyond that it barely moves after 32. This resembles the trend DINOv observed; a concept only takes shape once enough visual examples are given.
Mixed Prompts
Averaging the text and visual embeddings gives 42.5 on COCO, between text's 45.8 and visual's 38.8; on LVIS val it gives 37.0, higher than either one alone (34.8 and 34.9). On common objects, visual prompts dilute the effect of text; on long-tail data, the two reinforce each other.
Data Engine Ablation
When both are trained on Objects365 and GoldG only, T-Rex2's text prompts score 46.1 on COCO, below Grounding DINO T's 48.1; but on LVIS minival T-Rex2 gets 34.9, with 32.7 on rare categories, against only 25.6 and 14.1 for Grounding DINO T. The two differ in text encoder (CLIP vs. BERT) and in how they fuse, and the paper doesn't separate out where this gap comes from.
SA-1B's effect cuts two ways. After it is added, interactive detection on COCO jumps from 41.1 to 56.6, while generic visual detection falls from 41.1 to 38.8. The authors suspect that the names TAP gave SA-1B's boxes aren't accurate enough, so the model learned the wrong semantics.
Speed
On an RTX 3090 with batch size 1, a full pass runs at 10.41 FPS for the Swin-T version and 3.62 FPS for the Swin-L version.
Late fusion pays off in interactive mode. The image backbone and encoder only need to run once, when the user uploads the image; after that, each added box only requires rerunning the visual prompt encoder and the decoder. That part runs at 33.33 FPS for Swin-T and still 19.96 FPS for Swin-L, fast enough for scenarios like auto-annotation, where corrections come one box at a time.
Limitations
The authors list two. First, on common objects, visual prompts sometimes interfere with text prompts, as the drop of mixed mode on COCO shows. Second, generic visual prompts need as many as 16 examples to be reliable; with too few examples, the concept doesn't take shape.
Conclusion
A single long-tail curve lays out the division of labor between text and visual prompts, and the per-category experiments back it up. Common objects go to text, and rare, hard-to-name ones go to visual; when the two are trained together, visual prompts are pulled from 14.0 AP up to 38.8, thanks to the concept anchors that text provides.
In practice, interactive mode is the most useful. The image only needs to be encoded once, and after that each added box brings a fresh detection within a few dozen milliseconds; T-Rex2 was later packaged into the annotation tool T-Rex Label and a counting app. The pity is that the paper says "Model API is now available," and to this day there is still only the API; the weights have never been released.
As for which prompt to choose, if you can name it, type it; if you can't, box it and show it; and for long-tail data, throw both in and average them.