From e04263f1f89592a3578f84cacc9f75d9d29361f8 Mon Sep 17 00:00:00 2001 From: Nathan Hughes Date: Sat, 28 Mar 2026 19:56:33 +0000 Subject: [PATCH 1/3] mirror c++ code settings --- .../semantic_inference/models/wrappers.py | 61 ++++++++----------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/semantic_inference/python/semantic_inference/models/wrappers.py b/semantic_inference/python/semantic_inference/models/wrappers.py index 2a4c237..70ac885 100644 --- a/semantic_inference/python/semantic_inference/models/wrappers.py +++ b/semantic_inference/python/semantic_inference/models/wrappers.py @@ -31,6 +31,7 @@ import dataclasses import os +import pathlib import cv2 import einops @@ -41,17 +42,15 @@ from spark_config import Config, register_config from torchvision.ops import box_convert -from semantic_inference import root_path +import semantic_inference.misc as misc def models_path(): - """Get path to pre-trained weight storage.""" - return root_path().parent.parent / "models" - - -def path_to_dot_semantic_inference(): """Get path to ~/.semantic_inference directory.""" - return os.getenv("HOME") + "/.semantic_inference" + model_dir = os.getenv("SEMANTIC_INFERENCE_MODEL_DIR") + mpath = pathlib.Path(model_dir or "~/.semantic_inference").expanduser().absolute() + misc.Logger.error(f"Using model path: {mpath}") + return mpath class FastSAMSegmentation(nn.Module): @@ -354,14 +353,11 @@ class YoloInstanceSegmenterBase(nn.Module): def __init__(self, config): """Store shared configuration.""" + import ultralytics super().__init__() + + ultralytics.settings.update({"weights_dir": str(models_path())}) self.config = config - self.confidence_threshold = config.confidence_threshold - self.min_segmentation_size = config.min_segmentation_size - self.overlap_merge_iou = config.overlap_merge_iou - self.model_weights = os.path.join( - path_to_dot_semantic_inference(), f"{self.config.model_name}" - ) def eval(self): """Override eval to avoid issues with yolo model.""" @@ -376,12 +372,12 @@ def forward(self, img): """Segment image.""" result = self.model( img, - conf=self.confidence_threshold, + conf=self.config.confidence_threshold, imgsz=(img.shape[0], img.shape[1]), # Set True to maintain original image size for masks, # required for hydra, but slow down the model retina_masks=True, - iou=self.overlap_merge_iou, + iou=self.config.overlap_merge_iou, )[0] # assume batch size 1 if result.masks is None: return None, None, None, None, (img.shape[0], img.shape[1]) @@ -392,13 +388,14 @@ def forward(self, img): confidences = result.boxes.conf.cpu() # float32 # filter out small masks - if self.min_segmentation_size > 0: + if self.config.min_segmentation_size > 0: mask_sizes = masks.sum(dim=(1, 2)) # number of pixels in each mask - size_indices = mask_sizes > self.min_segmentation_size + size_indices = mask_sizes > self.config.min_segmentation_size categories = categories[size_indices] masks = masks[size_indices] boxes = boxes[size_indices] confidences = confidences[size_indices] + return categories, masks, boxes, confidences, (masks.shape[1], masks.shape[2]) @@ -422,10 +419,10 @@ class YolosegInstanceSegmenterWrapper(YoloInstanceSegmenterBase): def __init__(self, config): """Load Yolov11 model.""" - from ultralytics import YOLO + import ultralytics super().__init__(config) - self.model = YOLO(self.model_weights) + self.model = ultralytics.YOLO(self.config.model_name) @classmethod def construct(cls, **kwargs): @@ -449,15 +446,13 @@ class YoloeInstanceSegmenterWrapper(YoloInstanceSegmenterBase): """Yoloe instance segmentation wrapper.""" def __init__(self, config): - from ultralytics import YOLOE + import ultralytics if not config.text_prompt: - raise ValueError( - "text_prompt must be provided for YoloeInstanceSegmenterWrapper" - ) + raise ValueError("text_prompt is required!") super().__init__(config) - self.model = YOLOE(self.model_weights) + self.model = ultralytics.YOLOE(self.config.model_name) self.model.set_classes(self.config.text_prompt) @@ -496,19 +491,15 @@ def __init__(self, config): self.erosion_kernel_size = config.erosion_kernel_size # hydra config pkg: only need relative path to the pkg installation dir - sam2_model_config_path = os.path.join( - "configs/sam2.1", config.sam2_model_config - ) - sam2_checkpoint_path = os.path.join( - path_to_dot_semantic_inference(), config.sam2_checkpoint + sam2_model_config_path = ( + pathlib.Path("configs") / "sam2.1" / config.sam2_model_config ) - grounding_dino_config_path = os.path.join( - path_to_dot_semantic_inference(), - "gdsam2_config", - config.grounding_dino_config, + sam2_checkpoint_path = models_path() / config.sam2_checkpoint + grounding_dino_config_path = ( + models_path() / "gdsam2_config" / config.grounding_dino_config ) - grounding_dino_checkpoint_path = os.path.join( - path_to_dot_semantic_inference(), config.grounding_dino_checkpoint + grounding_dino_checkpoint_path = ( + models_path() / config.grounding_dino_checkpoint ) # build SAM2 image predictor From c87ce2a5580802d36011f21a0fc57b79b3f09a4f Mon Sep 17 00:00:00 2001 From: Nathan Hughes Date: Sat, 28 Mar 2026 20:00:43 +0000 Subject: [PATCH 2/3] lower log level --- semantic_inference/python/semantic_inference/models/wrappers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semantic_inference/python/semantic_inference/models/wrappers.py b/semantic_inference/python/semantic_inference/models/wrappers.py index 70ac885..22dacc0 100644 --- a/semantic_inference/python/semantic_inference/models/wrappers.py +++ b/semantic_inference/python/semantic_inference/models/wrappers.py @@ -49,7 +49,7 @@ def models_path(): """Get path to ~/.semantic_inference directory.""" model_dir = os.getenv("SEMANTIC_INFERENCE_MODEL_DIR") mpath = pathlib.Path(model_dir or "~/.semantic_inference").expanduser().absolute() - misc.Logger.error(f"Using model path: {mpath}") + misc.Logger.debug(f"Using model path: {mpath}") return mpath From 8ff094dfa02a9073cca4638edaf3feee1aaff2d9 Mon Sep 17 00:00:00 2001 From: Nathan Hughes Date: Sat, 28 Mar 2026 20:01:17 +0000 Subject: [PATCH 3/3] fix formatting --- semantic_inference/python/semantic_inference/models/wrappers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/semantic_inference/python/semantic_inference/models/wrappers.py b/semantic_inference/python/semantic_inference/models/wrappers.py index 22dacc0..55e7be4 100644 --- a/semantic_inference/python/semantic_inference/models/wrappers.py +++ b/semantic_inference/python/semantic_inference/models/wrappers.py @@ -354,6 +354,7 @@ class YoloInstanceSegmenterBase(nn.Module): def __init__(self, config): """Store shared configuration.""" import ultralytics + super().__init__() ultralytics.settings.update({"weights_dir": str(models_path())})