pytorch / pytorch/vision

Parameter for transforms.ToTensor

Open
#4,210 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

module: transforms
Dominant language
Python
Stars
17.9k
Forks
7.3k
Avg merge
1d 15h
Merged PRs (30d)
13

Description

🚀 Feature

The method converts a PIL Image or numpy.ndarray to a tensor with scaling pixel values to the range [0,1]. The new parameter will provide an option to the user whether the pixel values will be scaled or not.

Motivation

I am working on Variational Encoders on images which requires processing images. I use a categorical distribution for the images to predict every pixel value which requires each pixel value to stay in the range [0, 255]. I use datasets from torchvision.datasets. However, when I load the data, the type is as PIL Image, and the only transform that converts the image to a tensor is ToTensor() and it scales the pixel values. I think this is really impractical because I think there should be an opiton for the user so it can be a generalizable method for every use. Thus, I ended up taking the method definitions from library and change it instead of using the package for transforms itself.

Pitch

I would suggest adding a parameter "scale" to the transform ToTensor(scale=None) or ToTensor(scale=[0, 1]) which scales the input based on the appropriate input or othwersie does what it already does as default. I saw few methods before with this scaling option so I found it appropriate and the scaling in the original method only happens in line of code so it should be easy to make it optional. Or otherwise I do not understand the motivation to not put an option even though I understand a lot of time the input is normalized by the user in application.

Alternatives

Alternatively, it could be a boolean variable which gives the option to whether scale or not if the math could cause any unwanted results.

Additional context

NA

cc @vfdev-5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the transforms.ToTensor entry point and review how it currently scales PIL images or numpy.ndarray values. Define the parameter behavior and verify that the default remains unchanged while an option preserves pixel values in the [0,255] range.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-vision
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.