-
Notifications
You must be signed in to change notification settings - Fork 59
/
train.py
288 lines (237 loc) · 11 KB
/
train.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
import os
import sys
proj_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(proj_dir)
from util import config, file_dir
from graph import Graph
from dataset import HazeData
from model.MLP import MLP
from model.LSTM import LSTM
from model.GRU import GRU
from model.GC_LSTM import GC_LSTM
from model.nodesFC_GRU import nodesFC_GRU
from model.PM25_GNN import PM25_GNN
from model.PM25_GNN_nosub import PM25_GNN_nosub
import arrow
import torch
from torch import nn
from tqdm import tqdm
import numpy as np
import pickle
import glob
import shutil
torch.set_num_threads(1)
use_cuda = torch.cuda.is_available()
device = torch.device('cuda' if use_cuda else 'cpu')
graph = Graph()
city_num = graph.node_num
batch_size = config['train']['batch_size']
epochs = config['train']['epochs']
hist_len = config['train']['hist_len']
pred_len = config['train']['pred_len']
weight_decay = config['train']['weight_decay']
early_stop = config['train']['early_stop']
lr = config['train']['lr']
results_dir = file_dir['results_dir']
dataset_num = config['experiments']['dataset_num']
exp_model = config['experiments']['model']
exp_repeat = config['train']['exp_repeat']
save_npy = config['experiments']['save_npy']
criterion = nn.MSELoss()
train_data = HazeData(graph, hist_len, pred_len, dataset_num, flag='Train')
val_data = HazeData(graph, hist_len, pred_len, dataset_num, flag='Val')
test_data = HazeData(graph, hist_len, pred_len, dataset_num, flag='Test')
in_dim = train_data.feature.shape[-1] + train_data.pm25.shape[-1]
wind_mean, wind_std = train_data.wind_mean, train_data.wind_std
pm25_mean, pm25_std = test_data.pm25_mean, test_data.pm25_std
def get_metric(predict_epoch, label_epoch):
haze_threshold = 75
predict_haze = predict_epoch >= haze_threshold
predict_clear = predict_epoch < haze_threshold
label_haze = label_epoch >= haze_threshold
label_clear = label_epoch < haze_threshold
hit = np.sum(np.logical_and(predict_haze, label_haze))
miss = np.sum(np.logical_and(label_haze, predict_clear))
falsealarm = np.sum(np.logical_and(predict_haze, label_clear))
csi = hit / (hit + falsealarm + miss)
pod = hit / (hit + miss)
far = falsealarm / (hit + falsealarm)
predict = predict_epoch[:,:,:,0].transpose((0,2,1))
label = label_epoch[:,:,:,0].transpose((0,2,1))
predict = predict.reshape((-1, predict.shape[-1]))
label = label.reshape((-1, label.shape[-1]))
mae = np.mean(np.mean(np.abs(predict - label), axis=1))
rmse = np.mean(np.sqrt(np.mean(np.square(predict - label), axis=1)))
return rmse, mae, csi, pod, far
def get_exp_info():
exp_info = '============== Train Info ==============\n' + \
'Dataset number: %s\n' % dataset_num + \
'Model: %s\n' % exp_model + \
'Train: %s --> %s\n' % (train_data.start_time, train_data.end_time) + \
'Val: %s --> %s\n' % (val_data.start_time, val_data.end_time) + \
'Test: %s --> %s\n' % (test_data.start_time, test_data.end_time) + \
'City number: %s\n' % city_num + \
'Use metero: %s\n' % config['experiments']['metero_use'] + \
'batch_size: %s\n' % batch_size + \
'epochs: %s\n' % epochs + \
'hist_len: %s\n' % hist_len + \
'pred_len: %s\n' % pred_len + \
'weight_decay: %s\n' % weight_decay + \
'early_stop: %s\n' % early_stop + \
'lr: %s\n' % lr + \
'========================================\n'
return exp_info
def get_model():
if exp_model == 'MLP':
return MLP(hist_len, pred_len, in_dim)
elif exp_model == 'LSTM':
return LSTM(hist_len, pred_len, in_dim, city_num, batch_size, device)
elif exp_model == 'GRU':
return GRU(hist_len, pred_len, in_dim, city_num, batch_size, device)
elif exp_model == 'nodesFC_GRU':
return nodesFC_GRU(hist_len, pred_len, in_dim, city_num, batch_size, device)
elif exp_model == 'GC_LSTM':
return GC_LSTM(hist_len, pred_len, in_dim, city_num, batch_size, device, graph.edge_index)
elif exp_model == 'PM25_GNN':
return PM25_GNN(hist_len, pred_len, in_dim, city_num, batch_size, device, graph.edge_index, graph.edge_attr, wind_mean, wind_std)
elif exp_model == 'PM25_GNN_nosub':
return PM25_GNN_nosub(hist_len, pred_len, in_dim, city_num, batch_size, device, graph.edge_index, graph.edge_attr, wind_mean, wind_std)
else:
raise Exception('Wrong model name!')
def train(train_loader, model, optimizer):
model.train()
train_loss = 0
for batch_idx, data in tqdm(enumerate(train_loader)):
optimizer.zero_grad()
pm25, feature, time_arr = data
pm25 = pm25.to(device)
feature = feature.to(device)
pm25_label = pm25[:, hist_len:]
pm25_hist = pm25[:, :hist_len]
pm25_pred = model(pm25_hist, feature)
loss = criterion(pm25_pred, pm25_label)
loss.backward()
optimizer.step()
train_loss += loss.item()
train_loss /= batch_idx + 1
return train_loss
def val(val_loader, model):
model.eval()
val_loss = 0
for batch_idx, data in tqdm(enumerate(val_loader)):
pm25, feature, time_arr = data
pm25 = pm25.to(device)
feature = feature.to(device)
pm25_label = pm25[:, hist_len:]
pm25_hist = pm25[:, :hist_len]
pm25_pred = model(pm25_hist, feature)
loss = criterion(pm25_pred, pm25_label)
val_loss += loss.item()
val_loss /= batch_idx + 1
return val_loss
def test(test_loader, model):
model.eval()
predict_list = []
label_list = []
time_list = []
test_loss = 0
for batch_idx, data in enumerate(test_loader):
pm25, feature, time_arr = data
pm25 = pm25.to(device)
feature = feature.to(device)
pm25_label = pm25[:, hist_len:]
pm25_hist = pm25[:, :hist_len]
pm25_pred = model(pm25_hist, feature)
loss = criterion(pm25_pred, pm25_label)
test_loss += loss.item()
pm25_pred_val = np.concatenate([pm25_hist.cpu().detach().numpy(), pm25_pred.cpu().detach().numpy()], axis=1) * pm25_std + pm25_mean
pm25_label_val = pm25.cpu().detach().numpy() * pm25_std + pm25_mean
predict_list.append(pm25_pred_val)
label_list.append(pm25_label_val)
time_list.append(time_arr.cpu().detach().numpy())
test_loss /= batch_idx + 1
predict_epoch = np.concatenate(predict_list, axis=0)
label_epoch = np.concatenate(label_list, axis=0)
time_epoch = np.concatenate(time_list, axis=0)
predict_epoch[predict_epoch < 0] = 0
return test_loss, predict_epoch, label_epoch, time_epoch
def get_mean_std(data_list):
data = np.asarray(data_list)
return data.mean(), data.std()
def main():
exp_info = get_exp_info()
print(exp_info)
exp_time = arrow.now().format('YYYYMMDDHHmmss')
train_loss_list, val_loss_list, test_loss_list, rmse_list, mae_list, csi_list, pod_list, far_list = [], [], [], [], [], [], [], []
for exp_idx in range(exp_repeat):
print('\nNo.%2d experiment ~~~' % exp_idx)
train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True)
val_loader = torch.utils.data.DataLoader(val_data, batch_size=batch_size, shuffle=False, drop_last=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=False, drop_last=True)
model = get_model()
model = model.to(device)
model_name = type(model).__name__
print(str(model))
optimizer = torch.optim.RMSprop(model.parameters(), lr=lr, weight_decay=weight_decay)
exp_model_dir = os.path.join(results_dir, '%s_%s' % (hist_len, pred_len), str(dataset_num), model_name, str(exp_time), '%02d' % exp_idx)
if not os.path.exists(exp_model_dir):
os.makedirs(exp_model_dir)
model_fp = os.path.join(exp_model_dir, 'model.pth')
val_loss_min = 100000
best_epoch = 0
train_loss_, val_loss_ = 0, 0
for epoch in range(epochs):
print('\nTrain epoch %s:' % (epoch))
train_loss = train(train_loader, model, optimizer)
val_loss = val(val_loader, model)
print('train_loss: %.4f' % train_loss)
print('val_loss: %.4f' % val_loss)
if epoch - best_epoch > early_stop:
break
if val_loss < val_loss_min:
val_loss_min = val_loss
best_epoch = epoch
print('Minimum val loss!!!')
torch.save(model.state_dict(), model_fp)
print('Save model: %s' % model_fp)
test_loss, predict_epoch, label_epoch, time_epoch = test(test_loader, model)
train_loss_, val_loss_ = train_loss, val_loss
rmse, mae, csi, pod, far = get_metric(predict_epoch, label_epoch)
print('Train loss: %0.4f, Val loss: %0.4f, Test loss: %0.4f, RMSE: %0.2f, MAE: %0.2f, CSI: %0.4f, POD: %0.4f, FAR: %0.4f' % (train_loss_, val_loss_, test_loss, rmse, mae, csi, pod, far))
if save_npy:
np.save(os.path.join(exp_model_dir, 'predict.npy'), predict_epoch)
np.save(os.path.join(exp_model_dir, 'label.npy'), label_epoch)
np.save(os.path.join(exp_model_dir, 'time.npy'), time_epoch)
train_loss_list.append(train_loss_)
val_loss_list.append(val_loss_)
test_loss_list.append(test_loss)
rmse_list.append(rmse)
mae_list.append(mae)
csi_list.append(csi)
pod_list.append(pod)
far_list.append(far)
print('\nNo.%2d experiment results:' % exp_idx)
print(
'Train loss: %0.4f, Val loss: %0.4f, Test loss: %0.4f, RMSE: %0.2f, MAE: %0.2f, CSI: %0.4f, POD: %0.4f, FAR: %0.4f' % (
train_loss_, val_loss_, test_loss, rmse, mae, csi, pod, far))
exp_metric_str = '---------------------------------------\n' + \
'train_loss | mean: %0.4f std: %0.4f\n' % (get_mean_std(train_loss_list)) + \
'val_loss | mean: %0.4f std: %0.4f\n' % (get_mean_std(val_loss_list)) + \
'test_loss | mean: %0.4f std: %0.4f\n' % (get_mean_std(test_loss_list)) + \
'RMSE | mean: %0.4f std: %0.4f\n' % (get_mean_std(rmse_list)) + \
'MAE | mean: %0.4f std: %0.4f\n' % (get_mean_std(mae_list)) + \
'CSI | mean: %0.4f std: %0.4f\n' % (get_mean_std(csi_list)) + \
'POD | mean: %0.4f std: %0.4f\n' % (get_mean_std(pod_list)) + \
'FAR | mean: %0.4f std: %0.4f\n' % (get_mean_std(far_list))
metric_fp = os.path.join(os.path.dirname(exp_model_dir), 'metric.txt')
with open(metric_fp, 'w') as f:
f.write(exp_info)
f.write(str(model))
f.write(exp_metric_str)
print('=========================\n')
print(exp_info)
print(exp_metric_str)
print(str(model))
print(metric_fp)
if __name__ == '__main__':
main()