GroundingDINO README

The project's README from github.com/IDEA-Research/GroundingDINO. As one markdown file: README.md.

:sauropod: Grounding DINO

PWC PWC
PWC PWC

IDEA-CVR, IDEA-Research

Shilong Liu, Zhaoyang Zeng, Tianhe Ren, Feng Li, Hao Zhang, Jie Yang, Chunyuan Li, Jianwei Yang, Hang Su, Jun Zhu, Lei Zhang:email:.

[Paper] [Demo] [BibTex]

PyTorch implementation and pretrained models for Grounding DINO. For details, see the paper Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection.

:sun_with_face: Helpful Tutorial

:sparkles: Highlight Projects

:bulb: Highlight

:fire: News

Description Paper introduction. ODinW Marrying Grounding DINO and GLIGEN gd_gligen

:star: Explanations/Tips for Grounding DINO Inputs and Outputs

:label: TODO

:hammer_and_wrench: Install

Note:

  1. If you have a CUDA environment, please make sure the environment variable CUDA_HOME is set. It will be compiled under CPU-only mode if no CUDA available.

Please make sure following the installation steps strictly, otherwise the program may produce:

NameError: name '_C' is not defined

If this happened, please reinstalled the groundingDINO by reclone the git and do all the installation steps again.

how to check cuda:

echo $CUDA_HOME

If it print nothing, then it means you haven't set up the path/

Run this so the environment variable will be set under current shell.

export CUDA_HOME=/path/to/cuda-11.3

Notice the version of cuda should be aligned with your CUDA runtime, for there might exists multiple cuda at the same time.

If you want to set the CUDA_HOME permanently, store it using:

echo 'export CUDA_HOME=/path/to/cuda' >> ~/.bashrc

after that, source the bashrc file and check CUDA_HOME:

source ~/.bashrc
echo $CUDA_HOME

In this example, /path/to/cuda-11.3 should be replaced with the path where your CUDA toolkit is installed. You can find this by typing which nvcc in your terminal:

For instance, if the output is /usr/local/cuda/bin/nvcc, then:

export CUDA_HOME=/usr/local/cuda

Installation:

1.Clone the GroundingDINO repository from GitHub.

git clone https://github.com/IDEA-Research/GroundingDINO.git
  1. Change the current directory to the GroundingDINO folder.
cd GroundingDINO/
  1. Install the required dependencies in the current directory.
pip install -e .
  1. Download pre-trained model weights.
mkdir weights
cd weights
wget -q https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
cd ..

:arrow_forward: Demo

