From d8974e348142cc83492d33ede0bca7d91a9af7b0 Mon Sep 17 00:00:00 2001 From: cvhub Date: Tue, 21 Jul 2026 09:48:14 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20fix(update=5Fchecker):=20dec?= =?UTF-8?q?lare=20packaging=20runtime=20dependency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index a0fcec7..68198e8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,6 +29,7 @@ dependencies = [ "pydantic>=2.12.0", "openai>=1.99.1", "requests>=2.26.0", + "packaging>=23.2", "opencv-python-headless>=4.11.0", "loguru", "numpy", From 1b19c1578af65c9812c4f0bc53beb79ae2234355 Mon Sep 17 00:00:00 2001 From: cvhub Date: Tue, 21 Jul 2026 09:49:34 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=94=84=20ci(openapi):=20validate=20de?= =?UTF-8?q?pendency=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/check-openapi.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/check-openapi.yml b/.github/workflows/check-openapi.yml index 6cbbeb1..6fd00bb 100644 --- a/.github/workflows/check-openapi.yml +++ b/.github/workflows/check-openapi.yml @@ -6,6 +6,7 @@ on: - "app/api/**" - "app/schemas/**" - "app/main.py" + - "pyproject.toml" - "docs/openapi.json" - "scripts/export_openapi.py" push: @@ -15,6 +16,7 @@ on: - "app/api/**" - "app/schemas/**" - "app/main.py" + - "pyproject.toml" - "docs/openapi.json" - "scripts/export_openapi.py" workflow_dispatch: From 70599dc41e1d8fcb997636f0032d8403631bdc9b Mon Sep 17 00:00:00 2001 From: cvhub Date: Tue, 21 Jul 2026 23:53:39 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=90=9B=20fix(segment=5Fanything=5F2):?= =?UTF-8?q?=20correct=20device=20handling=20and=20concurrent=20prompt=20in?= =?UTF-8?q?ference=20(#39)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/segment_anything_2.py | 95 +++++++++++++++++++------------- 1 file changed, 58 insertions(+), 37 deletions(-) diff --git a/app/models/segment_anything_2.py b/app/models/segment_anything_2.py index e20202a..80f26c7 100644 --- a/app/models/segment_anything_2.py +++ b/app/models/segment_anything_2.py @@ -1,8 +1,9 @@ from collections import OrderedDict +from contextlib import nullcontext from hashlib import md5 from loguru import logger from PIL import Image -from typing import Any, Dict, List +from typing import Any, Dict, List, Optional import threading import numpy as np @@ -60,24 +61,25 @@ def load(self): "configs/sam2.1", self.params.get("model_cfg") + ".yaml" ) checkpoint_path = self.params.get("model_path") - device = self.params.get("device", "cuda") + requested_device = torch.device(self.params.get("device", "cuda")) # select the device for computation - if torch.cuda.is_available() and "cuda" in device: - self.device = torch.device("cuda") - elif torch.backends.mps.is_available() and "mps" in device: - self.device = torch.device("mps") + if torch.cuda.is_available() and requested_device.type == "cuda": + self.device = requested_device + elif ( + torch.backends.mps.is_available() + and requested_device.type == "mps" + ): + self.device = requested_device else: self.device = torch.device("cpu") if self.device.type == "cuda": - # use bfloat16 for the entire notebook - torch.autocast("cuda", dtype=torch.bfloat16).__enter__() # turn on tfloat32 for Ampere GPUs (https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices) - if torch.cuda.get_device_properties(0).major >= 8: + if torch.cuda.get_device_properties(self.device).major >= 8: torch.backends.cuda.matmul.allow_tf32 = True torch.backends.cudnn.allow_tf32 = True - elif device.type == "mps": + elif self.device.type == "mps": print( "\nSupport for MPS devices is preliminary. SAM 2 is trained with CUDA and might " "give numerically different outputs and sometimes degraded performance on MPS. " @@ -89,6 +91,7 @@ def load(self): ) sam2_model = build_sam2(model_cfg, checkpoint_path, device=self.device) self.predictor = SAM2ImagePredictor(sam2_model) + self.predict_lock = threading.Lock() cache_size = self.params.get("cache_size", 10) self.image_embedding_cache = LRUCache(maxsize=cache_size) @@ -114,7 +117,13 @@ def predict( logger.warning("No prompts provided") return {"shapes": [], "description": "", "replace": False} - return self._predict_with_marks(image, marks, params) + autocast_context = ( + torch.autocast("cuda", dtype=torch.bfloat16) + if self.device.type == "cuda" + else nullcontext() + ) + with self.predict_lock, autocast_context: + return self._predict_with_marks(image, marks, params) def _predict_with_marks( self, @@ -231,10 +240,15 @@ def _predict_with_marks( if box_coords is not None: box_coords = np.array(box_coords) - if len(box_coords.shape) == 1: - box_coords = box_coords[None, :] - elif len(box_coords.shape) == 2 and box_coords.shape[0] == 1: - box_coords = box_coords[None, :] + if len(box_coords) == 1: + box_coords = box_coords[0] + elif point_coords is not None: + point_coords = np.repeat( + point_coords[None, ...], len(box_coords), axis=0 + ) + point_labels = np.repeat( + point_labels[None, ...], len(box_coords), axis=0 + ) masks, scores, _ = self.predictor.predict( point_coords=point_coords, @@ -247,34 +261,37 @@ def _predict_with_marks( logger.warning("No masks predicted") return {"shapes": [], "description": "", "replace": False} - if multimask_output: - sorted_ind = np.argsort(scores)[::-1] - masks = masks[sorted_ind] - scores = scores[sorted_ind] - best_mask = masks[0] - best_score = scores[0] - else: - if len(masks.shape) == 3: - best_mask = masks[0] - else: - best_mask = masks - best_score = scores[0] if len(scores) > 0 else 1.0 + if len(masks.shape) == 3: + masks = masks[None, ...] + if len(scores.shape) == 1: + scores = scores[None, ...] - if isinstance(best_mask, torch.Tensor): - best_mask = best_mask.cpu().numpy() - if isinstance(best_score, torch.Tensor): - best_score = best_score.cpu().item() + if len(masks.shape) != 4 or len(scores.shape) != 2: + raise ValueError( + f"Unexpected SAM2 output shapes: masks={masks.shape}, " + f"scores={scores.shape}" + ) epsilon_factor = params.get( "epsilon_factor", self.params.get("epsilon_factor", 0.001) ) - shapes = self._convert_results_to_shapes( - best_mask, - show_boxes, - show_masks, - epsilon_factor, - ) + shapes = [] + for index in range(len(masks)): + best_index = ( + int(np.argmax(scores[index])) if multimask_output else 0 + ) + best_mask = masks[index, best_index] + best_score = float(scores[index, best_index]) + shapes.extend( + self._convert_results_to_shapes( + best_mask, + show_boxes, + show_masks, + epsilon_factor, + best_score, + ) + ) return {"shapes": shapes, "description": "", "replace": False} @@ -284,6 +301,7 @@ def _convert_results_to_shapes( show_boxes: bool = False, show_masks: bool = True, epsilon_factor: float = 0.001, + score: Optional[float] = None, ) -> List[Shape]: """Convert SAM2 results to Shape objects. @@ -292,6 +310,7 @@ def _convert_results_to_shapes( show_boxes: Whether to return bounding boxes. show_masks: Whether to return masks as polygons. epsilon_factor: Factor for polygon approximation epsilon. + score: Predicted mask quality score. Returns: List of Shape objects. @@ -351,6 +370,7 @@ def _convert_results_to_shapes( label="AUTOLABEL_OBJECT", shape_type="polygon", points=[[float(p[0]), float(p[1])] for p in points], + score=score, ) shapes.append(shape) @@ -379,6 +399,7 @@ def _convert_results_to_shapes( [x_max, y_max], [x_min, y_max], ], + score=score, ) shapes.append(shape)