Skip to content

Tensor.to does not work with feature as reference #6094

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

Closed
pmeier opened this issue May 26, 2022 · 0 comments · Fixed by #6095
Closed

Tensor.to does not work with feature as reference #6094

pmeier opened this issue May 26, 2022 · 0 comments · Fixed by #6095

Comments

@pmeier
Copy link
Collaborator

pmeier commented May 26, 2022

torch.Tensor.to accepts a tensor reference object as input in which case it converts to the reference dtype and device:

>>> a = torch.zeros((), dtype=torch.float)
>>> b = torch.ones((), dtype=torch.int)
>>> b
tensor(1, dtype=torch.int32)
>>> b.to(a)
tensor(1.)

This also works for our custom features:

>>> data = torch.rand(3, 2, 2)
>>> image = features.Image(data.to(torch.float64))
>>> image
Image([[[0.9595, 0.0947],
        [0.9553, 0.5563]],
       [[0.5435, 0.2975],
        [0.3037, 0.0863]],
       [[0.6253, 0.3481],
        [0.3518, 0.4499]]], dtype=torch.float64)
>>> image.to(torch.float32)
Image([[[0.9595, 0.0947],
        [0.9553, 0.5563]],
       [[0.5435, 0.2975],
        [0.3037, 0.0863]],
       [[0.6253, 0.3481],
        [0.3518, 0.4499]]])
>>> image.to(data)
Image([[[0.9595, 0.0947],
        [0.9553, 0.5563]],
       [[0.5435, 0.2975],
        [0.3037, 0.0863]],
       [[0.6253, 0.3481],
        [0.3518, 0.4499]]])

However, it doesn't work if we want to convert a plain tensor and use a custom feature as reference:

>>> data.to(image)
AttributeError: 'Tensor' object has no attribute 'color_space'

cc @bjuncek @pmeier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant