Grounded-Segment-Anything
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.
We are very willing to help everyone share and promote new projects based on Segment-Anything, Please checkout 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.
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).
- Segment Anything is a strong segmentation model. But it needs prompts (like boxes/points) to generate masks.
- Grounding DINO is a strong zero-shot detector which is capable of to generate high quality boxes and labels with free-form text.
- OSX is a strong and efficient one-stage motion capture method to generate high quality 3D human mesh from monucular image. We also release a large-scale upper-body dataset UBody for a more accurate reconstrution in the upper-body scene.
- Stable-Diffusion is an amazing strong text-to-image diffusion model.
- BLIP is a wonderful language-vision model for image understanding.
- Visual ChatGPT is a wonderful tool that connects ChatGPT and a series of Visual Foundation Models to enable sending and receiving images during chatting.
- VoxelNeXt is a clean, simple, and fully-sparse 3D object detector, which predicts objects directly upon sparse voxel features.
- Checkout the Segment Everything Everywhere All at Once demo! It supports segmenting with various types of prompts (text, point, scribble, referring image, etc.) and any combination of prompts.
- Checkout the OpenSeeD for the interactive segmentation with box input to generate mask.
- Visual instruction tuning with GPT-4! Please check out the multimodal model LLaVA: [Project Page] [Paper] [Demo] [Data] [Model]
- GroundingDINO: Detect Everything with Text Prompt
- GroundingDINO + Segment-Anything: Detect and Segment Everything with Text Prompt
- GroundingDINO + Segment-Anything + Stable-Diffusion: Detect, Segment and Generate Anything with Text Prompts
- Grounded-SAM + Stable-Diffusion Gradio APP
- Grounded-SAM + BLIP: Automatically Labeling System!
- Whisper + Grounded-SAM: Detect and Segment Everything with Speech!
- Grounded-SAM + Visual ChatGPT: Automatically Label & Generate Everything with ChatBot!
- Grounded-SAM + OSX: Text to 3D Whole-Body Mesh Recovery, Detect Anyone and Reconstruct his 3D Humen Mesh!
- Interactive Fashion-Edit Playground: Click for Segmentation And Editing!
- Interactive Human-face Editing Playground: Click And Editing Human Face!
The Amazing Demo Preview (Continual Updating)
chatbot.mp4
Tips
- If you want to detect multiple objects in one sentence with Grounding DINO, we suggest seperating each name with
.
. An example:cat . dog . chair .
๐ฅ BLIP + Grounded-SAM: Automatic Label System
Using BLIP to generate caption, extracting tags with ChatGPT, and using Grounded-SAM for box and mask generating. Here's the demo output:
๐ฅ Grounded-SAM+OSX: Promptable 3D Whole-Body Human Mesh Recovery
Using Grounded-SAM for box and mask generating, using OSX to estimate the SMPLX parameters and reconstruct 3D whole-body (body, face and hand) human mesh. Here's a demo:
-
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:
๐ฅ 3D-Box via Segment Anything
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.
๐ก Highlight Extension Projects
- Segment Everything Everywhere All at Once Support various types of prompts and any combination of prompts.
- Computer Vision in the Wild (CVinW) Readings for those who are interested in open-set tasks in computer vision.
- Visual instruction tuning with GPT-4! Please check out the multimodal model LLaVA: [Project Page] [Paper] [Demo] [Data] [Model]
- OpenSeeD: interactive segmentation with box input to generate mask.
- Zero-Shot Anomaly Detection by Yunkang Cao
- EditAnything: ControlNet + StableDiffusion based on the SAM segmentation mask by Shanghua Gao and Pan Zhou
- IEA: Image Editing Anything by Zhengcong Fei
- SAM-MMRorate: Combining Rotated Object Detector and SAM by Qingyun Li and Xue Yang
- Awesome-Anything by Gongfan Fang
- Prompt-Segment-Anything by Rockey
- WebUI for Segment-Anything and Grounded-SAM by Chengsong Zhang
- Inpainting Anything: Inpaint Anything with SAM + Inpainting models by Tao Yu
- Grounded Segment Anything From Objects to Parts: Combining Segment-Anything with VLPart & GLIP & Visual ChatGPT by Peize Sun and Shoufa Chen
- Narapi-SAM: Integration of Segment Anything into Narapi (A nice viewer for SAM) by MIC-DKFZ
- Grounded Segment Anything Colab by camenduru
- Optical Character Recognition with Segment Anything by Zhenhua Yang
- Transform Image into Unique Paragraph with ChatGPT, BLIP2, OFA, GRIT, Segment Anything, ControlNet by showlab
- Lang-Segment-Anything: Another awesome demo for combining GroundingDINO with Segment-Anything by Luca Medeiros
๐ฅณ ๐ Playground: Integrate SAM and OpenMMLab!- 3D-object via Segment Anything by Yukang Chen
- Image2Paragraph: Transform Image Into Unique Paragraph by Show Lab
- Zero-shot Scene Graph Generate with Grounded-SAM by JackWhite-rwx
- CLIP Surgery for Better Explainability with Enhancement in Open-Vocabulary Tasks by Eli-YiLi
- Panoptic-Segment-Anything: Zero-shot panoptic segmentation using SAM by segments-ai
- Caption-Anything: Generates Descriptive Captions for Any Object within an Image by Teng Wang
- Segment-Anything-3D: Transferring Segmentation Information of 2D Images to 3D Space by Yunhan Yang
- Expediting SAM without Fine-tuning by Weicong Liang and Yuhui Yuan
- Semantic Segment Anything: Providing Rich Sementic Category Annotations for SAM by Jiaqi Chen and Zeyu Yang and Li Zhang
๐ Notebook Demo
See our notebook file as an example.
๐ ๏ธ Installation
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.
Install with Docker
Open one terminal:
make run
That's it.
If you would like to allow visualization across docker container, open another terminal and type:
xhost +
Install without Docker
Install Segment Anything:
python -m pip install -e segment_anything
Install Grounding DINO:
python -m pip install -e GroundingDINO
Install diffusers:
pip install --upgrade diffusers[torch]
Install osx:
git submodule update --init --recursive
cd grounded-sam-osx && bash install.sh
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
๐ Run Grounding DINO Demo
- Download the checkpoint for Grounding Dino:
cd Grounded-Segment-Anything
wget https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
- Run demo
export CUDA_VISIBLE_DEVICES=0
python grounding_dino_demo.py \
--config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
--grounded_checkpoint groundingdino_swint_ogc.pth \
--input_image assets/demo1.jpg \
--output_dir "outputs" \
--box_threshold 0.3 \
--text_threshold 0.25 \
--text_prompt "bear" \
--device "cuda"
- The model prediction visualization will be saved in
output_dir
as follow:
๐โโ๏ธ Run Grounded-Segment-Anything Demo
- Download the checkpoint for Segment Anything and Grounding Dino:
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
- Run Demo
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 model prediction visualization will be saved in
output_dir
as follow:
โท๏ธ Run Grounded-Segment-Anything + 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"
๐๏ธ Run Grounded-Segment-Anything + Inpainting Gradio APP
python gradio_app.py
- The gradio_app visualization as follow:
๐ค Run Grounded-Segment-Anything + BLIP Demo
It is easy to generate pseudo labels automatically as follows:
- Use BLIP (or other caption models) to generate a caption.
- Extract tags from the caption. We use ChatGPT to handle the potential complicated sentences.
- Use Grounded-Segment-Anything to generate the boxes and masks.
- Run Demo
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 your_openai_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 Script will automatically download the necessary NLTK Data.
- The pseudo labels and model prediction visualization will be saved in
output_dir
as follows:
๐ฎ Run Grounded-Segment-Anything + Whisper Demo
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
].
# Example: enable chatgpt
export CUDA_VISIBLE_DEVICES=0
export OPENAI_KEY=your_openai_key
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_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"
๐ฌ Run ChatBot Demo
Following Visual ChatGPT, we add a ChatBot for our project. Currently, it supports:
- "Descripe the image."
- "Detect the dog (and the cat) in the image."
- "Segment anything in the image."
- "Segment the dog (and the cat) in the image."
- "Help me label the image."
- "Replace the dog with a cat in the image."
To use the ChatBot:
- Install whisper if you want to use audio as input.
- Set the default model setting in the tool
Grounded_dino_sam_inpainting
. - Run Demo
export CUDA_VISIBLE_DEVICES=0
python chatbot.py
๐บ Run Grounded-Segment-Anything + OSX Demo
-
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"
- The model prediction visualization will be saved in
output_dir
as follow:
- We also support promptable 3D whole-body mesh recovery. For example, you can track someone with with a text prompt and estimate his 3D pose and shape :
![]() |
---|
A person with pink clothes |
![]() |
---|
A man with a sunglasses |
๐บ Run Grounded-Segment-Anything + VISAM Demo
-
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
๐ Acknowledgements
Citation
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}
}
@inproceedings{ShilongLiu2023GroundingDM,
title={Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection},
author={Shilong Liu and Zhaoyang Zeng and Tianhe Ren and Feng Li and Hao Zhang and Jie Yang and Chunyuan Li and Jianwei Yang and Hang Su and Jun Zhu and Lei Zhang},
year={2023}
}