-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
96 lines (76 loc) · 3.32 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
import os
import random
import argparse
import configparser
import numpy as np
from torch.backends import cudnn
import torch
from solver import Solver
import sys
class Logger(object):
def __init__(self, filename='default.log', add_flag=True, stream=sys.stdout):
self.terminal = stream
self.filename = filename
self.add_flag = add_flag
def write(self, message):
if self.add_flag:
with open(self.filename, 'a+') as log:
self.terminal.write(message)
log.write(message)
else:
with open(self.filename, 'w') as log:
self.terminal.write(message)
log.write(message)
def flush(self):
pass
def main(config):
cudnn.benchmark = True
if (not os.path.exists(config.model_save_path)):
mkdir(config.model_save_path)
solver = Solver(vars(config))
if config.mode == 'train':
solver.train()
elif config.mode == 'test':
solver.test()
else:
solver.train()
solver.test()
return solver
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, help='configuration file')
args = parser.parse_args()
fileconfig = configparser.ConfigParser()
fileconfig.read(args.config)
parser.add_argument('--lr', type=float, default=fileconfig['train']['lr'])
parser.add_argument('--gpu', type=str, default=fileconfig['train']['gpu'])
parser.add_argument('--num_epochs', type=int, default=fileconfig['train']['epoch'])
parser.add_argument('--anormly_ratio', type=float, default=fileconfig['train']['ar'])
parser.add_argument('--batch_size', type=int, default=fileconfig['train']['bs'])
parser.add_argument('--seed', type = int, default = fileconfig['train']['seed'])
parser.add_argument('--win_size', type=int, default=fileconfig['data']['ws'])
parser.add_argument('--input_c', type=int, default=fileconfig['data']['ic'])
parser.add_argument('--output_c', type=int, default=fileconfig['data']['oc'])
parser.add_argument('--dataset', type=str, default=fileconfig['data']['ds'])
parser.add_argument('--data_path', type=str, default=fileconfig['data']['dp'])
parser.add_argument('--d_model', type=int, default=fileconfig['param']['d'])
parser.add_argument('--e_layers', type=int, default=fileconfig['param']['l'])
parser.add_argument('--fr', type=float, default=fileconfig['param']['fr'])
parser.add_argument('--tr', type=float, default=fileconfig['param']['tr'])
parser.add_argument('--seq_size', type=int, default=fileconfig['param']['ss'])
parser.add_argument('--mode', type=str, default=fileconfig['model']['mode'])
parser.add_argument('--model_save_path', type=str, default=fileconfig['model']['msp'])
config = parser.parse_args()
args = vars(config)
if config.seed is not None:
random.seed(config.seed)
np.random.seed(config.seed)
torch.manual_seed(config.seed)
torch.cuda.manual_seed(config.seed)
torch.backends.cudnn.deterministic = True
sys.stdout = Logger("result/"+ config.dataset +".log", sys.stdout)
print('------------ Options -------------')
for k, v in sorted(args.items()):
print('%s: %s' % (str(k), str(v)))
print('-------------- End ----------------')
main(config)