Back to Mmdetection

Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection

configs/grounding_dino/README.md

3.3.030.7 KB
Original Source

Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection

Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection

<!-- [ALGORITHM] -->

Abstract

In this paper, we present an open-set object detector, called Grounding DINO, by marrying Transformer-based detector DINO with grounded pre-training, which can detect arbitrary objects with human inputs such as category names or referring expressions. The key solution of open-set object detection is introducing language to a closed-set detector for open-set concept generalization. To effectively fuse language and vision modalities, we conceptually divide a closed-set detector into three phases and propose a tight fusion solution, which includes a feature enhancer, a language-guided query selection, and a cross-modality decoder for cross-modality fusion. While previous works mainly evaluate open-set object detection on novel categories, we propose to also perform evaluations on referring expression comprehension for objects specified with attributes. Grounding DINO performs remarkably well on all three settings, including benchmarks on COCO, LVIS, ODinW, and RefCOCO/+/g. Grounding DINO achieves a 52.5 AP on the COCO detection zero-shot transfer benchmark, i.e., without any training data from COCO. It sets a new record on the ODinW zero-shot benchmark with a mean 26.1 AP.

<div align=center> </div>

Installation

shell
cd $MMDETROOT

# source installation
pip install -r requirements/multimodal.txt

# or mim installation
mim install mmdet[multimodal]

NOTE

Grounding DINO utilizes BERT as the language model, which requires access to https://huggingface.co/. If you encounter connection errors due to network access, you can download the required files on a computer with internet access and save them locally. Finally, modify the lang_model_name field in the config to the local path. Please refer to the following code:

python
from transformers import BertConfig, BertModel
from transformers import AutoTokenizer

config = BertConfig.from_pretrained("bert-base-uncased")
model = BertModel.from_pretrained("bert-base-uncased", add_pooling_layer=False, config=config)
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")

config.save_pretrained("your path/bert-base-uncased")
model.save_pretrained("your path/bert-base-uncased")
tokenizer.save_pretrained("your path/bert-base-uncased")

Inference

cd $MMDETROOT

wget https://download.openmmlab.com/mmdetection/v3.0/grounding_dino/groundingdino_swint_ogc_mmdet-822d7e9d.pth

python demo/image_demo.py \
	demo/demo.jpg \
	configs/grounding_dino/grounding_dino_swin-t_pretrain_obj365_goldg_cap4m.py \
	--weights groundingdino_swint_ogc_mmdet-822d7e9d.pth \
	--texts 'bench . car .'
<div align=center> </div>

COCO Results and Models

ModelBackboneStyleCOCO mAPOfficial COCO mAPPre-Train DataConfigDownload
Grounding DINO-TSwin-TZero-shot48.548.4O365,GoldG,Cap4Mconfigmodel
Grounding DINO-TSwin-TFinetune58.1(+0.9)57.2O365,GoldG,Cap4Mconfigmodel | log
Grounding DINO-BSwin-BZero-shot56.956.7COCO,O365,GoldG,Cap4M,OpenImage,ODinW-35,RefCOCOconfigmodel
Grounding DINO-BSwin-BFinetune59.7COCO,O365,GoldG,Cap4M,OpenImage,ODinW-35,RefCOCOconfigmodel | log
Grounding DINO-R50R50Scratch48.9(+0.8)48.1configmodel | log

Note:

  1. The weights corresponding to the zero-shot model are adopted from the official weights and converted using the script. We have not retrained the model for the time being.
  2. Finetune refers to fine-tuning on the COCO 2017 dataset. The R50 model is trained using 8 NVIDIA GeForce 3090 GPUs, while the remaining models are trained using 16 NVIDIA GeForce 3090 GPUs. The GPU memory usage is approximately 8.5GB.
  3. Our performance is higher than the official model due to two reasons: we modified the initialization strategy and introduced a log scaler.

LVIS Results

ModelMiniVal APrMiniVal APcMiniVal APfMiniVal APVal1.0 APrVal1.0 APcVal1.0 APfVal1.0 APPre-Train DataConfigDownload
Grounding DINO-T18.824.234.728.810.115.329.920.1O365,GoldG,Cap4Mconfigmodel
Grounding DINO-B27.933.437.234.719.024.132.926.7COCO,O365,GoldG,Cap4M,OpenImage,ODinW-35,RefCOCOconfigmodel

Note:

  1. The above are zero-shot evaluation results.
  2. The evaluation metric we used is LVIS FixAP. For specific details, please refer to Evaluating Large-Vocabulary Object Detectors: The Devil is in the Details.

ODinW (Object Detection in the Wild) Results

Learning visual representations from natural language supervision has recently shown great promise in a number of pioneering works. In general, these language-augmented visual models demonstrate strong transferability to a variety of datasets and tasks. However, it remains challenging to evaluate the transferablity of these models due to the lack of easy-to-use evaluation toolkits and public benchmarks. To tackle this, we build ELEVATER 1 , the first benchmark and toolkit for evaluating (pre-trained) language-augmented visual models. ELEVATER is composed of three components. (i) Datasets. As downstream evaluation suites, it consists of 20 image classification datasets and 35 object detection datasets, each of which is augmented with external knowledge. (ii) Toolkit. An automatic hyper-parameter tuning toolkit is developed to facilitate model evaluation on downstream tasks. (iii) Metrics. A variety of evaluation metrics are used to measure sample-efficiency (zero-shot and few-shot) and parameter-efficiency (linear probing and full model fine-tuning). ELEVATER is platform for Computer Vision in the Wild (CVinW), and is publicly released at https://computer-vision-in-the-wild.github.io/ELEVATER/

Results and models of ODinW13

MethodGLIP-T(A)OfficialGLIP-T(B)OfficialGLIP-T(C)OfficialGroundingDINO-TGroundingDINO-B
AerialMaritimeDrone0.1230.1220.1100.1100.1300.1300.1730.281
Aquarium0.1750.1740.1730.1690.1910.1900.1950.445
CottontailRabbits0.6860.6860.6880.6880.7440.7440.7990.808
EgoHands0.0130.0130.0030.0040.3140.3150.6080.764
NorthAmericaMushrooms0.5020.5020.3670.3670.2970.2960.5070.675
Packages0.5890.5890.0830.0830.6990.6990.6870.670
PascalVOC0.5120.5120.5410.5400.5650.5650.5630.711
pistols0.3390.3390.5020.5010.5030.5040.7260.771
pothole0.0070.0070.0300.0300.0580.0580.2150.478
Raccoon0.0750.0740.2850.2880.2410.2440.5490.541
ShellfishOpenImages0.2530.2530.3370.3380.3000.3020.3930.650
thermalDogsAndPeople0.3720.3720.4750.4750.5100.5100.6570.633
VehiclesOpenImages0.5740.5660.5620.5470.5490.5340.6130.647
Average0.3250.3240.3200.3180.3920.3920.5140.621

Results and models of ODinW35

MethodGLIP-T(A)OfficialGLIP-T(B)OfficialGLIP-T(C)OfficialGroundingDINO-TGroundingDINO-B
AerialMaritimeDrone_large0.1230.1220.1100.1100.1300.1300.1730.281
AerialMaritimeDrone_tiled0.1740.1740.1720.1720.1720.1720.2060.364
AmericanSignLanguageLetters0.0010.0010.0030.0030.0090.0090.0020.096
Aquarium0.1750.1750.1730.1710.1920.1820.1950.445
BCCD0.0160.0160.0010.0010.0000.0000.1610.584
boggleBoards0.0000.0000.0000.0000.0000.0000.0000.134
brackishUnderwater0.0160..0130.0210.0270.0200.0220.0210.454
ChessPieces0.0010.0010.0000.0000.0010.0010.0000.000
CottontailRabbits0.7100.7090.6830.6830.7520.7520.8060.797
dice0.0050.0050.0040.0040.0040.0040.0040.082
DroneControl0.0160.0170.0060.0080.0050.0070.0420.638
EgoHands_generic0.0090.0100.0050.0060.5100.5080.6080.764
EgoHands_specific0.0010.0010.0040.0060.0030.0040.0020.687
HardHatWorkers0.0290.0290.0230.0230.0330.0330.0460.439
MaskWearing0.0070.0070.0030.0020.0050.0050.0040.406
MountainDewCommercial0.2180.2270.1990.1970.4780.4630.4300.580
NorthAmericaMushrooms0.5020.5020.4500.4500.4970.4970.4710.501
openPoetryVision0.0000.0000.0000.0000.0000.0000.0000.051
OxfordPets_by_breed0.0010.0020.0020.0040.0010.0020.0030.799
OxfordPets_by_species0.0160.0110.0120.0090.0130.0090.0110.872
PKLot0.0020.0020.0000.0000.0000.0000.0010.774
Packages0.5690.5690.2790.2790.7120.7120.6950.728
PascalVOC0.5120.5120.5410.5400.5650.5650.5630.711
pistols0.3390.3390.5020.5010.5030.5040.7260.771
plantdoc0.0020.0020.0070.0070.0090.0090.0050.376
pothole0.0070.0100.0240.0250.0850.1010.2150.478
Raccoons0.0750.0740.2850.2880.2410.2440.5490.541
selfdrivingCar0.0710.0720.0740.0740.0810.0800.0890.318
ShellfishOpenImages0.2530.2530.3370.3380.3000.3020.3930.650
ThermalCheetah0.0280.0280.0000.0000.0280.0280.0870.290
thermalDogsAndPeople0.3720.3720.4750.4750.5100.5100.6570.633
UnoCards0.0000.0000.0000.0010.0020.0030.0060.754
VehiclesOpenImages0.5740.5660.5620.5470.5490.5340.6130.647
WildfireSmoke0.0000.0000.0000.0000.0170.0170.1340.410
websiteScreenshots0.0030.0040.0030.0050.0050.0060.0120.175
Average0.1340.1340.1380.1380.1790.1780.2270.492

Flickr30k Results

ModelPre-Train DataVal R@1Val R@5Val R@10Tesst R@1Test R@5Test R@10ConfigDownload
Grounding DINO-TO365,GoldG,Cap4M87.896.698.088.196.998.2configmodel | log

Note:

  1. @1,5,10 refers to precision at the top 1, 5, and 10 positions in a predicted ranked list.
  2. The pretraining data used by Grounding DINO-T is O365,GoldG,Cap4M, and the corresponding evaluation configuration is (grounding_dino_swin-t_pretrain_zeroshot_refcoco)[refcoco/grounding_dino_swin-t_pretrain_zeroshot_refcoco.py].

Test Command

shell
cd mmdetection
bash tools/dist_test.sh configs/grounding_dino/flickr30k/grounding_dino_swin-t-pretrain_zeroshot_flickr30k.py checkpoints/groundingdino_swint_ogc_mmdet-822d7e9d.pth 8

Referring Expression Comprehension Results

| Method | Grounding DINO-T (O365,GoldG,Cap4M) | Grounding DINO-B (COCO,O365,GoldG,Cap4M,OpenImage,ODinW-35,RefCOCO) | | --------------------------------------- | ----------------------------------------- | ------------------------------------------------------------------------- | | RefCOCO val @1,5,10 | 50.77/89.45/94.86 | 84.61/97.88/99.10 | | RefCOCO testA @1,5,10 | 57.45/91.29/95.62 | 88.65/98.89/99.63 | | RefCOCO testB @1,5,10 | 44.97/86.54/92.88 | 80.51/96.64/98.51 | | RefCOCO+ val @1,5,10 | 51.64/86.35/92.57 | 73.67/96.60/98.65 | | RefCOCO+ testA @1,5,10 | 57.25/86.74/92.65 | 82.19/97.92/99.09 | | RefCOCO+ testB @1,5,10 | 46.35/84.05/90.67 | 64.10/94.25/97.46 | | RefCOCOg val @1,5,10 | 60.42/92.10/96.18 | 78.33/97.28/98.57 | | RefCOCOg test @1,5,10 | 59.74/92.08/96.28 | 78.11/97.06/98.65 | | gRefCOCO val Pr@(F1=1, IoU≥0.5),N-acc | 41.32/91.82 | 46.18/81.44 | | gRefCOCO testA Pr@(F1=1, IoU≥0.5),N-acc | 27.23/90.24 | 38.60/76.06 | | gRefCOCO testB Pr@(F1=1, IoU≥0.5),N-acc | 29.70/93.49 | 35.87/80.58 |

Note:

  1. @1,5,10 refers to precision at the top 1, 5, and 10 positions in a predicted ranked list.
  2. Pr@(F1=1, IoU≥0.5),N-acc from the paper GREC: Generalized Referring Expression Comprehension
  3. The pretraining data used by Grounding DINO-T is O365,GoldG,Cap4M, and the corresponding evaluation configuration is (grounding_dino_swin-t_pretrain_zeroshot_refcoco)[refcoco/grounding_dino_swin-t_pretrain_zeroshot_refcoco.py].
  4. The pretraining data used by Grounding DINO-B is COCO,O365,GoldG,Cap4M,OpenImage,ODinW-35,RefCOCO, and the corresponding evaluation configuration is (grounding_dino_swin-t_pretrain_zeroshot_refcoco)[refcoco/grounding_dino_swin-b_pretrain_zeroshot_refcoco.py].

Test Command

shell
cd mmdetection
./tools/dist_test.sh configs/grounding_dino/refcoco/grounding_dino_swin-t_pretrain_zeroshot_refexp.py https://download.openmmlab.com/mmdetection/v3.0/grounding_dino/groundingdino_swint_ogc_mmdet-822d7e9d.pth 8
./tools/dist_test.sh configs/grounding_dino/refcoco/grounding_dino_swin-b_pretrain_zeroshot_refexp.py https://download.openmmlab.com/mmdetection/v3.0/grounding_dino/groundingdino_swinb_cogcoor_mmdet-55949c9c.pth 8

