Skip to content

lambda functions should be avoided in constructors #1883

@juampatronics

Description

@juampatronics

Describe the bug

pickle.dumps(GeneralizedDiceLoss()) fails, unlike, for example, pickle.dumps(DiceLoss()). The culprit is a lambda function in GeneralizedDiceLoss.init. This is a problem when serializing your model on checkpoints, or for training on several GPUs.
This applies not only to loss functions, but optimizers and transformers as well.

To Reproduce

Steps to reproduce the behavior:

 import monai.losses as mloss
 pickle.dumps(mloss.GeneralizedDiceLoss())

Expected behavior
It should just return a byte representation of the loss function.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment

Ensuring you use the relevant python executable, please paste the output of:

python -c 'import monai; monai.config.print_debug_info()'

================================
Printing MONAI config...

MONAI version: 0.4.0
Numpy version: 1.19.2
Pytorch version: 1.7.1
MONAI flags: HAS_EXT = False, USE_COMPILED = False
MONAI rev id: 0563a44

Optional dependencies:
Pytorch Ignite version: 0.4.4
Nibabel version: 3.2.1
scikit-image version: 0.17.2
Pillow version: 8.1.0
Tensorboard version: 2.4.1
gdown version: 3.12.2
TorchVision version: 0.8.2
ITK version: 5.1.2
tqdm version: 4.58.0
lmdb version: 1.1.1
psutil version: 5.8.0

For details about installing the optional dependencies, please visit:
https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies

================================
Printing system config...

System: Linux
Linux version: Ubuntu 20.04.2 LTS
Platform: Linux-5.8.0-48-generic-x86_64-with-glibc2.10
Processor: x86_64
Machine: x86_64
Python version: 3.8.8
Process name: python
Command: ['python', '-c', 'import monai; monai.config.print_debug_info()']
Open files: [popenfile(path='/home/juanpablo/Documents/github/MONAI/monai/log.txt', fd=1, position=808, mode='w', flags=32769)]
Num physical CPUs: 18
Num logical CPUs: 36
Num usable CPUs: 36
CPU usage (%): [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0, 0.0, 3.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
CPU freq. (MHz): 1207
Load avg. in last 1, 5, 15 mins (%): [0.8, 0.8, 0.7]
Disk usage (%): 47.0
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 251.4
Available memory (GB): 243.3
Used memory (GB): 5.6

================================
Printing GPU config...

Num GPUs: 2
Has CUDA: True
CUDA version: 11.0
cuDNN enabled: True
cuDNN version: 8005
Current device: 0
Library compiled for CUDA architectures: ['sm_37', 'sm_50', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'sm_80', 'compute_37']
Info for GPU: 1
Name: GeForce RTX 3090
Is integrated: False
Is multi GPU board: False
Multi processor count: 82
Total memory (GB): 23.7
Cached memory (GB): 0.0
Allocated memory (GB): 0.0
CUDA capability (maj.min): 8.6

Additional context
Add any other context about the problem here.

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