Skip to content

Various doc enhancements #7326

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 8 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
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
sys.path.append(os.path.abspath("."))

torchvision.disable_beta_transforms_warning()
import torchvision.datapoints # Don't remove, otherwise the docs for datapoints aren't linked properly
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain what is happening?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly I have no idea.


# -- General configuration ------------------------------------------------

Expand Down
6 changes: 6 additions & 0 deletions docs/source/datapoints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ Datapoints
==========

.. currentmodule:: torchvision.datapoints

Datapoints are tensor subclasses which the :mod:`~torchvision.transforms.v2` v2 transforms use under the hood to
dispatch their inputs to the appropriate lower-level kernels. Most users do not
need to manipulate datapoints directly and can simply rely on dataset wrapping -
see e.g. :ref:`sphx_glr_auto_examples_plot_transforms_v2_e2e.py`.

.. autosummary::
:toctree: generated/
:template: class.rst
Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ architectures, and common image transformations for computer vision.
:maxdepth: 2
:caption: Package Reference

datapoints
transforms
datapoints
models
datasets
utils
Expand Down
8 changes: 7 additions & 1 deletion docs/source/transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ Miscellaneous
Conversion
----------

.. note::
Beware, some of these conversion transforms below will scale the values
while performing the conversion, while some may not do any scaling. By
scaling, we mean e.g. that a ``uint8`` -> ``float32`` would map the [0,
255] range into [0, 1] (and vice-versa).

.. autosummary::
:toctree: generated/
:template: class.rst
Expand All @@ -211,8 +217,8 @@ Conversion
v2.PILToTensor
v2.ToImageTensor
ConvertImageDtype
v2.ConvertImageDtype
v2.ConvertDtype
v2.ConvertImageDtype
v2.ToDtype
v2.ConvertBoundingBoxFormat

Expand Down
15 changes: 11 additions & 4 deletions torchvision/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ def __repr__(self) -> str:


class ToTensor:
"""Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor. This transform does not support torchscript.
"""Convert a PIL Image or ndarray to tensor and scale the values accordingly.

This transform does not support torchscript.

Converts a PIL Image or numpy.ndarray (H x W x C) in the range
[0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]
Expand Down Expand Up @@ -139,7 +141,9 @@ def __repr__(self) -> str:


class PILToTensor:
"""Convert a ``PIL Image`` to a tensor of the same type. This transform does not support torchscript.
"""Convert a PIL Image to a tensor of the same type - this does not scale values.

This transform does not support torchscript.

Converts a PIL Image (H x W x C) to a Tensor of shape (C x H x W).
"""
Expand All @@ -166,7 +170,8 @@ def __repr__(self) -> str:


class ConvertImageDtype(torch.nn.Module):
"""Convert a tensor image to the given ``dtype`` and scale the values accordingly
"""Convert a tensor image to the given ``dtype`` and scale the values accordingly.

This function does not support PIL Image.

Args:
Expand Down Expand Up @@ -194,7 +199,9 @@ def forward(self, image):


class ToPILImage:
"""Convert a tensor or an ndarray to PIL Image. This transform does not support torchscript.
"""Convert a tensor or an ndarray to PIL Image - this does not scale values.

This transform does not support torchscript.

Converts a torch.*Tensor of shape C x H x W or a numpy ndarray of shape
H x W x C to a PIL Image while preserving the value range.
Expand Down
4 changes: 1 addition & 3 deletions torchvision/transforms/v2/_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ def __init__(
if p is None:
p = [1] * len(transforms)
elif len(p) != len(transforms):
raise ValueError(
f"Length of p doesn't match the number of transforms: " f"{len(p)} != {len(transforms)}"
)
raise ValueError(f"Length of p doesn't match the number of transforms: {len(p)} != {len(transforms)}")

super().__init__()

Expand Down
2 changes: 1 addition & 1 deletion torchvision/transforms/v2/_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class ToTensor(Transform):
"""[BETA] Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
"""[BETA] Convert a PIL Image or ndarray to tensor and scale the values accordingly.

.. betastatus:: ToTensor transform

Expand Down
4 changes: 3 additions & 1 deletion torchvision/transforms/v2/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class ConvertBoundingBoxFormat(Transform):
"""[BETA] Convert bounding box coordinates to the given ``format``, e.g. from "CXCYWH" to "XYXY".
"""[BETA] Convert bounding box coordinates to the given ``format``, eg from "CXCYWH" to "XYXY".
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. here, had to remove full dot


.. betastatus:: ConvertBoundingBoxFormat transform

Expand All @@ -18,6 +18,7 @@ class ConvertBoundingBoxFormat(Transform):
Possible values are defined by :class:`~torchvision.datapoints.BoundingBoxFormat` and
string values match the enums, e.g. "XYXY" or "XYWH" etc.
"""

_transformed_types = (datapoints.BoundingBox,)

def __init__(self, format: Union[str, datapoints.BoundingBoxFormat]) -> None:
Expand Down Expand Up @@ -79,6 +80,7 @@ class ClampBoundingBox(Transform):
.. betastatus:: ClampBoundingBox transform

"""

_transformed_types = (datapoints.BoundingBox,)

def _transform(self, inpt: datapoints.BoundingBox, params: Dict[str, Any]) -> datapoints.BoundingBox:
Expand Down
8 changes: 5 additions & 3 deletions torchvision/transforms/v2/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,15 @@ def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:


class ToDtype(Transform):
"""[BETA] Converts the input to a specific dtype.
"""[BETA] Converts the input to a specific dtype - this does not scale values.

.. betastatus:: ToDtype transform

Args:
dtype (dtype or dict of Datapoint -> dtype): The dtype to convert to. A dict can be passed to specify
per-datapoint conversions, e.g. ``dtype={datapoints.Image: torch.float32, datapoints.Video: torch.float64}``.
dtype (``torch.dtype`` or dict of ``Datapoint`` -> ``torch.dtype``): The dtype to convert to.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrapping in double quotes seems to fix the sad formatting you noticed @pmeier . We should fix those on the rest of the docs

A dict can be passed to specify per-datapoint conversions, e.g.
``dtype={datapoints.Image: torch.float32, datapoints.Video:
torch.float64}``.
"""

_transformed_types = (torch.Tensor,)
Expand Down
7 changes: 4 additions & 3 deletions torchvision/transforms/v2/_type_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class PILToTensor(Transform):
"""[BETA] Convert a ``PIL Image`` to a tensor of the same type.
"""[BETA] Convert a PIL Image to a tensor of the same type - this does not scale values.

.. betastatus:: PILToTensor transform

Expand All @@ -27,7 +27,8 @@ def _transform(self, inpt: PIL.Image.Image, params: Dict[str, Any]) -> torch.Ten


class ToImageTensor(Transform):
"""[BETA] Convert a tensor or an ndarray or PIL Image to :class:`~torchvision.datapoints.Image`.
"""[BETA] Convert a tensor, ndarray, or PIL Image to :class:`~torchvision.datapoints.Image`
; this does not scale values.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency with all other docstrings.

Suggested change
; this does not scale values.
- this does not scale values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for ; because I suspect sphinx to treat the - as a list entry as it's the first character of the line


.. betastatus:: ToImageTensor transform

Expand All @@ -43,7 +44,7 @@ def _transform(


class ToImagePIL(Transform):
"""[BETA] Convert a tensor or an ndarray to PIL Image.
"""[BETA] Convert a tensor or an ndarray to PIL Image - this does not scale values.

.. betastatus:: ToImagePIL transform

Expand Down