| 20 | |
| 21 | |
| 22 | class SAM2AutomaticMaskGenerator: |
| 23 | |
| 24 | def __init__( |
| 25 | self, |
| 26 | model: SAM2Base, |
| 27 | points_per_side: Optional[int] = 32, |
| 28 | points_per_batch: int = 64, |
| 29 | pred_iou_thresh: float = 0.8, |
| 30 | stability_score_thresh: float = 0.95, |
| 31 | stability_score_offset: float = 1.0, |
| 32 | mask_threshold: float = 0.0, |
| 33 | box_nms_thresh: float = 0.7, |
| 34 | crop_n_layers: int = 0, |
| 35 | crop_nms_thresh: float = 0.7, |
| 36 | crop_overlap_ratio: float = 512 / 1500, |
| 37 | crop_n_points_downscale_factor: int = 1, |
| 38 | point_grids: Optional[List[np.ndarray]] = None, |
| 39 | min_mask_region_area: int = 0, |
| 40 | output_mode: str = "binary_mask", |
| 41 | use_m2m: bool = False, |
| 42 | multimask_output: bool = True, |
| 43 | **kwargs, |
| 44 | ) -> None: |
| 45 | """ |
| 46 | Using a SAM 2 model, generates masks for the entire image. |
| 47 | Generates a grid of point prompts over the image, then filters |
| 48 | low quality and duplicate masks. The default settings are chosen |
| 49 | for SAM 2 with a HieraL backbone. |
| 50 | |
| 51 | Arguments: |
| 52 | model (Sam): The SAM 2 model to use for mask prediction. |
| 53 | points_per_side (int or None): The number of points to be sampled |
| 54 | along one side of the image. The total number of points is |
| 55 | points_per_side**2. If None, 'point_grids' must provide explicit |
| 56 | point sampling. |
| 57 | points_per_batch (int): Sets the number of points run simultaneously |
| 58 | by the model. Higher numbers may be faster but use more GPU memory. |
| 59 | pred_iou_thresh (float): A filtering threshold in [0,1], using the |
| 60 | model's predicted mask quality. |
| 61 | stability_score_thresh (float): A filtering threshold in [0,1], using |
| 62 | the stability of the mask under changes to the cutoff used to binarize |
| 63 | the model's mask predictions. |
| 64 | stability_score_offset (float): The amount to shift the cutoff when |
| 65 | calculated the stability score. |
| 66 | mask_threshold (float): Threshold for binarizing the mask logits |
| 67 | box_nms_thresh (float): The box IoU cutoff used by non-maximal |
| 68 | suppression to filter duplicate masks. |
| 69 | crop_n_layers (int): If >0, mask prediction will be run again on |
| 70 | crops of the image. Sets the number of layers to run, where each |
| 71 | layer has 2**i_layer number of image crops. |
| 72 | crop_nms_thresh (float): The box IoU cutoff used by non-maximal |
| 73 | suppression to filter duplicate masks between different crops. |
| 74 | crop_overlap_ratio (float): Sets the degree to which crops overlap. |
| 75 | In the first crop layer, crops will overlap by this fraction of |
| 76 | the image length. Later layers with more crops scale down this overlap. |
| 77 | crop_n_points_downscale_factor (int): The number of points-per-side |
| 78 | sampled in layer n is scaled down by crop_n_points_downscale_factor**n. |
| 79 | point_grids (list(np.ndarray) or None): A list over explicit grids |
nothing calls this directly
no outgoing calls
no test coverage detected