"vscode:/vscode.git/clone" did not exist on "ec4c35fe99e5e5e02f38d94d310615828dd3a2c8"
_bounding_box.py 3.45 KB
Newer Older
1
2
from __future__ import annotations

3
from enum import Enum
4
from typing import Any, Optional, Tuple, Union
Philip Meier's avatar
Philip Meier committed
5
6
7

import torch

8
from ._datapoint import Datapoint
Philip Meier's avatar
Philip Meier committed
9
10


11
class BoundingBoxFormat(Enum):
Philip Meier's avatar
Philip Meier committed
12
13
14
15
16
17
18
19
20
    """[BETA] Coordinate format of a bounding box.

    Available formats are

    * ``XYXY``
    * ``XYWH``
    * ``CXCYWH``
    """

21
22
23
    XYXY = "XYXY"
    XYWH = "XYWH"
    CXCYWH = "CXCYWH"
Philip Meier's avatar
Philip Meier committed
24
25


26
class BoundingBoxes(Datapoint):
Philip Meier's avatar
Philip Meier committed
27
28
29
30
31
    """[BETA] :class:`torch.Tensor` subclass for bounding boxes.

    Args:
        data: Any data that can be turned into a tensor with :func:`torch.as_tensor`.
        format (BoundingBoxFormat, str): Format of the bounding box.
Philip Meier's avatar
Philip Meier committed
32
        canvas_size (two-tuple of ints): Height and width of the corresponding image or video.
Philip Meier's avatar
Philip Meier committed
33
34
35
36
37
38
39
40
        dtype (torch.dtype, optional): Desired data type of the bounding box. If omitted, will be inferred from
            ``data``.
        device (torch.device, optional): Desired device of the bounding box. If omitted and ``data`` is a
            :class:`torch.Tensor`, the device is taken from it. Otherwise, the bounding box is constructed on the CPU.
        requires_grad (bool, optional): Whether autograd should record operations on the bounding box. If omitted and
            ``data`` is a :class:`torch.Tensor`, the value is taken from it. Otherwise, defaults to ``False``.
    """

Philip Meier's avatar
Philip Meier committed
41
    format: BoundingBoxFormat
Philip Meier's avatar
Philip Meier committed
42
    canvas_size: Tuple[int, int]
Philip Meier's avatar
Philip Meier committed
43

44
    @classmethod
45
46
47
    def _wrap(cls, tensor: torch.Tensor, *, format: Union[BoundingBoxFormat, str], canvas_size: Tuple[int, int]) -> BoundingBoxes:  # type: ignore[override]
        if isinstance(format, str):
            format = BoundingBoxFormat[format.upper()]
48
49
        bounding_boxes = tensor.as_subclass(cls)
        bounding_boxes.format = format
Philip Meier's avatar
Philip Meier committed
50
        bounding_boxes.canvas_size = canvas_size
51
        return bounding_boxes
52

53
    def __new__(
Philip Meier's avatar
Philip Meier committed
54
        cls,
55
56
57
        data: Any,
        *,
        format: Union[BoundingBoxFormat, str],
Philip Meier's avatar
Philip Meier committed
58
        canvas_size: Tuple[int, int],
59
60
        dtype: Optional[torch.dtype] = None,
        device: Optional[Union[torch.device, str, int]] = None,
61
        requires_grad: Optional[bool] = None,
62
    ) -> BoundingBoxes:
63
        tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad)
Philip Meier's avatar
Philip Meier committed
64
        return cls._wrap(tensor, format=format, canvas_size=canvas_size)
65

66
    @classmethod
67
    def wrap_like(
68
        cls,
69
        other: BoundingBoxes,
70
        tensor: torch.Tensor,
71
        *,
72
        format: Optional[Union[BoundingBoxFormat, str]] = None,
Philip Meier's avatar
Philip Meier committed
73
        canvas_size: Optional[Tuple[int, int]] = None,
74
75
    ) -> BoundingBoxes:
        """Wrap a :class:`torch.Tensor` as :class:`BoundingBoxes` from a reference.
Philip Meier's avatar
Philip Meier committed
76
77

        Args:
78
79
            other (BoundingBoxes): Reference bounding box.
            tensor (Tensor): Tensor to be wrapped as :class:`BoundingBoxes`
Philip Meier's avatar
Philip Meier committed
80
81
            format (BoundingBoxFormat, str, optional): Format of the bounding box.  If omitted, it is taken from the
                reference.
Philip Meier's avatar
Philip Meier committed
82
            canvas_size (two-tuple of ints, optional): Height and width of the corresponding image or video. If
Philip Meier's avatar
Philip Meier committed
83
84
85
                omitted, it is taken from the reference.

        """
86
87
        return cls._wrap(
            tensor,
88
            format=format if format is not None else other.format,
Philip Meier's avatar
Philip Meier committed
89
            canvas_size=canvas_size if canvas_size is not None else other.canvas_size,
90
91
        )

92
    def __repr__(self, *, tensor_contents: Any = None) -> str:  # type: ignore[override]
Philip Meier's avatar
Philip Meier committed
93
        return self._make_repr(format=self.format, canvas_size=self.canvas_size)