Unverified Commit 217e26fc authored by James Thewlis's avatar James Thewlis Committed by GitHub
Browse files

Negative padding for functional_tensor symmetric (#2749)



* Negative padding for functional_tensor symmetric

Along with #2744 this will make negative padding
uniform between PIL and Tensor #2381

* Enable tests for negative symmetric pad with tensor
Co-authored-by: default avatarvfdev <vfdev.5@gmail.com>
parent 6b41eb0b
......@@ -285,9 +285,6 @@ class Tester(TransformsTester):
self._test_fn_on_batch(batch_tensors, F.pad, padding=script_pad, **kwargs)
with self.assertRaises(ValueError, msg="Padding can not be negative for symmetric padding_mode"):
F_t.pad(tensor, (-2, -3), padding_mode="symmetric")
def _test_adjust_fn(self, fn, fn_pil, fn_t, configs, tol=2.0 + 1e-10, agg_method="max"):
script_fn = torch.jit.script(fn)
torch.manual_seed(15)
......
......@@ -123,9 +123,7 @@ class Tester(TransformsTester):
def test_pad(self):
for m in ["constant", "edge", "reflect", "symmetric"]:
fill = 127 if m == "constant" else 0
# Negative pad currently unsupported for Tensor and symmetric
multipliers = [1] if m == "symmetric" else [1, -1]
for mul in multipliers:
for mul in [1, -1]:
# Test functional.pad (PIL and Tensor) with padding as single int
self._test_functional_op(
"pad", fn_kwargs={"padding": mul * 2, "fill": fill, "padding_mode": m}
......
......@@ -621,6 +621,13 @@ def _hsv2rgb(img):
def _pad_symmetric(img: Tensor, padding: List[int]) -> Tensor:
# padding is left, right, top, bottom
# crop if needed
if padding[0] < 0 or padding[1] < 0 or padding[2] < 0 or padding[3] < 0:
crop_left, crop_right, crop_top, crop_bottom = [-min(x, 0) for x in padding]
img = img[..., crop_top:img.shape[-2] - crop_bottom, crop_left:img.shape[-1] - crop_right]
padding = [max(x, 0) for x in padding]
in_sizes = img.size()
x_indices = [i for i in range(in_sizes[-1])] # [0, 1, 2, 3, ...]
......@@ -723,8 +730,6 @@ def pad(img: Tensor, padding: List[int], fill: int = 0, padding_mode: str = "con
padding_mode = "replicate"
elif padding_mode == "symmetric":
# route to another implementation
if p[0] < 0 or p[1] < 0 or p[2] < 0 or p[3] < 0: # no any support for torch script
raise ValueError("Padding can not be negative for symmetric padding_mode")
return _pad_symmetric(img, p)
need_squeeze = False
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment