Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify list_data_collate and collate_meta_tensor using collate #5917

Closed
wyli opened this issue Jan 31, 2023 · 4 comments · Fixed by #7165
Closed

simplify list_data_collate and collate_meta_tensor using collate #5917

wyli opened this issue Jan 31, 2023 · 4 comments · Fixed by #7165

Comments

@wyli
Copy link
Contributor

wyli commented Jan 31, 2023

the generic collate API is implemented in pytorch

pytorch/pytorch#85748

def collate(batch, *, collate_fn_map: Optional[Dict[Union[Type, Tuple[Type, ...]], Callable]] = None):
    r"""
        General collate function that handles collection type of element within each batch
        and opens function registry to deal with specific element types. `default_collate_fn_map`
        provides default collate functions for tensors, numpy arrays, numbers and strings.

probably the monai helper functions could be simplified:

MONAI/monai/data/utils.py

Lines 448 to 451 in 50c3f32

def list_data_collate(batch: Sequence):
"""
Enhancement for PyTorch DataLoader default collate.
If dataset already returns a list of batch data that generated in transforms, need to merge all data to 1 list.

MONAI/monai/data/utils.py

Lines 427 to 430 in 50c3f32

def collate_meta_tensor(batch):
"""collate a sequence of meta tensor sequences/dictionaries into
a single batched metatensor or a dictionary of batched metatensor"""
if not isinstance(batch, Sequence):

@wyli wyli changed the title simplify list_data_collate and collate_meta_tensor using simplify list_data_collate and collate_meta_tensor using collate Jan 31, 2023
@Nic-Ma
Copy link
Contributor

Nic-Ma commented Jan 31, 2023

It may also affect this feature request #5909?
CC @yiheng-wang-nv

@kbressem
Copy link
Contributor

kbressem commented Mar 8, 2023

It would be cool if in this contribution, the collate_meta_tensor could be more permissive, if keys in the metadata are missing. I work with data from multiple datasets and frequently get errors, as keys are not the same across the different images. I usually just pop these keys in the metadata, but it would be nice, if this would not be an error at all.

  File "/home/bressekk/miniconda3/envs/monailabel/lib/python3.9/site-packages/torch/utils/data/_utils/worker.py", line 302, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/bressekk/miniconda3/envs/monailabel/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py", line 61, in fetch
    return self.collate_fn(data)
  File "/home/bressekk/miniconda3/envs/monailabel/lib/python3.9/site-packages/monai/data/utils.py", line 470, in list_data_collate
    ret = collate_meta_tensor(data)
  File "/home/bressekk/miniconda3/envs/monailabel/lib/python3.9/site-packages/monai/data/utils.py", line 439, in collate_meta_tensor
    return {k: collate_meta_tensor([d[k] for d in batch]) for k in elem_0}
  File "/home/bressekk/miniconda3/envs/monailabel/lib/python3.9/site-packages/monai/data/utils.py", line 439, in <dictcomp>
    return {k: collate_meta_tensor([d[k] for d in batch]) for k in elem_0}
  File "/home/bressekk/miniconda3/envs/monailabel/lib/python3.9/site-packages/monai/data/utils.py", line 434, in collate_meta_tensor
    collated.meta = default_collate([i.meta or TraceKeys.NONE for i in batch])
  File "/home/bressekk/miniconda3/envs/monailabel/lib/python3.9/site-packages/torch/utils/data/_utils/collate.py", line 265, in default_collate
    return collate(batch, collate_fn_map=default_collate_fn_map)
  File "/home/bressekk/miniconda3/envs/monailabel/lib/python3.9/site-packages/torch/utils/data/_utils/collate.py", line 128, in collate
    return elem_type({key: collate([d[key] for d in batch], collate_fn_map=collate_fn_map) for key in elem})
  File "/home/bressekk/miniconda3/envs/monailabel/lib/python3.9/site-packages/torch/utils/data/_utils/collate.py", line 128, in <dictcomp>
    return elem_type({key: collate([d[key] for d in batch], collate_fn_map=collate_fn_map) for key in elem})
  File "/home/bressekk/miniconda3/envs/monailabel/lib/python3.9/site-packages/torch/utils/data/_utils/collate.py", line 128, in <listcomp>
    return elem_type({key: collate([d[key] for d in batch], collate_fn_map=collate_fn_map) for key in elem})
KeyError: 'ITK_non_uniform_sampling_deviation'

@Nic-Ma
Copy link
Contributor

Nic-Ma commented Mar 9, 2023

Hi @wyli ,

Could you please help double confirm it?

Thanks in advance.

@wyli
Copy link
Contributor Author

wyli commented Mar 9, 2023

created an additional feature request to make sure the requirement tracked

KumoLiu added a commit to KumoLiu/MONAI that referenced this issue Oct 26, 2023
Signed-off-by: KumoLiu <yunl@nvidia.com>
wyli pushed a commit that referenced this issue Oct 26, 2023
Fixes #5917


### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: KumoLiu <yunl@nvidia.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants