Skip to content

Negative padding for functional_tensor symmetric #2749

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 4 commits into from
Oct 5, 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: 0 additions & 3 deletions test/test_functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,6 @@ def test_pad(self):

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)
Expand Down
4 changes: 1 addition & 3 deletions test/test_transforms_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ def test_color_jitter(self):
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}
Expand Down
9 changes: 7 additions & 2 deletions torchvision/transforms/functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...]
Expand Down Expand Up @@ -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
Expand Down