question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`F.pad` is broken for PIL images and `fill: str`

See original GitHub issue

On the surface F.pad for PIL images seems to support str arguments for fill:

https://github.com/pytorch/vision/blob/053e7ebd1b50b7a506cfd6e2389043ec05414b1c/torchvision/transforms/functional_pil.py#L157-L158

Internally though, it calls _parse_fill

https://github.com/pytorch/vision/blob/053e7ebd1b50b7a506cfd6e2389043ec05414b1c/torchvision/transforms/functional_pil.py#L304-L308

which in most cases relies on fill being numeric. Thus, actually passing a string doesn’t work although PIL supports it.

>>> image = PIL.Image.fromarray(torch.randint(0, 256, (256, 256, 3), dtype=torch.uint8).numpy())
>>> F.pad(image, 128, fill="black")
ValueError: invalid literal for int() with base 10: 'black'
>>> PIL.ImageOps.expand(image, border=128, fill="black")
<PIL.Image.Image image mode=RGB size=512x512 at 0x7FA4DD37FBD0>

This traces back to #2284 and specifically to

https://github.com/pytorch/vision/blob/37a0d8d68ab2c537d2cda1136d0366874d2fad5f/torchvision/transforms/functional.py#L334-L337

which will makes no sense for fill: str.

Given that #2284 was included from 0.7.0 upwards and we haven’t gotten any report yet, my guess is that the functionality is not really used. Since we want to deprecate fill: str anyway (https://github.com/pytorch/vision/pull/5621#discussion_r827271131), I would opt to simply remove all mentions of it. Thoughts?

cc @vfdev-5 @datumbox

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pmeiercommented, Mar 16, 2022

Looking again for all str mentions, the only additional mention of a str representing a numeric value is RandomErasing as you found. Still, value: str is actually not correct here. It should be value: Literal["random"]:

https://github.com/pytorch/vision/blob/9edd22c8a07a8c99324a73ec4dd6c553e2c8d410/torchvision/transforms/transforms.py#L1653-L1656

1reaction
datumboxcommented, Mar 16, 2022

That’s indeed a very convenient bug. We should remove it rather than fix it, I agree. Nevertheless we should consider removing/deprecating string colours from the transforms not only for pad but also for all other transforms as these won’t be compatible with the new API for non-PIL images.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pad an image on all sides in PyTorch? - GeeksforGeeks
Pad () method is used for padding an image. This method accepts images like PIL Image and Tensor Image. The tensor image is...
Read more >
torchvision.transforms - PyTorch
All transformations accept PIL Image, Tensor Image or batch of Tensor Images as ... constant: pads with a constant value, this value is...
Read more >
Saving a Numpy array as an image - python - Stack Overflow
An answer using PIL (just in case it's useful). given a numpy array "A": from PIL import Image im = Image.fromarray(A) im.save("your_file.jpeg").
Read more >
PIL.TiffImagePlugin - Pillow (PIL Fork) 9.3.0 documentation
Source code for PIL.TiffImagePlugin. # # The Python Imaging Library. # $Id$ # # TIFF file handling # # TIFF is a flexible,...
Read more >
Pillow 2.2.1 - PyPI
Pillow. Python Imaging Library (Fork). Pillow is the “friendly” PIL fork by Alex Clark and Contributors. PIL is the Python Imaging Library by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found