MCPcopy Create free account
hub / github.com/ImprintLab/Medical-SAM2 / SAM2AutomaticMaskGenerator

Class SAM2AutomaticMaskGenerator

sam2_train/automatic_mask_generator.py:36–434  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected