Skip to content

Frequently asked questions and answers

Wenqi Li edited this page Jun 20, 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 ..."?


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:

pip uninstall monai
pip install git+https://github.com/Project-MONAI/MONAI#egg=MONAI

To check the versioning info:

import monai
monai.config.print_config()
print(monai.__file__)
Clone this wiki locally