-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Uniformize negative padding values support between PIL and Tensor #2381
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
Comments
I could try this. Potential ways to make it work for PIL are
Any reasons to prefer either one? |
These verbose but hopefully correct changes would make it work for the slicing approach: (edit: would also need to do the same for palette mode which is handled further up in the file) diff --git a/torchvision/transforms/functional_pil.py b/torchvision/transforms/functional_pil.py
index 693be83..acb09f2 100644
--- a/torchvision/transforms/functional_pil.py
+++ b/torchvision/transforms/functional_pil.py
@@ -339,6 +339,17 @@ def pad(img, padding, fill=0, padding_mode="constant"):
return img
img = np.asarray(img)
+
+ crop_top = -min(pad_top, 0)
+ crop_bottom = -min(pad_bottom, 0)
+ crop_left = -min(pad_left, 0)
+ crop_right = -min(pad_right, 0)
+
+ pad_top = max(pad_top, 0)
+ pad_bottom = max(pad_bottom, 0)
+ pad_left = max(pad_left, 0)
+ pad_right = max(pad_right, 0)
+
# RGB image
if len(img.shape) == 3:
img = np.pad(img, ((pad_top, pad_bottom), (pad_left, pad_right), (0, 0)), padding_mode)
@@ -346,7 +357,7 @@ def pad(img, padding, fill=0, padding_mode="constant"):
if len(img.shape) == 2:
img = np.pad(img, ((pad_top, pad_bottom), (pad_left, pad_right)), padding_mode)
- return Image.fromarray(img)
+ return Image.fromarray(img[crop_top:img.shape[0] - crop_bottom, crop_left:img.shape[1] - crop_right])
@torch.jit.unused
(Or with .crop(): Modified test scriptimport torch
import torchvision
from torchvision.transforms.functional import pad, to_pil_image
import numpy as np
print(torch.__version__, torchvision.__version__)
x = torch.randint(0, 256, size=(3, 32, 32), dtype=torch.uint8)
x_pil = to_pil_image(x)
padding_examples = [(-1, -2, -3, -4),
(1, 2, 3, 4),
(-1, 2, -3, 4),
(1, 2, -3, -4),
(1, 2, 3, -4)]
for padding in padding_examples:
print('Test', padding)
refs = {}
for m in ["constant", "edge", "reflect"]:
try:
refs[m] = pad(x, padding, padding_mode=m)
except ValueError:
print("Tensor: Failed with ", m)
for m in ["constant", "edge", "reflect", "symmetric"]:
try:
y = pad(x_pil, padding, padding_mode=m)
if m in refs:
y = np.array(y)
z = np.array(refs[m])
print(z.shape)
print(y.shape)
print(np.allclose(z.transpose(1, 2, 0), y))
except ValueError:
print("PIL: Failed with ", m) |
@jamt9000 thanks for the suggestions and sorry for late reply. Can we just use PIL |
Along with pytorch#2744 this will make negative padding uniform between PIL and Tensor pytorch#2381
* Negative padding for functional_pil #2381 * Tests for PIL negative padding #2381 * Move PIL vs tensor test inside test_pad * Adapt test_pad from test_transforms_tensor.py Co-authored-by: vfdev <[email protected]>
* 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: vfdev <[email protected]>
* Negative padding for functional_pil pytorch#2381 * Tests for PIL negative padding pytorch#2381 * Move PIL vs tensor test inside test_pad * Adapt test_pad from test_transforms_tensor.py Co-authored-by: vfdev <[email protected]>
* Negative padding for functional_tensor symmetric Along with pytorch#2744 this will make negative padding uniform between PIL and Tensor pytorch#2381 * Enable tests for negative symmetric pad with tensor Co-authored-by: vfdev <[email protected]>
* Negative padding for functional_pil pytorch#2381 * Tests for PIL negative padding pytorch#2381 * Move PIL vs tensor test inside test_pad * Adapt test_pad from test_transforms_tensor.py Co-authored-by: vfdev <[email protected]>
* Negative padding for functional_tensor symmetric Along with pytorch#2744 this will make negative padding uniform between PIL and Tensor pytorch#2381 * Enable tests for negative symmetric pad with tensor Co-authored-by: vfdev <[email protected]>
🚀 Feature
Currently, negative padding values for
functional.pad
are not supported for any kind of padding mode and data typeThis is due to
np.pad
used internally for PIL input.Motivation
It would be better to provide uniform behaviour for
pad
with respect of input data type and negative padding values:The text was updated successfully, but these errors were encountered: