question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Output shape of CropOrPad is not equal to target_shape when a mask is specified

See original GitHub issue

🐛Bug

The shape of the image returned by the transform CropOrPad does not match the target shape, depending on the values of target_shape and the image shape.

To reproduce

Re using the testing code from https://github.com/fepegar/torchio/pull/435/files:

import numpy as np
import torchio as tio

image = np.random.rand(1, 16, 16, 11)
mask = np.zeros_like(image, dtype=bool)
mask[0, 7, 5] = True
subject = tio.Subject(
    image=tio.ScalarImage(tensor=image),
    mask=tio.LabelMap(tensor=mask),
)
transform = tio.CropOrPad((12, 12, 1), mask_name='mask')
transformed = transform(subject)
assert transformed.shape == (1, 12, 12, 1), transformed.shape

In this case, the transformed image depth is equal to 0.

Other cases are failing: for instance if the image shape is (1, 16, 16, 10) and the target_shape is (12, 12, 3), the output image depth is 2.

Expected behavior

When specifying a mask_name, the output shape should always be equal to the target shape. I think the behavior is wrong only when mask_name is not None.

I believe there is a rounding error in this method https://github.com/fepegar/torchio/blob/master/torchio/transforms/preprocessing/spatial/crop_or_pad.py#L156.

Actual behavior

Depending on the image shape and the target shape, sometimes, the output shape is not equal to the target shape.

System info

Output of

Platform: macOS-10.14.6-x86_64-i386-64bit
TorchIO:  0.18.28
PyTorch:  1.7.1
NumPy:    1.19.4
Python:   3.8.5 (default, Sep  4 2020, 02:22:02)
[Clang 10.0.0 ]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
fabien-brulportcommented, Feb 12, 2021

Thanks a lot for the fix, this is working for me with the new release 0.18.29.

1reaction
fepegarcommented, Feb 12, 2021

Hi, @fabien-brulport. Thanks for reporting and for hinting at that code.

I have refactored that bit, removing all roundings. I’ll commit the fix in a second. I have also shamefully removed a failing unit test, but I’m quite confident that the new implementation should be more robust.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preprocessing - TorchIO - Read the Docs
RuntimeWarning – If a 4D image is masked with a 3D mask, the mask will be expanded ... Resample images so the output...
Read more >
IndexError: The shape of the mask [1, 1682] at index 0 does ...
In the test loop, you're re-using a variable of the training phase, which I think (you didn't provide the full stacktrace) is just...
Read more >
MSOT based on VXM - Kaggle
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager.
Read more >
https://raw.githubusercontent.com/fepegar/torchio-...
The cell is hidden by default on Google Colab because the code is not directly ... Python dictionary\n", "\n", "The output type will...
Read more >
IndexError: The shape of the mask at index 0 does not match ...
It cant be small, it's pretty much the whole dataset + training script. The sizes of the labels are specified above. My other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found