-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.py
402 lines (342 loc) · 18.2 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import sys, json, argparse, random, re, os, shutil
sys.path.append("src/")
import numpy as np
import pandas as pd
import logging
from datetime import datetime
from pathlib import Path
import math
import os.path as osp
import networkx as nx
import pdb
from torch.optim.lr_scheduler import ReduceLROnPlateau, OneCycleLR
import torch
import torch.nn as nn
import torch.nn.functional as func
from torch import optim
import torch.multiprocessing as mp
from torch_geometric.data import Data, Batch, DataLoader
from torch_geometric.utils import to_dense_batch, k_hop_subgraph
from utils import common_tools as ct
from utils.my_math import masked_mae_np, masked_mape_np, masked_mse_np
from utils.data_convert import generate_samples
from src.model.model import Basic_Model
from src.model.ewc import EWC
from src.trafficDataset import TrafficDataset
from src.model import detect
from src.model import replay
result = {3:{"mae":{}, "mape":{}, "rmse":{}}, 6:{"mae":{}, "mape":{}, "rmse":{}}, 12:{"mae":{}, "mape":{}, "rmse":{}}}
pin_memory = True
n_work = 16
def update(src, tmp):
for key in tmp:
if key!= "gpuid":
src[key] = tmp[key]
def load_best_model(args):
if (args.load_first_year and args.year <= args.begin_year+1) or args.train == 0:
load_path = args.first_year_model_path
loss = load_path.split("/")[-1].replace(".pkl", "")
else:
loss = []
for filename in os.listdir(osp.join(args.model_path, args.logname+args.time, str(args.year-1))):
loss.append(filename[0:-4])
loss = sorted(loss)
load_path = osp.join(args.model_path, args.logname+args.time, str(args.year-1), loss[0]+".pkl")
args.logger.info("[*] load from {}".format(load_path))
state_dict = torch.load(load_path, map_location=args.device)["model_state_dict"]
if 'tcn2.weight' in state_dict:
del state_dict['tcn2.weight']
del state_dict['tcn2.bias']
model = Basic_Model(args)
model.load_state_dict(state_dict)
model = model.to(args.device)
return model, loss[0]
def init(args):
conf_path = osp.join(args.conf)
info = ct.load_json_file(conf_path)
info["time"] = datetime.now().strftime("%Y-%m-%d-%H:%M:%S.%f")
update(vars(args), info)
vars(args)["path"] = osp.join(args.model_path, args.logname+args.time)
ct.mkdirs(args.path)
del info
def init_log(args):
log_dir, log_filename = args.path, args.logname
logger = logging.getLogger(__name__)
ct.mkdirs(log_dir)
logger.setLevel(logging.INFO)
fh = logging.FileHandler(osp.join(log_dir, log_filename+".log"))
fh.setLevel(logging.INFO)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(message)s")
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
logger.info("logger name:%s", osp.join(log_dir, log_filename+".log"))
vars(args)["logger"] = logger
return logger
def seed_set(seed=0):
max_seed = (1 << 32) - 1
random.seed(seed)
np.random.seed(random.randint(0, max_seed))
torch.manual_seed(random.randint(0, max_seed))
torch.cuda.manual_seed(random.randint(0, max_seed))
torch.cuda.manual_seed_all(random.randint(0, max_seed))
torch.backends.cudnn.benchmark = False # if benchmark=True, deterministic will be False
torch.backends.cudnn.deterministic = True
def train(inputs, args):
# Model Setting
global result
path = osp.join(args.path, str(args.year))
ct.mkdirs(path)
if args.loss == "mse": lossfunc = func.mse_loss
elif args.loss == "huber": lossfunc = func.smooth_l1_loss
# Dataset Definition
if args.strategy == 'incremental' and args.year > args.begin_year:
train_loader = DataLoader(TrafficDataset("", "", x=inputs["train_x"][:, :, args.subgraph.numpy()], y=inputs["train_y"][:, :, args.subgraph.numpy()], \
edge_index="", mode="subgraph"), batch_size=args.batch_size, shuffle=True, pin_memory=pin_memory, num_workers=n_work)
val_loader = DataLoader(TrafficDataset("", "", x=inputs["val_x"][:, :, args.subgraph.numpy()], y=inputs["val_y"][:, :, args.subgraph.numpy()], \
edge_index="", mode="subgraph"), batch_size=args.batch_size, shuffle=False, pin_memory=pin_memory, num_workers=n_work)
graph = nx.Graph()
graph.add_nodes_from(range(args.subgraph.size(0)))
graph.add_edges_from(args.subgraph_edge_index.numpy().T)
adj = nx.to_numpy_array(graph)
adj = adj / (np.sum(adj, 1, keepdims=True) + 1e-6)
vars(args)["sub_adj"] = torch.from_numpy(adj).to(torch.float).to(args.device)
else:
train_loader = DataLoader(TrafficDataset(inputs, "train"), batch_size=args.batch_size, shuffle=True, pin_memory=pin_memory, num_workers=n_work)
val_loader = DataLoader(TrafficDataset(inputs, "val"), batch_size=args.batch_size, shuffle=False, pin_memory=pin_memory, num_workers=n_work)
vars(args)["sub_adj"] = vars(args)["adj"]
test_loader = DataLoader(TrafficDataset(inputs, "test"), batch_size=args.batch_size, shuffle=False, pin_memory=pin_memory, num_workers=n_work)
args.logger.info("[*] Year " + str(args.year) + " Dataset load!")
# Model Definition
if args.init == True and args.year > args.begin_year:
gnn_model, _ = load_best_model(args)
if args.ewc:
args.logger.info("[*] EWC! lambda {:.6f}".format(args.ewc_lambda))
model = EWC(gnn_model, args.adj, args.ewc_lambda, args.ewc_strategy)
ewc_loader = DataLoader(TrafficDataset(inputs, "train"), batch_size=args.batch_size, shuffle=False, pin_memory=pin_memory, num_workers=n_work)
model.register_ewc_params(ewc_loader, lossfunc, device)
else:
model = gnn_model
else:
gnn_model = Basic_Model(args).to(args.device)
model = gnn_model
# Model Optimizer
optimizer = optim.AdamW(model.parameters(), lr=args.lr)
args.logger.info("[*] Year " + str(args.year) + " Training start")
global_train_steps = len(train_loader) // args.batch_size +1
iters = len(train_loader)
lowest_validation_loss = 1e7
counter = 0
patience = 5
model.train()
use_time = []
for epoch in range(args.epoch):
training_loss = 0.0
start_time = datetime.now()
# Train Model
cn = 0
for batch_idx, data in enumerate(train_loader):
if epoch == 0 and batch_idx == 0:
args.logger.info("node number {}".format(data.x.shape))
data = data.to(device, non_blocking=pin_memory)
optimizer.zero_grad()
pred = model(data, args.sub_adj)
if args.strategy == "incremental" and args.year > args.begin_year:
pred, _ = to_dense_batch(pred, batch=data.batch)
data.y, _ = to_dense_batch(data.y, batch=data.batch)
pred = pred[:, args.mapping, :]
data.y = data.y[:, args.mapping, :]
loss = lossfunc(data.y, pred, reduction="mean")
if args.ewc and args.year > args.begin_year:
loss += model.compute_consolidation_loss()
training_loss += float(loss)
loss.backward()
optimizer.step()
cn += 1
if epoch == 0:
total_time = (datetime.now() - start_time).total_seconds()
else:
total_time += (datetime.now() - start_time).total_seconds()
use_time.append((datetime.now() - start_time).total_seconds())
training_loss = training_loss/cn
# Validate Model
validation_loss = 0.0
cn = 0
with torch.no_grad():
for batch_idx, data in enumerate(val_loader):
data = data.to(device,non_blocking=pin_memory)
pred = model(data, args.sub_adj)
if args.strategy == "incremental" and args.year > args.begin_year:
pred, _ = to_dense_batch(pred, batch=data.batch)
data.y, _ = to_dense_batch(data.y, batch=data.batch)
pred = pred[:, args.mapping, :]
data.y = data.y[:, args.mapping, :]
loss = masked_mae_np(data.y.cpu().data.numpy(), pred.cpu().data.numpy(), 0)
validation_loss += float(loss)
cn += 1
validation_loss = float(validation_loss/cn)
args.logger.info(f"epoch:{epoch}, training loss:{training_loss:.4f} validation loss:{validation_loss:.4f}")
# Early Stop
if validation_loss <= lowest_validation_loss:
counter = 0
lowest_validation_loss = round(validation_loss, 4)
torch.save({'model_state_dict': gnn_model.state_dict()}, osp.join(path, str(round(validation_loss,4))+".pkl"))
else:
counter += 1
if counter > patience:
break
best_model_path = osp.join(path, str(lowest_validation_loss)+".pkl")
best_model = Basic_Model(args)
best_model.load_state_dict(torch.load(best_model_path, args.device)["model_state_dict"])
best_model = best_model.to(args.device)
# Test Model
test_model(best_model, args, test_loader, pin_memory)
result[args.year] = {"total_time": total_time, "average_time": sum(use_time)/len(use_time), "epoch_num": epoch+1}
args.logger.info("Finished optimization, total time:{:.2f} s, best model:{}".format(total_time, best_model_path))
def test_model(model, args, testset, pin_memory):
model.eval()
pred_ = []
truth_ = []
loss = 0.0
with torch.no_grad():
cn = 0
for data in testset:
data = data.to(args.device, non_blocking=pin_memory)
pred = model(data, args.adj)
loss += func.mse_loss(data.y, pred, reduction="mean")
pred, _ = to_dense_batch(pred, batch=data.batch)
data.y, _ = to_dense_batch(data.y, batch=data.batch)
pred_.append(pred.cpu().data.numpy())
truth_.append(data.y.cpu().data.numpy())
cn += 1
loss = loss/cn
args.logger.info("[*] loss:{:.4f}".format(loss))
pred_ = np.concatenate(pred_, 0)
truth_ = np.concatenate(truth_, 0)
mae = metric(truth_, pred_, args)
return loss
def metric(ground_truth, prediction, args):
global result
pred_time = [3,6,12]
args.logger.info("[*] year {}, testing".format(args.year))
for i in pred_time:
mae = masked_mae_np(ground_truth[:, :, :i], prediction[:, :, :i], 0)
rmse = masked_mse_np(ground_truth[:, :, :i], prediction[:, :, :i], 0) ** 0.5
mape = masked_mape_np(ground_truth[:, :, :i], prediction[:, :, :i], 0)
args.logger.info("T:{:d}\tMAE\t{:.4f}\tRMSE\t{:.4f}\tMAPE\t{:.4f}".format(i,mae,rmse,mape))
result[i]["mae"][args.year] = mae
result[i]["mape"][args.year] = mape
result[i]["rmse"][args.year] = rmse
return mae
def main(args):
logger = init_log(args)
logger.info("params : %s", vars(args))
ct.mkdirs(args.save_data_path)
for year in range(args.begin_year, args.end_year+1):
# Load Data
graph = nx.from_numpy_matrix(np.load(osp.join(args.graph_path, str(year)+"_adj.npz"))["x"])
vars(args)["graph_size"] = graph.number_of_nodes()
vars(args)["year"] = year
inputs = generate_samples(31, osp.join(args.save_data_path, str(year)+'_30day'), np.load(osp.join(args.raw_data_path, str(year)+".npz"))["x"], graph, val_test_mix=True) \
if args.data_process else np.load(osp.join(args.save_data_path, str(year)+"_30day.npz"), allow_pickle=True)
args.logger.info("[*] Year {} load from {}_30day.npz".format(args.year, osp.join(args.save_data_path, str(year))))
adj = np.load(osp.join(args.graph_path, str(args.year)+"_adj.npz"))["x"]
adj = adj / (np.sum(adj, 1, keepdims=True) + 1e-6)
vars(args)["adj"] = torch.from_numpy(adj).to(torch.float).to(args.device)
if year == args.begin_year and args.load_first_year:
# Skip the first year, model has been trained and retrain is not needed
model, _ = load_best_model(args)
test_loader = DataLoader(TrafficDataset(inputs, "test"), batch_size=args.batch_size, shuffle=False, pin_memory=pin_memory, num_workers=n_work)
test_model(model, args, test_loader, pin_memory=True)
continue
if year > args.begin_year and args.strategy == "incremental":
# Load the best model
model, _ = load_best_model(args)
node_list = list()
# Obtain increase nodes
if args.increase:
cur_node_size = np.load(osp.join(args.graph_path, str(year)+"_adj.npz"))["x"].shape[0]
pre_node_size = np.load(osp.join(args.graph_path, str(year-1)+"_adj.npz"))["x"].shape[0]
node_list.extend(list(range(pre_node_size, cur_node_size)))
# Obtain influence nodes
if args.detect:
args.logger.info("[*] detect strategy {}".format(args.detect_strategy))
pre_data = np.load(osp.join(args.raw_data_path, str(year-1)+".npz"))["x"]
cur_data = np.load(osp.join(args.raw_data_path, str(year)+".npz"))["x"]
pre_graph = np.array(list(nx.from_numpy_matrix(np.load(osp.join(args.graph_path, str(year-1)+"_adj.npz"))["x"]).edges)).T
cur_graph = np.array(list(nx.from_numpy_matrix(np.load(osp.join(args.graph_path, str(year)+"_adj.npz"))["x"]).edges)).T
# 20% of current graph size will be sampled
vars(args)["topk"] = int(0.01*args.graph_size)
influence_node_list = detect.influence_node_selection(model, args, pre_data, cur_data, pre_graph, cur_graph)
node_list.extend(list(influence_node_list))
# Obtain sample nodes
if args.replay:
vars(args)["replay_num_samples"] = int(0.09*args.graph_size) #int(0.2*args.graph_size)- len(node_list)
args.logger.info("[*] replay node number {}".format(args.replay_num_samples))
replay_node_list = replay.replay_node_selection(args, inputs, model)
node_list.extend(list(replay_node_list))
node_list = list(set(node_list))
if len(node_list) > int(0.1*args.graph_size):
node_list = random.sample(node_list, int(0.1*args.graph_size))
# Obtain subgraph of node list
cur_graph = torch.LongTensor(np.array(list(nx.from_numpy_matrix(np.load(osp.join(args.graph_path, str(year)+"_adj.npz"))["x"]).edges)).T)
edge_list = list(nx.from_numpy_matrix(np.load(osp.join(args.graph_path, str(year)+"_adj.npz"))["x"]).edges)
graph_node_from_edge = set()
for (u,v) in edge_list:
graph_node_from_edge.add(u)
graph_node_from_edge.add(v)
node_list = list(set(node_list) & graph_node_from_edge)
if len(node_list) != 0 :
subgraph, subgraph_edge_index, mapping, _ = k_hop_subgraph(node_list, num_hops=args.num_hops, edge_index=cur_graph, relabel_nodes=True)
vars(args)["subgraph"] = subgraph
vars(args)["subgraph_edge_index"] = subgraph_edge_index
vars(args)["mapping"] = mapping
logger.info("number of increase nodes:{}, nodes after {} hop:{}, total nodes this year {}".format\
(len(node_list), args.num_hops, args.subgraph.size(), args.graph_size))
vars(args)["node_list"] = np.asarray(node_list)
# Skip the year when no nodes needed to be trained incrementally
if args.strategy != "retrain" and year > args.begin_year and len(args.node_list) == 0:
model, loss = load_best_model(args)
ct.mkdirs(osp.join(args.model_path, args.logname+args.time, str(args.year)))
torch.save({'model_state_dict': model.state_dict()}, osp.join(args.model_path, args.logname+args.time, str(args.year), loss+".pkl"))
test_loader = DataLoader(TrafficDataset(inputs, "test"), batch_size=args.batch_size, shuffle=False, pin_memory=pin_memory, num_workers=n_work)
test_model(model, args, test_loader, pin_memory=True)
logger.warning("[*] No increasing nodes at year " + str(args.year) + ", store model of the last year.")
continue
if args.train:
train(inputs, args)
else:
if args.auto_test:
model, _ = load_best_model(args)
test_loader = DataLoader(TrafficDataset(inputs, "test"), batch_size=args.batch_size, shuffle=False, pin_memory=pin_memory, num_workers=n_work)
test_model(model, args, test_loader, pin_memory=True)
for i in [3, 6, 12]:
for j in ['mae', 'rmse', 'mape']:
info = ""
for year in range(args.begin_year, args.end_year+1):
if i in result:
if j in result[i]:
if year in result[i][j]:
info+="{:.2f}\t".format(result[i][j][year])
logger.info("{}\t{}\t".format(i,j) + info)
for year in range(args.begin_year, args.end_year+1):
if year in result:
info = "year\t{}\ttotal_time\t{}\taverage_time\t{}\tepoch\t{}".format(year, result[year]["total_time"], result[year]["average_time"], result[year]['epoch_num'])
logger.info(info)
if __name__ == "__main__":
parser = argparse.ArgumentParser(formatter_class = argparse.RawTextHelpFormatter)
parser.add_argument("--conf", type = str, default = "conf/test.json")
parser.add_argument("--paral", type = int, default = 0)
parser.add_argument("--gpuid", type = int, default = 2)
parser.add_argument("--logname", type = str, default = "info")
parser.add_argument("--load_first_year", type = int, default = 0, help="0: training first year, 1: load from model path of first year")
parser.add_argument("--first_year_model_path", type = str, default = "res/district3F11T17/TrafficStream2021-05-09-11:56:33.516033/2011/27.4437.pkl", help='specify a pretrained model root')
args = parser.parse_args()
init(args)
seed_set(13)
device = torch.device("cuda:{}".format(args.gpuid)) if torch.cuda.is_available() and args.gpuid != -1 else "cpu"
vars(args)["device"] = device
main(args)