|
6 | 6 | "source": [
|
7 | 7 | "# Get started to MONAI bundle\n",
|
8 | 8 | "\n",
|
9 |
| - "MONAI bundle usually includes the stored weights of a model, TorchScript model, JSON files that include configs and metadata about the model, information for constructing training, inference, and post-processing transform sequences, plain-text description, legal information, and other data the model creator wishes to include.\n", |
| 9 | + "A MONAI bundle usually includes the stored weights of a model, TorchScript model, JSON files which include configs and metadata about the model, information for constructing training, inference, and post-processing transform sequences, plain-text description, legal information, and other data the model creator wishes to include.\n", |
10 | 10 | "\n",
|
11 |
| - "For more information about MONAI bundle description: https://docs.monai.io/en/latest/bundle_intro.html.\n", |
| 11 | + "For more information about MONAI bundles read the description: https://docs.monai.io/en/latest/bundle_intro.html.\n", |
12 | 12 | "\n",
|
13 |
| - "This notebook is step-by-step tutorial to help get started to develop a bundle package, which contains a config file to construct the training pipeline and also have a `metadata.json` file to define the metadata information.\n", |
| 13 | + "This notebook is a step-by-step tutorial to help get started to develop a bundle package, which contains a config file to construct the training pipeline and also has a `metadata.json` file to define the metadata information.\n", |
14 | 14 | "\n",
|
15 |
| - "This notebook mainly contains below sections:\n", |
| 15 | + "This notebook mainly contains the below sections:\n", |
16 | 16 | "- Define a training config with `JSON` or `YAML` format\n",
|
17 | 17 | "- Execute training based on bundle scripts and configs\n",
|
18 | 18 | "- Hybrid programming with config and python code\n",
|
|
21 | 21 | "- Instantiate a python object from a dictionary config with `_target_` indicating class or function name or module path.\n",
|
22 | 22 | "- Execute python expression from a string config with the `$` syntax.\n",
|
23 | 23 | "- Refer to other python object with the `@` syntax.\n",
|
24 |
| - "- Require other independent config items to execute or instantiate first with the `_requires_` syntax.\n", |
25 | 24 | "- Macro text replacement with the `%` syntax to simplify the config content.\n",
|
26 | 25 | "- Leverage the `_disabled_` syntax to tune or debug different components.\n",
|
27 | 26 | "- Override config content at runtime.\n",
|
|
144 | 143 | "source": [
|
145 | 144 | "## Define train config - Set imports and input / output environments\n",
|
146 | 145 | "\n",
|
147 |
| - "Now let's start to define the config file for a regular training task. MONAI bundle support both `JSON` and `YAML` format, here we use `JSON` as example.\n", |
| 146 | + "Now let's start to define the config file for a regular training task. MONAI bundles support both `JSON` and `YAML` format, here we use `JSON` as the example.\n", |
148 | 147 | "\n",
|
149 | 148 | "According to the predefined syntax of MONAI bundle, `$` indicates an expression to evaluate, `@` refers to another object in the config content. For more details about the syntax in bundle config, please check: https://docs.monai.io/en/latest/config_syntax.html.\n",
|
150 | 149 | "\n",
|
151 |
| - "Please note that MONAI bundle doesn't require any hard-code logic in the config, so users can define the config content in any structure.\n", |
| 150 | + "Please note that a MONAI bundle doesn't require any hard-coded logic in the config, so users can define the config content in any structure.\n", |
152 | 151 | "\n",
|
153 |
| - "For the first step, import `os` and `glob` to use in the expressions (start with `$`). Then define input / output environments and enable `cudnn.benchmark` for better performance." |
| 152 | + "For the first step, import `os` and `glob` to use in the expressions (start with `$`), then define input / output environments and enable `cudnn.benchmark` for better performance." |
154 | 153 | ]
|
155 | 154 | },
|
156 | 155 | {
|
|
164 | 163 | " \"$import os\",\n",
|
165 | 164 | " \"$import ignite\"\n",
|
166 | 165 | " ],\n",
|
167 |
| - " \"determinism\": \"$monai.utils.set_determinism(seed=123)\",\n", |
168 |
| - " \"cudnn_opt\": \"$setattr(torch.backends.cudnn, 'benchmark', True)\",\n", |
169 | 166 | " \"device\": \"$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\",\n",
|
170 | 167 | " \"ckpt_path\": \"/workspace/data/models/model.pt\",\n",
|
171 | 168 | " \"dataset_dir\": \"/workspace/data/Task09_Spleen\",\n",
|
|
325 | 322 | "cell_type": "markdown",
|
326 | 323 | "metadata": {},
|
327 | 324 | "source": [
|
328 |
| - "The train and validation image file names are organized into a list of dictionaries." |
| 325 | + "The train and validation image file names are organized into a list of dictionaries.\n", |
| 326 | + "\n", |
| 327 | + "Here we use `dataset` instance as 1 argument of `dataloader` by the `@` syntax, and please note that `\"#\"` in the reference id are interpreted as special characters to go one level further into the nested config structures. For example: `\"dataset\": \"@train#dataset\"`." |
329 | 328 | ]
|
330 | 329 | },
|
331 | 330 | {
|
|
430 | 429 | "\n",
|
431 | 430 | "Here we use MONAI engine `SupervisedTrainer` to execute a regular training.\n",
|
432 | 431 | "\n",
|
433 |
| - "`determinism` and `cudnn_opt` are not args of the trainer, but should execute them before training, so here mark them in the `_requires_` field.\n", |
434 |
| - "\n", |
435 | 432 | "If users have customized logic, then can put the logic in the `iteration_update` arg or implement their own `trainer` in python code and set `_target_` to the class directly."
|
436 | 433 | ]
|
437 | 434 | },
|
|
442 | 439 | "```json\n",
|
443 | 440 | "\"trainer\": {\n",
|
444 | 441 | " \"_target_\": \"SupervisedTrainer\",\n",
|
445 |
| - " \"_requires_\": [\"@determinism\", \"@cudnn_opt\"],\n", |
446 | 442 | " \"max_epochs\": 100,\n",
|
447 | 443 | " \"device\": \"@device\",\n",
|
448 | 444 | " \"train_data_loader\": \"@train#dataloader\",\n",
|
|
499 | 495 | "source": [
|
500 | 496 | "## Define metadata information\n",
|
501 | 497 | "\n",
|
502 |
| - "Optinally, we can define a `metadata` file in the bundle, which contains the metadata information relating to the model, including what the shape and format of inputs and outputs are, what the meaning of the outputs are, what type of model is present, and other information. The structure is a dictionary containing a defined set of keys with additional user-specified keys.\n", |
| 498 | + "We can define a `metadata` file in the bundle, which contains the metadata information relating to the model, including what the shape and format of inputs and outputs are, what the meaning of the outputs are, what type of model is present, and other information. The structure is a dictionary containing a defined set of keys with additional user-specified keys.\n", |
503 | 499 | "\n",
|
504 | 500 | "A typical `metadata` example is available: \n",
|
505 | 501 | "https://github.com/Project-MONAI/tutorials/blob/master/modules/bundles/spleen_segmentation/configs/metadata.json"
|
|
513 | 509 | "\n",
|
514 | 510 | "There are several predefined scripts in MONAI bundle module to help execute `regular training`, `metadata verification base on schema`, `network input / output verification`, `export to TorchScript model`, etc.\n",
|
515 | 511 | "\n",
|
516 |
| - "Here we leverage the `run` script and specify the ID of trainer in the config." |
| 512 | + "Here we leverage the `run` script and specify the ID of trainer in the config.\n", |
| 513 | + "\n", |
| 514 | + "Just define the entry point expressions in the config to execute in order, and specify the `runner_id` in CLI script." |
517 | 515 | ]
|
518 | 516 | },
|
519 | 517 | {
|
520 | 518 | "cell_type": "markdown",
|
521 | 519 | "metadata": {},
|
522 | 520 | "source": [
|
523 |
| - "`python -m monai.bundle run \"'train#trainer'\" --config_file configs/train.json`" |
| 521 | + "```json\n", |
| 522 | + "\"training\": [\n", |
| 523 | + " \"$monai.utils.set_determinism(seed=123)\",\n", |
| 524 | + " \"$setattr(torch.backends.cudnn, 'benchmark', True)\",\n", |
| 525 | + " \"$@train#trainer.run()\"\n", |
| 526 | + "]\n", |
| 527 | + "```" |
| 528 | + ] |
| 529 | + }, |
| 530 | + { |
| 531 | + "cell_type": "markdown", |
| 532 | + "metadata": {}, |
| 533 | + "source": [ |
| 534 | + "`python -m monai.bundle run training --config_file configs/train.json`" |
524 | 535 | ]
|
525 | 536 | },
|
526 | 537 | {
|
|
538 | 549 | "cell_type": "markdown",
|
539 | 550 | "metadata": {},
|
540 | 551 | "source": [
|
541 |
| - "`python -m monai.bundle run \"'train#trainer'\" --config_file configs/train.json --device \"\\$torch.device('cuda:1')\"`" |
| 552 | + "`python -m monai.bundle run training --config_file configs/train.json --device \"\\$torch.device('cuda:1')\"`" |
542 | 553 | ]
|
543 | 554 | },
|
544 | 555 | {
|
|
552 | 563 | "cell_type": "markdown",
|
553 | 564 | "metadata": {},
|
554 | 565 | "source": [
|
555 |
| - "`python -m monai.bundle run \"'train#trainer'\" --config_file configs/train.json --network \"%configs/test.json#network\"`" |
| 566 | + "`python -m monai.bundle run training --config_file configs/train.json --network \"%configs/test.json#network\"`" |
556 | 567 | ]
|
557 | 568 | },
|
558 | 569 | {
|
|
561 | 572 | "source": [
|
562 | 573 | "## Hybrid programming with config and python code\n",
|
563 | 574 | "\n",
|
564 |
| - "MONAI bundle is flexible to support customized logic, there are several ways to achieve that:\n", |
565 |
| - "- If defining own components like transform, loss, trainer, etc. in a python file, just use its module path in `_target_`.\n", |
| 575 | + "A MONAI bundle supports flexible customized logic, there are several ways to achieve this:\n", |
| 576 | + "\n", |
| 577 | + "- If defining own components like transform, loss, trainer, etc. in a python file, just use its module path in `_target_` within the config file.\n", |
566 | 578 | "- Parse the config in your own python program and do lazy instantiation with customized logic.\n",
|
567 | 579 | "\n",
|
568 | 580 | "Here we show an example to parse the config in python code and execute the training."
|
|
0 commit comments