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

[docs] Unclear if to_pil_image / to_tensor copy or zero-copy for CPU<->CPU #3783

Open
vadimkantorov opened this issue May 6, 2021 · 5 comments

Comments

@vadimkantorov
Copy link

vadimkantorov commented May 6, 2021

It currently uses a vague language "convert". It's not sure if "conversion" incurs a copy or not

cc @vfdev-5

@datumbox
Copy link
Contributor

datumbox commented May 6, 2021

I believe these two methods copy. This is because they go from one backend to a completely different one (say from PIL to PyTorch Tensor stored on GPU or vice-versa).

@NicolasHug
Copy link
Member

This is worth double-checking but in theory a no-copy should be possible, and I agree it would be valuable info to explicitly document this. PIL images implement __array_interface__ (https://numpy.org/doc/stable/reference/arrays.interface.html) so one can make no-copy conversions PIL <-> numpy, and there are ongoing efforts regarding tensor <-> numpy too: pytorch/pytorch#22402

@vadimkantorov
Copy link
Author

I'm meaning CPU<-> CPU conversions of course, where zero-copy conversion theoretically is possible, e.g. identically to NumPy <-> PyTorch conversion

If PIL implements __array_interface__, could the conversion be in essence torch.as_tensor and a permute?

@vadimkantorov vadimkantorov changed the title [docs] Unclear if to_pil_image / to_tensor copy or zero-copy [docs] Unclear if to_pil_image / to_tensor copy or zero-copy for CPU<->CPU May 7, 2021
@fmassa
Copy link
Member

fmassa commented May 10, 2021

Those functions could be zero-copy, except that we perform multiplication / division by 255, which means there will be overhead involved.

If you want zero-overhead, I would recommend using pil_to_tensor

@datumbox
Copy link
Contributor

Some relevant discussion about copying or not here: #3813 (review)

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

5 participants