Skip to content
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

Allow for transforms.ToTensor to return a Tensor of type torch.uint8 #1595

Closed
r-zenine opened this issue Nov 19, 2019 · 5 comments
Closed

Allow for transforms.ToTensor to return a Tensor of type torch.uint8 #1595

r-zenine opened this issue Nov 19, 2019 · 5 comments

Comments

@r-zenine
Copy link
Contributor

Hi Everyone,

This is my first issue in the project. Please forgive me if I overstep.

I would like to have the ability when I transform PIL images using Torchvision to get back a uint8 Tensor.

I am reimplementing a flavor of DQN with experience replay where I have to store frames of an atari game in a big buffer. Storing these tensors as uint8 as opposed to floats allows me to save some memory and have bigger buffers.

I don't know if it is the feature requested is even relevant for you and whether it makes sense to update ToTensor to do it.

I would like to contribute to PyTorch more in the future because I am a big fan of this piece of technology and would be willing to do the work as my first modest contribution if you think this can be useful.

Thank you all

@pmeier
Copy link
Collaborator

pmeier commented Nov 19, 2019

Hi @r-zenine

as of now images in torchvision are always torch.FloatTensor and it is assumed that the values lie in the interval between 0 and 1. Thus, I think, having it as a parameter in transforms.ToTensor is not useful at the moment. However, there is a movement to also support torch.ByteTensor (uint8) images in the future. (I think I read that proposal somewhere, but I don't find the issue / PR. @fmassa can you help me out here?)


I would like to contribute to PyTorch more in the future because I am a big fan of this piece of technology and would be willing to do the work as my first modest contribution if you think this can be useful.

That is great. You could look at issues tagged with help wanted. You can do so by adding label:"help wanted" in the issue search bar or click on the label of another issue tagged with it. In the context of torchvision.transforms #1375 could be a good starting point. Some reasonable simple transformations as center_crop are still open.


From your comment, I'm not sure if you know how to implement a custom transformation that does what you want. If you need help, feel free to ask about it in another comment.

@r-zenine
Copy link
Contributor Author

r-zenine commented Nov 19, 2019

Hi @pmeier,

I wanted to be as respectful as possible in my comment as not to overstep. Since I am fairly new to Torchvision, I assumed I might not be aware of some design decisions taken by the project and I did not want to be rude!

What is the rationale behind wanting only torch.FloatTensor in Torchvision ? Of course, there is the fact that convnets expect that an input. But am I missing something else?

@pmeier Thanks for suggesting #1375, I will take a look this week-end and try to put something together and maybe ask questions here if I need to.

@fmassa
Copy link
Member

fmassa commented Nov 20, 2019

Hi @r-zenine ,

Thanks for opening the issue!

As @pmeier pointed out, we currently always return float tensors between 0 and 1. This is good for consistency: all images are float in 0-1.
We could have other conventions, as native uint8 (in 0-255), or float in 0-255 for example.
We use float 0-1 for legacy reasons (Lua Torch used images in float 0-1), but it generally works ok.

The discussion that @pmeier mentioned I believe is #1179
In particular, I discuss the uint8 and storing in 0-1 or 0-255 in there.

Have a look and let me know your thoughts!

@r-zenine
Copy link
Contributor Author

Hi @fmassa,

Thank you very much for your response.

I am sorry, I would've liked to look at it now. Unfortunatly, I am a bit short on time today and tomorrow. If it's okay for you ? I will take a look at everything this week-end and share my thoughts.

Thanks,

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Oct 22, 2020

@r-zenine there can be also an option to use a custom ToTensor transform like it is done for targets in segmentation task:

class ToTensor(object):
def __call__(self, image, target):
image = F.to_tensor(image)
target = torch.as_tensor(np.asarray(target), dtype=torch.int64)
return image, target

Let me close the issue as stale, @r-zenine feel free to reopen if you need more help on that. Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants