From 66f3b10c167ff2451be6fb176538eff18560cd79 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Tue, 5 Nov 2024 15:25:20 -0500 Subject: [PATCH 1/9] update for breaking sam2 changes --- .../segment_anything_2_image/README.md | 18 +++++++++++++++--- .../examples/segment_anything_2_image/model.py | 6 +++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/label_studio_ml/examples/segment_anything_2_image/README.md b/label_studio_ml/examples/segment_anything_2_image/README.md index 97e97b3d..0fb7f264 100644 --- a/label_studio_ml/examples/segment_anything_2_image/README.md +++ b/label_studio_ml/examples/segment_anything_2_image/README.md @@ -133,16 +133,28 @@ cd label_studio_ml/examples/segment_anything_2_image pip install -r requirements.txt ``` -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) +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) +You should now have the following folder structure: + + | root directory + | label-studio-ml-backend + | label-studio-ml + | examples + | segment_anythng_2_image + | sam2 + | sam2 + | checkpoints 3. Then you can start the ML backend on the default port `9090`: ```bash -cd ../ -label-studio-ml start ./segment_anything_2_image +cd ~/sam2 +label-studio-ml start ../label-studio-ml-backend/label-studio-ml/examples/segment_anything_2_image ``` +Due to breaking changes from Meta, it is CRUCIAL that you run this command from the sam2 directory at your root directory. + 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). ## Running with Docker (coming soon) diff --git a/label_studio_ml/examples/segment_anything_2_image/model.py b/label_studio_ml/examples/segment_anything_2_image/model.py index 9d29c9f1..de2ec77e 100644 --- a/label_studio_ml/examples/segment_anything_2_image/model.py +++ b/label_studio_ml/examples/segment_anything_2_image/model.py @@ -1,6 +1,7 @@ import torch import numpy as np import os +import sys import pathlib from typing import List, Dict, Optional from uuid import uuid4 @@ -9,6 +10,9 @@ from label_studio_sdk.converter import brush from label_studio_sdk._extensions.label_studio_tools.core.utils.io import get_local_path from PIL import Image + +ROOT_DIR = os.getcwd() +sys.path.insert(0, ROOT_DIR) from sam2.build_sam import build_sam2 from sam2.sam2_image_predictor import SAM2ImagePredictor @@ -29,7 +33,7 @@ # build path to the model checkpoint -sam2_checkpoint = str(pathlib.Path(__file__).parent / SEGMENT_ANYTHING_2_REPO_PATH / "checkpoints" / MODEL_CHECKPOINT) +sam2_checkpoint = str(os.path.join(ROOT_DIR, "checkpoints", MODEL_CHECKPOINT)) sam2_model = build_sam2(MODEL_CONFIG, sam2_checkpoint, device=DEVICE) From 6bdc0425312ab70fac76140c23e1152cba9ac530 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Tue, 5 Nov 2024 15:27:33 -0500 Subject: [PATCH 2/9] update for breaking sam2 changes --- label_studio_ml/examples/segment_anything_2_image/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/label_studio_ml/examples/segment_anything_2_image/README.md b/label_studio_ml/examples/segment_anything_2_image/README.md index 0fb7f264..3e802039 100644 --- a/label_studio_ml/examples/segment_anything_2_image/README.md +++ b/label_studio_ml/examples/segment_anything_2_image/README.md @@ -133,7 +133,7 @@ cd label_studio_ml/examples/segment_anything_2_image pip install -r requirements.txt ``` -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) +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/segment-anything-2?tab=readme-ov-file#installation) You should now have the following folder structure: From 1bc0cabe83b9f0ca6436406d665cda15a97b3842 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Tue, 5 Nov 2024 15:31:31 -0500 Subject: [PATCH 3/9] update for new sam2 models --- label_studio_ml/examples/segment_anything_2_image/model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/label_studio_ml/examples/segment_anything_2_image/model.py b/label_studio_ml/examples/segment_anything_2_image/model.py index de2ec77e..6b8dcbe5 100644 --- a/label_studio_ml/examples/segment_anything_2_image/model.py +++ b/label_studio_ml/examples/segment_anything_2_image/model.py @@ -19,8 +19,8 @@ DEVICE = os.getenv('DEVICE', 'cuda') SEGMENT_ANYTHING_2_REPO_PATH = os.getenv('SEGMENT_ANYTHING_2_REPO_PATH', 'segment-anything-2') -MODEL_CONFIG = os.getenv('MODEL_CONFIG', 'sam2_hiera_l.yaml') -MODEL_CHECKPOINT = os.getenv('MODEL_CHECKPOINT', 'sam2_hiera_large.pt') +MODEL_CONFIG = os.getenv('MODEL_CONFIG', 'sam2.1_hiera_l.yaml') +MODEL_CHECKPOINT = os.getenv('MODEL_CHECKPOINT', 'sam2.1_hiera_large.pt') if DEVICE == 'cuda': # use bfloat16 for the entire notebook From eaf84187500c44941496c7cdf85ef0311aa8e622 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Tue, 5 Nov 2024 15:35:33 -0500 Subject: [PATCH 4/9] update for new sam2 models --- label_studio_ml/examples/segment_anything_2_image/model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/label_studio_ml/examples/segment_anything_2_image/model.py b/label_studio_ml/examples/segment_anything_2_image/model.py index 6b8dcbe5..ed0d196a 100644 --- a/label_studio_ml/examples/segment_anything_2_image/model.py +++ b/label_studio_ml/examples/segment_anything_2_image/model.py @@ -18,8 +18,8 @@ DEVICE = os.getenv('DEVICE', 'cuda') -SEGMENT_ANYTHING_2_REPO_PATH = os.getenv('SEGMENT_ANYTHING_2_REPO_PATH', 'segment-anything-2') -MODEL_CONFIG = os.getenv('MODEL_CONFIG', 'sam2.1_hiera_l.yaml') +SSEGMENT_ANYTHING_2_REPO_PATH = os.getenv('SEGMENT_ANYTHING_2_REPO_PATH', 'sam2') +MODEL_CONFIG = os.getenv('MODEL_CONFIG', 'configs/sam2.1/sam2.1_hiera_l.yaml') MODEL_CHECKPOINT = os.getenv('MODEL_CHECKPOINT', 'sam2.1_hiera_large.pt') if DEVICE == 'cuda': From 6752582c9a4c9f986044bc303dc3e251ab8fe9f6 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Tue, 5 Nov 2024 15:37:53 -0500 Subject: [PATCH 5/9] update for new sam2 models --- label_studio_ml/examples/segment_anything_2_image/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/label_studio_ml/examples/segment_anything_2_image/README.md b/label_studio_ml/examples/segment_anything_2_image/README.md index 3e802039..a50e5817 100644 --- a/label_studio_ml/examples/segment_anything_2_image/README.md +++ b/label_studio_ml/examples/segment_anything_2_image/README.md @@ -133,8 +133,7 @@ cd label_studio_ml/examples/segment_anything_2_image pip install -r requirements.txt ``` -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/segment-anything-2?tab=readme-ov-file#installation) - +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) You should now have the following folder structure: | root directory From 438760b79ec98c52c2fd2326c375284bf2f68afd Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 8 Nov 2024 09:17:04 -0500 Subject: [PATCH 6/9] add link to breaking change --- label_studio_ml/examples/segment_anything_2_image/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/label_studio_ml/examples/segment_anything_2_image/README.md b/label_studio_ml/examples/segment_anything_2_image/README.md index a50e5817..f151e35d 100644 --- a/label_studio_ml/examples/segment_anything_2_image/README.md +++ b/label_studio_ml/examples/segment_anything_2_image/README.md @@ -152,7 +152,7 @@ cd ~/sam2 label-studio-ml start ../label-studio-ml-backend/label-studio-ml/examples/segment_anything_2_image ``` -Due to breaking changes from Meta, it is CRUCIAL that you run this command from the sam2 directory at your root directory. +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. 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). From fb9faada9471cf2409483dfb6a31893134c3295f Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 8 Nov 2024 09:25:15 -0500 Subject: [PATCH 7/9] remove unused variables --- label_studio_ml/examples/segment_anything_2_image/model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/label_studio_ml/examples/segment_anything_2_image/model.py b/label_studio_ml/examples/segment_anything_2_image/model.py index ed0d196a..86118e4b 100644 --- a/label_studio_ml/examples/segment_anything_2_image/model.py +++ b/label_studio_ml/examples/segment_anything_2_image/model.py @@ -18,7 +18,6 @@ DEVICE = os.getenv('DEVICE', 'cuda') -SSEGMENT_ANYTHING_2_REPO_PATH = os.getenv('SEGMENT_ANYTHING_2_REPO_PATH', 'sam2') MODEL_CONFIG = os.getenv('MODEL_CONFIG', 'configs/sam2.1/sam2.1_hiera_l.yaml') MODEL_CHECKPOINT = os.getenv('MODEL_CHECKPOINT', 'sam2.1_hiera_large.pt') From e8a25b0d590a1d0c38f5bee645a911d2fc59bfe7 Mon Sep 17 00:00:00 2001 From: "LI, Gaoyang" <30971821+guajun@users.noreply.github.com> Date: Fri, 20 Dec 2024 10:19:46 +0800 Subject: [PATCH 8/9] Fix hyphens to underscores in sam2 branch (#665) --- label_studio_ml/examples/segment_anything_2_image/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/label_studio_ml/examples/segment_anything_2_image/README.md b/label_studio_ml/examples/segment_anything_2_image/README.md index f151e35d..9548668b 100644 --- a/label_studio_ml/examples/segment_anything_2_image/README.md +++ b/label_studio_ml/examples/segment_anything_2_image/README.md @@ -149,7 +149,7 @@ You should now have the following folder structure: ```bash cd ~/sam2 -label-studio-ml start ../label-studio-ml-backend/label-studio-ml/examples/segment_anything_2_image +label-studio-ml start ../label-studio-ml-backend/label_studio_ml/examples/segment_anything_2_image ``` 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. From 4df2829d5d4f3b1545ad5a8c2fa69c9078b8471a Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Mon, 23 Dec 2024 14:16:19 -0400 Subject: [PATCH 9/9] hyphens to underscores --- label_studio_ml/examples/segment_anything_2_image/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/label_studio_ml/examples/segment_anything_2_image/README.md b/label_studio_ml/examples/segment_anything_2_image/README.md index f151e35d..9548668b 100644 --- a/label_studio_ml/examples/segment_anything_2_image/README.md +++ b/label_studio_ml/examples/segment_anything_2_image/README.md @@ -149,7 +149,7 @@ You should now have the following folder structure: ```bash cd ~/sam2 -label-studio-ml start ../label-studio-ml-backend/label-studio-ml/examples/segment_anything_2_image +label-studio-ml start ../label-studio-ml-backend/label_studio_ml/examples/segment_anything_2_image ``` 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.