pipert.utils.structures.boxes

Module Contents

Classes

BoxMode

Enum of different ways to represent a box.

Boxes

This structure stores a list of boxes as a Nx4 torch.Tensor.

Functions

pairwise_iou(boxes1: pipert.utils.structures.boxes.Boxes, boxes2: pipert.utils.structures.boxes.Boxes) → torch.Tensor

Given two lists of boxes of size N and M,

matched_boxlist_iou(boxes1: pipert.utils.structures.boxes.Boxes, boxes2: pipert.utils.structures.boxes.Boxes) → torch.Tensor

Compute pairwise intersection over union (IOU) of two sets of matched

pipert.utils.structures.boxes._RawBoxType[source]
class pipert.utils.structures.boxes.BoxMode[source]

Bases: enum.Enum

Enum of different ways to represent a box.

Attributes:

XYXY_ABS: (x0, y0, x1, y1) in absolute floating points coordinates.

The coordinates in range [0, width or height].

XYWH_ABS: (x0, y0, w, h) in absolute floating points coordinates. XYXY_REL: (x0, y0, x1, y1) in range [0, 1]. They are relative to the size of the image. XYWH_REL: (x0, y0, w, h) in range [0, 1]. They are relative to the size of the image.

XYXY_ABS = 0[source]
XYWH_ABS = 1[source]
XYXY_REL = 2[source]
XYWH_REL = 3[source]
static convert(box: _RawBoxType, from_mode: pipert.utils.structures.boxes.BoxMode, to_mode: pipert.utils.structures.boxes.BoxMode)_RawBoxType[source]
Args:

box: can be a 4-tuple, 4-list or a Nx4 array/tensor. from_mode, to_mode (BoxMode)

Returns:

The converted box of the same type.

class pipert.utils.structures.boxes.Boxes(tensor: torch.Tensor)[source]

This structure stores a list of boxes as a Nx4 torch.Tensor. It supports some common methods about boxes (area, clip, nonempty, etc), and also behaves like a Tensor (support indexing, to(device), .device, and iteration over all boxes)

Attributes:

tensor: float matrix of Nx4.

BoxSizeType[source]
clone(self)pipert.utils.structures.boxes.Boxes[source]

Clone the Boxes.

Returns:

Boxes

to(self, device: str)pipert.utils.structures.boxes.Boxes[source]
area(self)torch.Tensor[source]

Computes the area of all the boxes.

Returns:

torch.Tensor: a vector with areas of each box.

clip(self, box_size: BoxSizeType)None[source]

Clip (in place) the boxes by limiting x coordinates to the range [0, width] and y coordinates to the range [0, height].

Args:

box_size (height, width): The clipping box’s size.

nonempty(self, threshold: int = 0)torch.Tensor[source]

Find boxes that are non-empty. A box is considered empty, if either of its side is no larger than threshold.

Returns:
Tensor:

a binary vector which represents whether each box is empty (False) or non-empty (True).

__getitem__(self, item: Union[int, slice, torch.BoolTensor])pipert.utils.structures.boxes.Boxes[source]
Returns:

Boxes: Create a new Boxes by indexing.

The following usage are allowed: 1. new_boxes = boxes[3]: return a Boxes which contains only one box. 2. new_boxes = boxes[2:10]: return a slice of boxes. 3. new_boxes = boxes[vector], where vector is a torch.BoolTensor

with length = len(boxes). Nonzero elements in the vector will be selected.

Note that the returned Boxes might share storage with this Boxes, subject to Pytorch’s indexing semantics.

__len__(self)int[source]
__repr__(self)str[source]

Return repr(self).

inside_box(self, box_size: BoxSizeType, boundary_threshold: int = 0)torch.Tensor[source]
Args:

box_size (height, width): Size of the reference box. boundary_threshold (int): Boxes that extend beyond the reference box

boundary by more than boundary_threshold are considered “outside”.

Returns:

a binary vector, indicating whether each box is inside the reference box.

get_centers(self)torch.Tensor[source]

Returns: The box centers in a Nx2 array of (x, y).

scale(self, scale_x: float, scale_y: float)None[source]

Scale the box with horizontal and vertical scaling factors

static cat(boxes_list: List[‘Boxes’])pipert.utils.structures.boxes.Boxes[source]

Concatenates a list of Boxes into a single Boxes

Arguments:

boxes_list (list[Boxes])

Returns:

Boxes: the concatenated Boxes

property device(self)str[source]
__iter__(self)Iterator[torch.Tensor][source]

Yield a box as a Tensor of shape (4,) at a time.

pipert.utils.structures.boxes.pairwise_iou(boxes1: pipert.utils.structures.boxes.Boxes, boxes2: pipert.utils.structures.boxes.Boxes)torch.Tensor[source]

Given two lists of boxes of size N and M, compute the IoU (intersection over union) between __all__ N x M pairs of boxes. The box order must be (xmin, ymin, xmax, ymax).

Args:

boxes1,boxes2 (Boxes): two Boxes. Contains N & M boxes, respectively.

Returns:

Tensor: IoU, sized [N,M].

pipert.utils.structures.boxes.matched_boxlist_iou(boxes1: pipert.utils.structures.boxes.Boxes, boxes2: pipert.utils.structures.boxes.Boxes)torch.Tensor[source]

Compute pairwise intersection over union (IOU) of two sets of matched boxes. The box order must be (xmin, ymin, xmax, ymax). Similar to boxlist_iou, but computes only diagonal elements of the matrix Arguments:

boxes1: (Boxes) bounding boxes, sized [N,4]. boxes2: (Boxes) bounding boxes, sized [N,4].

Returns:

(tensor) iou, sized [N].