Description Detection Dataset

shell
pip install ddd-dataset

| Method | mode | Grounding DINO-T (O365,GoldG,Cap4M) | Grounding DINO-B (COCO,O365,GoldG,Cap4M,OpenImage,ODinW-35,RefCOCO) | | -------------------------------- | -------- | ----------------------------------------- | ------------------------------------------------------------------------- | | FULL/short/middle/long/very long | concat | 17.2/18.0/18.7/14.8/16.3 | 20.2/20.4/21.1/18.8/19.8 | | FULL/short/middle/long/very long | parallel | 22.3/28.2/24.8/19.1/13.9 | 25.0/26.4/27.2/23.5/19.7 | | PRES/short/middle/long/very long | concat | 17.8/18.3/19.2/15.2/17.3 | 20.7/21.7/21.4/19.1/20.3 | | PRES/short/middle/long/very long | parallel | 21.0/27.0/22.8/17.5/12.5 | 23.7/25.8/25.1/21.9/19.3 | | ABS/short/middle/long/very long | concat | 15.4/17.1/16.4/13.6/14.9 | 18.6/16.1/19.7/18.1/19.1 | | ABS/short/middle/long/very long | parallel | 26.0/32.0/33.0/23.6/15.5 | 28.8/28.1/35.8/28.2/20.2 |

Note:

  1. Considering that the evaluation time for Inter-scenario is very long and the performance is low, it is temporarily not supported. The mentioned metrics are for Intra-scenario.
  2. concat is the default inference mode for Grounding DINO, where it concatenates multiple sub-sentences with "." to form a single sentence for inference. On the other hand, "parallel" performs inference on each sub-sentence in a for-loop.

Custom Dataset

To facilitate fine-tuning on custom datasets, we use a simple cat dataset as an example, as shown in the following steps.

1. Dataset Preparation

shell
cd mmdetection
wget https://download.openmmlab.com/mmyolo/data/cat_dataset.zip
unzip cat_dataset.zip -d data/cat/

cat dataset is a single-category dataset with 144 images, which has been converted to coco format.

<div align=center> </div>

2. Config Preparation

Due to the simplicity and small number of cat datasets, we use 8 cards to train 20 epochs, scale the learning rate accordingly, and do not train the language model, only the visual model.

The Details of the configuration can be found in grounding_dino_swin-t_finetune_8xb2_20e_cat

3. Visualization and Evaluation

Due to the Grounding DINO is an open detection model, so it can be detected and evaluated even if it is not trained on the cat dataset.

The single image visualization is as follows:

shell
cd mmdetection
python demo/image_demo.py data/cat/images/IMG_20211205_120756.jpg configs/grounding_dino/grounding_dino_swin-t_finetune_8xb2_20e_cat.py --weights https://download.openmmlab.com/mmdetection/v3.0/grounding_dino/groundingdino_swint_ogc_mmdet-822d7e9d.pth --texts cat.
<div align=center> </div>

The test dataset evaluation on single card is as follows:

shell
python tools/test.py configs/grounding_dino/grounding_dino_swin-t_finetune_8xb2_20e_cat.py https://download.openmmlab.com/mmdetection/v3.0/grounding_dino/groundingdino_swint_ogc_mmdet-822d7e9d.pth
text
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.867
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=1000 ] = 1.000
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=1000 ] = 0.931
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = -1.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = -1.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.867
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.903
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=300 ] = 0.907
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=1000 ] = 0.907
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.907

4. Model Training and Visualization

shell
./tools/dist_train.sh configs/grounding_dino/grounding_dino_swin-t_finetune_8xb2_20e_cat.py 8 --work-dir cat_work_dir

The model will be saved based on the best performance on the test set. The performance of the best model (at epoch 16) is as follows:

text
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.905
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=1000 ] = 1.000
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=1000 ] = 0.923
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = -1.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = -1.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.905
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.927
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=300 ] = 0.937
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=1000 ] = 0.937
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.937

We can find that after fine-tuning training, the training of the cat dataset is increased from 86.7 to 90.5.

If we do single image inference visualization again, the result is as follows:

shell
cd mmdetection
python demo/image_demo.py data/cat/images/IMG_20211205_120756.jpg configs/grounding_dino/grounding_dino_swin-t_finetune_8xb2_20e_cat.py --weights cat_work_dir/best_coco_bbox_mAP_epoch_16.pth --texts cat.
<div align=center> </div>