forked from gzerveas/mvts_transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
307 lines (255 loc) · 14.7 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
"""
Written by George Zerveas
If you use any part of the code in this repository, please consider citing the following paper:
George Zerveas et al. A Transformer-based Framework for Multivariate Time Series Representation Learning, in
Proceedings of the 27th ACM SIGKDD Conference on Knowledge Discovery and Data Mining (KDD '21), August 14--18, 2021
"""
import logging
logging.basicConfig(format='%(asctime)s | %(levelname)s : %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info("Loading packages ...")
import os
import sys
import time
import pickle
import json
# 3rd party packages
from tqdm import tqdm
import torch
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
# Project modules
from options import Options
from running import setup, pipeline_factory, validate, check_progress, NEG_METRICS
from utils import utils
from datasets.data import data_factory, Normalizer
from datasets.datasplit import split_dataset
from models.ts_transformer import model_factory
from models.loss import get_loss_module
from optimizers import get_optimizer
def main(config):
total_epoch_time = 0
total_eval_time = 0
total_start_time = time.time()
# Add file logging besides stdout
file_handler = logging.FileHandler(os.path.join(config['output_dir'], 'output.log'))
logger.addHandler(file_handler)
logger.info('Running:\n{}\n'.format(' '.join(sys.argv))) # command used to run
if config['seed'] is not None:
torch.manual_seed(config['seed'])
device = torch.device('cuda' if (torch.cuda.is_available() and config['gpu'] != '-1') else 'cpu')
logger.info("Using device: {}".format(device))
if device == 'cuda':
logger.info("Device index: {}".format(torch.cuda.current_device()))
# Build data
logger.info("Loading and preprocessing data ...")
data_class = data_factory[config['data_class']]
my_data = data_class(config['data_dir'], pattern=config['pattern'], n_proc=config['n_proc'], limit_size=config['limit_size'], config=config)
feat_dim = my_data.feature_df.shape[1] # dimensionality of data features
if config['task'] == 'classification':
validation_method = 'StratifiedShuffleSplit'
labels = my_data.labels_df.values.flatten()
else:
validation_method = 'ShuffleSplit'
labels = None
# Split dataset
test_data = my_data
test_indices = None # will be converted to empty list in `split_dataset`, if also test_set_ratio == 0
val_data = my_data
val_indices = []
if config['test_pattern']: # used if test data come from different files / file patterns
test_data = data_class(config['data_dir'], pattern=config['test_pattern'], n_proc=-1, config=config)
test_indices = test_data.all_IDs
if config['test_from']: # load test IDs directly from file, if available, otherwise use `test_set_ratio`. Can work together with `test_pattern`
test_indices = list(set([line.rstrip() for line in open(config['test_from']).readlines()]))
try:
test_indices = [int(ind) for ind in test_indices] # integer indices
except ValueError:
pass # in case indices are non-integers
logger.info("Loaded {} test IDs from file: '{}'".format(len(test_indices), config['test_from']))
if config['val_pattern']: # used if val data come from different files / file patterns
val_data = data_class(config['data_dir'], pattern=config['val_pattern'], n_proc=-1, config=config)
val_indices = val_data.all_IDs
# Note: currently a validation set must exist, either with `val_pattern` or `val_ratio`
# Using a `val_pattern` means that `val_ratio` == 0 and `test_ratio` == 0
if config['val_ratio'] > 0:
train_indices, val_indices, test_indices = split_dataset(data_indices=my_data.all_IDs,
validation_method=validation_method,
n_splits=1,
validation_ratio=config['val_ratio'],
test_set_ratio=config['test_ratio'], # used only if test_indices not explicitly specified
test_indices=test_indices,
random_seed=1337,
labels=labels)
train_indices = train_indices[0] # `split_dataset` returns a list of indices *per fold/split*
val_indices = val_indices[0] # `split_dataset` returns a list of indices *per fold/split*
else:
train_indices = my_data.all_IDs
if test_indices is None:
test_indices = []
logger.info("{} samples may be used for training".format(len(train_indices)))
logger.info("{} samples will be used for validation".format(len(val_indices)))
logger.info("{} samples will be used for testing".format(len(test_indices)))
with open(os.path.join(config['output_dir'], 'data_indices.json'), 'w') as f:
try:
json.dump({'train_indices': list(map(int, train_indices)),
'val_indices': list(map(int, val_indices)),
'test_indices': list(map(int, test_indices))}, f, indent=4)
except ValueError: # in case indices are non-integers
json.dump({'train_indices': list(train_indices),
'val_indices': list(val_indices),
'test_indices': list(test_indices)}, f, indent=4)
# Pre-process features
normalizer = None
if config['norm_from']:
with open(config['norm_from'], 'rb') as f:
norm_dict = pickle.load(f)
normalizer = Normalizer(**norm_dict)
elif config['normalization'] is not None:
normalizer = Normalizer(config['normalization'])
my_data.feature_df.loc[train_indices] = normalizer.normalize(my_data.feature_df.loc[train_indices])
if not config['normalization'].startswith('per_sample'):
# get normalizing values from training set and store for future use
norm_dict = normalizer.__dict__
with open(os.path.join(config['output_dir'], 'normalization.pickle'), 'wb') as f:
pickle.dump(norm_dict, f, pickle.HIGHEST_PROTOCOL)
if normalizer is not None:
if len(val_indices):
val_data.feature_df.loc[val_indices] = normalizer.normalize(val_data.feature_df.loc[val_indices])
if len(test_indices):
test_data.feature_df.loc[test_indices] = normalizer.normalize(test_data.feature_df.loc[test_indices])
# Create model
logger.info("Creating model ...")
model = model_factory(config, my_data)
if config['freeze']:
for name, param in model.named_parameters():
if name.startswith('output_layer'):
param.requires_grad = True
else:
param.requires_grad = False
logger.info("Model:\n{}".format(model))
logger.info("Total number of parameters: {}".format(utils.count_parameters(model)))
logger.info("Trainable parameters: {}".format(utils.count_parameters(model, trainable=True)))
# Initialize optimizer
if config['global_reg']:
weight_decay = config['l2_reg']
output_reg = None
else:
weight_decay = 0
output_reg = config['l2_reg']
optim_class = get_optimizer(config['optimizer'])
optimizer = optim_class(model.parameters(), lr=config['lr'], weight_decay=weight_decay)
start_epoch = 0
lr_step = 0 # current step index of `lr_step`
lr = config['lr'] # current learning step
# Load model and optimizer state
if args.load_model:
model, optimizer, start_epoch = utils.load_model(model, config['load_model'], optimizer, config['resume'],
config['change_output'],
config['lr'],
config['lr_step'],
config['lr_factor'])
model.to(device)
loss_module = get_loss_module(config)
if config['test_only'] == 'testset': # Only evaluate and skip training
dataset_class, collate_fn, runner_class = pipeline_factory(config)
test_dataset = dataset_class(test_data, test_indices)
test_loader = DataLoader(dataset=test_dataset,
batch_size=config['batch_size'],
shuffle=False,
num_workers=config['num_workers'],
pin_memory=True,
collate_fn=lambda x: collate_fn(x, max_len=model.max_len))
test_evaluator = runner_class(model, test_loader, device, loss_module,
print_interval=config['print_interval'], console=config['console'])
aggr_metrics_test, per_batch_test = test_evaluator.evaluate(keep_all=True)
print_str = 'Test Summary: '
for k, v in aggr_metrics_test.items():
print_str += '{}: {:8f} | '.format(k, v)
logger.info(print_str)
return
# Initialize data generators
dataset_class, collate_fn, runner_class = pipeline_factory(config)
val_dataset = dataset_class(val_data, val_indices)
val_loader = DataLoader(dataset=val_dataset,
batch_size=config['batch_size'],
shuffle=False,
num_workers=config['num_workers'],
pin_memory=True,
collate_fn=lambda x: collate_fn(x, max_len=model.max_len))
train_dataset = dataset_class(my_data, train_indices)
train_loader = DataLoader(dataset=train_dataset,
batch_size=config['batch_size'],
shuffle=True,
num_workers=config['num_workers'],
pin_memory=True,
collate_fn=lambda x: collate_fn(x, max_len=model.max_len))
trainer = runner_class(model, train_loader, device, loss_module, optimizer, l2_reg=output_reg,
print_interval=config['print_interval'], console=config['console'])
val_evaluator = runner_class(model, val_loader, device, loss_module,
print_interval=config['print_interval'], console=config['console'])
tensorboard_writer = SummaryWriter(config['tensorboard_dir'])
best_value = 1e16 if config['key_metric'] in NEG_METRICS else -1e16 # initialize with +inf or -inf depending on key metric
metrics = [] # (for validation) list of lists: for each epoch, stores metrics like loss, ...
best_metrics = {}
# Evaluate on validation before training
aggr_metrics_val, best_metrics, best_value = validate(val_evaluator, tensorboard_writer, config, best_metrics,
best_value, epoch=0)
metrics_names, metrics_values = zip(*aggr_metrics_val.items())
metrics.append(list(metrics_values))
logger.info('Starting training...')
for epoch in tqdm(range(start_epoch + 1, config["epochs"] + 1), desc='Training Epoch', leave=False):
mark = epoch if config['save_all'] else 'last'
epoch_start_time = time.time()
aggr_metrics_train = trainer.train_epoch(epoch) # dictionary of aggregate epoch metrics
epoch_runtime = time.time() - epoch_start_time
print()
print_str = 'Epoch {} Training Summary: '.format(epoch)
for k, v in aggr_metrics_train.items():
tensorboard_writer.add_scalar('{}/train'.format(k), v, epoch)
print_str += '{}: {:8f} | '.format(k, v)
logger.info(print_str)
logger.info("Epoch runtime: {} hours, {} minutes, {} seconds\n".format(*utils.readable_time(epoch_runtime)))
total_epoch_time += epoch_runtime
avg_epoch_time = total_epoch_time / (epoch - start_epoch)
avg_batch_time = avg_epoch_time / len(train_loader)
avg_sample_time = avg_epoch_time / len(train_dataset)
logger.info("Avg epoch train. time: {} hours, {} minutes, {} seconds".format(*utils.readable_time(avg_epoch_time)))
logger.info("Avg batch train. time: {} seconds".format(avg_batch_time))
logger.info("Avg sample train. time: {} seconds".format(avg_sample_time))
# evaluate if first or last epoch or at specified interval
if (epoch == config["epochs"]) or (epoch == start_epoch + 1) or (epoch % config['val_interval'] == 0):
aggr_metrics_val, best_metrics, best_value = validate(val_evaluator, tensorboard_writer, config,
best_metrics, best_value, epoch)
metrics_names, metrics_values = zip(*aggr_metrics_val.items())
metrics.append(list(metrics_values))
utils.save_model(os.path.join(config['save_dir'], 'model_{}.pth'.format(mark)), epoch, model, optimizer)
# Learning rate scheduling
if epoch == config['lr_step'][lr_step]:
utils.save_model(os.path.join(config['save_dir'], 'model_{}.pth'.format(epoch)), epoch, model, optimizer)
lr = lr * config['lr_factor'][lr_step]
if lr_step < len(config['lr_step']) - 1: # so that this index does not get out of bounds
lr_step += 1
logger.info('Learning rate updated to: ', lr)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
# Difficulty scheduling
if config['harden'] and check_progress(epoch):
train_loader.dataset.update()
val_loader.dataset.update()
# Export evolution of metrics over epochs
header = metrics_names
metrics_filepath = os.path.join(config["output_dir"], "metrics_" + config["experiment_name"] + ".xls")
book = utils.export_performance_metrics(metrics_filepath, metrics, header, sheet_name="metrics")
# Export record metrics to a file accumulating records from all experiments
utils.register_record(config["records_file"], config["initial_timestamp"], config["experiment_name"],
best_metrics, aggr_metrics_val, comment=config['comment'])
logger.info('Best {} was {}. Other metrics: {}'.format(config['key_metric'], best_value, best_metrics))
logger.info('All Done!')
total_runtime = time.time() - total_start_time
logger.info("Total runtime: {} hours, {} minutes, {} seconds\n".format(*utils.readable_time(total_runtime)))
return best_value
if __name__ == '__main__':
args = Options().parse() # `argsparse` object
config = setup(args) # configuration dictionary
main(config)