SAM3ImageSegmenter#
- class rapidtools.processing.SAM3ImageSegmenter(prompt: str | list[str] = '', model_id: str = 'facebook/sam3', device: str = 'auto', load_in_4bit: bool = True, batch_size: int = 4, threshold: float = 0.5, mask_threshold: float = 0.5, image_filter: Callable[[ImageAsset], bool] | None = None, cancel_event: Event | None = None, max_mask_side: int | None = None, model: SAM3Inference | None = None)[source]#
Bases:
objectPipeline component that uses local SAM 3 to segment images attached to assets.
This segmenter gathers filtered images across all
PhysicalAssetobjects, batches them together (to maximize GPU utilization without causing OOM errors), and stores the resulting segmentation masks directly in the corresponding asset’s attributes under the following keys:'sam3_masks':{image_id: masks}wheremasksis the per-image mask stack returned by the model (typically an(N, H, W)boolean array).'sam3_bounding_boxes':{image_id: boxes}(only when the model returns boxes).'sam3_scores':{image_id: scores}, one confidence per mask (only when the model reports scores).'ai_model_used': The Hugging Face model ID that was used.
- Parameters:
prompt (str | list[str], optional) – The text prompt (or list of prompts) to guide the segmentation (e.g.,
'building'or['building', 'tree']). Lists are joined into a single period-separated prompt. Defaults to''.model_id (str, optional) – The Hugging Face repository ID for the SAM 3 model. Defaults to
'facebook/sam3'.device (str, optional) – The compute device to use (
'cuda','cpu','auto'). Defaults to'auto'.load_in_4bit (bool, optional) – Whether to load the model using 4-bit quantization. Defaults to
True.batch_size (int, optional) – Number of images to process simultaneously across assets. Defaults to 4.
threshold (float, optional) – Confidence threshold for predictions. Defaults to 0.5.
mask_threshold (float, optional) – Threshold for binarizing the masks. Defaults to 0.5.
image_filter (Callable[[ImageAsset], bool] | None, optional) – A function that takes an
ImageAssetand returnsTrueif the image should be segmented. IfNone, all downloaded images attached to the asset are processed. Defaults toNone.cancel_event (Event | None, optional) – Cooperative cancellation flag. When set, the segmenter raises
OperationCanceledbefore starting the next batch. Defaults toNone.max_mask_side (int | None, optional) – Store the masks of images larger than this many pixels on their longer side at a reduced size (see
run_inference()), which keeps memory in check for crops of centimeter-resolution drone orthomosaics.None(default) stores masks at the image size.model (SAM3Inference | None, optional) – An already loaded SAM 3 to use instead of loading another copy (
model_id,deviceandload_in_4bitare then ignored); one copy fits an 8 GB GPU where two may not. Defaults toNone.
Example
Segment only the aerial crops of each asset:
>>> from rapidtools.processing import SAM3ImageSegmenter >>> segmenter = SAM3ImageSegmenter( ... prompt='building', ... batch_size=8, ... image_filter=lambda img: img.id.endswith('_aerial'), ... ) >>> collection = segmenter(my_collection) >>> masks = collection['bldg_01'].attributes['sam3_masks'] >>> masks['bldg_01_aerial'].shape # (num_instances, height, width) (2, 512, 512)