-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_rg.py
366 lines (294 loc) · 14.5 KB
/
train_rg.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
from __future__ import division, print_function
import argparse
import os
import numpy as np
import torch.optim as optim
from atalaya import Logger
from torch.optim import lr_scheduler
from utils import *
from modules import *
def parse_args(args):
parser = argparse.ArgumentParser()
# Device arguments
parser.add_argument('--seed', type=int, default=0,
help='Random seed (0 is no random-seed).')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='Disables CUDA training.')
# Data arguments
parser.add_argument('--data-folder', type=str, default='',
help='Path to the data folder.')
parser.add_argument('--suffix', type=str, default='armless',
help='Suffix for training data (e.g. "armless".')
parser.add_argument('--num-atoms', type=int, default=15,
help='Number of atoms in simulation.')
parser.add_argument('--dims', type=int, default=6,
help=('The number of input dimensions '
'(position + velocity).'))
parser.add_argument('--timesteps', type=int, default=100,
help='The number of time steps per sample.')
parser.add_argument('--dims-clinical', type=int, default=84,
help='Number of clinical features.')
# Training arguments
parser.add_argument('--epochs', type=int, default=1,
help='Number of epochs to train.')
parser.add_argument('--batch-size', type=int, default=2,
help='Number of samples per batch.')
parser.add_argument('--lr', type=float, default=0.0001,
help='Initial learning rate.')
parser.add_argument('--lr-decay', type=int, default=50,
help=('After how many epochs to decay LR by a factor'
'of gamma.'))
parser.add_argument('--gamma', type=float, default=0.5,
help='LR decay factor.')
parser.add_argument('--patience', type=int, default=50,
help='Early stopping patience.')
parser.add_argument('--encoder-dropout', type=float, default=0.0,
help='Dropout rate (1 - keep probability).')
parser.add_argument('--decoder-dropout', type=float, default=0.0,
help='Dropout rate (1 - keep probability).')
parser.add_argument('--temp', type=float, default=0.5,
help='Temperature for Gumbel softmax.')
parser.add_argument('--tau', type=float, default=0.5,
help='Temperature decay factor.')
# Model arguments
parser.add_argument('--encoder-hidden', type=int, default=256,
help='Number of hidden units.')
parser.add_argument('--decoder-hidden', type=int, default=256,
help='Number of hidden units.')
parser.add_argument('--skip-first', action='store_true', default=False,
help=('Skip first edge type in decoder, '
'i.e. it represents no-edge.'))
parser.add_argument('--hard', action='store_true', default=False,
help='Uses discrete samples in training forward pass.')
parser.add_argument('--prior', action='store_true', default=False,
help='Whether to use sparsity prior.')
parser.add_argument('--edge-types', type=int, default=2,
help='The number of edge types to infer.')
parser.add_argument('--no-factor', action='store_true', default=False,
help='Disables factor graph model.')
# Conditional arguments
parser.add_argument('--cond-hidden', action='store_true', default=False,
help='Conditionates the model in hidden layer.')
parser.add_argument('--cond-msgs', action='store_true', default=False,
help='Conditionates the model on messages level.')
# Loss arguments
parser.add_argument('--var', type=float, default=5e-5,
help='Output variance.')
parser.add_argument('--beta', type=float, default=1.0,
help='KL-divergence beta factor')
parser.add_argument('--mse-loss', action='store_true', default=False,
help='Use the MSE as the loss')
# Logger and Grapher arguments (using atalaya)
# Logger
parser.add_argument('--logger-folder', type=str, default='',
help=('Where to save the trained model, leave empty to'
' not save anything.'))
parser.add_argument('--no-verbose', action='store_true', default=False,
help='Display information in terminal')
parser.add_argument('--logger-name', type=str, default='exp',
help='First part of the logger name (e.g. "exp1".')
parser.add_argument('--load-params', type=str, default='',
help='Where to load the params. ')
parser.add_argument('--load-folder', type=str, default='',
help='Where to load the model. ')
# Grapher
parser.add_argument('--grapher', type=str, default='',
help='Name of the grapher. Leave empty for no grapher')
# if visdom
parser.add_argument('--visdom-url', type=str, default="http://localhost",
help='visdom URL (default: http://localhost).')
parser.add_argument('--visdom-port', type=int, default="8097",
help='visdom port (default: 8097)')
parser.add_argument('--visdom-username', type=str, default='',
help='Username of visdom server.')
parser.add_argument('--visdom-password', type=str, default='',
help='Password of visdom server.')
return parser.parse_args(args)
def run(mode, data_loader, encoder, decoder, optimizer, rel_rec,
rel_send, log_prior, skeleton, args):
history = {key: [] for key in ['mse', 'nll', 'kl', 'loss']}
for batch_idx, (data, clinical) in enumerate(data_loader):
data = data.to(args.device)
if args.conditional:
clinical = clinical.to(args.device)
if mode == 'train':
optimizer.zero_grad()
edges = np.repeat(skeleton[np.newaxis, :, :], data.shape[0], axis=0)
edges = Variable(torch.Tensor(edges)).to(args.device)
output = decoder(data, edges, rel_rec, rel_send, clinical)
loss_nll = nll_gaussian(output, data, args.var)
loss_kl = Variable(torch.Tensor([0])).to(args.device)
# args.beta = int((loss_nll/loss_kl) / 10)
if not np.isclose(args.beta, 0, rtol=1e-6):
loss_kl = args.beta*loss_kl
loss_mse = F.mse_loss(output, data)
# if mse_loss == true use it else use elbo
loss = loss_mse if args.mse_loss else loss_nll + loss_kl
if mode == 'train':
loss.backward()
optimizer.step()
history['loss'].append(loss.item())
history['mse'].append(loss_mse.item())
history['nll'].append(loss_nll.item())
history['kl'].append(loss_kl.item())
return history, None
def train(epoch, data_loader, encoder, decoder, optimizer, scheduler, rel_rec,
rel_send, log_prior, skeleton, args, logger):
decoder.train()
history, _ = run('train', data_loader, encoder, decoder, optimizer,
rel_rec, rel_send, log_prior, skeleton, args)
scheduler.step()
history = logger.register_plots(history, epoch, prefix='train')
return history['loss']
def test(mode, epoch, data_loader, encoder, decoder, rel_rec, rel_send,
log_prior, skeleton, args, logger):
decoder.eval()
with torch.no_grad():
history, aggr_posterior = run(mode, data_loader, encoder, decoder,
None, rel_rec, rel_send,
log_prior, skeleton, args)
if mode == 'post':
return None, aggr_posterior
history = logger.register_plots(history, epoch, prefix=mode)
return history['loss'], None
def generations(data_loader, decoder, rel_rec, rel_send, skeleton,
first_frame_params, args, logger):
decoder.eval()
path = os.path.join('Results', logger.name)
os.makedirs(path, exist_ok=True)
outputs = []
for batch_idx, (data, clinical) in enumerate(data_loader):
data = data.to(args.device)
clinical = clinical.to(args.device)
edges = np.repeat(skeleton[np.newaxis, :, :], data.shape[0], axis=0)
edges = Variable(torch.Tensor(edges)).to(args.device)
with torch.no_grad():
output = decoder(data, edges, rel_rec, rel_send,
clinical)
mse = F.mse_loss(output, data)
logger.add_scalar('generation_mse', mse.item(), batch_idx+1)
outputs.append(output.data.cpu().numpy())
logger.info("Saving data !")
# np.save(os.path.join(path, 'history.npy'), np.concatenate(history, axis=0))
np.save(os.path.join(path, 'output.npy'), np.concatenate(outputs, axis=0))
def main(args):
# get args
args = parse_args(args)
logger = Logger(name="{}_{}".format(args.logger_name, args.suffix),
path=args.logger_folder,
verbose=(not args.no_verbose),
grapher=args.grapher,
server=args.visdom_url,
port=args.visdom_port,
username=args.visdom_username,
password=args.visdom_password)
# add parameters to the logger
logger.add_parameters(args)
if args.load_folder or args.load_params:
path_data = args.data_folder
args = logger.restore_parameters(args.load_params if args.load_params
else args.load_folder)
args.data_folder = path_data
# if GPU available -> use device == cuda
cuda = not args.no_cuda and torch.cuda.is_available()
args.device = torch.device("cuda" if cuda else "cpu")
# if random seed use it, args.seed == 0 is non random seed
if args.seed:
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if cuda:
torch.cuda.manual_seed(args.seed)
args.factor = not args.no_factor
train_loader, val_loader, test_loader = load_data(args.batch_size,
args.data_folder,
args.suffix)
# get mean and std of the first frame on training samples
first_frame_params = get_params_first_frame(train_loader)
# skeleton
skeleton = load_skeleton(args.data_folder, args.suffix)
# Generate off-diagonal interaction graph
off_diag = np.ones([args.num_atoms, args.num_atoms]) \
- np.eye(args.num_atoms)
rel_rec = np.array(encode_onehot(np.where(off_diag)[1]), dtype=np.float32)
rel_send = np.array(encode_onehot(np.where(off_diag)[0]), dtype=np.float32)
rel_rec = torch.FloatTensor(rel_rec)
rel_send = torch.FloatTensor(rel_send)
# initialize decoder
decoder = RNNDecoder(n_in_node=args.dims,
n_atoms=args.num_atoms,
n_clinical=args.dims_clinical,
edge_types=args.edge_types,
n_hid=args.decoder_hidden,
cond_hidden=args.cond_hidden,
cond_msgs=args.cond_msgs,
do_prob=args.decoder_dropout,
skip_first=args.skip_first)
# add decoder to logger, will save chekpoints and best
logger.add('decoder', decoder)
# optimizer
optimizer = optim.Adam(list(decoder.parameters()),
lr=args.lr)
logger.add('optimizer', optimizer)
# scheduler for adam
scheduler = lr_scheduler.StepLR(optimizer,
step_size=args.lr_decay,
gamma=args.gamma)
logger.add('scheduler_opti', scheduler)
# TODO: sheduler for gumbel softmax
# # scheduler for temperature in gumble softmax
# scheduler = temp_scheduler.StepLR(optimizer,
# step_size=args.temp_decay,
# gamma=args.tau)
# logger.add('scheduler_tau', scheduler)
if args.load_folder:
logger.restore(args.load_folder)
log_prior = None
if args.prior:
logger.info("Using prior")
prior = np.zeros(args.edge_types)
prior[0] = 0.91
for idx in range(1, args.edge_types):
prior[idx] = 0.9 / (args.edge_types - 1)
log_prior = torch.FloatTensor(np.log(prior))
log_prior = torch.unsqueeze(log_prior, 0)
log_prior = torch.unsqueeze(log_prior, 0)
log_prior = Variable(log_prior).to(args.device)
decoder.to(args.device)
rel_rec = Variable(rel_rec).to(args.device)
rel_send = Variable(rel_send).to(args.device)
# check if the nri will be cond
args.conditional = (args.cond_hidden or args.cond_msgs)
# Train model
stop_early = 0
for epoch in range(1, args.epochs+1):
_ = train(epoch, train_loader, None, decoder, optimizer, scheduler,
rel_rec, rel_send, log_prior, skeleton, args, logger)
val_loss, _ = test('val', epoch, val_loader, None, decoder, rel_rec,
rel_send, log_prior, skeleton, args, logger)
_, _ = test('test', epoch, test_loader, None, decoder, rel_rec,
rel_send, log_prior, skeleton, args, logger)
# store a checkpoint and save if val_loss < min(all previous val_loss)
best_val_loss = logger.store(val_loss)
# updating stop_early if not improving in validation set
stop_early = 0 if best_val_loss else stop_early+1
if stop_early > args.patience:
logger.info(("Stopped training because it hasn't improve "
"performance in validation set for "
"{} epochs").format(args.patience))
break
logger.info("Optimization Finished!")
# test and generation with best model
# restore best model
if len(args.load_folder) == 0:
logger.restore(best=True)
# load the best model for a specific folder
else:
logger.restore(folder=args.load_folder, best=True)
# generate and computing mse
generations(test_loader, decoder, rel_rec, rel_send, skeleton,
first_frame_params, args, logger)
# close logger
logger.close()
if __name__ == '__main__':
import sys
main(sys.argv[1:])