-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
134 lines (121 loc) · 3.65 KB
/
main.py
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
"""
Author: Chris Xiao [email protected]
Date: 2024-02-15 16:24:56
LastEditors: Chris Xiao [email protected]
LastEditTime: 2024-03-31 23:17:31
FilePath: /mbp1413-final/main.py
Description: main script for the project
I Love IU
Copyright (c) 2024 by Chris Xiao [email protected], All Rights Reserved.
"""
from omegaconf import OmegaConf
import argparse
import os
from models import load_dataset, check_device, download_dataset, unet, unetr
from pathlib import Path
ROOT = Path(os.path.dirname(os.path.realpath(__file__)))
modules = {"unet": unet, "unetr": unetr}
def parse_command() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="MBP1413 Final Project")
parser.add_argument(
"-c", "--cfg", type=str, default="config.yaml", help="path to the config file"
)
parser.add_argument(
"-mo",
"--model",
type=str,
default="unet",
help="model to use, either unet or unetr",
)
parser.add_argument(
"-m",
"--mode",
type=str,
default="train",
help="mode of the program, either test or train",
)
parser.add_argument(
"-d",
"--download",
action="store_true",
help="use this if you want to download the dataset",
)
parser.add_argument(
"-r",
"--resume",
action="store_true",
help="use this if you want to continue a training",
)
parser.add_argument(
"-e", "--epochs", type=int, default=200, help="Number of epochs for training"
)
parser.add_argument(
"-l", "--learning_rate", type=float, default=0.005, help="Learning rate"
)
parser.add_argument(
"-opt",
"--optimizer",
type=str,
default="Adam",
help="Optimizer of model, default is Adam",
)
parser.add_argument(
"-sch",
"--scheduler",
action="store_true",
help="Use this parameter to use lr scheduler",
)
parser.add_argument(
"-sa",
"--save-arch",
action="store_true",
help="Use this if you want to save the architecture plot",
)
return parser.parse_args()
def main() -> None:
args = parse_command()
assert os.path.exists(args.cfg), "config file not found"
cfg = OmegaConf.load(args.cfg)
if args.download:
download_dataset(cfg)
train_path = os.path.join(ROOT, "datasets", "train")
test_path = os.path.join(ROOT, "datasets", "test")
tr_loader, val_loader, te_loader = load_dataset(train_path, test_path, cfg)
device = check_device(cfg)
model_name = args.model.lower()
if model_name in modules.keys():
model = modules[model_name](
cfg,
args.learning_rate,
args.epochs,
device,
args.model,
args.optimizer,
args.scheduler,
tr_loader,
val_loader,
te_loader,
)
else:
raise ValueError("model not supported")
if args.mode == "train":
print(f"Training {args.model}")
if args.resume:
model.load_checkpoint(mode="last")
else:
model.init_training_dir()
if args.save_arch:
model.plot_architecture(mode="train")
model.train()
print(f"Training completed for {args.model}")
elif args.mode == "test":
print(f"Testing {args.model}")
model.init_inference_dir()
if args.save_arch:
model.plot_architecture(mode="test")
model.test()
print(f"Testing completed for {args.model}")
else:
raise ValueError("mode not supported")
if __name__ == "__main__":
main()