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

pil_to_tensor returns non-contiguous tensor #4199

Closed
aaronzs opened this issue Jul 20, 2021 · 3 comments
Closed

pil_to_tensor returns non-contiguous tensor #4199

aaronzs opened this issue Jul 20, 2021 · 3 comments

Comments

@aaronzs
Copy link

aaronzs commented Jul 20, 2021

🐛 Bug torchvision.transforms.functional.pil_to_tensor returns non-contiguous tensor

To Reproduce

from PIL import Image
from torchvision.transforms.functional import pil_to_tensor

pic = Image.open("3_channel_pic.png")
pil_to_tensor(pic).is_contiguous()
# False

Expected behavior

.contiguous() is recommended to be used after .permute(), so that

  1. consistent behavior with to_tensor();
  2. increase computational efficiency

cc @vfdev-5

@datumbox
Copy link
Contributor

I believe this would require copying the memory here:

img = img.permute((2, 0, 1))

I think the original implementation was written to avoid this. Any thoughts @vfdev-5 ?

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Jul 29, 2021

Yes, img.permute((2, 0, 1)) returns non-contiguous tensor and avoids data copy. Here is a discussion on why keeping channels last memory format here: #2092 (comment)

@datumbox
Copy link
Contributor

@aaronzs I believe the above addresses the issue, so to keep things tidy I'm closing the ticket. If you still have concerns feel free to reopen.

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

3 participants