-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_ts_cot.py
184 lines (134 loc) · 5.69 KB
/
train_ts_cot.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import torch
import numpy as np
import argparse
import os
from datetime import datetime
from algorithm.ts_sea import TS_SEA
from algorithm.ts_cot import TS_CoT
from algorithm.ts2vec import TS2Vec
from algorithm.ts2tcc import TS_TCC
import tasks
import datautils
from utils import init_cuda, get_logger
import random
from configs.LoadConfig import load_json_config,config_to_json
def set_seed(seed):
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.determinstic = True
torch.backends.cudnn.benchmark = False
os.environ['PYTHONHASHSEED'] = str(seed)
def eval_mlp(device,logger,args):
model,train_data,train_labels, test_data, test_labels = build_model(device=device,args=args)
model.load(args.model_path)
out, eval_res = tasks.eval_classification(model, train_data, train_labels, test_data, test_labels,args)
for evals,val in eval_res.items() :
logger.info(f"{evals} : {val}")
# logger.info(f"Evaluation result: ACC: {eval_res['acc']} AUROC: {eval_res['auroc']}")
def init_agrs(args):
now = datetime.now().strftime("%Y%m%d_%H%M%S")
args.run_dir = os.path.join("exp_logs", args.run_desc, args.dataset,args.backbone_type,now) #'exp_logs/'+ args.run_desc + '/' + args.dataset + '/' + args.backbone_type+ '/'+ now
os.makedirs(args.run_dir, exist_ok=True)
# experiment_log_dir = os.path.join("exp_logs", args.run_desc, args.dataset,args.backbone_type)
# os.makedirs(args.run_dir, exist_ok=True)
# Logging
log_file_name = os.path.join(args.run_dir, f"training.log")
args.log_file_name = log_file_name
if args.dataloader is None :
args.dataloader = args.dataset
if args.eval:
args.epochs = 0
return args
def build_model(device,args):
##############################################################################
###### 加载模型骨架
##############################################################################
if args.backbone_type =="TS_CoT":
train_data, train_labels, test_data, test_labels = datautils.load_itwo_view(args)
args.in_dims = train_data[0].shape[-1]
model = TS_CoT(
input_dims=train_data[0].shape[-1],
output_dims=args.repr_dims,
device=device,
args=args
)
elif args.backbone_type =="TS_SEA":
# print(type(args.data_perc),2)
train_data, train_labels, test_data, test_labels = datautils.load_itri_view(args)
args.in_dims = train_data[0].shape[-1]
model = TS_SEA(
input_dims=train_data[0].shape[-1],
output_dims=args.repr_dims,
device=device,
args=args
)
elif args.backbone_type =="TS2Vec":
train_data, train_labels, test_data, test_labels = datautils.get_ts2vec_loader(args)
config = dict(
batch_size=args.batch_size,
lr=args.lr,
output_dims=args.repr_dims,
max_train_length=args.max_train_length
)
args.in_dims = train_data.shape[-1]
model = TS2Vec(
input_dims=train_data.shape[-1],
device=device,
**config
)
elif args.backbone_type =="TS_TCC":
train_data, train_labels, test_data, test_labels = datautils.get_ts2vec_loader(args)
args.in_dims = args.input_channels
model = TS_TCC(
device=device,
args=args
)
else :
raise Exception("Unknown Backbone")
return model,train_data,train_labels, test_data, test_labels
def train_model(config_path="configs/ts_cot.json"):
args = load_json_config(config_path)
args = init_agrs(args)
logger = get_logger(args.log_file_name)
device = init_cuda(args.gpu, seed=args.seed, max_threads=args.max_threads)
logger.info("=====================================================================")
for key,val in args.items():
logger.info(f"===== {str(key),str(val)}")
logger.info("=====================================================================")
logger.info('Loading data... ')
logger.info(f"Backbone is {args.backbone_type}")
##############################################################################
###### 模型训练
##############################################################################
if not args.eval:
model,train_data,_,_,_ = build_model(device=device,args=args)
model.fit_ts_cot(
train_data,
n_epochs=args.epochs
,logger=logger
)
args.model_path = f'{args.run_dir}/model.pkl'
model.save(args.model_path)
args.eval = True
logger.info(f"saving model to :{args.model_path}")
else:
logger.info('Unknown Backbone')
##############################################################################
###### 模型评估
######
##############################################################################
logger.info(args.eval)
if args.eval:
eval_mlp(device=device,logger=logger,args=args)
config_to_json(args,f'{args.run_dir}/config.json')
logger.info(os.path.basename(__file__))
logger.info("Finished.")
if __name__ == '__main__':
# train_model("/workspace/Civil/configs/ts_cot/ts_cot_4_epi.json")
# train_model("configs/ts_sea/ts_sea_4_har.json")
# train_model("configs/ts2vec/ts2vec_4_har.json")
# train_model("configs/ts_cot/ts_cot_4_har.json")
train_model("configs/ts2tcc/ts2tcc_4_har.json")