|
9 | 9 |
|
10 | 10 |
|
11 | 11 | TVTensors are Tensor subclasses introduced together with
|
12 |
| -``torchvision.transforms.v2``. This example showcases what these tv_tensors are |
| 12 | +``torchvision.transforms.v2``. This example showcases what these TVTensors are |
13 | 13 | and how they behave.
|
14 | 14 |
|
15 | 15 | .. warning::
|
16 | 16 |
|
17 |
| - **Intended Audience** Unless you're writing your own transforms or your own tv_tensors, you |
| 17 | + **Intended Audience** Unless you're writing your own transforms or your own TVTensors, you |
18 | 18 | probably do not need to read this guide. This is a fairly low-level topic
|
19 | 19 | that most users will not need to worry about: you do not need to understand
|
20 |
| - the internals of tv_tensors to efficiently rely on |
| 20 | + the internals of TVTensors to efficiently rely on |
21 | 21 | ``torchvision.transforms.v2``. It may however be useful for advanced users
|
22 | 22 | trying to implement their own datasets, transforms, or work directly with
|
23 |
| - the tv_tensors. |
| 23 | + the TVTensors. |
24 | 24 | """
|
25 | 25 |
|
26 | 26 | # %%
|
|
31 | 31 |
|
32 | 32 |
|
33 | 33 | # %%
|
34 |
| -# What are tv_tensors? |
35 |
| -# -------------------- |
| 34 | +# What are TVTensors? |
| 35 | +# ------------------- |
36 | 36 | #
|
37 | 37 | # TVTensors are zero-copy tensor subclasses:
|
38 | 38 |
|
|
46 | 46 | # Under the hood, they are needed in :mod:`torchvision.transforms.v2` to correctly dispatch to the appropriate function
|
47 | 47 | # for the input data.
|
48 | 48 | #
|
49 |
| -# :mod:`torchvision.tv_tensors` supports four types of tv_tensors: |
| 49 | +# :mod:`torchvision.tv_tensors` supports four types of TVTensors: |
50 | 50 | #
|
51 | 51 | # * :class:`~torchvision.tv_tensors.Image`
|
52 | 52 | # * :class:`~torchvision.tv_tensors.Video`
|
53 | 53 | # * :class:`~torchvision.tv_tensors.BoundingBoxes`
|
54 | 54 | # * :class:`~torchvision.tv_tensors.Mask`
|
55 | 55 | #
|
56 |
| -# What can I do with a tv_tensor? |
57 |
| -# ------------------------------- |
| 56 | +# What can I do with a TVTensor? |
| 57 | +# ------------------------------ |
58 | 58 | #
|
59 | 59 | # TVTensors look and feel just like regular tensors - they **are** tensors.
|
60 | 60 | # Everything that is supported on a plain :class:`torch.Tensor` like ``.sum()`` or
|
61 |
| -# any ``torch.*`` operator will also work on tv_tensors. See |
| 61 | +# any ``torch.*`` operator will also work on TVTensors. See |
62 | 62 | # :ref:`tv_tensor_unwrapping_behaviour` for a few gotchas.
|
63 | 63 |
|
64 | 64 | # %%
|
65 | 65 | # .. _tv_tensor_creation:
|
66 | 66 | #
|
67 |
| -# How do I construct a tv_tensor? |
68 |
| -# ------------------------------- |
| 67 | +# How do I construct a TVTensor? |
| 68 | +# ------------------------------ |
69 | 69 | #
|
70 | 70 | # Using the constructor
|
71 | 71 | # ^^^^^^^^^^^^^^^^^^^^^
|
72 | 72 | #
|
73 |
| -# Each tv_tensor class takes any tensor-like data that can be turned into a :class:`~torch.Tensor` |
| 73 | +# Each TVTensor class takes any tensor-like data that can be turned into a :class:`~torch.Tensor` |
74 | 74 |
|
75 | 75 | image = tv_tensors.Image([[[[0, 1], [1, 0]]]])
|
76 | 76 | print(image)
|
|
92 | 92 | print(image.shape, image.dtype)
|
93 | 93 |
|
94 | 94 | # %%
|
95 |
| -# Some tv_tensors require additional metadata to be passed in ordered to be constructed. For example, |
| 95 | +# Some TVTensors require additional metadata to be passed in ordered to be constructed. For example, |
96 | 96 | # :class:`~torchvision.tv_tensors.BoundingBoxes` requires the coordinate format as well as the size of the
|
97 | 97 | # corresponding image (``canvas_size``) alongside the actual values. These
|
98 | 98 | # metadata are required to properly transform the bounding boxes.
|
|
109 | 109 | # ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
110 | 110 | #
|
111 | 111 | # You can also use the :func:`~torchvision.tv_tensors.wrap` function to wrap a tensor object
|
112 |
| -# into a tv_tensor. This is useful when you already have an object of the |
| 112 | +# into a TVTensor. This is useful when you already have an object of the |
113 | 113 | # desired type, which typically happens when writing transforms: you just want
|
114 | 114 | # to wrap the output like the input.
|
115 | 115 |
|
|
125 | 125 | # .. _tv_tensor_unwrapping_behaviour:
|
126 | 126 | #
|
127 | 127 | # I had a TVTensor but now I have a Tensor. Help!
|
128 |
| -# ------------------------------------------------ |
| 128 | +# ----------------------------------------------- |
129 | 129 | #
|
130 | 130 | # By default, operations on :class:`~torchvision.tv_tensors.TVTensor` objects
|
131 | 131 | # will return a pure Tensor:
|
|
151 | 151 | # But I want a TVTensor back!
|
152 | 152 | # ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
153 | 153 | #
|
154 |
| -# You can re-wrap a pure tensor into a tv_tensor by just calling the tv_tensor |
| 154 | +# You can re-wrap a pure tensor into a TVTensor by just calling the TVTensor |
155 | 155 | # constructor, or by using the :func:`~torchvision.tv_tensors.wrap` function
|
156 | 156 | # (see more details above in :ref:`tv_tensor_creation`):
|
157 | 157 |
|
|
164 | 164 | # as a global config setting for the whole program, or as a context manager
|
165 | 165 | # (read its docs to learn more about caveats):
|
166 | 166 |
|
167 |
| -with tv_tensors.set_return_type("tv_tensor"): |
| 167 | +with tv_tensors.set_return_type("TVTensor"): |
168 | 168 | new_bboxes = bboxes + 3
|
169 | 169 | assert isinstance(new_bboxes, tv_tensors.BoundingBoxes)
|
170 | 170 |
|
|
203 | 203 | # There are a few exceptions to this "unwrapping" rule:
|
204 | 204 | # :meth:`~torch.Tensor.clone`, :meth:`~torch.Tensor.to`,
|
205 | 205 | # :meth:`torch.Tensor.detach`, and :meth:`~torch.Tensor.requires_grad_` retain
|
206 |
| -# the tv_tensor type. |
| 206 | +# the TVTensor type. |
207 | 207 | #
|
208 |
| -# Inplace operations on tv_tensors like ``obj.add_()`` will preserve the type of |
| 208 | +# Inplace operations on TVTensors like ``obj.add_()`` will preserve the type of |
209 | 209 | # ``obj``. However, the **returned** value of inplace operations will be a pure
|
210 | 210 | # tensor:
|
211 | 211 |
|
212 | 212 | image = tv_tensors.Image([[[0, 1], [1, 0]]])
|
213 | 213 |
|
214 | 214 | new_image = image.add_(1).mul_(2)
|
215 | 215 |
|
216 |
| -# image got transformed in-place and is still an Image tv_tensor, but new_image |
| 216 | +# image got transformed in-place and is still a TVTensor Image, but new_image |
217 | 217 | # is a Tensor. They share the same underlying data and they're equal, just
|
218 | 218 | # different classes.
|
219 | 219 | assert isinstance(image, tv_tensors.Image)
|
|
0 commit comments