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.

CropOrPad only crops Mask

See original GitHub issue

TorchIO version 0.15.0

What I did Create the following sequence of transforms:

input_H = 160
input_W = 160
input_D = 80

transforms = [
    torchio.transforms.ToCanonical(),
    torchio.transforms.Resample(target_spacing=(1,1,1)),
    torchio.transforms.RescaleIntensity(out_min_max=(0,1),percentiles=(0.5,99.5)),
    torchio.transforms.ZNormalization(),
    torchio.transforms.CropOrPad(target_shape=(input_D,input_H,input_W),padding_mode='constant',padding_fill=0,mask_name='mask'),
]
transform = Compose(transforms)

dataset = ImagesDataset(subjects, transform=transform)
sample = dataset[0]

I then checked the size of one of the images in the sample e.g. sample['t2']['data'].shape which returns torch.Size([1, 247, 345, 346]) whereas sample['mask']['data'].shape returns torch.Size([1, 80, 160, 160])

What I expected to happen Both the image and mask have the same size

What actually happened The sizes are different.

Possible reason In the crop and pad function (here) these lines should probably be changed

        padding_params = tuple(padding.tolist()) if padding.any() else None
        cropping_params = tuple(cropping.tolist()) if padding.any() else None

should be

        padding_params = tuple(padding.tolist()) if padding.any() else None
        cropping_params = tuple(cropping.tolist()) if cropping.any() else None

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
GReguigcommented, Apr 8, 2020

Ok, I will give it a look

1reaction
nwschurinkcommented, Apr 8, 2020

Ah ok I’ve found out why the check is not working. The shape check is only performed when doing a center crop (e.g _compute_center_crop_or_pad) (here). However, the check is not performed when performing a crop on the mask’s center (e.g. _compute_mask_center_crop_or_pad)

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 along the channels (first) ... method...
Read more >
Crop transforms (augmentations.crops.transforms)
Crop and pad images by pixel amounts or fractions of image sizes. Cropping removes pixels at the sides (i.e. extracts a subimage from...
Read more >
Cropping image from a binary mask - python - Stack Overflow
Currently, it is only possible to plot an image with an overlay mask on the object. I want to crop the object out...
Read more >
Transforms — MONAI 1.1.0 Documentation
So the crop center will only come from the valid image areas. ... Mask data must have the same spatial size as the...
Read more >
tf.image.resize_with_crop_or_pad | TensorFlow v2.11.0
Crops and/or pads an image to a target width and height. ... image[:,:,0] # print first channel just for demo purposes
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