to_tensor and pil_to_tensor inconsistency
See original GitHub issueto_tensor
uses from_numpy
which never copies and always returns a CPU tensor. But pil_to_tensor
uses as_tensor
, which would return a CUDA tensor if the default tensor type is changed by the user (e.g., https://github.com/pytorch/pytorch/issues/39088). I think a discussion on what the correct behavior is is needed, followed with a patch to make the two functions consistent.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Pytorch transform.ToTensor() changes image
I want to convert images to tensor using torchvision.transforms.ToTensor() , after processing I printed the image but the image became so ...
Read more >Correct order for torchvision.transforms? (inconsistent ...
Hi @mrdbourke remember that ToTensor() normalize the image between 0 and 1 but RandAugment can be applit to a Tensor (that's what ToTensor() ......
Read more >10 PyTorch Transformations for Data Scientists
In this blog, I'll be using an image of Ragnar (my favorite fictional character) to perform transforms. PyTorch Transformations Totensor. 2.
Read more >Converting an image to a Torch Tensor in Python
PILToTensor () or transforms.ToTensor(). Convert the image to tensor using the above-defined transform. Print the tensor values. The below image ...
Read more >transforms.ToTensor() 与transforms.PILToTensor()_GIS
img = PilImage.open(path) img = np.array(img) print(img) #不改变像素值大小[0~255] print(transforms.ToTensor()(img )) #改变像素值大小.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s been in pytorch since a long time ago. Probably before 1.0 if I remember correctly. As long as you return a CUDA tensor from your dataset code and use proper start method, the main process will receive a CUDA tensor, regardless of num_workers.
It is always not advertised nor recommended because it makes CUDA memory management difficult, and that moving to GPU really fast and not usually the bottleneck as long as you use pin_memroy.
This issue is not about whether pytorch supports loading CUDA tensors. It is about torchvision, a library built on top of pytorch, should return CUDA tensor or not in ToTensor that is called in the dataset code. Any dataset code that yields CUDA tensors has always been working fine.
I’m not quite sure about that? Is their any way to useDataLoader
to load data to GPU? Currently, when I setnum_workers=0
orspawn
start method, I still get CPU tensors based on what you have illustrated.If I useDataloader
, I need to transform them tocuda
manually. If I want to load data directly to gpu, I need to code the pipeline indali
by myself.I might be not familiar with new pytorch features. Is there any update about that? It’ll be a great news to know it’s supported.I see that the imagenet example still need to usecuda()
to transform data to gpu, which is originally loaded fromDataLoader
.~~https://github.com/pytorch/examples/blob/master/imagenet/main.py#L283-L284~~
Update: Ah, it’s always doable if
torch.utils.data.Dataset
return a CUDA tensor, which could be implemented by setting transforms intorchvision.datasets.XXX
. Sorry for the misleading information above, and it’s certainly not recommended. I only considered some direct-to-gpu strategy likedali
in previous statement.The purpose of this issue should be the functionality about
ToTensor()
, whether it always transform data to cpu, or transform according to the default tensor type. My opinion is to forcecpu
as a quick fix for consistency, and discuss the feasibility about the latter method later.