transforms.rst 4.06 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

Scriptable transforms
vfdev's avatar
vfdev committed
19
---------------------
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

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

vfdev's avatar
vfdev committed
36
37
Compositions of transforms
--------------------------
38

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
39
40
.. autoclass:: Compose

41
Transforms on PIL Image
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
42
43
-----------------------

44
.. autoclass:: CenterCrop
45

46
.. autoclass:: ColorJitter
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
47

48
.. autoclass:: FiveCrop
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
49

50
.. autoclass:: Grayscale
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
51

52
.. autoclass:: Pad
53

54
.. autoclass:: RandomAffine
55

56
.. autoclass:: RandomApply
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
57

58
.. autoclass:: RandomChoice
59

60
.. autoclass:: RandomCrop
61

62
.. autoclass:: RandomGrayscale
63

64
.. autoclass:: RandomHorizontalFlip
65

66
.. autoclass:: RandomOrder
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
67

68
69
.. autoclass:: RandomPerspective

70
.. autoclass:: RandomResizedCrop
71

72
73
.. autoclass:: RandomRotation

74
75
76
77
78
79
80
81
82
.. autoclass:: RandomSizedCrop

.. autoclass:: RandomVerticalFlip

.. autoclass:: Resize

.. autoclass:: Scale

.. autoclass:: TenCrop
83

84
85
.. autoclass:: GaussianBlur

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
86
87
88
Transforms on torch.\*Tensor
----------------------------

surgan12's avatar
surgan12 committed
89
90
.. autoclass:: LinearTransformation

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
91
92
93
94
.. autoclass:: Normalize
	:members: __call__
	:special-members:

95
96
.. autoclass:: RandomErasing

Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
97
98
99
Conversion Transforms
---------------------

100
.. autoclass:: ToPILImage
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
101
102
103
	:members: __call__
	:special-members:

104
.. autoclass:: ToTensor
Sasank Chilamkurthy's avatar
Sasank Chilamkurthy committed
105
106
107
108
109
110
111
112
	:members: __call__
	:special-members:

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

.. autoclass:: Lambda

113
114
115
116

Functional Transforms
---------------------

117
118
119
120
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.
121
122
123

Example:
you can apply a functional transform with the same parameters to multiple images like this:
124
125
126
127
128
129
130

.. code:: python

    import torchvision.transforms.functional as TF
    import random

    def my_segmentation_transforms(image, segmentation):
131
        if random.random() > 0.5:
132
133
134
135
136
137
            angle = random.randint(-30, 30)
            image = TF.rotate(image, angle)
            segmentation = TF.rotate(segmentation, angle)
        # more transforms ...
        return image, segmentation

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

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


160
161
.. automodule:: torchvision.transforms.functional
    :members: