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

Add warning in a multiprocessing special case #6830

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions monai/data/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

from __future__ import annotations

import warnings

import torch
from torch.utils.data import DataLoader as _TorchDataLoader
from torch.utils.data import Dataset

from monai.data.meta_obj import get_track_meta
from monai.data.utils import list_data_collate, set_rnd, worker_init_fn

__all__ = ["DataLoader"]
Expand Down Expand Up @@ -88,4 +91,16 @@ def __init__(self, dataset: Dataset, num_workers: int = 0, **kwargs) -> None:
if "worker_init_fn" not in kwargs:
kwargs["worker_init_fn"] = worker_init_fn

if (
"multiprocessing_context" in kwargs
and kwargs["multiprocessing_context"] == "spawn"
and not get_track_meta()
):
warnings.warn(
"Please be aware: Return type of the dataloader will not be a Tensor as expected but"
" a MetaTensor instead! This is because 'spawn' creates a new process where _TRACK_META"
" is initialized to True again. Context:_TRACK_META is set to False and"
" multiprocessing_context to spawn"
)

super().__init__(dataset=dataset, num_workers=num_workers, **kwargs)