|
8 | 8 | "\n",
|
9 | 9 | "This notebook will introduce `AutoRunner`, the interface to run the Auto3Dseg pipeline with minimal user inputs.\n",
|
10 | 10 | "\n",
|
11 |
| - "## 1. Set up environment, imports and datasets\n", |
12 |
| - "### 1.1 Set up Environment" |
| 11 | + "Specifically, it will show the features below:\n", |
| 12 | + "1. Use `AutoRunner` with an input config file `input.yaml` example\n", |
| 13 | + "2. How to prepare an `input.yaml`\n", |
| 14 | + "3. How to configure the input/ouput folders\n", |
| 15 | + "4. How to set the internal parameters of **Auto3DSeg** components\n", |
| 16 | + "5. How to apply hyper parameter optimization\n", |
| 17 | + "\n", |
| 18 | + "## Setup environment" |
13 | 19 | ]
|
14 | 20 | },
|
15 | 21 | {
|
16 | 22 | "cell_type": "code",
|
17 |
| - "execution_count": 1, |
| 23 | + "execution_count": null, |
18 | 24 | "metadata": {},
|
19 | 25 | "outputs": [],
|
20 | 26 | "source": [
|
21 |
| - "!python -c \"import monai\" || pip install -q \"monai-weekly[nibabel]\"" |
| 27 | + "!python -c \"import monai\" || pip install -q \"monai-weekly[nibabel, nni, tqdm, cucim, yaml, optuna]\"" |
22 | 28 | ]
|
23 | 29 | },
|
24 | 30 | {
|
25 | 31 | "cell_type": "markdown",
|
26 | 32 | "metadata": {},
|
27 | 33 | "source": [
|
28 |
| - "### 1.2 Set up imports" |
| 34 | + "## Setup imports" |
29 | 35 | ]
|
30 | 36 | },
|
31 | 37 | {
|
|
44 | 50 | ],
|
45 | 51 | "source": [
|
46 | 52 | "import os\n",
|
| 53 | + "import tempfile\n", |
47 | 54 | "import torch\n",
|
48 | 55 | "\n",
|
49 |
| - "from pathlib import Path\n", |
50 |
| - "\n", |
51 | 56 | "from monai.bundle.config_parser import ConfigParser\n",
|
52 | 57 | "from monai.apps import download_and_extract\n",
|
53 | 58 | "\n",
|
|
59 | 64 | "cell_type": "markdown",
|
60 | 65 | "metadata": {},
|
61 | 66 | "source": [
|
62 |
| - "### 1.3 Download public datasets" |
| 67 | + "## Download dataset" |
63 | 68 | ]
|
64 | 69 | },
|
65 | 70 | {
|
66 | 71 | "cell_type": "code",
|
67 |
| - "execution_count": 3, |
| 72 | + "execution_count": null, |
68 | 73 | "metadata": {},
|
69 |
| - "outputs": [ |
70 |
| - { |
71 |
| - "name": "stderr", |
72 |
| - "output_type": "stream", |
73 |
| - "text": [ |
74 |
| - "Task04_Hippocampus.tar: 27.1MB [00:15, 1.88MB/s] " |
75 |
| - ] |
76 |
| - }, |
77 |
| - { |
78 |
| - "name": "stdout", |
79 |
| - "output_type": "stream", |
80 |
| - "text": [ |
81 |
| - "2022-10-18 08:11:37,235 - INFO - Downloaded: Task04_Hippocampus.tar\n", |
82 |
| - "2022-10-18 08:11:37,235 - INFO - Expected md5 is None, skip md5 check for file Task04_Hippocampus.tar.\n", |
83 |
| - "2022-10-18 08:11:37,236 - INFO - Writing into directory: ..\n" |
84 |
| - ] |
85 |
| - }, |
86 |
| - { |
87 |
| - "name": "stderr", |
88 |
| - "output_type": "stream", |
89 |
| - "text": [ |
90 |
| - "\n" |
91 |
| - ] |
92 |
| - } |
93 |
| - ], |
| 74 | + "outputs": [], |
94 | 75 | "source": [
|
95 |
| - "root = str(Path(\".\"))\n", |
| 76 | + "directory = os.environ.get(\"MONAI_DATA_DIRECTORY\")\n", |
| 77 | + "root_dir = tempfile.mkdtemp() if directory is None else directory\n", |
| 78 | + "print(root_dir)\n", |
| 79 | + "\n", |
96 | 80 | "msd_task = \"Task04_Hippocampus\"\n",
|
97 | 81 | "resource = \"https://msd-for-monai.s3-us-west-2.amazonaws.com/\" + msd_task + \".tar\"\n",
|
98 |
| - "compressed_file = os.path.join(root, msd_task + \".tar\")\n", |
99 |
| - "if os.path.exists(root):\n", |
100 |
| - " download_and_extract(resource, compressed_file, root)\n", |
101 | 82 | "\n",
|
102 |
| - "dataroot = os.path.join(root, msd_task)\n", |
| 83 | + "compressed_file = os.path.join(root_dir, msd_task + \".tar\")\n", |
| 84 | + "dataroot = os.path.join(root_dir, msd_task)\n", |
| 85 | + "if os.path.exists(dataroot):\n", |
| 86 | + " download_and_extract(resource, compressed_file, root_dir)\n", |
| 87 | + "\n", |
103 | 88 | "datalist_file = os.path.join(\"..\", \"tasks\", \"msd\", msd_task, \"msd_\" + msd_task.lower() + \"_folds.json\")"
|
104 | 89 | ]
|
105 | 90 | },
|
106 | 91 | {
|
107 | 92 | "cell_type": "markdown",
|
108 | 93 | "metadata": {},
|
109 | 94 | "source": [
|
110 |
| - "### 1.4 Prepare a input YAML configuration" |
| 95 | + "## Prepare a input YAML configuration" |
111 | 96 | ]
|
112 | 97 | },
|
113 | 98 | {
|
|
117 | 102 | "outputs": [],
|
118 | 103 | "source": [
|
119 | 104 | "data_src_cfg = {\n",
|
120 |
| - " \"name\": msd_task, # optional\n", |
121 |
| - " \"task\": \"segmentation\", # optional\n", |
| 105 | + " \"name\": msd_task, # optional, it is only for your own record\n", |
| 106 | + " \"task\": \"segmentation\", # optional, it is only for your own record\n", |
122 | 107 | " \"modality\": \"MRI\", # required\n",
|
123 | 108 | " \"datalist\": datalist_file, # required\n",
|
124 | 109 | " \"dataroot\": dataroot, # required\n",
|
|
131 | 116 | "cell_type": "markdown",
|
132 | 117 | "metadata": {},
|
133 | 118 | "source": [
|
134 |
| - "## 2. Run the Auto3DSeg pipeline in a few lines of code\n", |
| 119 | + "## Run the Auto3DSeg pipeline in a few lines of code\n", |
135 | 120 | "\n",
|
136 | 121 | "Below is the typical usage of AutoRunner\n",
|
137 | 122 | "```python\n",
|
|
143 | 128 | "\n",
|
144 | 129 | "If the user would like to perform a full training in the tutorial, it is recommended to uncomment the `runner.run()` appended at the end of each code block.\n",
|
145 | 130 | "\n",
|
146 |
| - "### 2.1 Use the default setting" |
| 131 | + "## Use the default setting with the input YAML file" |
147 | 132 | ]
|
148 | 133 | },
|
149 | 134 | {
|
150 | 135 | "cell_type": "code",
|
151 |
| - "execution_count": 5, |
| 136 | + "execution_count": null, |
152 | 137 | "metadata": {},
|
153 |
| - "outputs": [ |
154 |
| - { |
155 |
| - "name": "stdout", |
156 |
| - "output_type": "stream", |
157 |
| - "text": [ |
158 |
| - "2022-10-18 08:11:37,523 - INFO - ./work_dir does not exists. Creating...\n", |
159 |
| - "2022-10-18 08:11:37,524 - INFO - ./work_dir created to save all results\n", |
160 |
| - "2022-10-18 08:11:37,524 - INFO - Loading ./input.yaml for AutoRunner and making a copy in /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/input.yaml\n", |
161 |
| - "2022-10-18 08:11:37,531 - INFO - The output_dir is not specified. /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/ensemble_output will be used to save ensemble predictions\n", |
162 |
| - "2022-10-18 08:11:37,533 - INFO - Directory /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/ensemble_output is created to save ensemble predictions\n" |
163 |
| - ] |
164 |
| - } |
165 |
| - ], |
| 138 | + "outputs": [], |
166 | 139 | "source": [
|
167 | 140 | "runner = AutoRunner(input=input)\n",
|
168 | 141 | "# runner.run()"
|
|
172 | 145 | "cell_type": "markdown",
|
173 | 146 | "metadata": {},
|
174 | 147 | "source": [
|
175 |
| - "### 2.2 Use the dictionary instead of a YAML file as the input" |
| 148 | + "## Use the default setting with the dictionary instead of the YAML file as the input" |
176 | 149 | ]
|
177 | 150 | },
|
178 | 151 | {
|
179 | 152 | "cell_type": "code",
|
180 |
| - "execution_count": 6, |
| 153 | + "execution_count": null, |
181 | 154 | "metadata": {},
|
182 |
| - "outputs": [ |
183 |
| - { |
184 |
| - "name": "stdout", |
185 |
| - "output_type": "stream", |
186 |
| - "text": [ |
187 |
| - "2022-10-18 08:11:37,674 - INFO - Work directory ./work_dir is used to save all results\n", |
188 |
| - "2022-10-18 08:11:37,676 - INFO - The output_dir is not specified. /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/ensemble_output will be used to save ensemble predictions\n" |
189 |
| - ] |
190 |
| - } |
191 |
| - ], |
| 155 | + "outputs": [], |
192 | 156 | "source": [
|
193 | 157 | "runner = AutoRunner(input=data_src_cfg)\n",
|
194 | 158 | "# runner.run()"
|
|
198 | 162 | "cell_type": "markdown",
|
199 | 163 | "metadata": {},
|
200 | 164 | "source": [
|
201 |
| - "## 3 Customize and configure the Auto3Dseg\n", |
202 |
| - "### 3.1 Set your working directory" |
| 165 | + "## Customize working directory\n", |
| 166 | + "`AutoRunner` provides the user interfaces to save all the intermediate and final results in a user-specified location.\n", |
| 167 | + "Here we use `./my_workspace` as an example" |
203 | 168 | ]
|
204 | 169 | },
|
205 | 170 | {
|
|
228 | 193 | "cell_type": "markdown",
|
229 | 194 | "metadata": {},
|
230 | 195 | "source": [
|
231 |
| - "### 3.2 Use cached result to save computation time\n", |
| 196 | + "## Customize result caching\n", |
232 | 197 | "\n",
|
233 |
| - "AutoRunner saves intermediate results by default. The user can choose whether it uses the cached results or restart from scratch.\n", |
| 198 | + "AutoRunner saves intermediate results by default to save computation time.\n", |
| 199 | + "The user can choose whether it uses the cached results or restart from scratch.\n", |
234 | 200 | "\n",
|
235 | 201 | "If the users want to start from scratch, they can set `not_use_cache` to True"
|
236 | 202 | ]
|
|
265 | 231 | "cell_type": "markdown",
|
266 | 232 | "metadata": {},
|
267 | 233 | "source": [
|
268 |
| - "### 3.3 Output Ensemble Result\n", |
| 234 | + "## Customize the output folder to save ensemble result\n", |
269 | 235 | "\n",
|
270 | 236 | "AutoRunner will perform inference on the testing data specified by the `datalist` in the data source config input. The inference result will be written to the `ensemble_output` folder under the working directory in the form of `nii.gz`. The user can choose the format by adding keyword arguments to the AutoRunner. A list of argument can be found in [MONAI tranforms documentation](https://docs.monai.io/en/stable/transforms.html#saveimage)."
|
271 | 237 | ]
|
|
294 | 260 | "cell_type": "markdown",
|
295 | 261 | "metadata": {},
|
296 | 262 | "source": [
|
297 |
| - "## 4 Setting Auto3DSeg internal parameters\n", |
298 |
| - "### 4.1 Change the number of folds for cross-validation" |
| 263 | + "## Setting Auto3DSeg internal parameters\n", |
| 264 | + "`Auto3DSeg` has four steps: data analysis, algorithm generation, training, and ensemble. Users can configure the internal parameters of the `AutoRunner` object to customize some steps in the pipeline.\n", |
| 265 | + "\n", |
| 266 | + "Below, we begin the experiments with a smaller number of cross-validation folds. The default is 5 in the algorithm but we set it to 2 here:" |
299 | 267 | ]
|
300 | 268 | },
|
301 | 269 | {
|
|
323 | 291 | "cell_type": "markdown",
|
324 | 292 | "metadata": {},
|
325 | 293 | "source": [
|
326 |
| - "### 4.2 Customize traininig parameters by override the default values" |
327 |
| - ] |
328 |
| - }, |
329 |
| - { |
330 |
| - "cell_type": "code", |
331 |
| - "execution_count": 11, |
332 |
| - "metadata": {}, |
333 |
| - "outputs": [ |
334 |
| - { |
335 |
| - "name": "stdout", |
336 |
| - "output_type": "stream", |
337 |
| - "text": [ |
338 |
| - "2022-10-18 08:11:38,312 - INFO - Work directory ./work_dir is used to save all results\n", |
339 |
| - "2022-10-18 08:11:38,314 - INFO - Loading ./input.yaml for AutoRunner and making a copy in /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/input.yaml\n", |
340 |
| - "2022-10-18 08:11:38,320 - INFO - The output_dir is not specified. /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/ensemble_output will be used to save ensemble predictions\n" |
341 |
| - ] |
342 |
| - } |
343 |
| - ], |
344 |
| - "source": [ |
345 |
| - "runner = AutoRunner(input=input)\n", |
346 |
| - "# Note: among the provided bundles, most networks takes \"num_iterations\" to control the training iterations except segresnet\n", |
347 |
| - "train_param = {\"num_iterations\": 8}\n", |
348 |
| - "runner.set_training_params(params=train_param)\n", |
349 |
| - "# runner.run()" |
350 |
| - ] |
351 |
| - }, |
352 |
| - { |
353 |
| - "cell_type": "markdown", |
354 |
| - "metadata": {}, |
355 |
| - "source": [ |
356 |
| - "#### 4.2.1 A common set of training parameter for all algorithm templates\n", |
| 294 | + "## Customize training parameters by override the default values\n", |
357 | 295 | "\n",
|
358 |
| - "Note: This is for demo purpose. The user doesn't need to specify this training params.\n", |
| 296 | + "`set_training_params` in `AutoRunner` provides an interface to change all algorithms' training parameters in one line. \n", |
359 | 297 | "\n",
|
360 |
| - "**Auto3DSeg** uses bundle templates to perform training, validation, and inference. The number of epochs/iterations of training is specified by the config files in each template. While we can override them, it is also noted that some bundle templates may use \"num_iterations\" and other may use \"num_epochs\" to iterate. Below is code-block to convert num_epoch to iteration style and override all algorithms with the same training parameters for 1-GPU/2-GPU machine. " |
| 298 | + "Note: **Auto3DSeg** uses bundle templates to perform training, validation, and inference. The number of epochs/iterations of training is specified by the config files in each template. While we can override them, it is also noted that some bundle templates may use `num_iterations` and other may use `num_epochs` to iterate.\n", |
| 299 | + "\n", |
| 300 | + "For demo purpose, below is code-block to convert num_epoch to iteration style and override all algorithms with the same training parameters for 1-GPU/2-GPU machine. \n" |
361 | 301 | ]
|
362 | 302 | },
|
363 | 303 | {
|
|
384 | 324 | " \"num_epochs\": num_epoch,\n",
|
385 | 325 | " \"num_warmup_iterations\": n_iter_val,\n",
|
386 | 326 | "}\n",
|
| 327 | + "runner = AutoRunner(input=input)\n", |
387 | 328 | "runner.set_training_params(params=train_param)\n",
|
388 | 329 | "# runner.run()\n"
|
389 | 330 | ]
|
|
392 | 333 | "cell_type": "markdown",
|
393 | 334 | "metadata": {},
|
394 | 335 | "source": [
|
395 |
| - "### 4.3 Customize the ensemble method (mean vs. majority voting)" |
| 336 | + "## Customize the ensemble method\n", |
| 337 | + "\n", |
| 338 | + "There are two supported methods: \"AlgoEnsembleBestN\" and \"AlgoEnsembleBestByFold\"" |
396 | 339 | ]
|
397 | 340 | },
|
398 | 341 | {
|
|
420 | 363 | "cell_type": "markdown",
|
421 | 364 | "metadata": {},
|
422 | 365 | "source": [
|
423 |
| - "### 4.4 Customize the inference parameters by override the default values" |
| 366 | + "## Customize the inference parameters by override the default values" |
424 | 367 | ]
|
425 | 368 | },
|
426 | 369 | {
|
|
454 | 397 | "cell_type": "markdown",
|
455 | 398 | "metadata": {},
|
456 | 399 | "source": [
|
457 |
| - "## 5 Train model with HPO (NNI Grid-search)\n", |
458 |
| - "### 5.1 Apply HPO to search hyper-parameter in Auto3DSeg\n", |
| 400 | + "## Train model with HPO (NNI Grid-search)\n", |
459 | 401 | "\n",
|
460 |
| - "Note: Auto3DSeg supports hyper parameter optimization (HPO) via NNI and Optuna backends. Notebook of how to use these modules can be found in this directory.\n", |
461 |
| - "AutoRunner supports NNI backend with a grid search method via automatically generating a the NNI config and run `nnictl` commands in subprocess.\n", |
462 |
| - "Note: to run the HPO, you need to ensure the development environment has `nni` package. Please refer to the [MONAI Installation Guide](https://docs.monai.io/en/stable/installation.html#installing-the-recommended-dependencies) for how to install the recommended dependencies." |
| 402 | + "**Auto3DSeg** supports hyper parameter optimization (HPO) via `NNI` and `Optuna` backends.\n", |
| 403 | + "AutoRunner supports `NNI` backend with a grid search method via automatically generating a the `NNI` config and run `nnictl` commands in subprocess.\n", |
| 404 | + "\n", |
| 405 | + "Note: to run the HPO, you need to ensure the development environment has `nni` package.\n", |
| 406 | + "Please refer to the [MONAI Installation Guide](https://docs.monai.io/en/stable/installation.html#installing-the-recommended-dependencies) for how to install the recommended dependencies." |
463 | 407 | ]
|
464 | 408 | },
|
465 | 409 | {
|
|
488 | 432 | "cell_type": "markdown",
|
489 | 433 | "metadata": {},
|
490 | 434 | "source": [
|
491 |
| - "### 5.2 Override the templated values\n", |
| 435 | + "## Override the templated values\n", |
| 436 | + "\n", |
| 437 | + "The default `NNI` config that `AutoRunner` looks like below. User can override some of the parameters via the `set_hpo_params` interface:\n", |
492 | 438 | "\n",
|
493 |
| - "AutoRunner uses the following NNI config in its HPO module\n", |
494 | 439 | "```python\n",
|
495 | 440 | "default_nni_config = {\n",
|
496 | 441 | " \"trialCodeDirectory\": \".\",\n",
|
|
501 | 446 | " \"tuner\": {\"name\": \"GridSearch\"},\n",
|
502 | 447 | " \"trainingService\": {\"platform\": \"local\", \"useActiveGpu\": True},\n",
|
503 | 448 | "}\n",
|
504 |
| - "```\n", |
505 |
| - "\n", |
506 |
| - "It can be override by setting the hpo parameters" |
| 449 | + "```" |
507 | 450 | ]
|
508 | 451 | },
|
509 | 452 | {
|
|
534 | 477 | "cell_type": "markdown",
|
535 | 478 | "metadata": {},
|
536 | 479 | "source": [
|
537 |
| - "## 6 Conclusion\n", |
| 480 | + "For more details about the usage of **Auto3DSeg** HPO features, please check the [Auto3DSeg NNI Notebok](./hpo_nni.ipynb) and [Auto3DSeg Optuna Notebook](./hpo_optuna.ipynb)" |
| 481 | + ] |
| 482 | + }, |
| 483 | + { |
| 484 | + "cell_type": "markdown", |
| 485 | + "metadata": {}, |
| 486 | + "source": [ |
| 487 | + "## Conclusion\n", |
538 | 488 | "\n",
|
539 | 489 | "Here we demonstrate how to use the AutoRunner APIs to customize your **Auto3DSeg** pipeline with mininal inputs. Don't forget you need to execute the `run` command to start the training and make everything take effect.\n",
|
540 | 490 | "\n",
|
|
546 | 496 | ],
|
547 | 497 | "metadata": {
|
548 | 498 | "kernelspec": {
|
549 |
| - "display_name": "Python 3.8.13 ('base')", |
| 499 | + "display_name": "Python 3.8.10 64-bit", |
550 | 500 | "language": "python",
|
551 | 501 | "name": "python3"
|
552 | 502 | },
|
|
560 | 510 | "name": "python",
|
561 | 511 | "nbconvert_exporter": "python",
|
562 | 512 | "pygments_lexer": "ipython3",
|
563 |
| - "version": "3.8.13" |
| 513 | + "version": "3.8.10" |
564 | 514 | },
|
565 | 515 | "orig_nbformat": 4,
|
566 | 516 | "vscode": {
|
567 | 517 | "interpreter": {
|
568 |
| - "hash": "d4d1e4263499bec80672ea0156c357c1ee493ec2b1c70f0acce89fc37c4a6abe" |
| 518 | + "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1" |
569 | 519 | }
|
570 | 520 | }
|
571 | 521 | },
|
|
0 commit comments