Skip to content

Frequently asked questions and answers

Wenqi Li edited this page Sep 27, 2020 · 16 revisions

A list of frequently asked questions and answers maintained by the core developer team, based on user feedback and discussions.

Q1. The randomised data transformations generate insufficiently shuffled outputs. How can I resolve this issue?

Q2. How do I sample 2D slices from 3D volumes?

Q3. After installing monai with pip, why does importing modules give an error "No module named ..."?

Q4. When running a randomised transform chain, why does the transform raise an AttributeError: 'mtrand.RandomState' object has no attribute 'random'?

Q5. When running the Jupyter notebook examples, why does it raise pickling errors such as "_pickle.PicklingError: Can't pickle <function ..."?


Q1. The randomised data transformations generate insufficiently shuffled outputs. How can I resolve this issue?

This is likely because the random number generator in MONAI is duplicated when multiple workers are initialised by torch.utils.data.Dataloader (see also Pytorch FAQ, #398).

A possible solution is to set up random seeds in workers via the worker_init_fn option:

def worker_init_fn(worker_id):
    worker_info = torch.utils.data.get_worker_info()
    try:
        worker_info.dataset.transform.set_random_state(worker_info.seed % (2 ** 32))
    except AttributeError:
        pass

dataloader = torch.utils.data.DataLoader(..., worker_init_fn=worker_init_fn)

Q2. How to sample 2D slices from 3D volumes?

This could be achieved by setting a 3D window size, followed by "squeezing" the length one spatial dimension (see also: #299). For example,

train_transforms = Compose([
    ...
    RandSpatialCropd(keys=['img', 'seg'], roi_size=[96, 96, 1], random_size=False),
    SqueezeDimd(keys=['img', 'seg'], dim=-1),  # remove the last spatial dimension
    ...
])

Q3. After installing monai with pip, why does importing modules give an error "No module named ..."?

Probably the pip installed default version doesn't have that module. The PyPI release is behind the dev version. To have the latest dev version of MONAI, please follow the documentation: https://docs.monai.io/en/latest/installation.html.

Q4. When running a randomised transform chain, why does the transform raise an AttributeError: 'mtrand.RandomState' object has no attribute 'random'?

A possible reason is that the Numpy package doesn't match the minimal requirement of MONAI. To have a proper version of Numpy, please run:

pip install -r https://raw.githubusercontent.com/Project-MONAI/MONAI/master/requirements.txt

Q5. When running the Jupyter notebook examples, why does it raise pickling errors such as "_pickle.PicklingError: Can't pickle <function CropForegroundd. at 0x000001E>"?

This is possibly related to an issue of multiprocessing on Windows. A simple workaround is to set num_workers to 0 in the code sections. Or creating new python script out of the examples, which imports parent code as a module, this relies on if __name__ == '__main__': blocks in the script preventing setup code being called in the child since __name__ won't be __main__ in that case (see also https://github.com/Project-MONAI/MONAI/pull/307).

Clone this wiki locally