Reference for ultralytics/models/sam/amg.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/sam/amg.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.sam.amg.is_box_near_crop_edge
is_box_near_crop_edge(
boxes: torch.Tensor,
crop_box: List[int],
orig_box: List[int],
atol: float = 20.0,
) -> torch.Tensor
Determines if bounding boxes are near the edge of a cropped image region using a specified tolerance.
Source code in ultralytics/models/sam/amg.py
ultralytics.models.sam.amg.batch_iterator
Yields batches of data from input arguments with specified batch size for efficient processing.
Source code in ultralytics/models/sam/amg.py
ultralytics.models.sam.amg.calculate_stability_score
calculate_stability_score(
masks: torch.Tensor, mask_threshold: float, threshold_offset: float
) -> torch.Tensor
Computes the stability score for a batch of masks.
The stability score is the IoU between binary masks obtained by thresholding the predicted mask logits at high and low values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks | Tensor | Batch of predicted mask logits. | required |
mask_threshold | float | Threshold value for creating binary masks. | required |
threshold_offset | float | Offset applied to the threshold for creating high and low binary masks. | required |
Returns:
Type | Description |
---|---|
Tensor | Stability scores for each mask in the batch. |
Notes
- One mask is always contained inside the other.
- Memory is saved by preventing unnecessary cast to torch.int64.
Examples:
>>> masks = torch.rand(10, 256, 256) # Batch of 10 masks
>>> mask_threshold = 0.5
>>> threshold_offset = 0.1
>>> stability_scores = calculate_stability_score(masks, mask_threshold, threshold_offset)
Source code in ultralytics/models/sam/amg.py
ultralytics.models.sam.amg.build_point_grid
Generate a 2D grid of evenly spaced points in the range [0,1]x[0,1] for image segmentation tasks.
Source code in ultralytics/models/sam/amg.py
ultralytics.models.sam.amg.build_all_layer_point_grids
build_all_layer_point_grids(
n_per_side: int, n_layers: int, scale_per_layer: int
) -> List[np.ndarray]
Generates point grids for multiple crop layers with varying scales and densities.
Source code in ultralytics/models/sam/amg.py
ultralytics.models.sam.amg.generate_crop_boxes
generate_crop_boxes(
im_size: Tuple[int, ...], n_layers: int, overlap_ratio: float
) -> Tuple[List[List[int]], List[int]]
Generates crop boxes of varying sizes for multi-scale image processing, with layered overlapping regions.
Source code in ultralytics/models/sam/amg.py
ultralytics.models.sam.amg.uncrop_boxes_xyxy
Uncrop bounding boxes by adding the crop box offset to their coordinates.
Source code in ultralytics/models/sam/amg.py
ultralytics.models.sam.amg.uncrop_points
Uncrop points by adding the crop box offset to their coordinates.
Source code in ultralytics/models/sam/amg.py
ultralytics.models.sam.amg.uncrop_masks
Uncrop masks by padding them to the original image size, handling coordinate transformations.
Source code in ultralytics/models/sam/amg.py
ultralytics.models.sam.amg.remove_small_regions
Removes small disconnected regions or holes in a mask based on area threshold and mode.
Source code in ultralytics/models/sam/amg.py
ultralytics.models.sam.amg.batched_mask_to_box
Calculates bounding boxes in XYXY format around binary masks, handling empty masks and various input shapes.