| 51 | |
| 52 | |
| 53 | class SemanticSamAutomaticMaskGenerator: |
| 54 | def __init__( |
| 55 | self, |
| 56 | model, |
| 57 | points_per_side: Optional[int] = 32, |
| 58 | points_per_batch: int = 200, |
| 59 | pred_iou_thresh: float = 0.88, |
| 60 | stability_score_thresh: float = 0.92, |
| 61 | stability_score_offset: float = 1.0, |
| 62 | box_nms_thresh: float = 0.7, |
| 63 | crop_n_layers: int = 0, |
| 64 | crop_nms_thresh: float = 0.7, |
| 65 | crop_overlap_ratio: float = 512 / 1500, |
| 66 | crop_n_points_downscale_factor: int = 1, |
| 67 | point_grids: Optional[List[np.ndarray]] = None, |
| 68 | min_mask_region_area: int = 10, |
| 69 | output_mode: str = "binary_mask", |
| 70 | level: list = [1, 2, 3, 4, 5, 6], |
| 71 | ) -> None: |
| 72 | """ |
| 73 | Using a SAM model, generates masks for the entire image. |
| 74 | Generates a grid of point prompts over the image, then filters |
| 75 | low quality and duplicate masks. The default settings are chosen |
| 76 | for SAM with a ViT-H backbone. |
| 77 | |
| 78 | Arguments: |
| 79 | model (Sam): The SAM model to use for mask prediction. |
| 80 | points_per_side (int or None): The number of points to be sampled |
| 81 | along one side of the image. The total number of points is |
| 82 | points_per_side**2. If None, 'point_grids' must provide explicit |
| 83 | point sampling. |
| 84 | points_per_batch (int): Sets the number of points run simultaneously |
| 85 | by the model. Higher numbers may be faster but use more GPU memory. |
| 86 | pred_iou_thresh (float): A filtering threshold in [0,1], using the |
| 87 | model's predicted mask quality. |
| 88 | stability_score_thresh (float): A filtering threshold in [0,1], using |
| 89 | the stability of the mask under changes to the cutoff used to binarize |
| 90 | the model's mask predictions. |
| 91 | stability_score_offset (float): The amount to shift the cutoff when |
| 92 | calculated the stability score. |
| 93 | box_nms_thresh (float): The box IoU cutoff used by non-maximal |
| 94 | suppression to filter duplicate masks. |
| 95 | crops_n_layers (int): If >0, mask prediction will be run again on |
| 96 | crops of the image. Sets the number of layers to run, where each |
| 97 | layer has 2**i_layer number of image crops. |
| 98 | crops_nms_thresh (float): The box IoU cutoff used by non-maximal |
| 99 | suppression to filter duplicate masks between different crops. |
| 100 | crop_overlap_ratio (float): Sets the degree to which crops overlap. |
| 101 | In the first crop layer, crops will overlap by this fraction of |
| 102 | the image length. Later layers with more crops scale down this overlap. |
| 103 | crop_n_points_downscale_factor (int): The number of points-per-side |
| 104 | sampled in layer n is scaled down by crop_n_points_downscale_factor**n. |
| 105 | point_grids (list(np.ndarray) or None): A list over explicit grids |
| 106 | of points used for sampling, normalized to [0,1]. The nth grid in the |
| 107 | list is used in the nth crop layer. Exclusive with points_per_side. |
| 108 | min_mask_region_area (int): If >0, postprocessing will be applied |
| 109 | to remove disconnected regions and holes in masks with area smaller |
| 110 | than min_mask_region_area. Requires opencv. |
no outgoing calls
no test coverage detected