Skip to content

Uniformize negative padding values support between PIL and Tensor #2381

@vfdev-5

Description

@vfdev-5

🚀 Feature

Currently, negative padding values for functional.pad are not supported for any kind of padding mode and data type

import torch
import torchvision
from torchvision.transforms.functional import pad, to_pil_image

print(torch.__version__, torchvision.__version__)

x = torch.randint(0, 256, size=(3, 32, 32), dtype=torch.uint8)
x_pil = to_pil_image(x)
padding = (-1, -2, -3, -4)

for m in ["constant", "edge", "reflect"]:
    try:
        pad(x, padding, padding_mode=m)
    except ValueError:
        print("Tensor: Failed with ", m)

for m in ["constant", "edge", "reflect", "symmetric"]:
    try:
        pad(x_pil, padding, padding_mode=m)
    except ValueError:
        print("PIL: Failed with ", m)

> 1.7.0.dev20200701 0.8.0.dev20200701
> PIL: Failed with  edge
> PIL: Failed with  reflect
> PIL: Failed with  symmetric

This 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:

  • raise an error
  • support it in the cases

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions