forked from jiejiang-jojo/fast-seq2point
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_pytorch.py
428 lines (328 loc) · 15 KB
/
main_pytorch.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import argparse
import numpy as np
import os
import time
import json
#import matplotlib.pyplot as plt
import torch
import torch.optim as optim
from utilities import (create_folder, get_filename, create_logging,
mean_absolute_error, signal_aggregate_error)
from utilities import *
from data_generator import DataGenerator, TestDataGenerator
from models import move_data_to_gpu
from models import *
#TODO Add binarized loss function and optimizer
def loss_func(output, target):
assert output.shape == target.shape
return torch.mean(torch.abs(output - target))
def loss_func_binary(output, target):
assert output.shape == target.shape
return F.binary_cross_entropy(output, target)
def accuracy(Y, Y_hat):
return (Y == Y_hat).sum() / Y.size
def binary_metrics(outputs, targets):
outputs = binarize(outputs, args.model_threshold)
(tp, fn, fp, tn) = tp_fn_fp_tn(outputs, targets)
precision_value = precision(outputs, targets)
recall_value = recall(outputs, targets)
f1_score = f_value(precision_value, recall_value)
auc = roc_auc(outputs, targets)
ap = average_precision(outputs, targets)
metric_dict = {'tp': tp, 'fn': fn, 'fp': fp, 'tn': tn,
'precision': '{:.4f}'.format(precision_value),
'recall': '{:.4f}'.format(recall_value),
'f1_score': '{:.4f}'.format(f1_score),
'auc': '{:.4f}'.format(auc),
'average_precision': '{:.4f}'.format(ap)}
return metric_dict
def evaluate(model, generator, data_type, max_iteration, cuda, binary=False):
"""Evaluate.
Args:
model: object.
generator: object.
data_type: 'train' | 'validate'.
max_iteration: int, maximum iteration for validation
cuda: bool.
Returns:
mae: float
"""
# Generate function
generate_func = generator.generate_validate(data_type=data_type,
max_iteration=max_iteration)
# Forward
(outputs, targets) = forward(model=model,
generate_func=generate_func,
cuda=cuda,
has_target=True)
if binary:
logging.info('----binary is true and return binary metrics----')
return binary_metrics(outputs, targets)
else:
logging.info('----binary is false and only mae is returned----')
outputs = generator.inverse_transform(outputs)
targets = generator.inverse_transform(targets)
mae = mean_absolute_error(outputs, targets)
return mae
def forward(model, generate_func, cuda, has_target):
"""Forward data to a model.
Args:
model: object
generate_func: generate function
cuda: bool
has_target: bool, True if generate_func yield (batch_x, batch_y),
False if generate_func yield (batch_x)
Returns:
(outputs, targets) | outputs
"""
model.eval()
outputs = []
targets = []
# Evaluate on mini-batch
for data in generate_func:
if has_target:
(batch_x, batch_y) = data
targets.append(batch_y)
else:
batch_x = data
batch_x = move_data_to_gpu(batch_x, cuda)
# Predict
batch_output = model(batch_x)
outputs.append(batch_output.data.cpu().numpy())
if has_target:
outputs = np.concatenate(outputs, axis=0)
targets = np.concatenate(targets, axis=0)
return outputs, targets
else:
return outputs
def train(args):
logging.info('config=%s', json.dumps(vars(args)))
# Arguments & parameters
workspace = args.workspace
cuda = args.cuda
# Load model
model_class, model_params = MODELS[args.model]
model = model_class(**{k: args.model_params[k] for k in model_params if k in args.model_params})
if args.train_model is not None:
logging.info("continue training ...")
model_path = os.path.join(workspace, 'logs', get_filename(__file__),
args.train_model)
checkpoint = torch.load(model_path)
model.load_state_dict(checkpoint['state_dict'])
logging.info("sequence length: {}".format(model.seq_len))
if cuda:
model.cuda()
# Paths
hdf5_path = os.path.join(workspace, 'data.h5')
models_dir = os.path.join(workspace, 'models', get_filename(__file__))
create_folder(models_dir)
# Data generator
generator = DataGenerator(hdf5_path=hdf5_path,
target_device=args.target_device,
train_house_list=args.train_house_list,
validate_house_list=args.validate_house_list,
batch_size=args.batch_size,
seq_len=model.seq_len,
width=args.width,
binary_threshold=args.binary_threshold,
balance_threshold=args.balance_threshold,
balance_positive=args.balance_positive)
# Optimizer
learning_rate = 1e-3 # 4.7101286972462485e-05
optimizer = optim.Adam(model.parameters(), lr=learning_rate, betas=(0.9, 0.999),
eps=1e-08, weight_decay=0.)
iteration = 0
train_bgn_time = time.time()
for (batch_x, batch_y) in generator.generate():
if iteration > 1000*100:
break
# Evaluate
if iteration % 1000 == 0:
train_fin_time = time.time()
tr_result_dict = evaluate(model=model,
generator=generator,
data_type='train',
max_iteration=args.validate_max_iteration,
cuda=cuda,
binary=args.binary_threshold is not None)
va_result_dict = evaluate(model=model,
generator=generator,
data_type='validate',
max_iteration=args.validate_max_iteration,
cuda=cuda,
binary=args.binary_threshold is not None)
logging.info('train: {}'.format(tr_result_dict))
logging.info('validate: {}'.format(va_result_dict))
train_time = train_fin_time - train_bgn_time
validate_time = time.time() - train_fin_time
logging.info(
'iteration: {}, train time: {:.3f} s, validate time: {:.3f} s'.format(
iteration, train_time, validate_time))
logging.info('------------------------------------')
train_bgn_time = time.time()
# Reduce learning rate
if iteration % 1000 == 0 and iteration > 0 and learning_rate > 5e-5:
for param_group in optimizer.param_groups:
learning_rate *= 0.9
param_group['lr'] = learning_rate
batch_x = move_data_to_gpu(batch_x, cuda)
batch_y = move_data_to_gpu(batch_y, cuda)
# Forward
forward_time = time.time()
model.train()
output = model(batch_x)
# Loss
if args.binary_threshold is not None:
loss = loss_func_binary(output, batch_y)
else:
loss = loss_func(output, batch_y)
# Backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Save model
if (iteration>1) and (iteration % 1000 == 0):
save_out_dict = {'iteration': iteration,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict()}
save_out_path = args.basename + '_{}_{}_iter_{}_wd_{}_sl_{}.tar'.format(
args.target_device,
args.model,
iteration+0,
args.width,
model.seq_len
)
create_folder(os.path.dirname(save_out_path))
torch.save(save_out_dict, save_out_path)
logging.info('Save model to {}'.format(save_out_path))
iteration += 1
def inference(args):
logging.info('config=%s', json.dumps(vars(args)))
# Arguments & parameters
workspace = args.workspace
cuda = args.cuda
# Paths
hdf5_path = os.path.join(workspace, 'data.h5')
model_path = os.path.join(workspace, 'logs', get_filename(__file__),
args.inference_model)
# Load model
model_class, model_params = MODELS[args.model]
model = model_class(**{k: args.model_params[k] for k in model_params if k in args.model_params})
checkpoint = torch.load(model_path)
model.load_state_dict(checkpoint['state_dict'])
if cuda:
model.cuda()
# Data generator
generator = TestDataGenerator(hdf5_path=hdf5_path,
target_device=args.target_device,
train_house_list=args.train_house_list,
seq_len=model.seq_len,
steps=args.width * args.batch_size,
binary_threshold=args.binary_threshold)
generate_func = generator.generate_inference(house=args.inference_house)
# Forward
inference_time = time.time()
outputs = forward(model=model, generate_func=generate_func, cuda=cuda, has_target=False)
outputs = np.concatenate([output[0] for output in outputs])
if args.binary_threshold is not None:
logging.info('----binary threshold is not none and binary metrics are returned----')
targets = generator.get_target()
logging.info('Inference time: {} s'.format(time.time() - inference_time))
metric_dict = binary_metrics(outputs, targets)
logging.info('Metrics: {}'.format(metric_dict))
else:
logging.info('----binary threshold is none and mae and sae metrics are returned----')
outputs = generator.inverse_transform(outputs)
logging.info('Inference time: {} s'.format(time.time() - inference_time))
# Calculate metrics
source = generator.get_source()
targets = generator.get_target()
valid_data = np.ones_like(source)
for i in range(len(source)):
if (source[i]==0) or (source[i] < targets[i]):
valid_data[i] = 0
mae = mean_absolute_error(outputs * valid_data, targets * valid_data)
sae = signal_aggregate_error(outputs * valid_data, targets * valid_data)
mae_allzero = mean_absolute_error(outputs*0, targets * valid_data)
sae_allmean = signal_aggregate_error(outputs*0+generator.mean_y, targets * valid_data)
metric_dict = dict({'MAE': mae, 'MAE_zero': mae_allzero, 'SAE': sae, 'SAE_mean': sae_allmean}, **binary_metrics(((outputs - args.eval_binary_threshold) > 0).astype('float'), ((targets - args.eval_binary_threshold) > 0).astype('float')))
logging.info('Metrics: {}'.format(metric_dict))
#np.save(workspace+'/outputs/'+args.inference_model+'_'+args.inference_house+'_'+'prediction.npy', outputs)
#np.save(workspace+'/outputs/'+args.inference_model+'_'+args.inference_house+'_'+'groundtruth.npy', targets)
#np.save(workspace+'/outputs/'+args.inference_model+'_'+args.inference_house+'_'+'source.npy', source)
class DefaultNamespace(argparse.Namespace):
""" When the requested attribute does not exists return None instead of throw AttributeError.
Ex. Namespace().abc --> throw Attribute Error
DefaultNamespace().abc --> return None
"""
def __getattr__(self, name):
return None
def consolidate_args(args):
""" Merge different source of configuration. """
# Loading config into args
with open(args.config) as fin:
config = json.load(fin)
config.update({k: v for k, v in args.__dict__.items() if v is not None})
args = DefaultNamespace(**config)
# Loading commandline model parameters into model_params
model_param_setting = {k: v for k, v in args.__dict__.items() if k.startswith('pm_')}
if 'model_params' not in args.__dict__:
args.__dict__['model_params'] = {}
for k, v in model_param_setting.items():
args.__dict__.pop(k)
if v is not None:
try:
args.model_params[k[3:].replace('-', '_')] = int(v)
except:
args.model_params[k[3:]] = v
if args.binary_threshold is not None:
args.model_params['to_binary'] = True
return args
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='')
subparsers = parser.add_subparsers(dest='mode')
model_params = set()
for _, (_, mps) in MODELS.items():
for p in mps:
model_params.add(p)
parser_train = subparsers.add_parser('train')
parser_train.add_argument('--workspace', type=str, required=True)
parser_train.add_argument('--config', type=str, required=True)
parser_train.add_argument('--cuda', action='store_true', default=False)
parser_train.add_argument('--width', type=int)
parser_train.add_argument('--binary-threshold', type=float, default=None)
parser_train.add_argument('--balance-threshold', type=float, default=None)
parser_train.add_argument('--balance-positive', type=float, default=None)
parser_train.add_argument('--train-model', type=str, default=None)
for p in model_params:
parser_train.add_argument('--pm-' + p.replace('_', '-'), type=str, metavar='<{}>'.format(p))
parser_inference = subparsers.add_parser('inference')
parser_inference.add_argument('--workspace', type=str, required=True)
parser_inference.add_argument('--config', type=str, required=True)
parser_inference.add_argument('--inference-model', type=str)
parser_inference.add_argument('--inference-house', type=str)
parser_inference.add_argument('--binary-threshold', type=float, default=None)
parser_inference.add_argument('--eval-binary-threshold', type=float, default=None)
parser_inference.add_argument('--model-threshold', type=float, default=None)
parser_inference.add_argument('--cuda', action='store_true', default=False)
for p in model_params:
parser_inference.add_argument('--pm-' + p.replace('_', '-'), type=str, metavar='<{}>'.format(p))
args = parser.parse_args()
args = consolidate_args(args)
# Write out log
logs_dir = os.path.join(args.workspace, 'logs', get_filename(__file__))
logging = create_logging(logs_dir, filemode='w')
logging.info(args)
if args.mode == 'train':
args.__dict__['basename'] = logging.getLoggerClass().root.handlers[0].baseFilename[:-4]
config_to_save = args.basename + '.config.json'
logging.info('Saving config to ' + config_to_save)
ignores = set(['workspace', 'config', 'cuda', 'mode'])
with open(config_to_save, 'w') as fout:
json.dump({k: v for k, v in args.__dict__.items()
if k not in ignores}, fout)
train(args)
elif args.mode == 'inference':
inference(args)
else:
raise Exception('Error!')