Skip to content

AttributeError in DataLoader when using RandGridDistortiond transform #8604

Closed

Description

@albertojbc

Description
I have tried to include RandGridDistortiond transform as part of my data augmentation pipeline. However, when I include this transform the following error raises:

Exception has occurred: AttributeError
Caught AttributeError in DataLoader worker process 0.
AttributeError: Caught AttributeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "path/lib/python3.10/site-packages/torch/utils/data/_utils/worker.py", line 351, in _worker_loop
    data = fetcher.fetch(index)  # type: ignore[possibly-undefined]
  File "path/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 55, in fetch
    return self.collate_fn(data)
  File "path/lib/python3.10/site-packages/monai/data/utils.py", line 514, in list_data_collate
    ret[key] = collate_fn(data_for_batch)
  File "path/lib/python3.10/site-packages/torch/utils/data/_utils/collate.py", line 398, in default_collate
    return collate(batch, collate_fn_map=default_collate_fn_map)
  File "path/lib/python3.10/site-packages/torch/utils/data/_utils/collate.py", line 155, in collate
    return collate_fn_map[elem_type](batch, collate_fn_map=collate_fn_map)
  File "path/lib/python3.10/site-packages/monai/data/utils.py", line 458, in collate_meta_tensor_fn
    collated = collate_fn(batch)  # type: ignore
  File "path/lib/python3.10/site-packages/torch/utils/data/_utils/collate.py", line 269, in collate_tensor_fn
    numel = sum(x.numel() for x in batch)
  File "path/lib/python3.10/site-packages/torch/utils/data/_utils/collate.py", line 269, in <genexpr>
    numel = sum(x.numel() for x in batch)
AttributeError: 'int' object has no attribute 'numel'

Reproduction details
This is how I defined the transform:

RandGridDistortiond(keys=["image"], prob=0.3, distort_limit=(-0.35, 0.35), padding_mode='zeros'),

Environment details
Python 3.10.16
MONAI version: 1.3.1
Numpy version: 1.24.3
Pytorch version: 2.5.0+cu124
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: 96bfda0

Optional dependencies:
Pytorch Ignite version: 0.4.11
ITK version: 5.3.0
Nibabel version: 5.1.0
scikit-image version: 0.22.0
scipy version: 1.11.1
Pillow version: 9.5.0
Tensorboard version: 2.14.1
gdown version: 4.7.1
TorchVision version: 0.20.0+cu124
tqdm version: 4.65.0
lmdb version: 1.4.1
psutil version: 5.9.0
pandas version: 2.0.3
einops version: 0.7.0
transformers version: 4.49.0
mlflow version: 2.7.1
pynrrd version: 1.0.0
clearml version: 1.13.1

Found solution
I managed to solve this issue in my case by changing the __call__ function of RandGridDistortiond in monai/transforms/spatial/dictionary.py.
Instead of:

if not self._do_transform:
      out: dict[Hashable, torch.Tensor] = convert_to_tensor(d, track_meta=get_track_meta())
      return out

Use this:

if not self._do_transform:
      for key in self.key_iterator(d):
          d[key] = convert_to_tensor(d[key], track_meta=get_track_meta())
      return d

For me, this change made the DataLoader work properly.

Activity

  1. added 2 commits that reference this issue on Mar 22, 2026
    e815c23
    7d5bf42
  2. AlexanderSanin commented on May 18, 2026

    @AlexanderSanin
    Contributor

    Hi @albertojbc , I've submitted a fix for this in #8862. The issue was that convert_to_tensor was called on the entire data dictionary when the transform is skipped, converting metadata integers into 0-dim tensors. The fix only converts the target keys via self.key_iterator, matching how the transform handles keys in the normal path. Happy to adjust if needed.

  3. ericspod commented on May 19, 2026

    @ericspod
    Member

    @HeyangQin we'd like to get this fix in, you had a PR on this first but there were unresolved issues I mentioned when reviewed. If you'd like to come back to it your PR has the priority here. Thanks!

  4. AlexanderSanin commented on Jun 17, 2026

    @AlexanderSanin
    Contributor

    I've submitted a PR implementing this: #8920. Please review

  5. added a commit that references this issue on Oct 3, 2026
    868041a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions