Skip to content

Invertd Not Working With Resized #4907

@lukepolson

Description

@lukepolson

Invertd is not able to invert Resized for data points that aren't changed in size. Consider the following very basic example: I have a pipeline that resizes all axial slices to a size of 128x128:

transform = Compose([Resized('X', (-1,128,128))])

Suppose I have the following data I want to resize. Some of the data already has slices of size 128x128, but not all:

test_input = [{'X': torch.ones((1,10,128,128))}, {'X': torch.ones((1,10,144,144))}]

I can apply the transform to resize all data points such that they have axial slices of size 128x128:

test_output = transform(test_input)

The issue then comes if I want to invert resize some output data. For example, consider the very basic neural network that simply multiplies all values by 2:

for d in test_output:
    d['Y'] = 2*d['X']

I want to resize the Ys such that they correspond with the original Xs. I use an inverse transform:

transform_inverse = Invertd(keys='Y', transform=transform, orig_keys='X')

Now the following command will run successfully (recall that index 1 refers to the data point that was initially 144x144):

transform_inverse(test_output[1])

but the following won't run (recall that index 0 refers to the data point that was initially 128x128, and so Resized wouldn't modify it):

transform_inverse(test_output[0])

I believe this is because Resized isn't actually applied to this data point (because its already the right size) and thus it isn't tracked in the transforms applied to X (and thus the inverse can't be applied to Y). Obviously there are hack-ish ways to get around this, but it becomes annoying when you're dealing with a transform that has many different operations, and dealing with a large dataset.

Solution: I just commented out 845-846 of monai.transforms.spatial.array

if tuple(img.shape[1:]) == spatial_size_:  # spatial shape is already the desired
    return convert_to_tensor(img, track_meta=get_track_meta())  # type: ignore

but you might want to modify them so that transform metadata is stored.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions