pipert.utils.structures.masks

Module Contents

Classes

PolygonMasks

This class stores the segmentation masks for all objects in one image, in the form of polygons.

Functions

polygons_to_bitmask(polygons: List[np.ndarray], height: int, width: int) → numpy.ndarray

Args:

rasterize_polygons_within_box(polygons: List[np.ndarray], box: numpy.ndarray, mask_size: int) → torch.Tensor

Rasterize the polygons into a mask image and

pipert.utils.structures.masks.polygons_to_bitmask(polygons: List[np.ndarray], height: int, width: int)numpy.ndarray[source]
Args:

polygons (list[ndarray]): each array has shape (Nx2,) height, width (int)

Returns:

ndarray: a bool mask of shape (height, width)

pipert.utils.structures.masks.rasterize_polygons_within_box(polygons: List[np.ndarray], box: numpy.ndarray, mask_size: int)torch.Tensor[source]

Rasterize the polygons into a mask image and crop the mask content in the given box. The cropped mask is resized to (mask_size, mask_size).

This function is used when generating training targets for mask head in Mask R-CNN. Given original ground-truth masks for an image, new ground-truth mask training targets in the size of mask_size x mask_size must be provided for each predicted box. This function will be called to produce such targets.

Args:

polygons (list[ndarray[float]]): a list of polygons, which represents an instance. box: 4-element numpy array mask_size (int):

Returns:

Tensor: BoolTensor of shape (mask_size, mask_size)

class pipert.utils.structures.masks.PolygonMasks(polygons: List[List[Union[torch.Tensor, np.ndarray]]])[source]

This class stores the segmentation masks for all objects in one image, in the form of polygons.

Attributes:

polygons: list[list[ndarray]]. Each ndarray is a float64 vector representing a polygon.

to(self, *args: Any, **kwargs: Any)pipert.utils.structures.masks.PolygonMasks[source]
get_bounding_boxes(self)pipert.utils.structures.boxes.Boxes[source]

Returns: Boxes: tight bounding boxes around polygon masks.

nonempty(self)torch.Tensor[source]

Find masks that are non-empty.

Returns:
Tensor:

a BoolTensor which represents whether each mask is empty (False) or not (True).

__getitem__(self, item: Union[int, slice, List[int], torch.BoolTensor])pipert.utils.structures.masks.PolygonMasks[source]

Support indexing over the instances and return a PolygonMasks object. item can be:

  1. An integer. It will return an object with only one instance.

  2. A slice. It will return an object with the selected instances.

  3. A list[int]. It will return an object with the selected instances, correpsonding to the indices in the list.

  4. A vector mask of type BoolTensor, whose length is num_instances. It will return an object with the instances whose mask is nonzero.

__iter__(self)Iterator[List[torch.Tensor]][source]

Yields: list[ndarray]: the polygons for one instance. Each Tensor is a float64 vector representing a polygon.

__repr__(self)str[source]

Return repr(self).

__len__(self)int[source]
crop_and_resize(self, boxes: torch.Tensor, mask_size: int)torch.Tensor[source]

Crop each mask by the given box, and resize results to (mask_size, mask_size). This can be used to prepare training targets for Mask R-CNN.

Args:

boxes (Tensor): Nx4 tensor storing the boxes for each mask mask_size (int): the size of the rasterized mask.

Returns:

Tensor: A bool tensor of shape (N, mask_size, mask_size), where N is the number of predicted boxes for this image.