Skip to content

Commit

Permalink
294 update bundles to use BundleWorkflow interface (Project-MONAI#339)
Browse files Browse the repository at this point in the history
Part of Project-MONAI#294 .

### Description
This PR updates several bundles with `BundleWorkflow ` interface, and
also fix a few errors. Commands in all bundles are tested with
`monai==1.2.0rc3` locally with the changes except the following two:
~~1. `wholeBody_ct_segmentation`~~ (checked)
~~2. `pathology_nuclei_segmentation_classification`~~ (checked)
~~because I do not have these two datasets locally.~~

### Status
**Ready**

### Please ensure all the checkboxes:
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Codeformat tests passed locally by running `./runtests.sh
--codeformat`.
- [ ] In-line docstrings updated.
- [ ] Update `version` and `changelog` in `metadata.json` if changing an
existing bundle.
- [ ] Please ensure the naming rules in config files meet our
requirements (please refer to: `CONTRIBUTING.md`).
- [ ] Ensure versions of packages such as `monai`, `pytorch` and `numpy`
are correct in `metadata.json`.
- [ ] Descriptions should be consistent with the content, such as
`eval_metrics` of the provided weights and TorchScript modules.
- [ ] Files larger than 25MB are excluded and replaced by providing
download links in `large_file.yml`.
- [ ] Avoid using path that contains personal information within config
files (such as use `/home/your_name/` for `"bundle_root"`).

---------

Signed-off-by: Yiheng Wang <vennw@nvidia.com>
Signed-off-by: tangy5 <yucheng.tang@vanderbilt.edu>
Co-authored-by: tangy5 <yucheng.tang@vanderbilt.edu>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 4, 2023
1 parent 19e093c commit 77576c3
Show file tree
Hide file tree
Showing 71 changed files with 397 additions and 270 deletions.
3 changes: 2 additions & 1 deletion ci/verify_tensorrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def verify_tensorrt(bundle_path: str, net_id: str, config_file: str, precision:
trt_export(
net_id=net_id,
filepath=trt_model_path,
ckpt_file=os.path.join(bundle_path, "models/models.pt"),
ckpt_file=os.path.join(bundle_path, "models/model.pt"),
meta_file=os.path.join(bundle_path, "configs/metadata.json"),
config_file=os.path.join(bundle_path, config_file),
precision=precision,
Expand Down Expand Up @@ -63,6 +63,7 @@ def verify_all_tensorrt_bundles(models_path="models"):
except BaseException:
print(f"verify bundle {bundle} with precision {precision} failed.")
raise
print("all TensorRT supported bundles are verified correctly.")


if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions models/brats_mri_segmentation/configs/evaluate.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@
"summary_ops": "*"
}
],
"evaluating": [
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"initialize": [
"$setattr(torch.backends.cudnn, 'benchmark', True)"
],
"run": [
"$@validate#evaluator.run()"
]
}
9 changes: 6 additions & 3 deletions models/brats_mri_segmentation/configs/inference.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"transforms": [
{
"_target_": "LoadImaged",
"keys": "image"
"keys": "image",
"image_only": false
},
{
"_target_": "NormalizeIntensityd",
Expand Down Expand Up @@ -129,8 +130,10 @@
"val_handlers": "@handlers",
"amp": true
},
"evaluating": [
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"initialize": [
"$setattr(torch.backends.cudnn, 'benchmark', True)"
],
"run": [
"$@evaluator.run()"
]
}
9 changes: 5 additions & 4 deletions models/brats_mri_segmentation/configs/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20220324.json",
"version": "0.3.9",
"version": "0.4.0",
"changelog": {
"0.4.0": "adapt to BundleWorkflow interface",
"0.3.9": "black autofix format and add name tag",
"0.3.8": "modify dataset key name",
"0.3.7": "restructure readme to match updated template",
Expand All @@ -17,9 +18,9 @@
"0.1.1": "update for MetaTensor",
"0.1.0": "complete the model package"
},
"monai_version": "1.0.1",
"pytorch_version": "1.13.0",
"numpy_version": "1.22.4",
"monai_version": "1.2.0rc3",
"pytorch_version": "1.13.1",
"numpy_version": "1.22.2",
"optional_packages_version": {
"nibabel": "4.0.1",
"pytorch-ignite": "0.4.9",
Expand Down
12 changes: 8 additions & 4 deletions models/brats_mri_segmentation/configs/multi_gpu_train.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
},
"validate#dataloader#sampler": "@validate#sampler",
"validate#evaluator#val_handlers": "$None if dist.get_rank() > 0 else @validate#handlers",
"training": [
"initialize": [
"$import torch.distributed as dist",
"$dist.init_process_group(backend='nccl')",
"$dist.is_initialized() or dist.init_process_group(backend='nccl')",
"$torch.cuda.set_device(@device)",
"$monai.utils.set_determinism(seed=123)",
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"$@train#trainer.run()",
"$setattr(torch.backends.cudnn, 'benchmark', True)"
],
"run": [
"$@train#trainer.run()"
],
"finalize": [
"$dist.destroy_process_group()"
]
}
9 changes: 6 additions & 3 deletions models/brats_mri_segmentation/configs/train.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"keys": [
"image",
"label"
]
],
"image_only": false
},
{
"_target_": "ConvertToMultiChannelBasedOnBratsClassesd",
Expand Down Expand Up @@ -318,9 +319,11 @@
"amp": "@amp"
}
},
"training": [
"initialize": [
"$monai.utils.set_determinism(seed=123)",
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"$setattr(torch.backends.cudnn, 'benchmark', True)"
],
"run": [
"$@train#trainer.run()"
]
}
8 changes: 4 additions & 4 deletions models/brats_mri_segmentation/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,24 @@ For more details usage instructions, visit the [MONAI Bundle Configuration Page]

#### Execute training:
```
python -m monai.bundle run training --meta_file configs/metadata.json --config_file configs/train.json --logging_file configs/logging.conf
python -m monai.bundle run --config_file configs/train.json
```

#### Override the `train` config to execute multi-GPU training:
```
torchrun --standalone --nnodes=1 --nproc_per_node=8 -m monai.bundle run training --meta_file configs/metadata.json --config_file "['configs/train.json','configs/multi_gpu_train.json']" --logging_file configs/logging.conf
torchrun --standalone --nnodes=1 --nproc_per_node=8 -m monai.bundle run --config_file "['configs/train.json','configs/multi_gpu_train.json']"
```

Please note that the distributed training-related options depend on the actual running environment; thus, users may need to remove `--standalone`, modify `--nnodes`, or do some other necessary changes according to the machine used. For more details, please refer to [pytorch's official tutorial](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html).

#### Override the `train` config to execute evaluation with the trained model:
```
python -m monai.bundle run evaluating --meta_file configs/metadata.json --config_file "['configs/train.json','configs/evaluate.json']" --logging_file configs/logging.conf
python -m monai.bundle run --config_file "['configs/train.json','configs/evaluate.json']"
```

#### Execute inference:
```
python -m monai.bundle run evaluating --meta_file configs/metadata.json --config_file configs/inference.json --logging_file configs/logging.conf
python -m monai.bundle run --config_file configs/inference.json
```

# References
Expand Down
6 changes: 4 additions & 2 deletions models/endoscopic_inbody_classification/configs/evaluate.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
"summary_ops": "*"
}
],
"evaluating": [
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"initialize": [
"$setattr(torch.backends.cudnn, 'benchmark', True)"
],
"run": [
"$@validate#evaluator.run()"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"$import os",
"$import torch"
],
"bundle_root": "/workspace/bundle/endoscopic_inbody_classification",
"bundle_root": ".",
"dataset_dir": "/workspace/data/endoscopic_inbody_classification",
"test_json": "$@bundle_root+'/label/test_samples.json'",
"test_fp": "$open(@test_json,'r', encoding='utf8')",
Expand Down Expand Up @@ -105,8 +105,10 @@
"postprocessing": "@postprocessing",
"val_handlers": "@handlers"
},
"evaluating": [
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"initialize": [
"$setattr(torch.backends.cudnn, 'benchmark', True)"
],
"run": [
"$@evaluator.run()"
]
}
9 changes: 5 additions & 4 deletions models/endoscopic_inbody_classification/configs/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20220324.json",
"version": "0.3.6",
"version": "0.3.7",
"changelog": {
"0.3.7": "adapt to BundleWorkflow interface",
"0.3.6": "add name tag",
"0.3.5": "fix a comment issue in the data_process script",
"0.3.4": "add note for multi-gpu training with example dataset",
Expand All @@ -15,9 +16,9 @@
"0.1.0": "complete the first version model package",
"0.0.1": "initialize the model package structure"
},
"monai_version": "1.0.1",
"pytorch_version": "1.13.0",
"numpy_version": "1.22.4",
"monai_version": "1.2.0rc3",
"pytorch_version": "1.13.1",
"numpy_version": "1.22.2",
"optional_packages_version": {
"nibabel": "4.0.1",
"pytorch-ignite": "0.4.9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
},
"validate#dataloader#sampler": "@validate#sampler",
"validate#evaluator#val_handlers": "$None if dist.get_rank() > 0 else @validate#handlers",
"training": [
"initialize": [
"$import torch.distributed as dist",
"$dist.init_process_group(backend='nccl')",
"$dist.is_initialized() or dist.init_process_group(backend='nccl')",
"$torch.cuda.set_device(@device)",
"$monai.utils.set_determinism(seed=123)",
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"$@train#trainer.run()",
"$setattr(torch.backends.cudnn, 'benchmark', True)"
],
"run": [
"$@train#trainer.run()"
],
"finalize": [
"$dist.destroy_process_group()"
]
}
10 changes: 6 additions & 4 deletions models/endoscopic_inbody_classification/configs/train.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"$import json",
"$import ignite"
],
"bundle_root": "/workspace/bundle/endoscopic_inbody_classification",
"bundle_root": ".",
"ckpt_dir": "$@bundle_root + '/models'",
"output_dir": "$@bundle_root + '/eval'",
"dataset_dir": "/workspace/data/endoscopic_inbody_classification",
Expand Down Expand Up @@ -251,9 +251,11 @@
"val_handlers": "@validate#handlers"
}
},
"training": [
"$monai.utils.set_determinism(seed=0)",
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"initialize": [
"$monai.utils.set_determinism(seed=123)",
"$setattr(torch.backends.cudnn, 'benchmark', True)"
],
"run": [
"$@train#trainer.run()"
]
}
21 changes: 5 additions & 16 deletions models/endoscopic_inbody_classification/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,14 @@ For more details usage instructions, visit the [MONAI Bundle Configuration Page]
#### Execute training:

```
python -m monai.bundle run training \
--meta_file configs/metadata.json \
--config_file configs/train.json \
--logging_file configs/logging.conf
python -m monai.bundle run --config_file configs/train.json
```

#### Override the `train` config to execute multi-GPU training:

```
torchrun --standalone --nnodes=1 --nproc_per_node=2 -m monai.bundle run training \
--meta_file configs/metadata.json \
--config_file "['configs/train.json','configs/multi_gpu_train.json']" \
--logging_file configs/logging.conf
torchrun --standalone --nnodes=1 --nproc_per_node=2 -m monai.bundle run \
--config_file "['configs/train.json','configs/multi_gpu_train.json']"
```

Please note that the distributed training-related options depend on the actual running environment; thus, users may need to remove `--standalone`, modify `--nnodes`, or do some other necessary changes according to the machine used. For more details, please refer to [pytorch's official tutorial](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html).
Expand All @@ -97,19 +92,13 @@ In addition, if using the 20 samples example dataset, the preprocessing script w
#### Override the `train` config to execute evaluation with the trained model:

```
python -m monai.bundle run evaluating \
--meta_file configs/metadata.json \
--config_file "['configs/train.json','configs/evaluate.json']" \
--logging_file configs/logging.conf
python -m monai.bundle run --config_file "['configs/train.json','configs/evaluate.json']"
```

#### Execute inference:

```
python -m monai.bundle run evaluating \
--meta_file configs/metadata.json \
--config_file configs/inference.json \
--logging_file configs/logging.conf
python -m monai.bundle run --config_file configs/inference.json
```
The classification result of every images in `test.json` will be printed to the screen.

Expand Down
6 changes: 4 additions & 2 deletions models/endoscopic_tool_segmentation/configs/evaluate.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@
"summary_ops": "*"
}
],
"evaluating": [
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"initialize": [
"$setattr(torch.backends.cudnn, 'benchmark', True)"
],
"run": [
"$@validate#evaluator.run()"
]
}
8 changes: 5 additions & 3 deletions models/endoscopic_tool_segmentation/configs/inference.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"$import glob",
"$import os"
],
"bundle_root": "/workspace/bundle/endoscopic_tool_segmentation",
"bundle_root": ".",
"output_dir": "$@bundle_root + '/eval'",
"dataset_dir": "/workspace/data/endoscopic_tool_dataset",
"datalist": "$list(sorted(glob.glob(os.path.join(@dataset_dir,'test', '*', '*[!seg].jpg'))))",
Expand Down Expand Up @@ -130,8 +130,10 @@
"postprocessing": "@postprocessing",
"val_handlers": "@handlers"
},
"evaluating": [
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"initialize": [
"$setattr(torch.backends.cudnn, 'benchmark', True)"
],
"run": [
"$@evaluator.run()"
]
}
7 changes: 4 additions & 3 deletions models/endoscopic_tool_segmentation/configs/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20220324.json",
"version": "0.4.3",
"version": "0.4.4",
"changelog": {
"0.4.4": "adapt to BundleWorkflow interface",
"0.4.3": "update this bundle to support TensorRT convert",
"0.4.2": "support monai 1.2 new FlexibleUNet",
"0.4.1": "add name tag",
Expand All @@ -15,8 +16,8 @@
"0.0.1": "initialize the model package structure"
},
"monai_version": "1.2.0rc3",
"pytorch_version": "1.13.0",
"numpy_version": "1.22.4",
"pytorch_version": "1.13.1",
"numpy_version": "1.22.2",
"optional_packages_version": {
"nibabel": "4.0.1",
"pytorch-ignite": "0.4.9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
},
"validate#dataloader#sampler": "@validate#sampler",
"validate#handlers#1#_disabled_": "$dist.get_rank() > 0",
"evaluating": [
"initialize": [
"$import torch.distributed as dist",
"$dist.init_process_group(backend='nccl')",
"$dist.is_initialized() or dist.init_process_group(backend='nccl')",
"$torch.cuda.set_device(@device)",
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"$import logging",
"$@validate#evaluator.logger.setLevel(logging.WARNING if dist.get_rank() > 0 else logging.INFO)",
"$@validate#evaluator.run()",
"$@validate#evaluator.logger.setLevel(logging.WARNING if dist.get_rank() > 0 else logging.INFO)"
],
"run": [
"$@validate#evaluator.run()"
],
"finalize": [
"$dist.destroy_process_group()"
]
}
Loading

0 comments on commit 77576c3

Please sign in to comment.