plot_datapoints.py 10.4 KB
Newer Older
1
2
3
4
5
"""
==============
Datapoints FAQ
==============

Nicolas Hug's avatar
Nicolas Hug committed
6
7
8
9
.. note::
    Try on `collab <https://colab.research.google.com/github/pytorch/vision/blob/gh-pages/main/_generated_ipynb_notebooks/plot_datapoints.ipynb>`_
    or :ref:`go to the end <sphx_glr_download_auto_examples_v2_transforms_plot_datapoints.py>` to download the full example code.

10

11
12
13
14
15
16
17
18
19
20
21
22
23
Datapoints are Tensor subclasses introduced together with
``torchvision.transforms.v2``. This example showcases what these datapoints are
and how they behave.

.. warning::

    **Intended Audience** Unless you're writing your own transforms or your own datapoints, you
    probably do not need to read this guide. This is a fairly low-level topic
    that most users will not need to worry about: you do not need to understand
    the internals of datapoints to efficiently rely on
    ``torchvision.transforms.v2``. It may however be useful for advanced users
    trying to implement their own datasets, transforms, or work directly with
    the datapoints.
24
25
"""

26
# %%
27
28
29
30
import PIL.Image

import torch
from torchvision import datapoints
31
from torchvision.transforms.v2 import functional as F
32
33


34
# %%
35
36
37
38
39
40
41
42
43
44
45
# What are datapoints?
# --------------------
#
# Datapoints are zero-copy tensor subclasses:

tensor = torch.rand(3, 256, 256)
image = datapoints.Image(tensor)

assert isinstance(image, torch.Tensor)
assert image.data_ptr() == tensor.data_ptr()

46
# %%
47
48
49
# Under the hood, they are needed in :mod:`torchvision.transforms.v2` to correctly dispatch to the appropriate function
# for the input data.
#
50
51
52
53
54
55
56
# :mod:`torchvision.datapoints` supports four types of datapoints:
#
# * :class:`~torchvision.datapoints.Image`
# * :class:`~torchvision.datapoints.Video`
# * :class:`~torchvision.datapoints.BoundingBoxes`
# * :class:`~torchvision.datapoints.Mask`
#
57
58
59
60
61
# What can I do with a datapoint?
# -------------------------------
#
# Datapoints look and feel just like regular tensors - they **are** tensors.
# Everything that is supported on a plain :class:`torch.Tensor` like ``.sum()`` or
62
# any ``torch.*`` operator will also work on datapoints. See
63
64
65
66
67
# :ref:`datapoint_unwrapping_behaviour` for a few gotchas.

# %%
# .. _datapoint_creation:
#
68
69
70
# How do I construct a datapoint?
# -------------------------------
#
71
72
73
# Using the constructor
# ^^^^^^^^^^^^^^^^^^^^^
#
74
75
76
77
78
79
# Each datapoint class takes any tensor-like data that can be turned into a :class:`~torch.Tensor`

image = datapoints.Image([[[[0, 1], [1, 0]]]])
print(image)


80
# %%
81
82
83
84
85
86
87
# Similar to other PyTorch creations ops, the constructor also takes the ``dtype``, ``device``, and ``requires_grad``
# parameters.

float_image = datapoints.Image([[[0, 1], [1, 0]]], dtype=torch.float32, requires_grad=True)
print(float_image)


88
# %%
89
# In addition, :class:`~torchvision.datapoints.Image` and :class:`~torchvision.datapoints.Mask` can also take a
90
91
# :class:`PIL.Image.Image` directly:

92
image = datapoints.Image(PIL.Image.open("../assets/astronaut.jpg"))
93
94
print(image.shape, image.dtype)

95
# %%
96
97
98
99
100
101
102
103
104
# Some datapoints require additional metadata to be passed in ordered to be constructed. For example,
# :class:`~torchvision.datapoints.BoundingBoxes` requires the coordinate format as well as the size of the
# corresponding image (``canvas_size``) alongside the actual values. These
# metadata are required to properly transform the bounding boxes.

