|
10 | 10 | "\n",
|
11 | 11 | "Specifically, it will show the features below:\n",
|
12 | 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", |
| 13 | + "2. How to prepare the config file `input.yaml`\n", |
| 14 | + "3. How to configure the paths for inputs, outputs, and intermediate results\n", |
15 | 15 | "4. How to set the internal parameters of **Auto3DSeg** components\n",
|
16 |
| - "5. How to apply hyper parameter optimization\n", |
| 16 | + "5. How to use a 3rd party hyper parameter optimization(HPO) package with `AutoRunner`\n", |
17 | 17 | "\n",
|
18 | 18 | "## Setup environment"
|
19 | 19 | ]
|
|
36 | 36 | },
|
37 | 37 | {
|
38 | 38 | "cell_type": "code",
|
39 |
| - "execution_count": 2, |
40 |
| - "metadata": {}, |
41 |
| - "outputs": [ |
42 |
| - { |
43 |
| - "name": "stderr", |
44 |
| - "output_type": "stream", |
45 |
| - "text": [ |
46 |
| - "/opt/conda/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", |
47 |
| - " from .autonotebook import tqdm as notebook_tqdm\n" |
48 |
| - ] |
49 |
| - } |
50 |
| - ], |
| 39 | + "execution_count": null, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [], |
51 | 42 | "source": [
|
52 | 43 | "import os\n",
|
53 | 44 | "import tempfile\n",
|
|
82 | 73 | "\n",
|
83 | 74 | "compressed_file = os.path.join(root_dir, msd_task + \".tar\")\n",
|
84 | 75 | "dataroot = os.path.join(root_dir, msd_task)\n",
|
85 |
| - "if os.path.exists(dataroot):\n", |
| 76 | + "if not os.path.exists(dataroot):\n", |
86 | 77 | " download_and_extract(resource, compressed_file, root_dir)\n",
|
87 | 78 | "\n",
|
88 | 79 | "datalist_file = os.path.join(\"..\", \"tasks\", \"msd\", msd_task, \"msd_\" + msd_task.lower() + \"_folds.json\")"
|
|
97 | 88 | },
|
98 | 89 | {
|
99 | 90 | "cell_type": "code",
|
100 |
| - "execution_count": 4, |
| 91 | + "execution_count": null, |
101 | 92 | "metadata": {},
|
102 | 93 | "outputs": [],
|
103 | 94 | "source": [
|
104 |
| - "data_src_cfg = {\n", |
| 95 | + "input_cfg = {\n", |
105 | 96 | " \"name\": msd_task, # optional, it is only for your own record\n",
|
106 | 97 | " \"task\": \"segmentation\", # optional, it is only for your own record\n",
|
107 | 98 | " \"modality\": \"MRI\", # required\n",
|
108 | 99 | " \"datalist\": datalist_file, # required\n",
|
109 | 100 | " \"dataroot\": dataroot, # required\n",
|
110 | 101 | "}\n",
|
111 | 102 | "input = './input.yaml'\n",
|
112 |
| - "ConfigParser.export_config_file(data_src_cfg, input)" |
| 103 | + "ConfigParser.export_config_file(input_cfg, input)" |
113 | 104 | ]
|
114 | 105 | },
|
115 | 106 | {
|
|
154 | 145 | "metadata": {},
|
155 | 146 | "outputs": [],
|
156 | 147 | "source": [
|
157 |
| - "runner = AutoRunner(input=data_src_cfg)\n", |
| 148 | + "runner = AutoRunner(input=input_cfg)\n", |
158 | 149 | "# runner.run()"
|
159 | 150 | ]
|
160 | 151 | },
|
|
169 | 160 | },
|
170 | 161 | {
|
171 | 162 | "cell_type": "code",
|
172 |
| - "execution_count": 7, |
173 |
| - "metadata": {}, |
174 |
| - "outputs": [ |
175 |
| - { |
176 |
| - "name": "stdout", |
177 |
| - "output_type": "stream", |
178 |
| - "text": [ |
179 |
| - "2022-10-18 08:11:37,812 - INFO - ./my_workspace does not exists. Creating...\n", |
180 |
| - "2022-10-18 08:11:37,813 - INFO - ./my_workspace created to save all results\n", |
181 |
| - "2022-10-18 08:11:37,815 - INFO - Loading ./input.yaml for AutoRunner and making a copy in /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/my_workspace/input.yaml\n", |
182 |
| - "2022-10-18 08:11:37,818 - INFO - The output_dir is not specified. /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/my_workspace/ensemble_output will be used to save ensemble predictions\n", |
183 |
| - "2022-10-18 08:11:37,819 - INFO - Directory /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/my_workspace/ensemble_output is created to save ensemble predictions\n" |
184 |
| - ] |
185 |
| - } |
186 |
| - ], |
| 163 | + "execution_count": null, |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [], |
187 | 166 | "source": [
|
188 | 167 | "runner = AutoRunner(work_dir='./my_workspace', input=input)\n",
|
189 | 168 | "# runner.run()"
|
|
203 | 182 | },
|
204 | 183 | {
|
205 | 184 | "cell_type": "code",
|
206 |
| - "execution_count": 8, |
207 |
| - "metadata": {}, |
208 |
| - "outputs": [ |
209 |
| - { |
210 |
| - "name": "stdout", |
211 |
| - "output_type": "stream", |
212 |
| - "text": [ |
213 |
| - "2022-10-18 08:11:37,936 - INFO - Work directory ./work_dir is used to save all results\n", |
214 |
| - "2022-10-18 08:11:37,938 - INFO - Loading ./input.yaml for AutoRunner and making a copy in /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/input.yaml\n", |
215 |
| - "2022-10-18 08:11:37,940 - 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" |
216 |
| - ] |
217 |
| - } |
218 |
| - ], |
| 185 | + "execution_count": null, |
| 186 | + "metadata": {}, |
| 187 | + "outputs": [], |
219 | 188 | "source": [
|
220 | 189 | "# This will restart from scratch and not use any cached results\n",
|
221 | 190 | "runner = AutoRunner(input=input, not_use_cache=True)\n",
|
|
238 | 207 | },
|
239 | 208 | {
|
240 | 209 | "cell_type": "code",
|
241 |
| - "execution_count": 9, |
242 |
| - "metadata": {}, |
243 |
| - "outputs": [ |
244 |
| - { |
245 |
| - "name": "stdout", |
246 |
| - "output_type": "stream", |
247 |
| - "text": [ |
248 |
| - "2022-10-18 08:11:38,055 - INFO - Work directory ./work_dir is used to save all results\n", |
249 |
| - "2022-10-18 08:11:38,057 - INFO - Loading ./input.yaml for AutoRunner and making a copy in /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/input.yaml\n", |
250 |
| - "2022-10-18 08:11:38,061 - INFO - Directory ./output_dir is created to save ensemble predictions\n" |
251 |
| - ] |
252 |
| - } |
253 |
| - ], |
| 210 | + "execution_count": null, |
| 211 | + "metadata": {}, |
| 212 | + "outputs": [], |
254 | 213 | "source": [
|
255 | 214 | "runner = AutoRunner(input=input, output_dir='./output_dir')\n",
|
256 | 215 | "# runner.run()"
|
|
268 | 227 | },
|
269 | 228 | {
|
270 | 229 | "cell_type": "code",
|
271 |
| - "execution_count": 10, |
272 |
| - "metadata": {}, |
273 |
| - "outputs": [ |
274 |
| - { |
275 |
| - "name": "stdout", |
276 |
| - "output_type": "stream", |
277 |
| - "text": [ |
278 |
| - "2022-10-18 08:11:38,182 - INFO - Work directory ./work_dir is used to save all results\n", |
279 |
| - "2022-10-18 08:11:38,186 - INFO - Loading ./input.yaml for AutoRunner and making a copy in /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/input.yaml\n", |
280 |
| - "2022-10-18 08:11:38,194 - 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" |
281 |
| - ] |
282 |
| - } |
283 |
| - ], |
| 230 | + "execution_count": null, |
| 231 | + "metadata": {}, |
| 232 | + "outputs": [], |
284 | 233 | "source": [
|
285 | 234 | "runner = AutoRunner(input=input)\n",
|
286 | 235 | "runner.set_num_fold(num_fold=2)\n",
|
|
302 | 251 | },
|
303 | 252 | {
|
304 | 253 | "cell_type": "code",
|
305 |
| - "execution_count": 12, |
| 254 | + "execution_count": null, |
306 | 255 | "metadata": {},
|
307 | 256 | "outputs": [],
|
308 | 257 | "source": [
|
309 |
| - "max_epochs = 2000\n", |
| 258 | + "max_epochs = 2\n", |
310 | 259 | "\n",
|
311 |
| - "num_gpus = 1 if \"multigpu\" in data_src_cfg and not data_src_cfg[\"multigpu\"] else torch.cuda.device_count()\n", |
| 260 | + "# safeguard to ensure max_epochs is greater or equal to 2\n", |
| 261 | + "max_epochs = max(max_epochs, 2)\n", |
| 262 | + "\n", |
| 263 | + "num_gpus = 1 if \"multigpu\" in input_cfg and not input_cfg[\"multigpu\"] else torch.cuda.device_count()\n", |
312 | 264 | "\n",
|
313 | 265 | "num_epoch = max_epochs\n",
|
314 | 266 | "num_images_per_batch = 2\n",
|
|
326 | 278 | "}\n",
|
327 | 279 | "runner = AutoRunner(input=input)\n",
|
328 | 280 | "runner.set_training_params(params=train_param)\n",
|
329 |
| - "# runner.run()\n" |
| 281 | + "# runner.run()" |
330 | 282 | ]
|
331 | 283 | },
|
332 | 284 | {
|
|
340 | 292 | },
|
341 | 293 | {
|
342 | 294 | "cell_type": "code",
|
343 |
| - "execution_count": 13, |
344 |
| - "metadata": {}, |
345 |
| - "outputs": [ |
346 |
| - { |
347 |
| - "name": "stdout", |
348 |
| - "output_type": "stream", |
349 |
| - "text": [ |
350 |
| - "2022-10-18 08:11:38,613 - INFO - Work directory ./work_dir is used to save all results\n", |
351 |
| - "2022-10-18 08:11:38,615 - INFO - Loading ./input.yaml for AutoRunner and making a copy in /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/input.yaml\n", |
352 |
| - "2022-10-18 08:11:38,618 - 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" |
353 |
| - ] |
354 |
| - } |
355 |
| - ], |
| 295 | + "execution_count": null, |
| 296 | + "metadata": {}, |
| 297 | + "outputs": [], |
356 | 298 | "source": [
|
357 | 299 | "runner = AutoRunner(input=input)\n",
|
358 | 300 | "runner.set_ensemble_method(ensemble_method_name=\"AlgoEnsembleBestByFold\")\n",
|
|
368 | 310 | },
|
369 | 311 | {
|
370 | 312 | "cell_type": "code",
|
371 |
| - "execution_count": 14, |
372 |
| - "metadata": {}, |
373 |
| - "outputs": [ |
374 |
| - { |
375 |
| - "name": "stdout", |
376 |
| - "output_type": "stream", |
377 |
| - "text": [ |
378 |
| - "2022-10-18 08:11:38,783 - INFO - Work directory ./work_dir is used to save all results\n", |
379 |
| - "2022-10-18 08:11:38,784 - INFO - Loading ./input.yaml for AutoRunner and making a copy in /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/input.yaml\n", |
380 |
| - "2022-10-18 08:11:38,786 - 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" |
381 |
| - ] |
382 |
| - } |
383 |
| - ], |
| 313 | + "execution_count": null, |
| 314 | + "metadata": {}, |
| 315 | + "outputs": [], |
384 | 316 | "source": [
|
385 | 317 | "# set model ensemble method\n",
|
386 | 318 | "pred_params = {\n",
|
|
397 | 329 | "cell_type": "markdown",
|
398 | 330 | "metadata": {},
|
399 | 331 | "source": [
|
400 |
| - "## Train model with HPO (NNI Grid-search)\n", |
| 332 | + "## Train model with HPO\n", |
401 | 333 | "\n",
|
402 | 334 | "**Auto3DSeg** supports hyper parameter optimization (HPO) via `NNI` and `Optuna` backends.\n",
|
| 335 | + "If you wound like to the use `Optuna`, please check the [notebook](hpo_optuna.ipynb) for detailed usage.\n", |
| 336 | + "\n", |
| 337 | + "Here we demonstrate the HPO option with `NNI` by Microsoft.\n", |
| 338 | + "Please install it via `pip install nni` if you hope to execute HPO with it in tutorial and haven't done so in the beginning of the notebook.\n", |
403 | 339 | "AutoRunner supports `NNI` backend with a grid search method via automatically generating a the `NNI` config and run `nnictl` commands in subprocess.\n",
|
404 | 340 | "\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." |
| 341 | + "## Use `AutoRunner` with `NNI` backend to perform grid-search\n", |
| 342 | + "\n", |
| 343 | + "After `runner.run()` is executed, `nni` will attempt to start a web service using port 8088 by default. If you are running the tutorial in a remote host, please make sure the port is available on the system." |
407 | 344 | ]
|
408 | 345 | },
|
409 | 346 | {
|
410 | 347 | "cell_type": "code",
|
411 |
| - "execution_count": 15, |
412 |
| - "metadata": {}, |
413 |
| - "outputs": [ |
414 |
| - { |
415 |
| - "name": "stdout", |
416 |
| - "output_type": "stream", |
417 |
| - "text": [ |
418 |
| - "2022-10-18 08:11:38,907 - INFO - Work directory ./work_dir is used to save all results\n", |
419 |
| - "2022-10-18 08:11:38,908 - INFO - Loading ./input.yaml for AutoRunner and making a copy in /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/input.yaml\n", |
420 |
| - "2022-10-18 08:11:38,910 - 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" |
421 |
| - ] |
422 |
| - } |
423 |
| - ], |
| 348 | + "execution_count": null, |
| 349 | + "metadata": {}, |
| 350 | + "outputs": [], |
424 | 351 | "source": [
|
425 | 352 | "runner = AutoRunner(input=input, hpo=True)\n",
|
426 | 353 | "search_space = {\"learning_rate\": {\"_type\": \"choice\", \"_value\": [0.0001, 0.001, 0.01, 0.1]}}\n",
|
|
451 | 378 | },
|
452 | 379 | {
|
453 | 380 | "cell_type": "code",
|
454 |
| - "execution_count": 16, |
455 |
| - "metadata": {}, |
456 |
| - "outputs": [ |
457 |
| - { |
458 |
| - "name": "stdout", |
459 |
| - "output_type": "stream", |
460 |
| - "text": [ |
461 |
| - "2022-10-18 08:11:39,042 - INFO - Work directory ./work_dir is used to save all results\n", |
462 |
| - "2022-10-18 08:11:39,046 - INFO - Loading ./input.yaml for AutoRunner and making a copy in /workspace/monai/tutorials-in-dev/auto3dseg/notebooks/work_dir/input.yaml\n", |
463 |
| - "2022-10-18 08:11:39,054 - 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" |
464 |
| - ] |
465 |
| - } |
466 |
| - ], |
| 381 | + "execution_count": null, |
| 382 | + "metadata": {}, |
| 383 | + "outputs": [], |
467 | 384 | "source": [
|
468 | 385 | "runner = AutoRunner(input=input, hpo=True)\n",
|
469 | 386 | "hpo_params = {\"maxTrialNumber\": 20}\n",
|
|
496 | 413 | ],
|
497 | 414 | "metadata": {
|
498 | 415 | "kernelspec": {
|
499 |
| - "display_name": "Python 3.8.10 64-bit", |
| 416 | + "display_name": "Python 3.8.13 ('base')", |
500 | 417 | "language": "python",
|
501 | 418 | "name": "python3"
|
502 | 419 | },
|
|
510 | 427 | "name": "python",
|
511 | 428 | "nbconvert_exporter": "python",
|
512 | 429 | "pygments_lexer": "ipython3",
|
513 |
| - "version": "3.8.10" |
| 430 | + "version": "3.8.13" |
514 | 431 | },
|
515 | 432 | "orig_nbformat": 4,
|
516 | 433 | "vscode": {
|
517 | 434 | "interpreter": {
|
518 |
| - "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1" |
| 435 | + "hash": "d4d1e4263499bec80672ea0156c357c1ee493ec2b1c70f0acce89fc37c4a6abe" |
519 | 436 | }
|
520 | 437 | }
|
521 | 438 | },
|
|
0 commit comments