HumanSignal / label-studio-ml-backend

Configs and boilerplates for Label Studio's Machine Learning backend
Apache License 2.0
545 stars 249 forks source link

Issue running SAM 2 Video tracking #645

Open vs-nth opened 1 week ago

vs-nth commented 1 week ago

Hello, I'm trying to run Sam2 with docker compose on my local system for video tracking. However, the upstream repo for Segment anything 2 updated the checkpoints to point to Sam2.1 and as a result the default configuration does not work. Here's the contents of my docker-compose.yml file.

version: "3.8"
services:
  label_studio:
    image: heartexlabs/label-studio:latest
    container_name: label_studio
    ports:
      - 8508:8080
    volumes:
      - ./mydata:/label-studio/data:rw

  segment_anything_2_video:
    container_name: segment_anything_2_video
    image: humansignal/segment_anything_2_video:v0
    build:
      context: .
      args:
        TEST_ENV: ${TEST_ENV}
    environment:
      # specify these parameters if you want to use basic auth for the model server
      - BASIC_AUTH_USER=
      - BASIC_AUTH_PASS=
      # set the log level for the model server
      - LOG_LEVEL=DEBUG
      # any other parameters that you want to pass to the model server
      - ANY=PARAMETER
      # specify the number of workers and threads for the model server
      - WORKERS=1
      - THREADS=8
      # specify the model directory (likely you don't need to change this)
      - MODEL_DIR=/data/models
      # specify device
      - DEVICE=cuda  # or 'cpu' (coming soon)
      # SAM2 model config
      - MODEL_CONFIG=sam2.1_hiera_l.yaml
      # SAM2 checkpoint
      - MODEL_CHECKPOINT=sam2.1_hiera_large.pt

      # Specify the Label Studio URL and API key to access
      # uploaded, local storage and cloud storage files.
      # Do not use 'localhost' as it does not work within Docker containers.
      # Use prefix 'http://' or 'https://' for the URL always.
      # Determine the actual IP using 'ifconfig' (Linux/Mac) or 'ipconfig' (Windows).
      - LABEL_STUDIO_URL=http://label_studio:8080
      - LABEL_STUDIO_API_KEY=
      - CUDA_VISIBLE_DEVICES=0
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    ports:
      - "9090:9090"
    volumes:
      - "./data:/data:rw"

volumes:
  mydata:
  data:

I set the checkpoint and the config to Sam2.1 and on running docker compose up, i encounter this error:

segment_anything_2_video  |   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
segment_anything_2_video  |   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
segment_anything_2_video  |   File "/app/_wsgi.py", line 33, in <module>
segment_anything_2_video  |     from model import NewModel
segment_anything_2_video  |   File "/app/model.py", line 39, in <module>
segment_anything_2_video  |     predictor = build_sam2_video_predictor(MODEL_CONFIG, sam2_checkpoint)
segment_anything_2_video  |   File "/segment-anything-2/sam2/build_sam.py", line 127, in build_sam2_video_predictor
segment_anything_2_video  |     cfg = compose(config_name=config_file, overrides=hydra_overrides)
segment_anything_2_video  |   File "/opt/conda/lib/python3.10/site-packages/hydra/compose.py", line 32, in compose
segment_anything_2_video  |     assert (
segment_anything_2_video  | AssertionError: GlobalHydra is not initialized, use @hydra.main() or call one of the hydra initialization methods first
segment_anything_2_video  | [2024-10-06 21:12:17 +0000] [7] [INFO] Worker exiting (pid: 7)

Anything obvious that i'm doing wrong?

Thank you!

FunnyPocketBook commented 1 week ago

Hey! It looks like with the update to SAM2.1 some things in the back have changed. If you just want to use SAM2 and not necessarily SAM 2.1, then you can change the following in the Dockerfile in the segment_anything_2 directory:

-RUN cd / && git clone --depth 1 --branch main --single-branch https://github.com/facebookresearch/segment-anything-2.git
+RUN cd / && git clone --branch main --single-branch https://github.com/facebookresearch/segment-anything-2.git && cd segment-anything-2 && git checkout 0f6515ae853c40420ea8e3dd250f8031bbf03023

This essentially just checks out the repo to the commit before SAM2.1 was added. Then changing the env variables MODEL_CHECKPOINT and MODEL_CONFIG back to sam2 and rebuilding the image worked for me!