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:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Thanks a lot for the fix, this is working for me with the new release
0.18.29
.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.