bboxes = datapoints.BoundingBoxes(
    [[17, 16, 344, 495], [0, 10, 0, 10]],
    format=datapoints.BoundingBoxFormat.XYXY,
    canvas_size=image.shape[-2:]
105
)
106
107
108
print(bboxes)

# %%
109
110
# Using ``datapoints.wrap()``
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^
111
#
112
# You can also use the :func:`~torchvision.datapoints.wrap` function to wrap a tensor object
113
114
# into a datapoint. This is useful when you already have an object of the
# desired type, which typically happens when writing transforms: you just want
115
# to wrap the output like the input.
116
117

new_bboxes = torch.tensor([0, 20, 30, 40])
118
new_bboxes = datapoints.wrap(new_bboxes, like=bboxes)
119
120
assert isinstance(new_bboxes, datapoints.BoundingBoxes)
assert new_bboxes.canvas_size == bboxes.canvas_size
121
122


123
# %%
124
# The metadata of ``new_bboxes`` is the same as ``bboxes``, but you could pass
125
# it as a parameter to override it.
126
#
127
128
129
# Do I have to wrap the output of the datasets myself?
# ----------------------------------------------------
#
130
131
# TODO: Move this in another guide - this is user-facing, not dev-facing.
#
132
133
134
135
136
# Only if you are using custom datasets. For the built-in ones, you can use
# :func:`torchvision.datasets.wrap_dataset_for_transforms_v2`. Note that the function also supports subclasses of the
# built-in datasets. Meaning, if your custom dataset subclasses from a built-in one and the output type is the same, you
# also don't have to wrap manually.
#
137
138
139
140
141
142
143
144
145
146
147
# If you have a custom dataset, for example the ``PennFudanDataset`` from
# `this tutorial <https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html>`_, you have two options:
#
# 1. Perform the wrapping inside ``__getitem__``:

class PennFudanDataset(torch.utils.data.Dataset):
    ...

    def __getitem__(self, item):
        ...

148
149
        target["bboxes"] = datapoints.BoundingBoxes(
            bboxes,
150
            format=datapoints.BoundingBoxFormat.XYXY,
Philip Meier's avatar
Philip Meier committed
151
            canvas_size=F.get_size(img),
152
153
154
155
156
157
158
159
160
161
162
        )
        target["labels"] = labels
        target["masks"] = datapoints.Mask(masks)

        ...

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        ...

163
# %%
164
165
166
167
168
# 2. Perform the wrapping inside a custom transformation at the beginning of your pipeline:


class WrapPennFudanDataset:
    def __call__(self, img, target):
169
        target["boxes"] = datapoints.BoundingBoxes(
170
171
            target["boxes"],
            format=datapoints.BoundingBoxFormat.XYXY,
Philip Meier's avatar
Philip Meier committed
172
            canvas_size=F.get_size(img),
173
174
175
176
177
178
179
180
181
182
183
184
185
186
        )
        target["masks"] = datapoints.Mask(target["masks"])
        return img, target


...


def get_transform(train):
    transforms = []
    transforms.append(WrapPennFudanDataset())
    transforms.append(T.PILToTensor())
    ...

187
# %%
188
189
# .. note::
#
190
#    If both :class:`~torchvision.datapoints.BoundingBoxes` and :class:`~torchvision.datapoints.Mask`'s are included in
191
192
193
194
195
196
197
198
#    the sample, ``torchvision.transforms.v2`` will transform them both. Meaning, if you don't need both, dropping or
#    at least not wrapping the obsolete parts, can lead to a significant performance boost.
#
#    For example, if you are using the ``PennFudanDataset`` for object detection, not wrapping the masks avoids
#    transforming them over and over again in the pipeline just to ultimately ignoring them. In general, it would be
#    even better to not load the masks at all, but this is not possible in this example, since the bounding boxes are
#    generated from the masks.
#
199
# .. _datapoint_unwrapping_behaviour:
200
#
201
202
203
# I had a Datapoint but now I have a Tensor. Help!
# ------------------------------------------------
#
204
205
# By default, operations on :class:`~torchvision.datapoints.Datapoint` objects
# will return a pure Tensor:
206

207

208
assert isinstance(bboxes, datapoints.BoundingBoxes)
209

210
211
# Shift bboxes by 3 pixels in both H and W
new_bboxes = bboxes + 3
212

213
214
215
216
217
218
219
220
221
222
assert isinstance(new_bboxes, torch.Tensor)
assert not isinstance(new_bboxes, datapoints.BoundingBoxes)

# %%
# .. note::
#
#    This behavior only affects native ``torch`` operations. If you are using
#    the built-in ``torchvision`` transforms or functionals, you will always get
#    as output the same type that you passed as input (pure ``Tensor`` or
#    ``Datapoint``).
223
224

# %%
225
226
227
228
# But I want a Datapoint back!
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# You can re-wrap a pure tensor into a datapoint by just calling the datapoint
229
230
# constructor, or by using the :func:`~torchvision.datapoints.wrap` function
# (see more details above in :ref:`datapoint_creation`):
231
232

new_bboxes = bboxes + 3
233
new_bboxes = datapoints.wrap(new_bboxes, like=bboxes)
234
assert isinstance(new_bboxes, datapoints.BoundingBoxes)
235

236
# %%
237
238
239
240
241
242
243
244
245
246
# Alternatively, you can use the :func:`~torchvision.datapoints.set_return_type`
# as a global config setting for the whole program, or as a context manager:

with datapoints.set_return_type("datapoint"):
    new_bboxes = bboxes + 3
assert isinstance(new_bboxes, datapoints.BoundingBoxes)

# %%
# Why is this happening?
# ^^^^^^^^^^^^^^^^^^^^^^
247
#
248
249
250
251
252
253
254
255
256
# **For performance reasons**. :class:`~torchvision.datapoints.Datapoint`
# classes are Tensor subclasses, so any operation involving a
# :class:`~torchvision.datapoints.Datapoint` object will go through the
# `__torch_function__
# <https://pytorch.org/docs/stable/notes/extending.html#extending-torch>`_
# protocol. This induces a small overhead, which we want to avoid when possible.
# This doesn't matter for built-in ``torchvision`` transforms because we can
# avoid the overhead there, but it could be a problem in your model's
# ``forward``.
257
#
258
259
260
261
262
263
264
265
# **The alternative isn't much better anyway.** For every operation where
# preserving the :class:`~torchvision.datapoints.Datapoint` type makes
# sense, there are just as many operations where returning a pure Tensor is
# preferable: for example, is ``img.sum()`` still an :class:`~torchvision.datapoints.Image`?
# If we were to preserve :class:`~torchvision.datapoints.Datapoint` types all
# the way, even model's logits or the output of the loss function would end up
# being of type :class:`~torchvision.datapoints.Image`, and surely that's not
# desirable.
266
#
267
268
# .. note::
#
269
#    This behaviour is something we're actively seeking feedback on. If you find this surprising or if you
270
271
272
#    have any suggestions on how to better support your use-cases, please reach out to us via this issue:
#    https://github.com/pytorch/vision/issues/7319
#
273
274
275
# Exceptions
# ^^^^^^^^^^
#
276
# There are a few exceptions to this "unwrapping" rule:
277
#
278
279
280
281
282
283
# 1. Operations like :meth:`~torch.Tensor.clone`, :meth:`~torch.Tensor.to`,
#    :meth:`torch.Tensor.detach` and :meth:`~torch.Tensor.requires_grad_` retain
#    the datapoint type.
# 2. Inplace operations on datapoints like ``.add_()`` preserve they type. However,
#    the **returned** value of inplace operations will be unwrapped into a pure
#    tensor:
284
285
286
287
288

image = datapoints.Image([[[0, 1], [1, 0]]])

new_image = image.add_(1).mul_(2)

289
290
291
292
# image got transformed in-place and is still an Image datapoint, but new_image
# is a Tensor. They share the same underlying data and they're equal, just
# different classes.
assert isinstance(image, datapoints.Image)
293
294
295
296
print(image)

assert isinstance(new_image, torch.Tensor) and not isinstance(new_image, datapoints.Image)
assert (new_image == image).all()
297
assert new_image.data_ptr() == image.data_ptr()