Skip to content

737 Update bundle tutorial for the CLI scripts #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions modules/bundle/get_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
"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",
"\n",
"This notebook mainly contains the below sections:\n",
"- Define a training config with `JSON` or `YAML` format\n",
"- Execute training based on bundle scripts and configs\n",
"- Hybrid programming with config and python code\n",
"- Define a training config with `JSON` or `YAML` format.\n",
"- Execute training based on bundle scripts and configs.\n",
"- Execute other scripts for bundle functionalities.\n",
"- Hybrid programming with config and python code.\n",
"\n",
"You can find the usage examples of MONAI bundle key features and syntax in this tutorial, like:\n",
"- Instantiate a python object from a dictionary config with `_target_` indicating class or function name or module path.\n",
Expand Down Expand Up @@ -511,9 +512,7 @@
"source": [
"## Execute training with bundle script - `run`\n",
"\n",
"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",
"\n",
"Here we leverage the `run` script and specify the ID of trainer in the config.\n",
"There are several predefined scripts in MONAI bundle module, here we leverage the `run` script and specify the ID of trainer in the config.\n",
"\n",
"Just define the entry point expressions in the config to execute in order, and specify the `runner_id` in CLI script."
]
Expand All @@ -535,7 +534,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"`python -m monai.bundle run training --config_file configs/train.json`"
"```shell\n",
"python -m monai.bundle run training --config_file configs/train.json\n",
"```"
]
},
{
Expand All @@ -546,28 +547,52 @@
"\n",
"To override some config items at runtime, users can specify the target `id` and `value` at command line, or override the `id` with some content in another config file. Here we set the device to `cuda:1` at runtime.\n",
"\n",
"Please note that \"#\" and \"$\" may be meaningful syntax for some `shell` and `CLI` tools, so may need to add escape character or quotes for them in the command line, like: `\"\\$torch.device('cuda:1')\"`. For more details: https://github.com/google/python-fire/blob/v0.4.0/fire/parser.py#L60."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`python -m monai.bundle run training --config_file configs/train.json --device \"\\$torch.device('cuda:1')\"`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Override content from another config file."
"Please note that \"#\" and \"$\" may be meaningful syntax for some `shell` and `CLI` tools, so may need to add escape character or quotes for them in the command line, like: `\"\\$torch.device('cuda:1')\"`. For more details: https://github.com/google/python-fire/blob/v0.4.0/fire/parser.py#L60.\n",
"```shell\n",
"python -m monai.bundle run training --config_file configs/train.json --device \"\\$torch.device('cuda:1')\"\n",
"```\n",
"Override content from another config file.\n",
"```shell\n",
"python -m monai.bundle run training --config_file configs/train.json --network \"%configs/test.json#network\"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`python -m monai.bundle run training --config_file configs/train.json --network \"%configs/test.json#network\"`"
"## Execute other bundle scripts\n",
"\n",
"Besides `run`, there are also many other scripts for bundle functionalities. All the scripts are available at: https://docs.monai.io/en/latest/bundle.html#scripts.\n",
"\n",
"Here is some typical examples:\n",
"\n",
"1. Initialize a bundle directory based on the template and pretrained checkpoint weights.\n",
"```shell\n",
"python -m monai.bundle init_bundle --bundle_dir <target dir> --ckpt_file <checkpoint path>\n",
"```\n",
"\n",
"2. Export the model checkpoint to a `TorchScript` model at the given filepath with metadata and config included as JSON files.\n",
"```shell\n",
"python -m monai.bundle ckpt_export network --filepath <export path> --ckpt_file <checkpoint path> --config_file <config path>\n",
"```\n",
"\n",
"3. Verify the format of provided `metadata` file based on the predefined `schema`.\n",
"```shell\n",
"python -m monai.bundle verify_metadata --meta_file <meta path>\n",
"```\n",
"\n",
"4. Verify the input and output data shape and data type of network defined in the metadata. It will test with fake Tensor data according to the required data shape in `metadata`.\n",
"```shell\n",
"python -m monai.bundle verify_net_in_out network --meta_file <metadata path> --config_file <config path>\n",
"```\n",
"The acceptable data shape in the metadata can support `\"*\"` for any size, or use an expression with Python mathematical operators and one character variables to represent dependence on an unknown quantity, for example, `\"2**p\"` represents a size which must be a power of 2, `\"2**p*n\"` must be a multiple of a power of 2. `\"spatial_shape\": [ \"32 * n\", \"2 ** p * n\", \"*\"]`.\n",
"\n",
"\n",
"5. Download a bundle from Github release or URL.\n",
"```shell\n",
"python -m monai.bundle download --name <bundle_name>\n",
"```"
]
},
{
Expand Down