Skip to content

Commit e15e81f

Browse files
sottefmassa
authored andcommitted
Improve docs of functional transforms (#602)
Give a short example of how to build a transform pipeline for a segmentation task.
1 parent f91a182 commit e15e81f

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

docs/source/transforms.rst

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ torchvision.transforms
33

44
.. currentmodule:: torchvision.transforms
55

6-
Transforms are common image transforms. They can be chained together using :class:`Compose`
6+
Transforms are common image transformations. They can be chained together using :class:`Compose`.
7+
Additionally, there is the :mod:`torchvision.transforms.functional` module.
8+
Functional transforms give fine-grained control over the transformations.
9+
This is useful if you have to build a more complex transformation pipeline
10+
(e.g. in the case of segmentation tasks).
711

812
.. autoclass:: Compose
913

@@ -78,5 +82,24 @@ Generic Transforms
7882
Functional Transforms
7983
---------------------
8084

85+
Functional transforms give you fine-grained control of the transformation pipeline.
86+
As opposed to the transformations above, functional transforms don't contain a random number
87+
generator for their parameters.
88+
That means you have to specify/generate all parameters, but you can reuse the functional transform.
89+
For example, you can apply a functional transform to multiple images like this:
90+
91+
.. code:: python
92+
93+
import torchvision.transforms.functional as TF
94+
import random
95+
96+
def my_segmentation_transforms(image, segmentation):
97+
if random.random() > 5:
98+
angle = random.randint(-30, 30)
99+
image = TF.rotate(image, angle)
100+
segmentation = TF.rotate(segmentation, angle)
101+
# more transforms ...
102+
return image, segmentation
103+
81104
.. automodule:: torchvision.transforms.functional
82105
:members:

0 commit comments

Comments
 (0)