Open vadimkantorov opened 3 years ago
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).
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: https://github.com/pytorch/pytorch/issues/22402
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
?
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
Some relevant discussion about copying or not here: https://github.com/pytorch/vision/pull/3813#pullrequestreview-657743067
It currently uses a vague language "convert". It's not sure if "conversion" incurs a copy or not
cc @vfdev-5