SAM2 detector
SAM2_detector
¶
Attributes¶
Classes¶
SAMV2Detector
¶
Bases: Detector
Source code in tree_detection_framework/detection/SAM2_detector.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
Methods:¶
__init__(device=DEFAULT_DEVICE, sam2_checkpoint=Path(CHECKPOINTS_FOLDER, 'sam2.1_hiera_large.pt'), model_cfg='configs/sam2.1/sam2.1_hiera_l.yaml', postprocessors=None, score_metric='predicted_iou', points_per_side=64, points_per_batch=128, pred_iou_thresh=0.5, stability_score_thresh=0.7, stability_score_offset=0.7, crop_n_layers=1, box_nms_thresh=1, crop_n_points_downscale_factor=2, min_mask_region_area=25.0, use_m2m=True)
¶
Create a SAM2 detector. Args: device (torch.device): Device to run the model on. sam2_checkpoint (Path): Path to the SAM2 checkpoint. model_cfg (str): Path to the SAM2 model config. postprocessors (list, optional): See docstring for Detector class. Defaults to None. score_metric (str): Metric to use as prediction score. Either "stability_score" or "predicted_iou". Defaults to "predicted_iou". points_per_side (int): Number of points sampled along each side of the image. points_per_batch (int): Number of points processed simultaneously. pred_iou_thresh (float): Threshold for filtering masks by predicted IoU. stability_score_thresh (float): Threshold for filtering masks by stability score. stability_score_offset (float): Offset applied when computing stability score. crop_n_layers (int): Number of crop layers to use. box_nms_thresh (float): NMS threshold for bounding boxes. crop_n_points_downscale_factor (int): Point downscale factor per crop layer. min_mask_region_area (float): Minimum area (pixels) for mask regions. use_m2m (bool): Whether to use mask-to-mask refinement.
Source code in tree_detection_framework/detection/SAM2_detector.py
call_predict(batch)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Tensor
|
4 dims Tensor with the first dimension having number of images in the batch |
required |
Returns:
| Type | Description |
|---|---|
|
masks List[List[Dict]]: list of dictionaries for each mask in the batch |
Source code in tree_detection_framework/detection/SAM2_detector.py
predict_batch(batch)
¶
Get predictions for a batch of images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
defaultDict
|
A batch from the dataloader |
required |
Returns:
| Name | Type | Description |
|---|---|---|
all_geometries |
List[List[MultiPolygon]]
|
A list of predictions one per image in the batch. The predictions for each image are a list of shapely objects. |
all_data_dicts |
Union[None, List[dict]]
|
Predicted scores and classes |