We plan to create a very interesting demo by combining Grounding DINO and Segment Anything which aims to detect and segment anything with text inputs! And we will continue to improve it and create more interesting demos based on this foundation. And we have already released an overall technical report about our project on arXiv, please check Grounded SAM: Assembling Open-World Models for Diverse Visual Tasks for more details.
We are very willing to help everyone share and promote new projects based on Segment-Anything, Please check out here for more amazing demos and works in the community: Highlight Extension Projects. You can submit a new issue (with project
tag) or a new pull request to add new project's links.
🍄 Why Building this Project?
The core idea behind this project is to combine the strengths of different models in order to build a very powerful pipeline for solving complex problems. And it's worth mentioning that this is a workflow for combining strong expert models, where all parts can be used separately or in combination, and can be replaced with any similar but different models (like replacing Grounding DINO with GLIP or other detectors / replacing Stable-Diffusion with ControlNet or GLIGEN/ Combining with ChatGPT).
🍇 Updates
2024/01/26
We have released a comprehensive technical report about our project on arXiv, please check Grounded SAM: Assembling Open-World Models for Diverse Visual Tasks for more details. And we are profoundly grateful for the contributions of all the contributors in this project.2023/12/17
Support Grounded-RepViT-SAM demo, thanks a lot for their great work!2023/12/16
Support Grounded-Edge-SAM demo, thanks a lot for their great work!2023/12/10
Support Grounded-Efficient-SAM demo, thanks a lot for their great work!2023/11/24
Release RAM++, which is the next generation of RAM. RAM++ can recognize any category with high accuracy, including both predefined common categories and diverse open-set categories.2023/11/23
Release our newly proposed visual prompt counting model T-Rex. The introduction Video and Demo is available in DDS now.2023/07/25
Support Light-HQ-SAM in EfficientSAM, credits to Mingqiao Ye and Lei Ke, thanks a lot for their great work!2023/07/14
Combining Grounding-DINO-B with SAM-HQ achieves 49.6 mean AP in Segmentation in the Wild competition zero-shot track, surpassing Grounded-SAM by 3.6 mean AP, thanks for their great work!2023/06/28
Combining Grounding-DINO with Efficient SAM variants including FastSAM and MobileSAM in EfficientSAM for faster annotating, thanks a lot for their great work!2023/06/20
By combining Grounding-DINO-L with SAM-ViT-H, Grounded-SAM achieves 46.0 mean AP in Segmentation in the Wild competition zero-shot track on CVPR 2023 workshop, surpassing UNINEXT (CVPR 2023) by about 4 mean AP.2023/06/16
Release RAM-Grounded-SAM Replicate Online Demo. Thanks a lot to Chenxi for providing this nice demo 🌹.2023/06/14
Support RAM-Grounded-SAM & SAM-HQ and update Simple Automatic Label Demo to support RAM, setting up a strong automatic annotation pipeline.2023/06/13
Checkout the Autodistill: Train YOLOv8 with ZERO Annotations tutorial to learn how to use Grounded-SAM + Autodistill for automated data labeling and real-time model training.2023/06/13
Support SAM-HQ in Grounded-SAM Demo for higher quality prediction.2023/06/12
Support RAM-Grounded-SAM for strong automatic labeling pipeline! Thanks for Recognize-Anything.2023/06/01
Our Grounded-SAM has been accepted to present a demo at ICCV 2023! See you in Paris!2023/05/23
: Support Image-Referring-Segment
, Audio-Referring-Segment
and Text-Referring-Segment
in ImageBind-SAM.2023/05/03
: Checkout the Automated Dataset Annotation and Evaluation with GroundingDINO and SAM which is an amazing tutorial on automatic labeling! Thanks a lot for Piotr Skalski and Roboflow!Here we provide some background knowledge that you may need to know before trying the demos.
Here we provide some impressive works you may find interesting:
We also list some awesome segment-anything extension projects here you may find interesting:
The code requires python>=3.8
, as well as pytorch>=1.7
and torchvision>=0.8
. Please follow the instructions here to install both PyTorch and TorchVision dependencies. Installing both PyTorch and TorchVision with CUDA support is strongly recommended.
Open one terminal:
make build-image
make run
That's it.
If you would like to allow visualization across docker container, open another terminal and type:
xhost +
You should set the environment variable manually as follows if you want to build a local GPU environment for Grounded-SAM:
export AM_I_DOCKER=False
export BUILD_WITH_CUDA=True
export CUDA_HOME=/path/to/cuda-11.3/
Install Segment Anything:
python -m pip install -e segment_anything
Install Grounding DINO:
pip install --no-build-isolation -e GroundingDINO
Install diffusers:
pip install --upgrade diffusers[torch]
Install osx:
git submodule update --init --recursive
cd grounded-sam-osx && bash install.sh
Install RAM & Tag2Text:
git clone https://github.com/xinyu1205/recognize-anything.git
pip install -r ./recognize-anything/requirements.txt
pip install -e ./recognize-anything/
The following optional dependencies are necessary for mask post-processing, saving masks in COCO format, the example notebooks, and exporting the model in ONNX format. jupyter
is also required to run the example notebooks.
pip install opencv-python pycocotools matplotlib onnxruntime onnx ipykernel
More details can be found in install segment anything and install GroundingDINO and install OSX
Let's start exploring our Grounding-SAM Playground and we will release more interesting demos in the future, stay tuned!
Here we list some notebook demo provided in this project:
:grapes: [arXiv Paper] :rose:[Try the Colab Demo] :sunflower: [Try Huggingface Demo] :mushroom: [Automated Dataset Annotation and Evaluation]
Here's the step-by-step tutorial on running GroundingDINO
demo:
Step 1: Download the pretrained weights
cd Grounded-Segment-Anything
# download the pretrained groundingdino-swin-tiny model
wget https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
Step 2: Running the demo
python grounding_dino_demo.py
Tips
.
. An example: cat . dog . chair .
Step 3: Check the annotated image
The annotated image will be saved as ./annotated_image.jpg
.
Here's the step-by-step tutorial on running Grounded-SAM
demo:
Step 1: Download the pretrained weights
cd Grounded-Segment-Anything
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
wget https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
We provide two versions of Grounded-SAM demo here:
Step 2: Running original grounded-sam demo
# depends on your device
export CUDA_VISIBLE_DEVICES=0
python grounded_sam_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--sam_checkpoint sam_vit_h_4b8939.pth \
--input_image assets/demo1.jpg \
--output_dir "outputs" \
--box_threshold 0.3 \
--text_threshold 0.25 \
--text_prompt "bear" \
--device "cuda"
The annotated results will be saved in ./outputs
as follows
Step 3: Running grounded-sam demo with sam-hq
Download the demo image
wget https://github.com/IDEA-Research/detrex-storage/releases/download/grounded-sam-storage/sam_hq_demo_image.png
Download SAM-HQ checkpoint here
Running grounded-sam-hq demo as follows:
export CUDA_VISIBLE_DEVICES=0
python grounded_sam_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--sam_hq_checkpoint ./sam_hq_vit_h.pth \ # path to sam-hq checkpoint
--use_sam_hq \ # set to use sam-hq model
--input_image sam_hq_demo_image.png \
--output_dir "outputs" \
--box_threshold 0.3 \
--text_threshold 0.25 \
--text_prompt "chair." \
--device "cuda"
The annotated results will be saved in ./outputs
as follows
Step 4: Running the updated grounded-sam demo (optional)
Note that this demo is almost same as the original demo, but with more elegant code.
python grounded_sam_simple_demo.py
The annotated results will be saved as ./groundingdino_annotated_image.jpg
and ./grounded_sam_annotated_image.jpg
Step 5: Running the Sam model with multi-gpu
export CUDA_VISIBLE_DEVICES=0,1
python grounded_sam_multi_gpu_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--sam_checkpoint sam_vit_h_4b8939.pth \
--input_path assets/car \
--output_dir "outputs" \
--box_threshold 0.3 \
--text_threshold 0.25 \
--text_prompt "car" \
--device "cuda"
You will see that the model is loaded once per GPU
Step 1: Download the pretrained weights
cd Grounded-Segment-Anything
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
wget https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
Step 2: Running grounded-sam inpainting demo
CUDA_VISIBLE_DEVICES=0
python grounded_sam_inpainting_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--sam_checkpoint sam_vit_h_4b8939.pth \
--input_image assets/inpaint_demo.jpg \
--output_dir "outputs" \
--box_threshold 0.3 \
--text_threshold 0.25 \
--det_prompt "bench" \
--inpaint_prompt "A sofa, high quality, detailed" \
--device "cuda"
The annotated and inpaint image will be saved in ./outputs
Step 3: Check the results
We support 6 tasks in the local Gradio APP:
python gradio_app.py
The Recognize Anything Models are a series of open-source and strong fundamental image recognition models, including RAM++, RAM and Tag2text.
It is seamlessly linked to generate pseudo labels automatically as follows:
Step 1: Init submodule and download the pretrained checkpoint
cd Grounded-Segment-Anything
git submodule init
git submodule update
GroundingDINO
, SAM
and RAM/Tag2Text
:wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
wget https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
wget https://huggingface.co/spaces/xinyu1205/Tag2Text/resolve/main/ram_swin_large_14m.pth
wget https://huggingface.co/spaces/xinyu1205/Tag2Text/resolve/main/tag2text_swin_14m.pth
Step 2: Running the demo with RAM
export CUDA_VISIBLE_DEVICES=0
python automatic_label_ram_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--ram_checkpoint ram_swin_large_14m.pth \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--sam_checkpoint sam_vit_h_4b8939.pth \
--input_image assets/demo9.jpg \
--output_dir "outputs" \
--box_threshold 0.25 \
--text_threshold 0.2 \
--iou_threshold 0.5 \
--device "cuda"
Step 2: Or Running the demo with Tag2Text
export CUDA_VISIBLE_DEVICES=0
python automatic_label_tag2text_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--tag2text_checkpoint tag2text_swin_14m.pth \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--sam_checkpoint sam_vit_h_4b8939.pth \
--input_image assets/demo9.jpg \
--output_dir "outputs" \
--box_threshold 0.25 \
--text_threshold 0.2 \
--iou_threshold 0.5 \
--device "cuda"
output_dir
as follows (right figure):It is easy to generate pseudo labels automatically as follows:
Run Demo
export OPENAI_API_KEY=your_openai_key
export OPENAI_API_BASE=https://closeai.deno.dev/v1
export CUDA_VISIBLE_DEVICES=0
python automatic_label_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--sam_checkpoint sam_vit_h_4b8939.pth \
--input_image assets/demo3.jpg \
--output_dir "outputs" \
--openai_key $OPENAI_API_KEY \
--box_threshold 0.25 \
--text_threshold 0.2 \
--iou_threshold 0.5 \
--device "cuda"
When you don't have a paid Account for ChatGPT is also possible to use NLTK instead. Just don't include the openai_key
Parameter when starting the Demo.
The pseudo labels and model prediction visualization will be saved in output_dir
as follows:
Detect and segment anything with speech!
Install Whisper
pip install -U openai-whisper
See the whisper official page if you have other questions for the installation.
Run Voice-to-Label Demo
Optional: Download the demo audio file
wget https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/demo_audio.mp3
export CUDA_VISIBLE_DEVICES=0
python grounded_sam_whisper_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--sam_checkpoint sam_vit_h_4b8939.pth \
--input_image assets/demo4.jpg \
--output_dir "outputs" \
--box_threshold 0.3 \
--text_threshold 0.25 \
--speech_file "demo_audio.mp3" \
--device "cuda"
Run Voice-to-inpaint Demo
You can enable chatgpt to help you automatically detect the object and inpainting order with --enable_chatgpt
.
Or you can specify the object you want to inpaint [stored in args.det_speech_file
] and the text you want to inpaint with [stored in args.inpaint_speech_file
].
export OPENAI_API_KEY=your_openai_key
export OPENAI_API_BASE=https://closeai.deno.dev/v1
# Example: enable chatgpt
export CUDA_VISIBLE_DEVICES=0
python grounded_sam_whisper_inpainting_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--sam_checkpoint sam_vit_h_4b8939.pth \
--input_image assets/inpaint_demo.jpg \
--output_dir "outputs" \
--box_threshold 0.3 \
--text_threshold 0.25 \
--prompt_speech_file assets/acoustics/prompt_speech_file.mp3 \
--enable_chatgpt \
--openai_key $OPENAI_API_KEY\
--device "cuda"
# Example: without chatgpt
export CUDA_VISIBLE_DEVICES=0
python grounded_sam_whisper_inpainting_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--sam_checkpoint sam_vit_h_4b8939.pth \
--input_image assets/inpaint_demo.jpg \
--output_dir "outputs" \
--box_threshold 0.3 \
--text_threshold 0.25 \
--det_speech_file "assets/acoustics/det_voice.mp3" \
--inpaint_speech_file "assets/acoustics/inpaint_voice.mp3" \
--device "cuda"
Following Visual ChatGPT, we add a ChatBot for our project. Currently, it supports:
To use the ChatBot:
Grounded_dino_sam_inpainting
.export OPENAI_API_KEY=your_openai_key
export OPENAI_API_BASE=https://closeai.deno.dev/v1
export CUDA_VISIBLE_DEVICES=0
python chatbot.py
Download the checkpoint osx_l_wo_decoder.pth.tar
from here for OSX:
Download the human model files and place it into grounded-sam-osx/utils/human_model_files
following the instruction of OSX.
Run Demo
export CUDA_VISIBLE_DEVICES=0
python grounded_sam_osx_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--sam_checkpoint sam_vit_h_4b8939.pth \
--osx_checkpoint osx_l_wo_decoder.pth.tar \
--input_image assets/osx/grounded_sam_osx_demo.png \
--output_dir "outputs" \
--box_threshold 0.3 \
--text_threshold 0.25 \
--text_prompt "humans, chairs" \
--device "cuda"
output_dir
as follows:A person with pink clothes |
A man with a sunglasses |
Download the checkpoint motrv2_dancetrack.pth
from here for MOTRv2:
See the more thing if you have other questions for the installation.
Run Demo
export CUDA_VISIBLE_DEVICES=0
python grounded_sam_visam.py \
--meta_arch motr \
--dataset_file e2e_dance \
--with_box_refine \
--query_interaction_layer QIMv2 \
--num_queries 10 \
--det_db det_db_motrv2.json \
--use_checkpoint \
--mot_path your_data_path \
--resume motrv2_dancetrack.pth \
--sam_checkpoint sam_vit_h_4b8939.pth \
--video_path DanceTrack/test/dancetrack0003
||
Release the interactive fashion-edit playground in here. Run in the notebook, just click for annotating points for further segmentation. Enjoy it!
Release human-face-edit branch here. We'll keep updating this branch with more interesting features. Here are some examples:
We extend the scope to 3D world by combining Segment Anything and VoxelNeXt. When we provide a prompt (e.g., a point / box), the result is not only 2D segmentation mask, but also 3D boxes. Please check voxelnext_3d_box for more details.
Our project wouldn't be possible without the contributions of these amazing people! Thank you all for making this project better.
If you find this project helpful for your research, please consider citing the following BibTeX entry.
@article{kirillov2023segany,
title={Segment Anything},
author={Kirillov, Alexander and Mintun, Eric and Ravi, Nikhila and Mao, Hanzi and Rolland, Chloe and Gustafson, Laura and Xiao, Tete and Whitehead, Spencer and Berg, Alexander C. and Lo, Wan-Yen and Doll{\'a}r, Piotr and Girshick, Ross},
journal={arXiv:2304.02643},
year={2023}
}
@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}
}
@misc{ren2024grounded,
title={Grounded SAM: Assembling Open-World Models for Diverse Visual Tasks},
author={Tianhe Ren and Shilong Liu and Ailing Zeng and Jing Lin and Kunchang Li and He Cao and Jiayu Chen and Xinyu Huang and Yukang Chen and Feng Yan and Zhaoyang Zeng and Hao Zhang and Feng Li and Jie Yang and Hongyang Li and Qing Jiang and Lei Zhang},
year={2024},
eprint={2401.14159},
archivePrefix={arXiv},
primaryClass={cs.CV}
}