Skip to content

Commit c26baa9

Browse files
committed
Relaxed tolerance in consistency tests for GaussianBlur and ElasticTransform
1 parent ca3ef2b commit c26baa9

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

test/test_prototype_transforms_consistency.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ def __init__(
323323
],
324324
# ElasticTransform needs larger images to avoid the needed internal padding being larger than the actual image
325325
make_images_kwargs=dict(DEFAULT_MAKE_IMAGES_KWARGS, sizes=[(163, 163), (72, 333), (313, 95)]),
326+
# We updated gaussian blur kernel generation with a faster and numerically more stable version
327+
# This brings float32 accumulation visible in elastic transform -> we need to relax consistency tolerance
328+
closeness_kwargs={"rtol": 1e-1, "atol": 1},
326329
),
327330
ConsistencyConfig(
328331
prototype_transforms.GaussianBlur,
@@ -333,6 +336,7 @@ def __init__(
333336
ArgsKwargs(kernel_size=3, sigma=0.7),
334337
ArgsKwargs(kernel_size=5, sigma=(0.3, 1.4)),
335338
],
339+
closeness_kwargs={"rtol": 1e-5, "atol": 1e-5},
336340
),
337341
ConsistencyConfig(
338342
prototype_transforms.RandomAffine,

torchvision/transforms/functional_tensor.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import math
21
import warnings
32
from typing import List, Optional, Tuple, Union
43

@@ -727,10 +726,13 @@ def perspective(
727726
return _apply_grid_transform(img, grid, interpolation, fill=fill)
728727

729728

730-
def _get_gaussian_kernel1d(kernel_size: int, sigma: float) -> torch.Tensor:
731-
lim = (kernel_size - 1) / (2 * math.sqrt(2) * sigma)
732-
x = torch.linspace(-lim, lim, steps=kernel_size)
733-
kernel1d = torch.softmax(-x.pow_(2), dim=0)
729+
def _get_gaussian_kernel1d(kernel_size: int, sigma: float) -> Tensor:
730+
ksize_half = (kernel_size - 1) * 0.5
731+
732+
x = torch.linspace(-ksize_half, ksize_half, steps=kernel_size)
733+
pdf = torch.exp(-0.5 * (x / sigma).pow(2))
734+
kernel1d = pdf / pdf.sum()
735+
734736
return kernel1d
735737

736738

0 commit comments

Comments
 (0)