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.

Error when trying to GET torchio.Subject from torchio.SubjectDataset with multiple images with different spatial size

See original GitHub issue

I am trying to construct torchio.SubjectDataset where each torchio.Subject has multiple images (different sequences). Potential problem is, all these images are DIFFERENT spatial sizes.

In order to bring them to same spatial size I’ve included CropOrPad transformation. But, when I try to GET one subject from the dataset, I am getting following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-79-1b7789238bcb> in <module>
----> 1 subj = dataset.train_set[0]
      2 #help(tio.Subject)
      3 subj.__dict__

/opt/conda/lib/python3.7/site-packages/torchio/data/dataset.py in __getitem__(self, index)
     83         # Apply transform (this is usually the bottleneck)
     84         if self._transform is not None:
---> 85             subject = self._transform(subject)
     86         return subject
     87 

/opt/conda/lib/python3.7/site-packages/torchio/transforms/transform.py in __call__(self, data)
    124             subject = copy.copy(subject)
    125         with np.errstate(all='raise', under='ignore'):
--> 126             transformed = self.apply_transform(subject)
    127         if self.keep is not None:
    128             for name, image in images_to_keep.items():

/opt/conda/lib/python3.7/site-packages/torchio/transforms/augmentation/composition.py in apply_transform(self, subject)
     45     def apply_transform(self, subject: Subject) -> Subject:
     46         for transform in self.transforms:
---> 47             subject = transform(subject)
     48         return subject
     49 

/opt/conda/lib/python3.7/site-packages/torchio/transforms/transform.py in __call__(self, data)
    124             subject = copy.copy(subject)
    125         with np.errstate(all='raise', under='ignore'):
--> 126             transformed = self.apply_transform(subject)
    127         if self.keep is not None:
    128             for name, image in images_to_keep.items():

/opt/conda/lib/python3.7/site-packages/torchio/transforms/augmentation/composition.py in apply_transform(self, subject)
     45     def apply_transform(self, subject: Subject) -> Subject:
     46         for transform in self.transforms:
---> 47             subject = transform(subject)
     48         return subject
     49 

/opt/conda/lib/python3.7/site-packages/torchio/transforms/transform.py in __call__(self, data)
    124             subject = copy.copy(subject)
    125         with np.errstate(all='raise', under='ignore'):
--> 126             transformed = self.apply_transform(subject)
    127         if self.keep is not None:
    128             for name, image in images_to_keep.items():

/opt/conda/lib/python3.7/site-packages/torchio/transforms/preprocessing/spatial/crop_or_pad.py in apply_transform(self, subject)
    238 
    239     def apply_transform(self, subject: Subject) -> Subject:
--> 240         padding_params, cropping_params = self.compute_crop_or_pad(subject)
    241         padding_kwargs = {'padding_mode': self.padding_mode}
    242         if padding_params is not None:

/opt/conda/lib/python3.7/site-packages/torchio/transforms/preprocessing/spatial/crop_or_pad.py in _compute_center_crop_or_pad(self, subject)
    157             subject: Subject,
    158             ) -> Tuple[Optional[TypeSixBounds], Optional[TypeSixBounds]]:
--> 159         source_shape = subject.spatial_shape
    160         # The parent class turns the 3-element shape tuple (w, h, d)
    161         # into a 6-element bounds tuple (w, w, h, h, d, d)

/opt/conda/lib/python3.7/site-packages/torchio/data/subject.py in spatial_shape(self)
    116             (181, 217, 181)
    117         """
--> 118         self.check_consistent_spatial_shape()
    119         return self.get_first_image().spatial_shape
    120 

/opt/conda/lib/python3.7/site-packages/torchio/data/subject.py in check_consistent_spatial_shape(self)
    294 
    295     def check_consistent_spatial_shape(self) -> None:
--> 296         self.check_consistent_attribute('spatial_shape')
    297 
    298     def check_consistent_orientation(self) -> None:

/opt/conda/lib/python3.7/site-packages/torchio/data/subject.py in check_consistent_attribute(self, attribute, relative_tolerance, absolute_tolerance, message)
    282                         }),
    283                     )
--> 284                     raise RuntimeError(message)
    285         except TypeError:
    286             # fallback for non-numeric values

RuntimeError: More than one value for "spatial_shape" found in subject images:
{'T1w': (416, 512, 36), 'T2w': (448, 512, 36)}

In summary, when it tries to apply CropOrPad transformation, it fails on following code block:

/opt/conda/lib/python3.7/site-packages/torchio/data/subject.py in check_consistent_spatial_shape(self)
    294 
    295     def check_consistent_spatial_shape(self) -> None:
--> 296         self.check_consistent_attribute('spatial_shape')
    297 
    298     def check_consistent_orientation(self) -> None:

How do I turn off check_consistent_attribute('spatial_shape')? Because in the end all subjects WILL BE same size. But this breaks things before CropOrPad is applied.

NOTE: Dataset is created SUCCESSFULLY, it is only when I try to GET one of its Subjects, and when transformations are being applied that code fails.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:22 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
justusschockcommented, Nov 19, 2021

@fepegar it might still make sense to add them as static class attributes. That way you could do something like this at the import and everything else would work the same:

import torchio as tio
tio.Subject.relative_attribute_tolerance = 1e-5 # applies to all instances since it is a static attribute
tio.Subject.absolute_attribute_tolerance = 1e-5

That way the function could default to None and use the class attributes if no other value was passed. It would also work with every instance, so you could do that once to change it globally.

1reaction
cepa995commented, Sep 4, 2021

Awesome! Thanks @fepegar for the help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Subject - TorchIO - Read the Docs
The Subject is a data structure used to store images associated with a subject and any other metadata necessary for processing. All transforms...
Read more >
TorchIO: A Python library for efficient loading, preprocessing ...
TorchIO : A Python library for efficient loading, preprocessing, augmentation and patch-based sampling of medical images in deep learning.
Read more >
TorchIO tutorial.ipynb - Colaboratory - Google Colab
In this tutorial, we will train a 3D U-Net to perform brain segmentation from T1-weighted MRI using the Information eXtraction from Images (IXI)...
Read more >
Deep learning segmentation of the nucleus basalis of Meynert ...
All augmentations were performed using the TorchIO python library [26]. ... The same coordinates in a subject's 3T MRI with various data ...
Read more >
CNN Pytorch only batches of spatial targets supported error
MNIST has 10 classes so your output should be of size [batch_size, 10] . Change the last linear layer to self.dense = nn.Linear(128,10)...
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