"vscode:/vscode.git/clone" did not exist on "c05ebb74b1a04376cc4f7863a66efec1457bdede"
transforms.rst 5.61 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
7
8
9
10
Transforms are common image transformations. They can be chained together using :class:`Compose`.
Additionally, there is the :mod:`torchvision.transforms.functional` module.
Functional transforms give fine-grained control over the transformations.
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
11

12
13
14
15
16
All transformations accept PIL Image, Tensor Image or batch of Tensor Images as input. 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. Batch of
Tensor Images is a tensor of ``(B, C, H, W)`` shape, where ``B`` is a number of images in the batch. Deterministic or
random transformations applied on the batch of Tensor Images identically transform all the images of the batch.

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
.. 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.

35
36

Scriptable transforms
vfdev's avatar
vfdev committed
37
---------------------
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

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``.

54

vfdev's avatar
vfdev committed
55
56
Compositions of transforms
--------------------------
57

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
58
59
.. autoclass:: Compose

60

vfdev's avatar
vfdev committed
61
62
Transforms on PIL Image and torch.\*Tensor
------------------------------------------
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
63

64
.. autoclass:: CenterCrop
vfdev's avatar
vfdev committed
65
    :members:
66

67
.. autoclass:: ColorJitter
vfdev's avatar
vfdev committed
68
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
69

70
.. autoclass:: FiveCrop
vfdev's avatar
vfdev committed
71
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
72

73
.. autoclass:: Grayscale
vfdev's avatar
vfdev committed
74
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
75

76
.. autoclass:: Pad
vfdev's avatar
vfdev committed
77
    :members:
78

79
.. autoclass:: RandomAffine
vfdev's avatar
vfdev committed
80
    :members:
81

82
.. autoclass:: RandomApply
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
83

84
.. autoclass:: RandomCrop
vfdev's avatar
vfdev committed
85
    :members:
86

87
.. autoclass:: RandomGrayscale
vfdev's avatar
vfdev committed
88
    :members:
89

90
.. autoclass:: RandomHorizontalFlip
vfdev's avatar
vfdev committed
91
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
92

93
.. autoclass:: RandomPerspective
vfdev's avatar
vfdev committed
94
    :members:
95

96
.. autoclass:: RandomResizedCrop
vfdev's avatar
vfdev committed
97
    :members:
98

99
.. autoclass:: RandomRotation
vfdev's avatar
vfdev committed
100
    :members:
101

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

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

.. autoclass:: Resize
vfdev's avatar
vfdev committed
109
    :members:
110
111

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

.. autoclass:: TenCrop
vfdev's avatar
vfdev committed
115
    :members:
116

117
.. autoclass:: GaussianBlur
vfdev's avatar
vfdev committed
118
    :members:
119

vfdev's avatar
vfdev committed
120
Transforms on PIL Image only
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
121
122
----------------------------

vfdev's avatar
vfdev committed
123
124
125
126
127
128
129
130
.. autoclass:: RandomChoice

.. autoclass:: RandomOrder


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

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

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
134
.. autoclass:: Normalize
vfdev's avatar
vfdev committed
135
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
136

137
.. autoclass:: RandomErasing
vfdev's avatar
vfdev committed
138
139
140
141
    :members:

.. autoclass:: ConvertImageDtype

142

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
143
144
145
Conversion Transforms
---------------------

146
.. autoclass:: ToPILImage
vfdev's avatar
vfdev committed
147
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
148

149
.. autoclass:: ToTensor
vfdev's avatar
vfdev committed
150
151
    :members:

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
152
153
154
155
156

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

.. autoclass:: Lambda
vfdev's avatar
vfdev committed
157
    :members:
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
158

159

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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:


176
177
178
Functional Transforms
---------------------

179
180
181
182
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.
That means you have to specify/generate all parameters, but you can reuse the functional transform.
183
184
185

Example:
you can apply a functional transform with the same parameters to multiple images like this:
186
187
188
189
190
191
192

.. code:: python

    import torchvision.transforms.functional as TF
    import random

    def my_segmentation_transforms(image, segmentation):
193
        if random.random() > 0.5:
194
195
196
197
198
199
            angle = random.randint(-30, 30)
            image = TF.rotate(image, angle)
            segmentation = TF.rotate(segmentation, angle)
        # more transforms ...
        return image, segmentation

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

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])


222
223
.. automodule:: torchvision.transforms.functional
    :members: