Skip to content

Encounter "TypeError: only integer scalar arrays can be converted to a scalar index" when using random flip #529

Closed
@hsiangyuzhao

Description

🐛Bug

To reproduce

augment = tio.Compose([
            tio.RandomFlip(p=0.5),
            #tio.RandomAffine(),
            #tio.RandomGamma(p=0.5),
            #tio.RandomNoise(p=0.5)
])
trainset = tio.SubjectsDataset(subjects=train_subjects, transform=tio.Compose([augment, preprocess]))
train_queue = tio.Queue(subjects_dataset=trainset,
                            max_length=max_length,
                            sampler=sampler,
                            samples_per_volume=samples_per_volume,
                            num_workers=0,
                            shuffle_patches=False,
                            shuffle_subjects=False)
train_loader = DataLoader(train_queue, batch_size=batch_size, num_workers=0, pin_memory=False)
for step, batch_data in enumerate(loader):
    ...

Expected behavior

Training patches shall be augmented.

Actual behavior

Traceback (most recent call last):
  File "<input>", line 85, in <module>
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/tqdm/std.py", line 1178, in __iter__
    for obj in iterable:
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 435, in __next__
    data = self._next_data()
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 475, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "/home/zhao/anaconda3/envs/torch/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 "/home/zhao/anaconda3/envs/torch/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 "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/data/queue.py", line 165, in __getitem__
    self._fill()
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/data/queue.py", line 229, in _fill
    subject = self._get_next_subject()
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/data/queue.py", line 239, in _get_next_subject
    subject = next(self.subjects_iterable)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 435, in __next__
    data = self._next_data()
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 1085, in _next_data
    return self._process_data(data)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 1111, in _process_data
    data.reraise()
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torch/_utils.py", line 428, in reraise
    raise self.exc_type(msg)
TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/zhao/anaconda3/envs/torch/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 "/home/zhao/anaconda3/envs/torch/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 "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/data/dataset.py", line 85, in __getitem__
    subject = self._transform(subject)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/transforms/transform.py", line 121, in __call__
    transformed = self.apply_transform(subject)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/transforms/augmentation/composition.py", line 47, in apply_transform
    subject = transform(subject)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/transforms/transform.py", line 121, in __call__
    transformed = self.apply_transform(subject)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/transforms/augmentation/composition.py", line 47, in apply_transform
    subject = transform(subject)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/transforms/transform.py", line 121, in __call__
    transformed = self.apply_transform(subject)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/transforms/augmentation/spatial/random_flip.py", line 60, in apply_transform
    transformed = transform(subject)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/transforms/transform.py", line 121, in __call__
    transformed = self.apply_transform(subject)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/transforms/augmentation/spatial/random_flip.py", line 91, in apply_transform
    _flip_image(image, axes)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/torchio/transforms/augmentation/spatial/random_flip.py", line 128, in _flip_image
    data = np.flip(data, axis=spatial_axes)
  File "/home/zhao/anaconda3/envs/torch/lib/python3.6/site-packages/numpy/lib/function_base.py", line 206, in flip
    indexer[axis] = slice(None, None, -1)
TypeError: only integer scalar arrays can be converted to a scalar index

System info

When I cancel random flip augmentation, the code would work fine.
I suppose this is because of the version of numpy. For latest numpy the error would occur, but for numpy version 1.19.1 the error won't happen.
System ubuntu 16.04, torchio version 0.18.37, python 3.6, pytorch 1.7.1, numpy 1.19.5.

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions