Skip to content

Commit 195ece9

Browse files
fix: sam2 (HumanSignal#663)
Co-authored-by: LI, Gaoyang <[email protected]>
1 parent 57470d2 commit 195ece9

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

label_studio_ml/examples/segment_anything_2_image/README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,27 @@ cd label_studio_ml/examples/segment_anything_2_image
133133
pip install -r requirements.txt
134134
```
135135

136-
2. Download [`segment-anything-2` repo](https://github.com/facebookresearch/segment-anything-2) into the root directory. Install SegmentAnything model and download checkpoints using [the official Meta documentation](https://github.com/facebookresearch/segment-anything-2?tab=readme-ov-file#installation)
137-
136+
2. Download [`segment-anything-2` repo](https://github.com/facebookresearch/sam2) into the root directory. Install SegmentAnything model and download checkpoints using [the official Meta documentation](https://github.com/facebookresearch/sam2?tab=readme-ov-file#installation)
137+
You should now have the following folder structure:
138+
139+
| root directory
140+
| label-studio-ml-backend
141+
| label-studio-ml
142+
| examples
143+
| segment_anythng_2_image
144+
| sam2
145+
| sam2
146+
| checkpoints
138147

139148
3. Then you can start the ML backend on the default port `9090`:
140149

141150
```bash
142-
cd ../
143-
label-studio-ml start ./segment_anything_2_image
151+
cd ~/sam2
152+
label-studio-ml start ../label-studio-ml-backend/label_studio_ml/examples/segment_anything_2_image
144153
```
145154

155+
Due to breaking changes from Meta [HERE](https://github.com/facebookresearch/sam2/blob/c2ec8e14a185632b0a5d8b161928ceb50197eddc/sam2/build_sam.py#L20), it is CRUCIAL that you run this command from the sam2 directory at your root directory.
156+
146157
4. Connect running ML backend server to Label Studio: go to your project `Settings -> Machine Learning -> Add Model` and specify `http://localhost:9090` as a URL. Read more in the official [Label Studio documentation](https://labelstud.io/guide/ml#Connect-the-model-to-Label-Studio).
147158

148159
## Running with Docker (coming soon)

label_studio_ml/examples/segment_anything_2_image/model.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import torch
22
import numpy as np
33
import os
4+
import sys
45
import pathlib
56
from typing import List, Dict, Optional
67
from uuid import uuid4
@@ -9,14 +10,16 @@
910
from label_studio_sdk.converter import brush
1011
from label_studio_sdk._extensions.label_studio_tools.core.utils.io import get_local_path
1112
from PIL import Image
13+
14+
ROOT_DIR = os.getcwd()
15+
sys.path.insert(0, ROOT_DIR)
1216
from sam2.build_sam import build_sam2
1317
from sam2.sam2_image_predictor import SAM2ImagePredictor
1418

1519

1620
DEVICE = os.getenv('DEVICE', 'cuda')
17-
SEGMENT_ANYTHING_2_REPO_PATH = os.getenv('SEGMENT_ANYTHING_2_REPO_PATH', 'segment-anything-2')
18-
MODEL_CONFIG = os.getenv('MODEL_CONFIG', 'sam2_hiera_l.yaml')
19-
MODEL_CHECKPOINT = os.getenv('MODEL_CHECKPOINT', 'sam2_hiera_large.pt')
21+
MODEL_CONFIG = os.getenv('MODEL_CONFIG', 'configs/sam2.1/sam2.1_hiera_l.yaml')
22+
MODEL_CHECKPOINT = os.getenv('MODEL_CHECKPOINT', 'sam2.1_hiera_large.pt')
2023

2124
if DEVICE == 'cuda':
2225
# use bfloat16 for the entire notebook
@@ -29,7 +32,7 @@
2932

3033

3134
# build path to the model checkpoint
32-
sam2_checkpoint = str(pathlib.Path(__file__).parent / SEGMENT_ANYTHING_2_REPO_PATH / "checkpoints" / MODEL_CHECKPOINT)
35+
sam2_checkpoint = str(os.path.join(ROOT_DIR, "checkpoints", MODEL_CHECKPOINT))
3336

3437
sam2_model = build_sam2(MODEL_CONFIG, sam2_checkpoint, device=DEVICE)
3538

0 commit comments

Comments
 (0)