Skip to content

[feature request] torch.as_tensor to support any object that NumPy's asarray or array can consume (consume __array_interface__) #58036

@vadimkantorov

Description

@vadimkantorov

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 for torch.as_tensor to support accepting __array_interface__ dicts directly. It may sometimes be conventient to store / manipulate these dictionaries directly, and then pass them to torch.as_tensor. Currently it produces Could 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 with np.asarray.

As side-effect, this would also eliminate the need for torchvision's F.to_tensor(pil_image)

Related: #54138

cc @mruberry @rgommers @heitorschueroff

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: numpyRelated to numpy support, and also numpy compatibility of our operatorstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions