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.

Unify Tensor and PIL transforms

See original GitHub issue

Now that most of the tensor transforms have been implemented https://github.com/pytorch/vision/issues/1375, it is time to make unify the implementations for PIL and Tensor so that torchvision.transforms and torchvision.transforms.functional works seamlessly between both datatypes.

As such, we would like that the following work with the same interface:

# functional interface
torchvision.transforms.functional.hflip(pil_image)
torchvision.transforms.functional.hflip(tensor_image)

# class interface
transform = torchvision.transforms.RandomHorizontalFlip(0.3)
transform(pil_image)
transform(tensor_image)

# torchscript support for tensor_image
script_transform = torch.jit.script(transform)
script_transform(tensor_image)

Example PRs adding support for hflip and vflip: https://github.com/pytorch/vision/pull/2282 https://github.com/pytorch/vision/pull/2283

We would like to add support for torchscript to the torchvision.transforms interface as well, which for simplicity might require a few changes. Most notably we would need the transforms to inherit from nn.Module.

Here is a list of transforms that can be readily converted:

  • adjust_brightness
  • adjust_contrast
  • adjust_saturation
  • crop
  • center_crop
  • five_crop
  • ten_crop
  • pad (#2373 #2345)
  • resize (#2394)
  • resized_crop (#2396)
  • perspective (#2558, #2561)
  • adjust_hue (#2566)
  • adjust_gamma (#2459)
  • rotate (#2495 #2496 #2553)
  • affine (#2444, #2468, #2478, #2553)
  • to_grayscale

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
voldemortXcommented, Oct 25, 2020

@voldemortX thanks for the feedback !

Wondering whether future commits will support fill color other than 0 for tensor affine transformations? It is kind of important for tasks like semantic segmentation when transforming labels.

Could you please open an issue for that such we could track this feature request and see what could be possible to do. Currently, the blocker is with pytorch grid_sample which has

padding_mode=“zeros”: use 0 for out-of-bound grid locations

New issue opened #2887

1reaction
vfdev-5commented, Sep 3, 2020

The issue can be closed as all mentioned transformations now support tensor and pil image as input.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Transforming and augmenting images - PyTorch
Most transformations accept both PIL images and tensor images, although some transformations are PIL-only and some are tensor-only.
Read more >
Reshaping torch tensors of PIL images leads to multiple gray ...
ToTensor permutes the array and returns a tensor in the shape [C, H, W]. Using reshape to swap the order of dimensions is...
Read more >
Source code for torchvision.transforms - chsasank.com
ndarray`` to tensor. Converts a PIL Image or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of...
Read more >
Complete Guide to the DataLoader Class in PyTorch
This post covers the PyTorch dataloader class. We'll show how to load built-in and custom datasets in PyTorch, plus how to transform and...
Read more >
Transforms — MONAI 1.0.1 Documentation
If data is of type MetaTensor , then the applied transform will be added to ... of the TorchVision transforms only work for...
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