torchdrivesim._iou_utils ======================== .. py:module:: torchdrivesim._iou_utils .. autoapi-nested-parse:: torch implementation of 2d oriented box intersection author: Lanxiao Li copied from https://github.com/lilanxiao/Rotated_IoU 2020.8 MIT License Copyright (c) 2020 Lanxiao Li Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Attributes ---------- .. autoapisummary:: torchdrivesim._iou_utils.EPSILON Functions --------- .. autoapisummary:: torchdrivesim._iou_utils.precision_rounding torchdrivesim._iou_utils.box_intersection_th torchdrivesim._iou_utils.box1_in_box2 torchdrivesim._iou_utils.box_in_box_th torchdrivesim._iou_utils.build_vertices torchdrivesim._iou_utils.sort_indices torchdrivesim._iou_utils.calculate_area torchdrivesim._iou_utils.oriented_box_intersection_2d torchdrivesim._iou_utils.box2corners_th torchdrivesim._iou_utils.box2corners_with_rear_factor torchdrivesim._iou_utils.iou_differentiable_fast torchdrivesim._iou_utils.iou_non_differentiable Module Contents --------------- .. py:data:: EPSILON :value: 1e-08 .. py:function:: precision_rounding(x, n_digits=6) .. py:function:: box_intersection_th(corners1: torch.Tensor, corners2: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor] find intersection points of rectangles. Convention: if two edges are collinear, there is no intersection point. :param corners1: B, N, 4, 2 :param corners2: B, N, 4, 2 :returns: intersections: B, N, 4, 4, 2 mask: B, N, 4, 4; bool :rtype: A tuple (intersectons, mask) where .. py:function:: box1_in_box2(corners1: torch.Tensor, corners2: torch.Tensor) -> torch.Tensor check if corners of box1 lie in box2 Convention: if a corner is exactly on the edge of the other box, it's also a valid point :param corners1: (B, N, 4, 2) :param corners2: (B, N, 4, 2) :returns: (B, N, 4) Bool :rtype: c1_in_2 .. py:function:: box_in_box_th(corners1: torch.Tensor, corners2: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor] check if corners of two boxes lie in each other :param corners1: (B, N, 4, 2) :param corners2: (B, N, 4, 2) :returns: c1_in_2: (B, N, 4) Bool. i-th corner of box1 in box2 c2_in_1: (B, N, 4) Bool. i-th corner of box2 in box1 :rtype: A tuple (c1_in_2, c2_in_1) where .. py:function:: build_vertices(corners1: torch.Tensor, corners2: torch.Tensor, c1_in_2: torch.Tensor, c2_in_1: torch.Tensor, inters: torch.Tensor, mask_inter: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor] find vertices of intersection area :param corners1: (B, N, 4, 2) :param corners2: (B, N, 4, 2) :param c1_in_2: Bool, (B, N, 4) :param c2_in_1: Bool, (B, N, 4) :param inters: (B, N, 4, 4, 2) :param mask_inter: (B, N, 4, 4) :returns: vertices: (B, N, 24, 2) vertices of intersection area. only some elements are valid mask: (B, N, 24) indicates valid elements in vertices :rtype: A tuple (vertices, mask) where .. py:function:: sort_indices(vertices: torch.Tensor, mask: torch.Tensor) -> torch.Tensor :param vertices: float (B, N, 24, 2) :param mask: bool (B, N, 24) :returns: bool (B, N, 9) .. note:: why 9? the polygon has maximal 8 vertices. +1 to duplicate the first element. the index should have following structure: (A, B, C, ... , A, X, X, X) and X indicates the index of arbitary elements in the last 16 (intersections not corners) with value 0 and mask False. (cause they have zero value and zero gradient) .. py:function:: calculate_area(idx_sorted: torch.Tensor, vertices: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor] calculate area of intersection :param idx_sorted: (B, N, 9) :type idx_sorted: torch.Tensor :param vertices: (B, N, 24, 2) :type vertices: torch.Tensor :returns: Tuple of (area, selected), where area: (B, N), area of intersection selected: (B, N, 9, 2), vertices of polygon with zero padding .. py:function:: oriented_box_intersection_2d(corners1: torch.Tensor, corners2: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor] Calculates the intersection area of 2D rectangles. :param corners1: (B, N, 4, 2) :param corners2: (B, N, 4, 2) :returns: area: (B, N), area of intersection selected: (B, N, 9, 2), vertices of polygon with zero padding :rtype: Tuple of (area, selected) .. py:function:: box2corners_th(box: torch.Tensor) -> torch.Tensor convert box coordinate to corners :param box: (B, N, 5) with x, y, w, h, alpha :returns: (B, N, 4, 2) corners .. py:function:: box2corners_with_rear_factor(box: torch.Tensor, rear_factor: float = 1) -> torch.Tensor Convert bounding box coordinates to corners. The returned corners are starting from the rear of the bounding box up to a rear factor. If the rear factor is 1, then the whole bounding box is returned. :param box: (B, N, 5) with x, y, w, h, alpha :param rear_factor: The relative amount of the bounding box will be preserved starting from the rear corners and up to rear_factor * w. If the factor is 1, then the whole bounding box is preserved. :returns: (B, N, 4, 2) corners .. py:function:: iou_differentiable_fast(box1: torch.Tensor, box2: torch.Tensor) -> torch.Tensor Calculates the differentiable (approx.) IOU between two sets of bounding boxes. B is the batch size and N is the number of bounding boxes. A bounding box is described by 5 values in this order: (x, y, w, h, alpha). This method uses the shoelace formula (https://en.wikipedia.org/wiki/Shoelace_formula) to calculate the area of a convex polygon (the overlapping area of two rectangles). It offers fast computation at the expense of some IoU accuracy. :param box1: (B, N, 5) :param box2: (B, N, 5) :returns: (B, N) :rtype: iou .. py:function:: iou_non_differentiable(boxes: torch.Tensor) -> torch.Tensor :param boxes: (N, 5) :returns: (N, N) :rtype: iou