Check your GPU ID (only if you're using a GPU)

nvidia-smi

Replace {GPU ID}, image_you_want_to_detect.jpg, and "dir you want to save the output" with appropriate values in the following command

CUDA_VISIBLE_DEVICES={GPU ID} python demo/inference_on_a_image.py \
-c groundingdino/config/GroundingDINO_SwinT_OGC.py \
-p weights/groundingdino_swint_ogc.pth \
-i image_you_want_to_detect.jpg \
-o "dir you want to save the output" \
-t "chair"
 [--cpu-only] # open it for cpu mode

If you would like to specify the phrases to detect, here is a demo:

CUDA_VISIBLE_DEVICES={GPU ID} python demo/inference_on_a_image.py \
-c groundingdino/config/GroundingDINO_SwinT_OGC.py \
-p ./groundingdino_swint_ogc.pth \
-i .asset/cat_dog.jpeg \
-o logs/1111 \
-t "There is a cat and a dog in the image ." \
--token_spans "[[[9, 10], [11, 14]], [[19, 20], [21, 24]]]"
 [--cpu-only] # open it for cpu mode

The token_spans specify the start and end positions of a phrases. For example, the first phrase is [[9, 10], [11, 14]]. "There is a cat and a dog in the image ."[9:10] = 'a', "There is a cat and a dog in the image ."[11:14] = 'cat'. Hence it refers to the phrase a cat . Similarly, the [[19, 20], [21, 24]] refers to the phrase a dog.

See the demo/inference_on_a_image.py for more details.

Running with Python:

from groundingdino.util.inference import load_model, load_image, predict, annotate
import cv2

model = load_model("groundingdino/config/GroundingDINO_SwinT_OGC.py", "weights/groundingdino_swint_ogc.pth")
IMAGE_PATH = "weights/dog-3.jpeg"
TEXT_PROMPT = "chair . person . dog ."
BOX_TRESHOLD = 0.35
TEXT_TRESHOLD = 0.25

image_source, image = load_image(IMAGE_PATH)

boxes, logits, phrases = predict(
    model=model,
    image=image,
    caption=TEXT_PROMPT,
    box_threshold=BOX_TRESHOLD,
    text_threshold=TEXT_TRESHOLD
)

annotated_frame = annotate(image_source=image_source, boxes=boxes, logits=logits, phrases=phrases)
cv2.imwrite("annotated_image.jpg", annotated_frame)

Web UI

We also provide a demo code to integrate Grounding DINO with Gradio Web UI. See the file demo/gradio_app.py for more details.

Notebooks

COCO Zero-shot Evaluations

We provide an example to evaluate Grounding DINO zero-shot performance on COCO. The results should be 48.5.

CUDA_VISIBLE_DEVICES=0 \
python demo/test_ap_on_coco.py \
 -c groundingdino/config/GroundingDINO_SwinT_OGC.py \
 -p weights/groundingdino_swint_ogc.pth \
 --anno_path /path/to/annoataions/ie/instances_val2017.json \
 --image_dir /path/to/imagedir/ie/val2017

:luggage: Checkpoints

name backbone Data box AP on COCO Checkpoint Config
1 GroundingDINO-T Swin-T O365,GoldG,Cap4M 48.4 (zero-shot) / 57.2 (fine-tune) GitHub link | HF link link
2 GroundingDINO-B Swin-B COCO,O365,GoldG,Cap4M,OpenImage,ODinW-35,RefCOCO 56.7 GitHub link | HF link link

:medal_military: Results

COCO Object Detection Results COCO
ODinW Object Detection Results ODinW
Marrying Grounding DINO with Stable Diffusion for Image Editing See our example notebook for more details. GD_SD
Marrying Grounding DINO with GLIGEN for more Detailed Image Editing. See our example notebook for more details. GD_GLIGEN

:sauropod: Model: Grounding DINO

Includes: a text backbone, an image backbone, a feature enhancer, a language-guided query selection, and a cross-modality decoder.

arch

:hearts: Acknowledgement

Our model is related to DINO and GLIP. Thanks for their great work!

We also thank great previous work including DETR, Deformable DETR, SMCA, Conditional DETR, Anchor DETR, Dynamic DETR, DAB-DETR, DN-DETR, etc. More related work are available at Awesome Detection Transformer. A new toolbox detrex is available as well.

Thanks Stable Diffusion and GLIGEN for their awesome models.

:black_nib: Citation

If you find our work helpful for your research, please consider citing the following BibTeX entry.

@article{liu2023grounding,
  title={Grounding dino: Marrying dino with grounded pre-training for open-set object detection},
  author={Liu, Shilong and Zeng, Zhaoyang and Ren, Tianhe and Li, Feng and Zhang, Hao and Yang, Jie and Li, Chunyuan and Yang, Jianwei and Su, Hang and Zhu, Jun and others},
  journal={arXiv preprint arXiv:2303.05499},
  year={2023}
}

GroundingDINO (IDEA-Research/GroundingDINO)

Searching Database…

这是什么?IDEA-Research 推出的开放集目标检测模型(发表于 ECCV 2024):给它一张图片加一段文字描述,它就能找出你的文字所描述的物体。这个仓库是官方的 PyTorch 实现,附带预训练模型。
这是直接运行的应用,还是用来做二次开发的代码?这是一个研究用的代码库,以 Python 包的形式安装,然后在自己的代码里调用,另外还需要下载模型权重文件。想快速试用的话,有 Gradio 网页版演示、Colab 演示和 Hugging Face 集成。
怎么把它跑起来?克隆仓库,用 cd 进入 GroundingDINO 目录,用 pip 安装依赖(如果用 CUDA,需先设置 CUDA_HOME),然后下载预训练模型权重,再在自己的图片上运行演示脚本。
安装上有什么坑吗?README 提醒要严格按安装步骤操作,否则程序可能出问题;建议的解决办法是重新克隆仓库并把所有步骤再做一遍。另外目前只开放了推理功能——训练代码还在待办清单里。
这主要是给研究人员用的,还是我能用在真实项目里?两者都可以:它出自研究实验室,但已经集成到 Hugging Face 里,Roboflow 的教程还演示了如何用它给数据集自动标注和评估,用来训练 YOLOv8 这类其他检测器。
输入什么,输出什么?输入一个(图片, 文字)组合。默认最多返回 900 个框,每个框都根据你提示词里的文字打分;得分高于 box_threshold 的框会保留,得分高于 text_threshold 的词成为预测出的标签。类别名称之间用点号分隔。
效果怎么样?README 重点提到:在没有使用任何 COCO 训练数据的情况下,零样本在 COCO 上达到 52.5 AP,微调后达到 63.0 AP。已发布的权重中,GroundingDINO-T 零样本 48.4 / 微调后 57.2,GroundingDINO-B 达到 56.7 AP。
可以下载哪些预训练权重?有两个:GroundingDINO-T 使用 Swin-T 骨干网络(在 O365、GoldG 和 Cap4M 上训练),以及更重的 GroundingDINO-B,使用 Swin-B 骨干网络,训练数据更多,包括 COCO、OpenImage 和 ODinW-35。两者都附带配置文件。
它能和 Segment Anything 配合使用吗?可以——Grounded-SAM 把它与 Segment Anything 结合,实现通过文字提示的分割;Grounded SAM 2 还增加了在开放世界场景中跟踪任意物体的能力。示例中还将它与 Stable Diffusion 和 GLIGEN 结合用于图像编辑。
Size
12.8 MB
Stars
10.7K
Age
3y
Commits · 90 Days