|
| 1 | +# MONAI Bundle Configuration |
| 2 | + |
| 3 | +The `monai.bundle` module supports building Python-based workflows via structured configurations. |
| 4 | + |
| 5 | +The main benefits are threefold: |
| 6 | + |
| 7 | +- it provides good readability and usability by separating system parameter settings from the Python code. |
| 8 | +- it describes workflow at a relatively high level and allows for different low-level implementations. |
| 9 | +- learning paradigms at a higher level such as federated learning and AutoML can be decoupled from the component details. |
| 10 | + |
| 11 | +Content: |
| 12 | + |
| 13 | +- [A basic example](#a-basic-example) |
| 14 | +- [Syntax examples explained](#syntax-examples-explained) |
| 15 | + - [`@` to interpolate with Python objects](#1--to-interpolate-with-python-objects) |
| 16 | + - [`$` to evaluate as Python expressions](#2--to-evaluate-as-python-expressions) |
| 17 | + - [`%` to textually replace configuration elements](#3--to-textually-replace-configuration-elements) |
| 18 | + - [`_target_` (`_disabled_` and `_requires_`) to instantiate a Python object](#4-instantiate-a-python-object) |
| 19 | +- [The command line interface](#the-command-line-interface) |
| 20 | +- [Recommendations](#recommendations) |
| 21 | + |
| 22 | +## A basic example |
| 23 | + |
| 24 | +Components as part of a workflow can be specified using `JSON` or `YAML` syntax, for example, a network architecture |
| 25 | +definition could be stored in a `demo_config.json` file with the following content: |
| 26 | + |
| 27 | +```json |
| 28 | +{ |
| 29 | + "demo_net": { |
| 30 | + "_target_": "monai.networks.nets.BasicUNet", |
| 31 | + "spatial_dims": 3, |
| 32 | + "in_channels": 1, |
| 33 | + "out_channels": 2, |
| 34 | + "features": [16, 16, 32, 32, 64, 64] |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +or alternatively, in `YAML` format (`demo_config.yaml`): |
| 40 | + |
| 41 | +```yaml |
| 42 | +demo_net: |
| 43 | + _target_: monai.networks.nets.BasicUNet |
| 44 | + spatial_dims: 3 |
| 45 | + in_channels: 1 |
| 46 | + out_channels: 2 |
| 47 | + features: [16, 16, 32, 32, 64, 64] |
| 48 | +``` |
| 49 | +
|
| 50 | +The configuration parser can instantiate the component as a Python object: |
| 51 | +
|
| 52 | +```py |
| 53 | +>>> from monai.bundle import ConfigParser |
| 54 | +>>> config = ConfigParser() |
| 55 | +>>> config.read_config("demo_config.json") |
| 56 | +>>> net = config.get_parsed_content("demo_net", instantiate=True) |
| 57 | +BasicUNet features: (16, 16, 32, 32, 64, 64). |
| 58 | +>>> print(type(net)) |
| 59 | +<class 'monai.networks.nets.basic_unet.BasicUNet'> |
| 60 | +``` |
| 61 | + |
| 62 | +or additionally, tune the input parameters then instantiate the component: |
| 63 | + |
| 64 | +```py |
| 65 | +>>> config["demo_net"]["features"] = [32, 32, 32, 64, 64, 64] |
| 66 | +>>> net = config.get_parsed_content("demo_net", instantiate=True) |
| 67 | +BasicUNet features: (32, 32, 32, 64, 64, 64). |
| 68 | +``` |
| 69 | + |
| 70 | +For more details on the `ConfigParser` API, please see https://docs.monai.io/en/latest/bundle.html#config-parser. |
| 71 | + |
| 72 | +## Syntax examples explained |
| 73 | + |
| 74 | +A few characters and keywords are interpreted beyond the plain texts, here are examples of the syntax: |
| 75 | + |
| 76 | +### 1. `@` to interpolate with Python objects |
| 77 | + |
| 78 | +```json |
| 79 | +"@preprocessing#transforms#keys" |
| 80 | +``` |
| 81 | + |
| 82 | +_Description:_ A reference to another configuration value defined at `preprocessing#transforms#keys`. |
| 83 | +where `#` indicates a sub-structure of this configuration file. |
| 84 | + |
| 85 | +```json |
| 86 | +"@preprocessing#1" |
| 87 | +``` |
| 88 | + |
| 89 | +_Description:_ `1` is interpreted as an integer, which is used to index (zero-based indexing) the `preprocessing` sub-structure. |
| 90 | + |
| 91 | +### 2. `$` to evaluate as Python expressions |
| 92 | + |
| 93 | +```json |
| 94 | +"$print(42)" |
| 95 | +``` |
| 96 | + |
| 97 | +_Description:_ `$` is a special character to indicate evaluating `print(42)` at runtime. |
| 98 | + |
| 99 | +```json |
| 100 | +"$[i for i in @datalist]" |
| 101 | +``` |
| 102 | + |
| 103 | +_Description:_ Create a list at runtime using the values in `datalist` as input. |
| 104 | + |
| 105 | +```json |
| 106 | +"$from torchvision.models import resnet18" |
| 107 | +``` |
| 108 | + |
| 109 | +_Description:_ `$` followed by an import statement is handled slightly differently from the |
| 110 | +Python expressions. The imported module `resnet18` will be available as a global variable |
| 111 | +to the other configuration sections. This is to simplify the use of external modules in the configuration. |
| 112 | + |
| 113 | +### 3. `%` to textually replace configuration elements |
| 114 | + |
| 115 | +```json |
| 116 | +"%demo_config.json#demo_net#in_channels" |
| 117 | +``` |
| 118 | + |
| 119 | +_Description:_ A macro to replace the current configuration element with the texts at `demo_net#in_channels` in the |
| 120 | +`demo_config.json` file. The replacement is done before instantiating or evaluating the components. |
| 121 | + |
| 122 | +### 4. instantiate a Python object |
| 123 | + |
| 124 | +```json |
| 125 | +{ |
| 126 | + "demo_name":{ |
| 127 | + "_target_": "my.python.module.Class", |
| 128 | + "args1": "string", |
| 129 | + "args2": 42} |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +_Description:_ This dictionary defines an object with a reference name `demo_name`, with an instantiable type |
| 134 | +specified at `_target_` and with input arguments `args1` and `args2`. |
| 135 | +This dictionary will be instantiated as a Pytorch object at runtime. |
| 136 | + |
| 137 | +`_target_` is a required key by monai bundle syntax for the Python object name. |
| 138 | +`args1` and `args2` should be compatible with the Python object to instantiate. |
| 139 | + |
| 140 | +```json |
| 141 | +{ |
| 142 | + "component_name": { |
| 143 | + "_target_": "my.module.Class", |
| 144 | + "_requires_": "@cudnn_opt", |
| 145 | + "_disabled_": "true"} |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +_Description:_ `_requires_` and `_disabled_` are optional keys. |
| 150 | +`_requires_` specifies references (string starts with `@`) or |
| 151 | +Python expression that will be evaluated/instantiated before `_target_` object is instantiated. |
| 152 | +It is useful when the component does not explicitly depend on the other ConfigItems via |
| 153 | +its arguments, but requires the dependencies to be instantiated/evaluated beforehand. |
| 154 | +`_disabled_` specifies a flag to indicate whether to skip the instantiation. |
| 155 | + |
| 156 | +## The command line interface |
| 157 | + |
| 158 | +In addition to the Pythonic APIs, a few command line interfaces (CLI) are provided to interact with the bundle. |
| 159 | +The primary usage is: |
| 160 | +```bash |
| 161 | +python -m monai.bundle COMMANDS |
| 162 | +``` |
| 163 | + |
| 164 | +where `COMMANDS` is one of the following: `run`, `verify_metadata`, `ckpt_export`, ... |
| 165 | +(please see `python -m monai.bundle --help` for a list of available options). |
| 166 | + |
| 167 | +To display a usage page for a command, for example `run`: |
| 168 | +```bash |
| 169 | +python -m monai.bundle run -- --help |
| 170 | +``` |
| 171 | + |
| 172 | +The support is provided by [Python Fire](https://github.com/google/python-fire), please |
| 173 | +make sure the optional dependency is installed, for example, |
| 174 | +using `pip install monai[fire]` or `pip install fire`. |
| 175 | +Details on the CLI argument parsing is provided in the |
| 176 | +[Python Fire Guide](https://github.com/google/python-fire/blob/master/docs/guide.md#argument-parsing). |
| 177 | + |
| 178 | +## Recommendations |
| 179 | +- Both `YAML` and `JSON` are supported, but the advanced features of these formats are not supported. |
| 180 | +- Using meaningful names for the configuration elements can improve the readability. |
| 181 | +- While it is possible to build complex configurations with the bundle syntax, |
| 182 | + simple structures with sparse uses of expressions or references are preferred. |
| 183 | +- For `$import <module>` in the configuration, please make sure there are instructions for the users to install |
| 184 | + the `<module>` if it is not a (optional) dependency of MONAI. |
0 commit comments