Segment Anything 2 in Docker. A simple, easy to use Docker image for Meta's SAM2 with GUI support for displaying figures, images, and masks. Built on top of the SAM2 repo: https://github.com/facebookresearch/segment-anything-2
π° New: We have a ROS Noetic supported image in the ROS Noetic branch!
This quickstart assumes you have access to an NVIDIA GPU. You should have installed the NVIDIA drivers and CUDA toolkit for your GPU beforehand. Also, make sure to install Docker here.
First, let's install the NVIDIA Container Toolkit:
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
&& curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - \
&& curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
sudo apt-get install -y nvidia-docker2
sudo systemctl restart docker
To get the SAM2 Docker image up and running, you can run (for NVIDIA GPUs that support at least CUDA 12.6)
sudo usermod -aG docker $USER
newgrp docker
docker run -it -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY --gpus all peasant98/sam2:latest bash
We have a CUDA 12.1 docker image too, which can be run as follows:
docker run -it -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY --gpus all peasant98/sam2:cuda-12.1 bash
From this shell, you can run SAM2, as well as display plots and images.
To check SAM2 is working within the container, we have an example in examples/image_predictor.py
to test the image mask generation. To run:
# mount this repo, which is assumed to be in the current directory
docker run -it -v /tmp/.X11-unix:/tmp/.X11-unix -v `pwd`/SAM2-Docker:/home/user/SAM2-Docker -e DISPLAY=$DISPLAY --gpus all peasant98/sam2:cuda-12.1 bash
# in the container!
cd SAM2-Docker/
python3 examples/image_predictor.py
To build and run the Dockerfile:
docker build -t sam2:latest .
And you can run as:
docker run -it -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY --gpus all sam2:latest bash
Example of running Python code to display masks: