-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
76 lines (59 loc) · 2.73 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
import argparse
import os
import yaml
import copy
import torch
import random
import numpy as np
from utils import dict2namespace, get_runner, namespace2dict
import torch.multiprocessing as mp
import torch.distributed as dist
def parse_args_and_config():
parser = argparse.ArgumentParser(description=globals()['__doc__'])
parser.add_argument('-c', '--config', type=str, default='configs/Template-MovingMNIST.yaml', help='Path to the config file')
parser.add_argument('-s', '--seed', type=int, default=777, help='Random seed')
parser.add_argument('-r', '--result_path', type=str, default='debug', help="The directory to save results")
parser.add_argument('-t', '--train', action='store_true', default=False, help='train the model')
parser.add_argument('--sample_to_eval', action='store_true', default=True, help='sample for evaluation')
parser.add_argument('--sample_at_start', action='store_true', default=True, help='sample at start(for debug)')
parser.add_argument('--save_top', action='store_true', default=True, help="save top loss checkpoint")
parser.add_argument('--resume_model', type=str, default=None, help='model checkpoint')
parser.add_argument('--resume_optim', type=str, default=None, help='optimizer checkpoint')
parser.add_argument('--max_epoch', type=int, default=None, help='optimizer checkpoint')
parser.add_argument('--max_steps', type=int, default=None, help='optimizer checkpoint')
args = parser.parse_args()
with open(args.config, 'r') as f:
dict_config = yaml.load(f, Loader=yaml.FullLoader)
namespace_config = dict2namespace(dict_config)
namespace_config.args = args
if args.resume_model is not None:
namespace_config.model.model_load_path = args.resume_model
if args.resume_optim is not None:
namespace_config.model.optim_sche_load_path = args.resume_optim
if args.max_epoch is not None:
namespace_config.training.n_epochs = args.max_epoch
if args.max_steps is not None:
namespace_config.training.n_steps = args.max_steps
dict_config = namespace2dict(namespace_config)
return namespace_config, dict_config
def set_random_seed(SEED=1234):
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.cuda.manual_seed_all(SEED)
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def main():
nconfig, dconfig = parse_args_and_config()
args = nconfig.args
set_random_seed(args.seed)
runner = get_runner(nconfig.runner, nconfig)
if nconfig.args.train:
runner.train()
else:
with torch.no_grad():
runner.test()
if __name__ == "__main__":
main()