Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SAM2 models and AI Batch Mode #1493

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions labelme/ai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gdown

from .efficient_sam import EfficientSam
from .segment_anything2_model import SegmentAnything2Model
from .segment_anything_model import SegmentAnythingModel
from .text_to_annotation import get_rectangles_from_texts # NOQA: F401
from .text_to_annotation import get_shapes_from_annotations # NOQA: F401
Expand Down Expand Up @@ -86,11 +87,38 @@ def __init__(self):
),
)

class SAM2HieraL(SegmentAnything2Model):
name = "SegmentAnything2 (accuracy)"

def __init__(self):
super().__init__(
encoder_path=gdown.cached_download(
url="https://github.com/jakep72/labelme/releases/download/SAM2/sam2_large.encoder.onnx", # NOQA
),
decoder_path=gdown.cached_download(
url="https://github.com/jakep72/labelme/releases/download/SAM2/sam2_large.decoder.onnx" # NOQA
),
)

class SAM2HieraT(SegmentAnything2Model):
name = "SegmentAnything2 (speed)"

def __init__(self):
super().__init__(
encoder_path=gdown.cached_download(
url="https://github.com/jakep72/labelme/releases/download/SAM2/sam2_hiera_tiny.encoder.onnx" # NOQA
),
decoder_path=gdown.cached_download(
url="https://github.com/jakep72/labelme/releases/download/SAM2/sam2_hiera_tiny.decoder.onnx" # NOQA
),
)

MODELS = [
SegmentAnythingModelVitB,
SegmentAnythingModelVitL,
SegmentAnythingModelVitH,
EfficientSamVitT,
EfficientSamVitS,
SAM2HieraT,
SAM2HieraL
]
3 changes: 1 addition & 2 deletions labelme/ai/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ def _get_contour_length(contour):
contour_end = np.r_[contour[1:], contour[0:1]]
return np.linalg.norm(contour_end - contour_start, axis=1).sum()


def compute_polygon_from_mask(mask):
contours = skimage.measure.find_contours(np.pad(mask, pad_width=1))
if len(contours) == 0:
logger.warning("No contour found, so returning empty polygon.")
return np.empty((0, 2), dtype=np.float32)

contour = max(contours, key=_get_contour_length)
POLYGON_APPROX_TOLERANCE = 0.004
polygon = skimage.measure.approximate_polygon(
Expand Down
Loading