Skip to content

Fixed problem with test error computation between results #2964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion test/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ def approxEqualTensorToPIL(self, tensor, pil_image, tol=1e-5, msg=None, agg_meth
if np_pil_image.ndim == 2:
np_pil_image = np_pil_image[:, :, None]
pil_tensor = torch.as_tensor(np_pil_image.transpose((2, 0, 1))).to(tensor)
err = getattr(torch, agg_method)(tensor - pil_tensor).item()
# error value can be mean absolute error, max abs error
err = getattr(torch, agg_method)(torch.abs(tensor - pil_tensor)).item()
self.assertTrue(
err < tol,
msg="{}: err={}, tol={}: \n{}\nvs\n{}".format(msg, err, tol, tensor[0, :10, :10], pil_tensor[0, :10, :10])
Expand Down
4 changes: 2 additions & 2 deletions test/test_functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ def test_adjust_hue(self):
F_pil.adjust_hue,
F_t.adjust_hue,
[{"hue_factor": f} for f in [-0.45, -0.25, 0.0, 0.25, 0.45]],
tol=0.1,
agg_method="mean"
tol=16.1,
agg_method="max"
)

def test_adjust_gamma(self):
Expand Down
4 changes: 2 additions & 2 deletions test/test_transforms_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ def test_color_jitter(self):
for f in [0.2, 0.5, (-0.2, 0.3), [-0.4, 0.5]]:
meth_kwargs = {"hue": f}
self._test_class_op(
"ColorJitter", meth_kwargs=meth_kwargs, test_exact_match=False, tol=0.1, agg_method="mean"
"ColorJitter", meth_kwargs=meth_kwargs, test_exact_match=False, tol=16.1, agg_method="max"
)

# All 4 parameters together
meth_kwargs = {"brightness": 0.2, "contrast": 0.2, "saturation": 0.2, "hue": 0.2}
self._test_class_op(
"ColorJitter", meth_kwargs=meth_kwargs, test_exact_match=False, tol=0.1, agg_method="mean"
"ColorJitter", meth_kwargs=meth_kwargs, test_exact_match=False, tol=12.1, agg_method="max"
)

def test_pad(self):
Expand Down
36 changes: 13 additions & 23 deletions torchvision/transforms/functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,32 +822,19 @@ def resize(img: Tensor, size: List[int], interpolation: int = 2) -> Tensor:
if (w <= h and w == size_w) or (h <= w and h == size_h):
return img

# make image NCHW
need_squeeze = False
if img.ndim < 4:
img = img.unsqueeze(dim=0)
need_squeeze = True

mode = _interpolation_modes[interpolation]

out_dtype = img.dtype
need_cast = False
if img.dtype not in (torch.float32, torch.float64):
need_cast = True
img = img.to(torch.float32)
img, need_cast, need_squeeze, out_dtype = _cast_squeeze_in(img, [torch.float32, torch.float64])

# Define align_corners to avoid warnings
align_corners = False if mode in ["bilinear", "bicubic"] else None

img = interpolate(img, size=[size_h, size_w], mode=mode, align_corners=align_corners)

if need_squeeze:
img = img.squeeze(dim=0)
if mode == "bicubic" and out_dtype == torch.uint8:
img = img.clamp(min=0, max=255)

if need_cast:
if mode == "bicubic":
img = img.clamp(min=0, max=255)
img = img.to(out_dtype)
img = _cast_squeeze_out(img, need_cast=need_cast, need_squeeze=need_squeeze, out_dtype=out_dtype)

return img

Expand Down Expand Up @@ -879,7 +866,7 @@ def _assert_grid_transform_inputs(
raise ValueError("Resampling mode '{}' is unsupported with Tensor input".format(resample))


def _cast_squeeze_in(img: Tensor, req_dtype: torch.dtype) -> Tuple[Tensor, bool, bool, torch.dtype]:
def _cast_squeeze_in(img: Tensor, req_dtypes: List[torch.dtype]) -> Tuple[Tensor, bool, bool, torch.dtype]:
need_squeeze = False
# make image NCHW
if img.ndim < 4:
Expand All @@ -888,8 +875,9 @@ def _cast_squeeze_in(img: Tensor, req_dtype: torch.dtype) -> Tuple[Tensor, bool,

out_dtype = img.dtype
need_cast = False
if out_dtype != req_dtype:
if out_dtype not in req_dtypes:
need_cast = True
req_dtype = req_dtypes[0]
img = img.to(req_dtype)
return img, need_cast, need_squeeze, out_dtype

Expand All @@ -899,15 +887,17 @@ def _cast_squeeze_out(img: Tensor, need_cast: bool, need_squeeze: bool, out_dtyp
img = img.squeeze(dim=0)

if need_cast:
# it is better to round before cast
img = torch.round(img).to(out_dtype)
if out_dtype in (torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64):
# it is better to round before cast
img = torch.round(img)
img = img.to(out_dtype)

return img


def _apply_grid_transform(img: Tensor, grid: Tensor, mode: str) -> Tensor:

img, need_cast, need_squeeze, out_dtype = _cast_squeeze_in(img, grid.dtype)
img, need_cast, need_squeeze, out_dtype = _cast_squeeze_in(img, [grid.dtype, ])

if img.shape[0] > 1:
# Apply same grid to a batch of images
Expand Down Expand Up @@ -1168,7 +1158,7 @@ def gaussian_blur(img: Tensor, kernel_size: List[int], sigma: List[float]) -> Te
kernel = _get_gaussian_kernel2d(kernel_size, sigma, dtype=dtype, device=img.device)
kernel = kernel.expand(img.shape[-3], 1, kernel.shape[0], kernel.shape[1])

img, need_cast, need_squeeze, out_dtype = _cast_squeeze_in(img, kernel.dtype)
img, need_cast, need_squeeze, out_dtype = _cast_squeeze_in(img, [kernel.dtype, ])

# padding = (left, right, top, bottom)
padding = [kernel_size[0] // 2, kernel_size[0] // 2, kernel_size[1] // 2, kernel_size[1] // 2]
Expand Down