MONAI icon indicating copy to clipboard operation
MONAI copied to clipboard

Save and load parameters of a pre-processing or augmentation (transforms) pipeline

Open MichaHK opened this issue 2 years ago • 0 comments

Description

After training a model, it is useful to be able to save the pre-processing steps used in training. Later, one can load the transforms along with the model at inference or production. At the moment, the composed transforms lack a method that can save the transforms. This feature can help with making monai-based deployments more reliable.

Proposed solution

I would suggest a similar solution to the one devised by albumentations (see https://albumentations.ai/docs/examples/serialization/). For example:

train_transforms = Compose([ScaleIntensity(), EnsureChannelFirst(), Resize((96, 96, 96)), RandRotate90()])
monai.transforms.save(transform, '/tmp/transform.json')
loaded_transform = monai.transforms.load('/tmp/transform.json')

The json would look like this (example from albumentations):

{
  "__version__": "1.3.1",
  "transform": {
    "__class_fullname__": "Compose",
    "p": 1,
    "transforms": [
      {
        "__class_fullname__": "albumentations.transforms.RandomCrop",
        "always_apply": true,
        "p": 1,
        "height": 768,
        "width": 768
      },
      {
        "__class_fullname__": "albumentations.transforms.RGBShift",
        "always_apply": false,
        "p": 0.5,
        "hue_shift_limit": (-20, 20),
        "sat_shift_limit": (-30, 30),
        "val_shift_limit": (-20, 20)
      }
    ],
    "bbox_params": null,
    "keypoint_params": null,
    "additional_targets": {},
    "is_check_shapes": true
  }
}

Alternatives considered

I could pickle the transforms. I think that is a bad solution as it relies on the specifically installed package versions, and is not readable. Therefore, it is not portable.

Additional context

None

MichaHK avatar Dec 12 '23 15:12 MichaHK