Project-MONAI / Project-MONAI/MONAI

Meta_dict and lazy resampling in apply transform

Open
#6,595 24 comments 0 reactions 1 assignee View on GitHub

@atbenmurray is already working on this.

Since Oct 20, 2023.

bug
Dominant language
Python
Stars
8.7k
Forks
1.6k
Avg merge
5d 1h
Merged PRs (30d)
20

Description

Hi Team, I'm not sure if this is a bug or it's expected.

When I set env var USE_META_DICT=1, some transforms will fail due to the "lazy" argument. If USE_META_DICT=0 or none, it works normally.

Currently, this is causing some functions to fail when USE_META_DICT is set to true (1).

Here is a code snipet for reproducing:

  1. run export USE_META_DICT=1
  2. Create a python script with following codes lazy_meta_dict.py, then run python lazy_meta_dict.py
from functools import partial

import numpy as np
import torch
from monai.data import CacheDataset, create_test_image_3d
from monai.transforms import (
    AddChanneld,
    Compose,
    CropForegroundd,
    DivisiblePadd,
)

def get_data(num_examples, input_size, data_type=np.asarray, include_label=True):
    custom_create_test_image_3d = partial(
        create_test_image_3d, *input_size, rad_max=7, num_seg_classes=1, num_objs=1
    )
    data = []
    for _ in range(num_examples):
        im, label = custom_create_test_image_3d()
        d = {}
        d["image"] = data_type(im)
        d["image_meta_dict"] = {"affine": np.eye(4)}
        if include_label:
            d["label"] = data_type(label)
            d["label_meta_dict"] = {"affine": np.eye(4)}
        d["label_transforms"] = []
        data.append(d)
    return data[0] if num_examples == 1 else data

def test_epistemic_scoring():
    input_size = (20, 20, 20)
    device = "cuda" if torch.cuda.is_available() else "cpu"
    keys = ["image", "label"]
    num_training_ims = 10
    train_data = get_data(num_training_ims, input_size)
    # print("Hey!!:{}".format(CropForegroundd.__dict__.items()))
    transforms = Compose(
        [
            AddChanneld(keys),
            CropForegroundd(keys, source_key="image"),
            DivisiblePadd(keys, 4),
        ]
    )

    train_ds = CacheDataset(train_data, transforms)

if __name__ == "__main__":
    test_epistemic_scoring()

Expected error:

  File "/home/yucheng/anaconda3/lib/python3.9/site-packages/monai/transforms/transform.py", line 145, in apply_transform
    return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
  File "/home/yucheng/anaconda3/lib/python3.9/site-packages/monai/transforms/transform.py", line 102, in _apply_transform
    return transform(data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(data)
TypeError: wrapper() got an unexpected keyword argument 'lazy'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.