pytorch / vision

Datasets, Transforms and Models specific to Computer Vision
https://pytorch.org/vision
BSD 3-Clause "New" or "Revised" License
16.14k stars 6.94k forks source link

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

Open vadimkantorov opened 3 years ago

vadimkantorov commented 3 years ago

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

cc @vfdev-5

datumbox commented 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).

NicolasHug commented 3 years ago

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

vadimkantorov commented 3 years ago

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?

fmassa commented 3 years ago

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 commented 3 years ago

Some relevant discussion about copying or not here: https://github.com/pytorch/vision/pull/3813#pullrequestreview-657743067