brails.processors.vlm_segmenter.segment_anything.utils.amg module

class brails.processors.vlm_segmenter.segment_anything.utils.amg.MaskData(**kwargs)

Bases: object

A structure for storing masks and their related data in batched format. Implements basic filtering and concatenation.

cat(new_stats: MaskData) None
filter(keep: Tensor) None
items() ItemsView[str, Any]
to_numpy() None
brails.processors.vlm_segmenter.segment_anything.utils.amg.area_from_rle(rle: Dict[str, Any]) int
brails.processors.vlm_segmenter.segment_anything.utils.amg.batch_iterator(batch_size: int, *args) Generator[List[Any], None, None]
brails.processors.vlm_segmenter.segment_anything.utils.amg.batched_mask_to_box(masks: Tensor) Tensor

Calculates boxes in XYXY format around masks. Return [0,0,0,0] for an empty mask. For input shape C1xC2x…xHxW, the output shape is C1xC2x…x4.

brails.processors.vlm_segmenter.segment_anything.utils.amg.box_xyxy_to_xywh(box_xyxy: Tensor) Tensor
brails.processors.vlm_segmenter.segment_anything.utils.amg.build_all_layer_point_grids(n_per_side: int, n_layers: int, scale_per_layer: int) List[ndarray]

Generates point grids for all crop layers.

brails.processors.vlm_segmenter.segment_anything.utils.amg.build_point_grid(n_per_side: int) ndarray

Generates a 2D grid of points evenly spaced in [0,1]x[0,1].

brails.processors.vlm_segmenter.segment_anything.utils.amg.calculate_stability_score(masks: Tensor, mask_threshold: float, threshold_offset: float) Tensor

Computes the stability score for a batch of masks. The stability score is the IoU between the binary masks obtained by thresholding the predicted mask logits at high and low values.

brails.processors.vlm_segmenter.segment_anything.utils.amg.coco_encode_rle(uncompressed_rle: Dict[str, Any]) Dict[str, Any]
brails.processors.vlm_segmenter.segment_anything.utils.amg.generate_crop_boxes(im_size: Tuple[int, ...], n_layers: int, overlap_ratio: float) Tuple[List[List[int]], List[int]]

Generates a list of crop boxes of different sizes. Each layer has (2**i)**2 boxes for the ith layer.

brails.processors.vlm_segmenter.segment_anything.utils.amg.is_box_near_crop_edge(boxes: Tensor, crop_box: List[int], orig_box: List[int], atol: float = 20.0) Tensor

Filter masks at the edge of a crop, but not at the edge of the original image.

brails.processors.vlm_segmenter.segment_anything.utils.amg.mask_to_rle_pytorch(tensor: Tensor) List[Dict[str, Any]]

Encodes masks to an uncompressed RLE, in the format expected by pycoco tools.

brails.processors.vlm_segmenter.segment_anything.utils.amg.remove_small_regions(mask: ndarray, area_thresh: float, mode: str) Tuple[ndarray, bool]

Removes small disconnected regions and holes in a mask. Returns the mask and an indicator of if the mask has been modified.

brails.processors.vlm_segmenter.segment_anything.utils.amg.rle_to_mask(rle: Dict[str, Any]) ndarray

Compute a binary mask from an uncompressed RLE.

brails.processors.vlm_segmenter.segment_anything.utils.amg.uncrop_boxes_xyxy(boxes: Tensor, crop_box: List[int]) Tensor
brails.processors.vlm_segmenter.segment_anything.utils.amg.uncrop_masks(masks: Tensor, crop_box: List[int], orig_h: int, orig_w: int) Tensor
brails.processors.vlm_segmenter.segment_anything.utils.amg.uncrop_points(points: Tensor, crop_box: List[int]) Tensor