-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_monai_bundle.py
More file actions
143 lines (129 loc) · 5.18 KB
/
Copy pathrun_monai_bundle.py
File metadata and controls
143 lines (129 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import argparse
import os
from monai.bundle.scripts import run
from monai.utils.misc import set_determinism
def get_parser():
parser = argparse.ArgumentParser(
description="Run a MONAI bundle with specified configurations."
)
parser.add_argument(
"--bundle",
type=str,
required=True,
help="Bundle directory name, e.g., brats21_softl1ace_dice_ce_1",
)
parser.add_argument(
"--mode",
type=str,
required=True,
choices=[
"train",
"inference_pred",
"inference_eval",
"temp_scale_train",
"temp_scale_eval",
"inference_eval_additional",
"temp_scale_eval_additional",
],
help="Operation mode.",
)
parser.add_argument(
"--seed",
type=int,
default=12345,
help="Seed for deterministic training.",
)
parser.add_argument(
"--debug", action="store_true", help="Enable debug mode to test train and val."
)
return parser
def get_config_files(bundle_root, mode, debug):
if mode == "train":
# Config order for training mode
config_files = [
os.path.join(bundle_root, "configs", "common.yaml"),
os.path.join(bundle_root, "configs", "train.yaml"),
os.path.join(bundle_root, "configs", "validation.yaml"),
os.path.join(bundle_root, "configs", "train.yaml"),
os.path.join(bundle_root, "configs", "loss.yaml"),
os.path.join(bundle_root, "configs", "data.yaml"),
]
elif mode == "inference_pred":
# Config order for inference prediction mode
config_files = [
os.path.join(bundle_root, "configs", "common.yaml"),
os.path.join(bundle_root, "configs", "validation.yaml"),
os.path.join(bundle_root, "configs", "inference_pred.yaml"),
os.path.join(bundle_root, "configs", "data.yaml"),
]
elif mode == "inference_eval":
# Config order for inference evaluation mode
config_files = [
os.path.join(bundle_root, "configs", "common.yaml"),
os.path.join(bundle_root, "configs", "validation.yaml"),
os.path.join(bundle_root, "configs", "inference_eval.yaml"),
os.path.join(bundle_root, "configs", "data.yaml"),
]
elif mode == "temp_scale_train":
# Special config order for temperature scaling: common, validation, data, temp_scale
config_files = [
os.path.join(bundle_root, "configs", "common.yaml"),
os.path.join(bundle_root, "configs", "validation.yaml"),
os.path.join(bundle_root, "configs", "data.yaml"),
os.path.join(bundle_root, "configs", "temp_scale.yaml"),
]
elif mode == "temp_scale_eval":
# Config order for temperature scaled model evaluation
config_files = [
os.path.join(bundle_root, "configs", "common.yaml"),
os.path.join(bundle_root, "configs", "validation.yaml"),
os.path.join(
bundle_root, "configs", "inference_eval.yaml"
), # Base evaluation config
os.path.join(bundle_root, "configs", "data.yaml"),
os.path.join(
bundle_root, "configs", "temp_scale_eval.yaml"
), # Override with temp scaled model
]
elif mode == "inference_eval_additional":
# Config order for inference evaluation with additional metrics
config_files = [
os.path.join(bundle_root, "configs", "common.yaml"),
os.path.join(bundle_root, "configs", "validation.yaml"),
os.path.join(bundle_root, "configs", "inference_eval_additional.yaml"),
os.path.join(bundle_root, "configs", "data.yaml"),
]
elif mode == "temp_scale_eval_additional":
# Config order for temperature scaled model evaluation with additional metrics
config_files = [
os.path.join(bundle_root, "configs", "common.yaml"),
os.path.join(bundle_root, "configs", "validation.yaml"),
os.path.join(bundle_root, "configs", "inference_eval_additional.yaml"),
os.path.join(bundle_root, "configs", "data.yaml"),
os.path.join(
bundle_root, "configs", "temp_scale_eval_additional.yaml"
), # Override with temp scaled model
]
else:
# Standard config order for other modes
raise ValueError(f"Unsupported mode: {mode}")
if debug:
config_files.append(os.path.join(bundle_root, "configs", "debug.yaml"))
return config_files
def main():
parser = get_parser()
args = parser.parse_args()
# Set the determinism seed
set_determinism(seed=args.seed)
# Prepend the "bundles" directory to the bundle path
bundle_root = os.path.join("bundles", args.bundle)
config_files = get_config_files(bundle_root, args.mode, args.debug)
run(
bundle_root=bundle_root,
meta_file=os.path.join(bundle_root, "configs", "metadata.json"),
config_file=config_files,
logging_file=os.path.join(bundle_root, "configs", "logging.conf"),
seed=args.seed,
)
if __name__ == "__main__":
main()