Skip to content

Commit fa9e946

Browse files
committed
Fix inconsistencies and typos in transforms.py
- All "Transforms on torch.*Tensor" document their __call__ in a consistent manner and __call__ is shown in sphinx. - Fixed typos, punctuation, and naming. - Fixed wrong format of doc strings.
1 parent 048d974 commit fa9e946

File tree

2 files changed

+36
-48
lines changed

2 files changed

+36
-48
lines changed

docs/source/transforms.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,23 +281,29 @@ Transforms on torch.\*Tensor
281281
----------------------------
282282

283283
.. autoclass:: LinearTransformation
284+
:members: __call__
285+
:special-members:
284286

285287
.. autoclass:: Normalize
286-
:members: __call__
287-
:special-members:
288+
:members: __call__
289+
:special-members:
288290

289291
.. autoclass:: RandomErasing
292+
:members: __call__
293+
:special-members:
294+
290295

291296
Conversion Transforms
292297
---------------------
293298

294299
.. autoclass:: ToPILImage
295-
:members: __call__
296-
:special-members:
300+
:members: __call__
301+
:special-members:
297302

298303
.. autoclass:: ToTensor
299-
:members: __call__
300-
:special-members:
304+
:members: __call__
305+
:special-members:
306+
301307

302308
Generic Transforms
303309
------------------

torchvision/transforms/transforms.py

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,18 @@ def __repr__(self):
145145

146146
class Normalize(object):
147147
"""Normalize a tensor image with mean and standard deviation.
148+
148149
Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels, this transform
149150
will normalize each channel of the input ``torch.*Tensor`` i.e.
150151
``input[channel] = (input[channel] - mean[channel]) / std[channel]``
151152
152153
.. note::
153-
This transform acts out of place, i.e., it does not mutates the input tensor.
154+
This transform acts out of place, i.e., it does not mutate the input tensor.
154155
155156
Args:
156157
mean (sequence): Sequence of means for each channel.
157158
std (sequence): Sequence of standard deviations for each channel.
158159
inplace(bool,optional): Bool to make this operation in-place.
159-
160160
"""
161161

162162
def __init__(self, mean, std, inplace=False):
@@ -165,13 +165,7 @@ def __init__(self, mean, std, inplace=False):
165165
self.inplace = inplace
166166

167167
def __call__(self, tensor):
168-
"""
169-
Args:
170-
tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
171-
172-
Returns:
173-
Tensor: Normalized Tensor image.
174-
"""
168+
"""Apply the transform to the given tensor and return the transformed tensor."""
175169
return F.normalize(tensor, self.mean, self.std, self.inplace)
176170

177171
def __repr__(self):
@@ -349,7 +343,7 @@ def __repr__(self):
349343

350344

351345
class RandomApply(RandomTransforms):
352-
"""Apply randomly a list of transformations with a given probability
346+
"""Apply randomly a list of transformations with a given probability.
353347
354348
Args:
355349
transforms (list or tuple): list of transformations
@@ -378,7 +372,7 @@ def __repr__(self):
378372

379373

380374
class RandomOrder(RandomTransforms):
381-
"""Apply a list of transformations in a random order
375+
"""Apply a list of transformations in a random order.
382376
"""
383377
def __call__(self, img):
384378
order = list(range(len(self.transforms)))
@@ -389,7 +383,7 @@ def __call__(self, img):
389383

390384

391385
class RandomChoice(RandomTransforms):
392-
"""Apply single transformation randomly picked from a list
386+
"""Apply single transformation randomly picked from a list.
393387
"""
394388
def __call__(self, img):
395389
t = random.choice(self.transforms)
@@ -706,7 +700,7 @@ def __init__(self, *args, **kwargs):
706700

707701

708702
class FiveCrop(object):
709-
"""Crop the given PIL Image into four corners and the central crop
703+
"""Crop the given PIL Image into four corners and the central crop.
710704
711705
.. Note::
712706
This transform returns a tuple of images and there may be a mismatch in the number of
@@ -746,7 +740,7 @@ def __repr__(self):
746740

747741
class TenCrop(object):
748742
"""Crop the given PIL Image into four corners and the central crop plus the flipped version of
749-
these (horizontal flipping is used by default)
743+
these (horizontal flipping is used by default).
750744
751745
.. Note::
752746
This transform returns a tuple of images and there may be a mismatch in the number of
@@ -819,13 +813,7 @@ def __init__(self, transformation_matrix, mean_vector):
819813
self.mean_vector = mean_vector
820814

821815
def __call__(self, tensor):
822-
"""
823-
Args:
824-
tensor (Tensor): Tensor image of size (C, H, W) to be whitened.
825-
826-
Returns:
827-
Tensor: Transformed image.
828-
"""
816+
"""Apply the transform to the given tensor and return the transformed tensor."""
829817
if tensor.size(0) * tensor.size(1) * tensor.size(2) != self.transformation_matrix.size(0):
830818
raise ValueError("tensor and transformation matrix have incompatible shape." +
831819
"[{} x {} x {}] != ".format(*tensor.size()) +
@@ -843,7 +831,7 @@ def __repr__(self):
843831

844832

845833
class ColorJitter(object):
846-
"""Randomly change the brightness, contrast and saturation of an image.
834+
"""Randomly change the brightness, contrast, saturation, and hue of an image.
847835
848836
Args:
849837
brightness (float or tuple of float (min, max)): How much to jitter brightness.
@@ -940,7 +928,7 @@ def __repr__(self):
940928

941929

942930
class RandomRotation(object):
943-
"""Rotate the image by angle.
931+
"""Rotate the image by degrees.
944932
945933
Args:
946934
degrees (sequence or float or int): Range of degrees to select from.
@@ -1013,7 +1001,7 @@ def __repr__(self):
10131001

10141002

10151003
class RandomAffine(object):
1016-
"""Random affine transformation of the image keeping center invariant
1004+
"""Random affine transformation of the image keeping center invariant.
10171005
10181006
Args:
10191007
degrees (sequence or float or int): Range of degrees to select from.
@@ -1212,9 +1200,11 @@ def __repr__(self):
12121200

12131201

12141202
class RandomErasing(object):
1215-
""" Randomly selects a rectangle region in an image and erases its pixels.
1216-
'Random Erasing Data Augmentation' by Zhong et al.
1217-
See https://arxiv.org/pdf/1708.04896.pdf
1203+
"""Randomly selects a rectangle region in an image and erases its pixels.
1204+
1205+
'Random Erasing Data Augmentation' by Zhong et al.
1206+
See https://arxiv.org/pdf/1708.04896.pdf
1207+
12181208
Args:
12191209
p: probability that the random erasing operation will be performed.
12201210
scale: range of proportion of erased area against input image.
@@ -1225,15 +1215,13 @@ class RandomErasing(object):
12251215
If a str of 'random', erasing each pixel with random values.
12261216
inplace: boolean to make this transform inplace. Default set to False.
12271217
1228-
Returns:
1229-
Erased Image.
1230-
# Examples:
1218+
Example:
12311219
>>> transform = transforms.Compose([
1232-
>>> transforms.RandomHorizontalFlip(),
1233-
>>> transforms.ToTensor(),
1234-
>>> transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
1235-
>>> transforms.RandomErasing(),
1236-
>>> ])
1220+
... transforms.RandomHorizontalFlip(),
1221+
... transforms.ToTensor(),
1222+
... transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
1223+
... transforms.RandomErasing(),
1224+
... ])
12371225
"""
12381226

12391227
def __init__(self, p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0, inplace=False):
@@ -1288,13 +1276,7 @@ def get_params(img, scale, ratio, value=0):
12881276
return 0, 0, img_h, img_w, img
12891277

12901278
def __call__(self, img):
1291-
"""
1292-
Args:
1293-
img (Tensor): Tensor image of size (C, H, W) to be erased.
1294-
1295-
Returns:
1296-
img (Tensor): Erased Tensor image.
1297-
"""
1279+
"""Apply the transform to the given tensor and return the transformed tensor."""
12981280
if random.uniform(0, 1) < self.p:
12991281
x, y, h, w, v = self.get_params(img, scale=self.scale, ratio=self.ratio, value=self.value)
13001282
return F.erase(img, x, y, h, w, v, self.inplace)

0 commit comments

Comments
 (0)