-
Notifications
You must be signed in to change notification settings - Fork 24.3k
[feature request] torch.as_tensor to support any object that NumPy's asarray or array can consume (consume __array_interface__) #58036
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
Comments
Thanks for this suggestion; I wonder if the Python Array API will also make a proposal here |
gh-54187 adds The array API standard asarray definition (the The use of |
I think both are important and useful. In some parts of code it's useful to be constraining and very explicit. In other parts of code it may be useful to swallow everything without having to think, is it PIL image, or NumPy or DLPack capsule from CuPy. If generic part is not there, users have to roll again and again their own checks of type name etc, which is more brittle and worse than tested library helper method for the same goal. |
@cpuhrsch At pytorch/vision#6278 (comment), it seems impossible to have a torch.is_tensor check and polymorphic code accepting both tensor and TensorList. It would be cool to be able to solve it somehow. Could it be done somehow without causing GPU->CPU synchronization by letting support torch.as_tensor consuming both Tensor and TensorLIst (doing then a torch.stack internally)? Or maybe at least torch.stack(x) should not do anything (at most a copy of input) if a tensor is provided as input and not a list? |
Uh oh!
There was an error while loading. Please reload this page.
This would enlarge the accepted set of inputs of
torch.as_tensor
and would support PIL images / h5py arrays. I think this feature request goes well in the theme of standardizing support for methods like__array_interface__
,__cuda_array_interface__
and such. It would be good fortorch.as_tensor
to support accepting__array_interface__
dicts directly. It may sometimes be conventient to store / manipulate these dictionaries directly, and then pass them totorch.as_tensor
. Currently it producesCould not infer dtype of dict
- which is also an unclear error message by the way.Currently, torch.as_tensor(pil_image) fails with
RuntimeError: Could not infer dtype of Image
, while it can be converted withnp.asarray
.As side-effect, this would also eliminate the need for torchvision's
F.to_tensor(pil_image)
Related: #54138
cc @mruberry @rgommers @heitorschueroff
The text was updated successfully, but these errors were encountered: