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

metatensor einops rearrange #6397

Open
wyli opened this issue Apr 19, 2023 · 1 comment
Open

metatensor einops rearrange #6397

wyli opened this issue Apr 19, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@wyli
Copy link
Contributor

wyli commented Apr 19, 2023

Describe the bug

import torch
from monai.data import MetaTensor
from einops import rearrange
from monai.transforms import Resize

# create a metatensor in batch mode
x = MetaTensor(torch.randn((1, 1, 481, 404, 3))).to("cuda:0")
x.is_batch = True
x.meta['affine'] = torch.eye(4)[None]

# select the first element in the batch and resize
out = Resize(spatial_size=(1024, 860, -1), align_corners=True, mode="trilinear")(x[0])
print(out.shape)  # works fine

# rearrange using einops
x_ = rearrange(x, "b c h w d -> (b c) h w d")
out = Resize(spatial_size=(1024, 860, -1), align_corners=True, mode="trilinear")(x_)
print(out.shape)  # error

error:

    affine = orig_affine @ to_affine_nd(len(orig_affine) - 1, affine, dtype=torch.float64)
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 4)

workaround (convert to a regular tensor as_tensor()):

x_ = rearrange(x, "b c h w d -> (b c) h w d")
if isinstance(x_, MetaTensor):
    x_ = x_.as_tensor()
out = Resize(spatial_size=(1024, 860, -1), align_corners=True, mode="trilinear")(x_)
print(out.shape)
@wyli wyli added the enhancement New feature or request label Apr 19, 2023
@aymuos15
Copy link

aymuos15 commented Sep 3, 2024

I want to pick up this if still relevant. Could someone give a starting guide as to how I could approach this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants