-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (74 loc) · 2.86 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
import argparse
import os
import random
import numpy as np
import torch
from utils.baseline import RunBaseline
from utils.utils import get_configs
def set_seed(seed, device):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if torch.cuda.is_available():
torch.cuda.set_device(device=device)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
def parse_args():
args = argparse.ArgumentParser()
args.add_argument("--config", "-f", help="path to config file", default="./configs/config_baseline.yaml")
args.add_argument("--mode", "-m", help="mode to run the baseline model on",
choices=["train", "test", "test_single"], default="train")
args.add_argument("--comment", "-c", help="comment for training", default="_")
args.add_argument("--weight", "-w", help="path to model weights", default=None)
args.add_argument("--device", "-d", help="set device number if you have multiple GPUs", default=0, type=int)
args = args.parse_args()
return args.config, args.comment, args.mode, args.weight, args.device
def load_model(path, comment, mode):
configs = get_configs(path)
## TODO : Update config outputs Data split into three directpris one for each class
residential = configs[0]
urban = configs[1]
interstate = configs[2]
output_dir = configs[3]
model_name = configs[4]
backbone = configs[5]
epochs = configs[6]
lr = configs[7]
resize_shape = configs[8]
optimizer = configs[9]
batch_size = configs[10]
log_step = configs[11]
custom_image_path = configs[12]
baseline = RunBaseline(comment=comment,
res_dir=residential,
urban_dir=urban,
interstate_dir = interstate,
model_name=model_name,
optimizer=optimizer,
num_epochs=epochs,
batch_size=batch_size,
log_step=log_step,
out_dir=output_dir,
lr=lr,
resize_shape=resize_shape,
mode=mode,
custom_image_path=custom_image_path)
return baseline
def main():
config, comment, mode, weight, device = parse_args()
set_seed(42, device)
net = load_model(config, comment, mode)
if mode == "train":
net.train()
print("Testing")
net.test()
elif mode == "test":
# make sure that the weights are present in the output folder
print("Testing")
net.test(weight=weight)
elif mode == "test_single":
net.test_on_single_images(weight=weight)
if __name__ == "__main__":
main()