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 if CropOrPad and RescaleIntensity are used together

See original GitHub issue

🐛Bug

I have an error when using ‘CropOrPad’ and ‘RescaleIntensity’ as part of the dataset transformations. Note that there is no error if the two transformations are switched.

To reproduce

from torchvision.transforms import Compose
from torch.utils.data import DataLoader

import torchio
from torchio.transforms import (
    RescaleIntensity,
    ToCanonical,
    CropOrPad,
)
from torchio import SubjectsDataset, DATA


transformations = (
        ToCanonical(),
        CropOrPad((192,192,128)),
        RescaleIntensity((-1,1),percentiles=(0.05, 99.5))
        )
transformations = Compose(transformations)

subject = torchio.datasets.Colin27()
dataset = SubjectsDataset([subject], transform=transformations)
loader = DataLoader(dataset, batch_size=1)
for batch in loader:
    print(batch['brain']['stem'])
  File "<stdin>", line 1, in <module>                                                                                                                                                                                                          
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 345, in __next__                                                                                                                                          
    data = self._next_data()                                                                                                                                                                                                                   
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 385, in _next_data                                                                                                                                        
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration                                                                                                                                                                       
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch                                                                                                                                            
    data = [self.dataset[idx] for idx in possibly_batched_index]                                                                                                                                                                               
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>                                                                                                                                       
    data = [self.dataset[idx] for idx in possibly_batched_index]                                                                                                                                                                               
  File "/workspace/home/local4/.local/lib/python3.6/site-packages/torchio/data/dataset.py", line 87, in __getitem__                                                                                                                            
    sample = self._transform(sample)                                                                                                                                                                                                           
  File "/opt/conda/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 70, in __call__                                                                                                                                     
    img = t(img)                                                                                                                                                                                                                               
  File "/workspace/home/local4/.local/lib/python3.6/site-packages/torchio/transforms/transform.py", line 95, in __call__                                                                                                                       
    transformed = self.apply_transform(sample)                                                                                                                                                                                                 
  File "/workspace/home/local4/.local/lib/python3.6/site-packages/torchio/transforms/preprocessing/intensity/normalization_transform.py", line 69, in apply_transform                                                                          
    self.apply_normalization(sample, image_name, mask)                                                                                                                                                                                         
  File "/workspace/home/local4/.local/lib/python3.6/site-packages/torchio/transforms/preprocessing/intensity/rescale.py", line 55, in apply_normalization                                                                                      
    image_dict[DATA] = self.rescale(image_dict[DATA], mask, image_name)                                                                                                                                                                        
  File "/workspace/home/local4/.local/lib/python3.6/site-packages/torchio/transforms/preprocessing/intensity/rescale.py", line 63, in rescale                                                                                                  
    array = tensor.clone().numpy()                                                                                                                                                                                                             
RuntimeError: unsupported operation: more than one element of the written-to tensor refers to a single memory location. Please clone() the tensor before performing the operation.

TorchIO et co version numpy 1.18.1 torch 1.5.0a0+8f84ded torchio 0.17.42 SimpleITK 1.2.2

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ReubenDocommented, Sep 24, 2020

Upgrading torch solved the problem. For the record, I was using the NVIDIA docker container: nvcr.io/nvidia/pytorch:20.03-py.

Thanks for your help guys!

0reactions
fepegarcommented, Sep 24, 2020

Can’t reproduce with your setup either (except I installed torch 1.5.0 instead of that specific revision). Can you upgrade torch and try again? If it doesn’t work I’ll try to reproduce on Linux next week.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preprocessing - TorchIO - Read the Docs
Rescale intensity values to a certain range. Parameters: ... If None , the minimum and maximum input intensities will be used.
Read more >
TorchIO: A Python library for efficient loading ... - arXiv
Data augmentation is used to artificially increase the size of the training datasets. Training with image subvolumes or patches decreases the ...
Read more >
TorchIO tutorial.ipynb - Colaboratory - Google Colab
The image class will be used by the transforms to decide whether or not to perform the operation. For example, spatial transforms must...
Read more >
MSOT based on VXM - Kaggle
historyVersion 23 of 23 ... It is recommended to use a virtual environment instead: ... CropOrPad(args['inshape3D']) #transform = tio.transforms.
Read more >
TorchIO: A Python library for efficient loading, preprocessing ...
Data augmentation is used to artificially increase the size of the training datasets. Training with image subvolumes or patches decreases ...
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