Closed Zhack47 closed 1 year ago
Hi, @Zhack47. Can you please share a minimal example I can reproduce?
This is more minimal and should trigger the bug (tested on the same machine as above)
import numpy as np
import torch
from torch.utils.data import DataLoader
from torchio.data.sampler.label import LabelSampler
from torchio import DATA, TYPE, LABEL, INTENSITY, IntensityTransform
class SimulateLowResolutionTransform(IntensityTransform):
def __init__(self):
super().__init__(1)
def apply_transform(self, subject):
keys = sorted(subject.keys())
for key in keys:
subject[key][DATA] = subject[key][DATA].unsqueeze(0)
return subject
if __name__ == "__main__":
import torchio as tio
st = SimulateLowResolutionTransform()
colin_dataset = tio.datasets.mni.Colin27()
ds_train = tio.SubjectsDataset([colin_dataset], transform=st)
sampler = LabelSampler((120, 120, 80))
patches_queue_train = tio.Queue(ds_train, max_length=32, samples_per_volume=4, sampler=sampler,
shuffle_patches=True, shuffle_subjects=True, num_workers=8)
training_loader = DataLoader(patches_queue_train, batch_size=2, shuffle=True)
for batch in training_loader:
print(batch["t1"][DATA].shape)
Thanks, @Zhack47. Good catch!
I think adding raise exception
after line 330 would do. Do you agree?
Would you like to contribute with a PR?
Thanks! I completely agree with this solution I am going to make a PR to fix this !
Fixed in v0.19.1
.
Is there an existing issue for this?
Bug summary
In the _get_next_subject() method of the Queue class, there is a tr / except statement which goes like this:
When an AssertionError arises and the if condition is not fulfilled, we get an UnboundLocalError telling us subject is not defined. The actual exception is lost, and this makes debugging harder. In my case the AssertionError was :
AssertionError: Output of SimulateLowResolutionTransform is 5D
which explicits betterr whatt my problem was.In order to remove the confusion, we could raise the original AssertionError if it does not fulfill the if statement
Code for reproduction
Actual outcome
Traceback (most recent call last): File "/home/zhack/Documents/THESE/4Net/fournet/utils/transforms/augmentations/spatial_augments.py", line 304, in
for batch in training_loader:
File "/home/zhack/.local/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 628, in next
data = self._next_data()
File "/home/zhack/.local/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 671, in _next_data
data = self._dataset_fetcher.fetch(index) # may raise StopIteration
File "/home/zhack/.local/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 58, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/zhack/.local/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 58, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/zhack/.local/lib/python3.10/site-packages/torchio/data/queue.py", line 170, in getitem
self._fill()
File "/home/zhack/.local/lib/python3.10/site-packages/torchio/data/queue.py", line 229, in _fill
subject = self._get_next_subject()
File "/home/zhack/.local/lib/python3.10/site-packages/torchio/data/queue.py", line 270, in _get_next_subject
return subject
UnboundLocalError: local variable 'subject' referenced before assignment
Error messages
Expected outcome
AssertionError: Output of SimulateLowResolutionTransform is 5D
System info