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:
🍇 [arXiv Paper] 🌹[Try the Colab Demo] 🌻 [Try Huggingface Demo] 🍄 [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
Running with Python (same as demo but you can run it anywhere after installing GroundingDINO)
from groundingdino.util.inference import load_model, load_image, predict, annotate
import cv2
model = load_model("GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py", "./groundingdino_swint_ogc.pth")
IMAGE_PATH = "assets/demo1.jpg"
TEXT_PROMPT = "bear."
BOX_THRESHOLD = 0.35
TEXT_THRESHOLD = 0.25
image_source, image = load_image(IMAGE_PATH)
boxes, logits, phrases = predict(
model=model,
image=image,
caption=TEXT_PROMPT,
box_threshold=BOX_THRESHOLD,
text_threshold=TEXT_THRESHOLD
)
annotated_frame = annotate(image_source=image_source, boxes=boxes, logits=logits, phrases=phrases)
cv2.imwrite("annotated_image.jpg", annotated_frame)