transforms.rst 6.8 KB
Newer Older
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
1
2
torchvision.transforms
======================
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
3
4
5

.. currentmodule:: torchvision.transforms

6
Transforms are common image transformations. They can be chained together using :class:`Compose`.
7
8
9
Most transform classes have a function equivalent: :ref:`functional
transforms <functional_transforms>` give fine-grained control over the
transformations.
10
11
This is useful if you have to build a more complex transformation pipeline
(e.g. in the case of segmentation tasks).
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
12

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Most transformations accept both `PIL <https://pillow.readthedocs.io>`_
images and tensor images, although some transformations are :ref:`PIL-only
<transforms_pil_only>` and some are :ref:`tensor-only
<transforms_tensor_only>`. The :ref:`conversion_transforms` may be used to
convert to and from PIL images.

The transformations that accept tensor images also accept batches of tensor
images. A Tensor Image is a tensor with ``(C, H, W)`` shape, where ``C`` is a
number of channels, ``H`` and ``W`` are image height and width. A batch of
Tensor Images is a tensor of ``(B, C, H, W)`` shape, where ``B`` is a number
of images in the batch.

The expected range of the values of a tensor image is implicitely defined by
the tensor dtype. Tensor images with a float dtype are expected to have
values in ``[0, 1)``. Tensor images with an integer dtype are expected to
have values in ``[0, MAX_DTYPE]`` where ``MAX_DTYPE`` is the largest value
that can be represented in that dtype.

Randomized transformations will apply the same transformation to all the
images of a given batch, but they will produce different transformations
across calls. For reproducible transformations across calls, you may use
:ref:`functional transforms <functional_transforms>`.
35

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
.. warning::

    Since v0.8.0 all random transformations are using torch default random generator to sample random parameters.
    It is a backward compatibility breaking change and user should set the random state as following:

    .. code:: python

        # Previous versions
        # import random
        # random.seed(12)

        # Now
        import torch
        torch.manual_seed(17)

    Please, keep in mind that the same seed for torch random generator and Python random generator will not
    produce the same results.

54
55

Scriptable transforms
vfdev's avatar
vfdev committed
56
---------------------
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

In order to script the transformations, please use ``torch.nn.Sequential`` instead of :class:`Compose`.

.. code:: python

    transforms = torch.nn.Sequential(
        transforms.CenterCrop(10),
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
    )
    scripted_transforms = torch.jit.script(transforms)

Make sure to use only scriptable transformations, i.e. that work with ``torch.Tensor`` and does not require
`lambda` functions or ``PIL.Image``.

For any custom transformations to be used with ``torch.jit.script``, they should be derived from ``torch.nn.Module``.

73

vfdev's avatar
vfdev committed
74
75
Compositions of transforms
--------------------------
76

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
77
78
.. autoclass:: Compose

79

vfdev's avatar
vfdev committed
80
81
Transforms on PIL Image and torch.\*Tensor
------------------------------------------
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
82

83
.. autoclass:: CenterCrop
vfdev's avatar
vfdev committed
84
    :members:
85

86
.. autoclass:: ColorJitter
vfdev's avatar
vfdev committed
87
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
88

89
.. autoclass:: FiveCrop
vfdev's avatar
vfdev committed
90
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
91

92
.. autoclass:: Grayscale
vfdev's avatar
vfdev committed
93
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
94

95
.. autoclass:: Pad
vfdev's avatar
vfdev committed
96
    :members:
97

98
.. autoclass:: RandomAffine
vfdev's avatar
vfdev committed
99
    :members:
100

101
.. autoclass:: RandomApply
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
102

103
.. autoclass:: RandomCrop
vfdev's avatar
vfdev committed
104
    :members:
105

106
.. autoclass:: RandomGrayscale
vfdev's avatar
vfdev committed
107
    :members:
108

109
.. autoclass:: RandomHorizontalFlip
vfdev's avatar
vfdev committed
110
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
111

112
.. autoclass:: RandomPerspective
vfdev's avatar
vfdev committed
113
    :members:
114

115
.. autoclass:: RandomResizedCrop
vfdev's avatar
vfdev committed
116
    :members:
117

118
.. autoclass:: RandomRotation
vfdev's avatar
vfdev committed
119
    :members:
120

121
.. autoclass:: RandomSizedCrop
vfdev's avatar
vfdev committed
122
    :members:
123
124

.. autoclass:: RandomVerticalFlip
vfdev's avatar
vfdev committed
125
    :members:
126
127

.. autoclass:: Resize
vfdev's avatar
vfdev committed
128
    :members:
129
130

.. autoclass:: Scale
vfdev's avatar
vfdev committed
131
    :members:
132
133

.. autoclass:: TenCrop
vfdev's avatar
vfdev committed
134
    :members:
135

136
.. autoclass:: GaussianBlur
vfdev's avatar
vfdev committed
137
    :members:
138

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
.. autoclass:: RandomInvert
    :members:

.. autoclass:: RandomPosterize
    :members:

.. autoclass:: RandomSolarize
    :members:

.. autoclass:: RandomAdjustSharpness
    :members:

.. autoclass:: RandomAutocontrast
    :members:

.. autoclass:: RandomEqualize
    :members:

157
158
.. _transforms_pil_only:

vfdev's avatar
vfdev committed
159
Transforms on PIL Image only
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
160
161
----------------------------

vfdev's avatar
vfdev committed
162
163
164
165
.. autoclass:: RandomChoice

.. autoclass:: RandomOrder

166
.. _transforms_tensor_only:
vfdev's avatar
vfdev committed
167
168
169
170

Transforms on torch.\*Tensor only
---------------------------------

surgan12's avatar
surgan12 committed
171
.. autoclass:: LinearTransformation
vfdev's avatar
vfdev committed
172
    :members:
surgan12's avatar
surgan12 committed
173

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
174
.. autoclass:: Normalize
vfdev's avatar
vfdev committed
175
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
176

177
.. autoclass:: RandomErasing
vfdev's avatar
vfdev committed
178
179
180
181
    :members:

.. autoclass:: ConvertImageDtype

182
.. _conversion_transforms:
183

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
184
185
186
Conversion Transforms
---------------------

187
.. autoclass:: ToPILImage
vfdev's avatar
vfdev committed
188
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
189

190
.. autoclass:: ToTensor
vfdev's avatar
vfdev committed
191
192
    :members:

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
193
194
195
196
197

Generic Transforms
------------------

.. autoclass:: Lambda
vfdev's avatar
vfdev committed
198
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
199

200

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
AutoAugment Transforms
----------------------

`AutoAugment <https://arxiv.org/pdf/1805.09501.pdf>`_ is a common Data Augmentation technique that can improve the accuracy of Image Classification models.
Though the data augmentation policies are directly linked to their trained dataset, empirical studies show that
ImageNet policies provide significant improvements when applied to other datasets.
In TorchVision we implemented 3 policies learned on the following datasets: ImageNet, CIFAR10 and SVHN.
The new transform can be used standalone or mixed-and-matched with existing transforms:

.. autoclass:: AutoAugmentPolicy
    :members:

.. autoclass:: AutoAugment
    :members:


217
218
.. _functional_transforms:

219
220
221
Functional Transforms
---------------------

222
223
224
Functional transforms give you fine-grained control of the transformation pipeline.
As opposed to the transformations above, functional transforms don't contain a random number
generator for their parameters.
225
226
That means you have to specify/generate all parameters, but the functional transform will give you
reproducible results across calls.
227
228
229

Example:
you can apply a functional transform with the same parameters to multiple images like this:
230
231
232
233
234
235
236

.. code:: python

    import torchvision.transforms.functional as TF
    import random

    def my_segmentation_transforms(image, segmentation):
237
        if random.random() > 0.5:
238
239
240
241
242
243
            angle = random.randint(-30, 30)
            image = TF.rotate(image, angle)
            segmentation = TF.rotate(segmentation, angle)
        # more transforms ...
        return image, segmentation

244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

Example:
you can use a functional transform to build transform classes with custom behavior:

.. code:: python

    import torchvision.transforms.functional as TF
    import random

    class MyRotationTransform:
        """Rotate by one of the given angles."""

        def __init__(self, angles):
            self.angles = angles

        def __call__(self, x):
            angle = random.choice(self.angles)
            return TF.rotate(x, angle)

    rotation_transform = MyRotationTransform(angles=[-30, -15, 0, 15, 30])


266
267
.. automodule:: torchvision.transforms.functional
    :members: