A Brief Introduction to the Transforms in this Package#
5 Minute Read
Summary#
In this tutorial, we will briefly demonstrate some of the parameterized transforms that we provide in this package.
For each
torchvision
-based transform, we provide one parameterized version with the same name that exposes the parameters of this transform.In addition, we provide useful tools and wrappers in order to ease working with parameterized transforms and porting to-and-from
torch
/torchvision
-based code.
Transforms from torchvision
but Parametrized!#
Please refer to the files transforms.py for more information while reading through this section.
Note that
torchvision.transforms.transform
provides35
transforms. Out of these,31
are atomic transforms and the remaining4
are composing transforms. All these transforms are listed below.
Atomic transforms from torchvision/transforms/transforms.py
(31
transforms)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Composing transforms from torchvision/transforms/transforms.py
(4
transforms)
|
|
|
|
For each transform provided in
torchvision/transforms/transforms.py
, we provide its parameterized counterpart inparameterized_transforms/transforms.py
. These parameterized transforms behave exactly identical to theirtorchvision
counterparts in terms of the augmentation functionality but provide access to their augmentation parameters as well. All these transforms that are provided inparameterized_transforms/transforms.py
are listed below–
Atomic transforms from parameterized_transforms/transforms.py (31
transforms)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Composing transforms from parameterized_transforms/transforms.py (4
transforms)
|
|
|
|
Useful Wrapper Transforms#
Please refer to the file wrappers.py for more information while reading through this section.
In addition to these parameterized transforms, we provide extra wrapper transforms that allow for miscellaneous useful functionality. In particular, we provide
4
useful wrapper transforms which are listed below–
|
|
|
|
These transforms are mostly intended to ease manipulation of train/test augmentation stacks and for porting to-and-from
torch
/torchvision
-based codebases. We briefly describe their usage below.ExtractDefaultParams
wrapper transform stores an atomic or a composing transform as its core transform. When given an imageimg
and parametersparams
as input, it fetches the default parameters of the core transform, appends them to the input parameters to obtain concatenated parametersconcat_params
, and returns the tuple of 1. the input imageimg
as it is, and 2. the concatenated parametersconcat_params
. The purpose of this wrapper transform is to expose the default parameters of any transform of interest without actually applying that transform.ApplyDefaultParams
wrapper transform stores an atomic or a composing transform as its core transform. It behaves identical toExtractDefaultParams
except for a subtle difference as suggested by its name. When given an imageimg
and parametersparams
as input, it fetches the default parameters of the core transform, applies the core transform defined by these default parameters on theimg
to get the default-augmented imageaug_img
, appends the default parameters to the input parameters in order to obtain concatenated parametersconcat_params
, and returns the tuple of 1. the default augmented imageaug_img
, and 2. the concatenated parametersconcat_params
.The reason to provide this subtly different wrapper transform is that there are some transforms where default parameters can preserve the image information but still change the image itself (for instance,
ToTensor
,ConvertImageDtype
,Resize
, etc.) and there are others where the information in the image is indeed lost ( for instance,CenterCrop
,Grayscale
, etc.). Given these possibilities, this wrapper is often helpful in easily generating the test transforms corresponding to the given train transforms; wrapping each component transform of a train transform stack often gives the desired test transform!
CastParamsToTensor
wrapper transform stores an atomic or a composing transform as its core transform. This transform is intended to wrap a finalized stack of core transforms, where the output parameter tuple is converted to atorch
-tensor withdtype=torch.float32
. In particular, when given an imageimg
and parametersparams
as input, it converts the parameters to atorch
-tensorparams_torch
withdtype=torch.float32
and returns the tuple of 1. the input imageimg
as it is, and 2. the parameters converted totorch
-tensorparams_torch
withdtype=torch.float32
.The utility of this wrapper transform will become clear in the tutorial on migrating code from
torch
/torchvision
to use this package!
DropParams
wrapper transform stores an atomic or a composing transform as its core transform. This transform is intended to help convert a parameterized transform into itstorchvision
-counterpart to help port code from our package back totorch
/torchvision
. As the name suggests, given given an imageimg
and parametersparams
as input, it just returns the imageimg
as it is, thereby dropping parameters. Thus, wrapping any parametrize transform with this wrapper transform, we get itstorchvision
-counterpart!In the subsequent tutorials, we will illustrate some of the atomic, composing, and wrapper transforms provided in this package so that readers fill comfortable working with them!
About the Next Tutorial#
Till now, we have studied the core structure of parameterized transforms, we have writte our own custom parameterized atomic and composing transforms, and we have briefly gone through the transforms provided in this package.
So, in the next tutorial 004-Parametrized-Transforms-in-Action, we will see the parameterized transforms in action; we will see how parameterized transforms implemented in the structure prescribed in this package allow us to perform cool things!
If you were wondering why do we need to define our transform with the specific templates illustrated in the previous tutorials, this tutorial would answer your doubts by showing how these transforms would fit with each other seamlessly and with other transform provided in the package. This should also help demonstrate the power and the capabilities of the parameterized transforms!