Skip to content

add docs for datapoints #7312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/source/datapoints.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Datapoints
==========

.. currentmodule:: torchvision.datapoints
.. autosummary::
:toctree: generated/
:template: class.rst

Image
Video
BoundingBoxFormat
BoundingBox
Mask
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ architectures, and common image transformations for computer vision.
:maxdepth: 2
:caption: Package Reference

datapoints
transforms
models
datasets
Expand Down
37 changes: 37 additions & 0 deletions torchvision/datapoints/_bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,35 @@


class BoundingBoxFormat(Enum):
"""[BETA] Coordinate format of a bounding box.

Available formats are

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

XYXY = "XYXY"
XYWH = "XYWH"
CXCYWH = "CXCYWH"


class BoundingBox(Datapoint):
"""[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.
spatial_size (two-tuple of ints): Height and width of the corresponding image or video.
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``.
"""

format: BoundingBoxFormat
spatial_size: Tuple[int, int]

Expand Down Expand Up @@ -52,6 +75,20 @@ def wrap_like(
format: Optional[BoundingBoxFormat] = None,
spatial_size: Optional[Tuple[int, int]] = None,
) -> BoundingBox:
"""Wrap a :class:`torch.Tensor` as :class:`BoundingBox` from a reference.

Args:
other (BoundingBox): Reference bounding box.
tensor (Tensor): Tensor to be wrapped as :class:`BoundingBox`
format (BoundingBoxFormat, str, optional): Format of the bounding box. If omitted, it is taken from the
reference.
spatial_size (two-tuple of ints, optional): Height and width of the corresponding image or video. If
omitted, it is taken from the reference.

"""
if isinstance(format, str):
format = BoundingBoxFormat.from_str(format.upper())

return cls._wrap(
tensor,
format=format if format is not None else other.format,
Expand Down
13 changes: 13 additions & 0 deletions torchvision/datapoints/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@


class Image(Datapoint):
"""[BETA] :class:`torch.Tensor` subclass for images.

Args:
data (tensor-like, PIL.Image.Image): Any data that can be turned into a tensor with :func:`torch.as_tensor` as
well as PIL images.
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``.
"""

@classmethod
def _wrap(cls, tensor: torch.Tensor) -> Image:
image = tensor.as_subclass(cls)
Expand Down
13 changes: 13 additions & 0 deletions torchvision/datapoints/_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@


class Mask(Datapoint):
"""[BETA] :class:`torch.Tensor` subclass for segmentation and detection masks.

Args:
data (tensor-like, PIL.Image.Image): Any data that can be turned into a tensor with :func:`torch.as_tensor` as
well as PIL images.
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``.
"""

@classmethod
def _wrap(cls, tensor: torch.Tensor) -> Mask:
return tensor.as_subclass(cls)
Expand Down
12 changes: 12 additions & 0 deletions torchvision/datapoints/_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@


class Video(Datapoint):
"""[BETA] :class:`torch.Tensor` subclass for videos.

Args:
data (tensor-like): Any data that can be turned into a tensor with :func:`torch.as_tensor`.
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``.
"""

@classmethod
def _wrap(cls, tensor: torch.Tensor) -> Video:
video = tensor.as_subclass(cls)
Expand Down