-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_rnn_sensor.py
398 lines (315 loc) · 13.3 KB
/
train_rnn_sensor.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
import os
import pprint
import argparse
import time
import torch
import numpy as np
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.nn.parallel import DistributedDataParallel
from torch.utils.data.distributed import DistributedSampler
import torch.nn.functional as F
from tqdm import tqdm
from torch import nn, optim
from torch.utils.data import DataLoader
from pytorch_msssim import MS_SSIM
from cdae.utils import *
from cdae.models.autoencoders import *
from cdae.models.rnns import SimpleLatentSensorPredictionNet
from cdae.datasets import TempAVImageSensorsSequenceDataset
from config.model.layers_config import *
def calculate_loss(
out: torch.Tensor,
target: torch.Tensor,
config: dict,
loss_msssim: MS_SSIM,
n_batch: int
)-> Tuple[torch.Tensor, np.ndarray]:
""" Single function to calculate loss both for train and validation.
Args:
out (torch.Tensor): model output.
target (torch.Tensor): groundtruth.
config (dict): configuration dictionary.
loss_msssim (pytorch_msssim.MS_SSIM): loss function.
n_batch (int): number of data points in the batch.
Returns:
Loss as torch tensor for backprop.
Array (numpy) of loss component breakdown.
"""
x_out, x_out_ae, x_out_lat, x_in_lat, s_out = out
x_in, s_in = target
xs = x_in.shape
n = n_batch * config["no_seq"]
# TODO: Optimize reshapes
if config["last_image"]:
# Using last prediction only for loss
x_in_temp = x_in[:, -1, :, :, :].reshape(-1, xs[2], xs[3], xs[4])
x_out_temp = x_out[:, -1, :, :, :].reshape(-1, xs[2], xs[3], xs[4])
s_in_temp = s_in[:, -1, :]
s_out_temp = s_out[:, -1, :]
else:
# Using all predicted frames for loss
x_in_temp = x_in[:, 1:, :, :, :].reshape(-1, xs[2], xs[3], xs[4])
x_out_temp = x_out.reshape(-1, xs[2], xs[3], xs[4])
s_in_temp = s_in[:, 1:, :]
s_out_temp = s_out
x_in_lat_temp = x_in_lat[:, 1:, :].reshape(-1, config["image_latent_size"])
x_out_lat_temp = x_out_lat.reshape(-1, config["image_latent_size"])
x_in_ae = x_in.reshape(-1, xs[2], xs[3], xs[4]) # Autoencoder input
# Losses
loss_image_1 = 1 - loss_msssim(x_out_ae, x_in_ae) # AE loss (pre-LSTM)
loss_image_2 = 1 - loss_msssim(x_out_temp, x_in_temp) # AE loss (post-LSTM)
loss_latent = F.smooth_l1_loss(x_out_lat_temp, x_in_lat_temp, reduction="sum") / n
loss_sensors = F.smooth_l1_loss(s_out_temp, s_in_temp, reduction="sum") / n
loss = (loss_image_1 + loss_image_2) / 2.0 + loss_latent + loss_sensors
# No batch size division for MS-SSIM
num_li = (
loss_image_1.detach().cpu().numpy() + loss_image_2.detach().cpu().numpy()
) / 2.0
num_ll = loss_latent.detach().cpu().numpy()
num_ls = loss_sensors.detach().cpu().numpy()
num_l = num_li + num_ll + num_ls
loss_num = np.array([num_l, num_li, num_ll, num_ls])
return loss, loss_num
def get_tt_desc(epoch, batch_idx, n_batches, lr, l):
""" Temporary function to get tqdm description string.
"""
s = f"Train Epoch {epoch:03} Batch: {(batch_idx+1):02}/{n_batches} LR {lr:.1e} \
Loss: {l[0]:.6f} (I): {l[1]:.6f} (L): {l[2]:.6f} (S): {l[3]:.6f}"
return s
def train(
epoch: int,
model: nn.Module,
dataloader: DataLoader,
optimizer: optim.Optimizer,
loss_msssim: MS_SSIM,
device: torch.device,
config: dict,
) -> np.ndarray:
""" Train model for one epoch.
Args:
epoch (int): epoch number.
model (torch.nn.Module): model to train.
dataloader (torch.utils.data.Dataloader): dataloader.
optimizer (torch.optim): optimizer.
loss_msssim (pytorch_msssim.MS_SSIM): MS-SSIM loss.
device (torch.device): device (cpu/gpu).
config (dict): config dictionary.
Returns:
loss_train (float): validation loss (numerical)
"""
model.train()
loss_train = np.zeros(4)
n_batches = len(dataloader)
lr = get_lr(optimizer)
tt = tqdm(dataloader, bar_format="|{bar:30}|{percentage:3.0f}%| {desc}")
for batch_idx, batch in enumerate(tt):
optimizer.zero_grad()
x_in, s_in = batch[0].to(device), batch[1].to(device)
s_in[:, :, -1] = s_in[:, :, -1] / 23.0 # TODO: Temp Fix Speed Scaling
x_out, x_out_ae, x_out_lat, x_in_lat, s_out = model(x_in, s_in)
loss, loss_num = calculate_loss(
(x_out, x_out_ae, x_out_lat, x_in_lat, s_out),
(x_in, s_in),
config,
loss_msssim,
n_batch=batch[0].shape[0],
)
tt.set_description(get_tt_desc(epoch, batch_idx, n_batches, lr, loss_num))
loss_train += loss_num
loss.backward()
optimizer.step()
loss_train = loss_train / n_batches
return loss_train
def validate(
epoch: int,
model: nn.Module,
dataloader: DataLoader,
loss_msssim: MS_SSIM,
device: torch.cuda.device,
config: dict,
) -> np.ndarray:
""" Validate model for one epoch.
Args:
epoch (int): epoch number.
model (torch.nn.Module): model to train.
dataloader (torch.utils.data.Dataloader): dataloader.
loss_msssim (pytorch_msssim.MS_SSIM): MS-SSIM loss.
device (torch.device): device (cpu/gpu).
config (dict): config dictionary.
Returns:
loss_val (float): validation loss (numerical)
"""
loss_val = np.zeros(4)
n_batches = len(dataloader)
tt = tqdm(dataloader, bar_format="|{bar:30}|{percentage:3.0f}%| {desc}")
with torch.no_grad():
model.eval()
for batch_idx, batch in enumerate(tt):
x_in, s_in = batch[0].to(device), batch[1].to(device)
s_in[:, :, -1] = s_in[:, :, -1] / 23.0 # TODO: Temp Fix Speed Scaling
x_out, x_out_ae, x_out_lat, x_in_lat, s_out = model(x_in, s_in)
_, loss_num = calculate_loss(
(x_out, x_out_ae, x_out_lat, x_in_lat, s_out),
(x_in, s_in),
config,
loss_msssim,
n_batch=batch[0].shape[0],
)
tt.set_description(get_tt_desc(epoch, batch_idx, n_batches, 0, loss_num))
loss_val += loss_num
loss_val = loss_val / n_batches
return loss_val
def train_model(
gpu: int,
args: argparse.Namespace,
config: dict
) -> None:
""" Train model loop. Set as separate function for distributed.
Args:
gpu (int): GPU id
args (Namespace): command line arguments
config (dict): config loaded from yaml file.
"""
# DistributedDataParallel
if config["distributed"]:
device = args.nr * args.gpus + gpu
# Initialize the process group
dist.init_process_group(
backend="nccl",
init_method='env://',
rank=device,
world_size=args.world_size
)
else:
device = torch.device(config["device"])
#set_reproducibility_values(seed=config['random_seed'], deterministic=config['deterministic'])
#logs_tra, logs_val = get_dataset_splits_by_logs(len(os.listdir(config["path_dataset"])), config['train_split'], config['validation_split'])
if "resume" in config:
raise NotImplementedError
# run_id = config["run_id"]
# epoch_start = config["epoch_last"] + 1 if "epoch_last" in config else config["epoch_best"] + 1
# lr = config["lr_last"]
# model = torch.load(config["resume"], map_location="cpu").to(device)
# print(f"Resumed {config['resume']}, epoch {epoch_start}, lr {lr}")
# prev_loss_val = config["loss_val"]
else:
run_id = generate_run_id()
epoch_start = 0
lr = config["lr"]
config["run_id"] = run_id
config["lr_last"] = lr
path_results = prepare_run(run_id, header=config["log_header"])
prev_loss_val = 1e6
print(run_id)
path_log = f"{path_results}/{run_id}_log.txt"
# TODO: Fix
if "resume" in config:
model = torch.load(config["resume"], map_location="cpu").to(device)
print(f"Resumed {config['resume']}")
else:
# Load autoencoder
if config["autoencoder_resume"] is None:
autoencoder = None
else:
autoencoder = get_autoencoder(config["autoencoder"], config["image_latent_size"])
autoencoder.load_state_dict(torch.load(config["autoencoder_resume"]))
print(f"AE Warm Start {config['autoencoder_resume']}")
# Load model
model = SimpleLatentSensorPredictionNet(config, autoencoder, device=device).to(device)
# Dataset
ds_train = TempAVImageSensorsSequenceDataset(config["path_train"], config)
ds_val = TempAVImageSensorsSequenceDataset(config["path_validation"], config)
if config["distributed"]:
model = DistributedDataParallel(model, device_ids=[gpu])
sampler_train = DistributedSampler(ds_train, num_replicas=args.world_size, rank=device)
sampler_val = DistributedSampler(ds_val, num_replicas=args.world_size, rank=device)
shuffle=False
else:
sampler_train = None
sampler_val = None
shuffle=True
# Dataloaders
dl_train = DataLoader(ds_train, config["batch_size"], shuffle, sampler_train, num_workers=config["num_workers"])
dl_val = DataLoader(ds_val, config["batch_size"], sampler_val, num_workers=config["num_workers"])
# ---------- OPTIMIZER/SCHEDULER ----------
optimizer = optim.Adam(model.parameters(), lr=config["lr"], weight_decay=config["reg"])
#scheduler = optim.lr_scheduler.MultiStepLR(optimizer, config['lr_scheduler'])
# ---------- LOSS ----------
n_channels = 1 if config["grayscale"] else 3
loss_msssim = MS_SSIM(data_range=1, size_average=True, channel=n_channels)
patience_lr = 0
patience_es = 0
save_config(config, f"{path_results}/{run_id}_config.yaml")
pprint.pprint(config)
# Main loop
for epoch in range(epoch_start, epoch_start + config["epochs"]):
time_start = time.time()
loss_train = train(epoch, model, dl_train, optimizer, loss_msssim, device, config)
elapsed_train = time.time() - time_start
loss_val = validate(epoch, model, dl_val, loss_msssim, device, config)
elapsed_total = time.time() - time_start
if loss_val[0] < prev_loss_val:
config["epoch_best"] = epoch
config["loss_val"] = loss_val[0]
torch.save(model, f"{path_results}/{run_id}_best.pth")
save_config(config, f"{path_results}/{run_id}_config.yaml")
patience_lr = 0
patience_es = 0
prev_loss_val = loss_val[0]
else:
patience_lr += 1
patience_es += 1
# Adjust lr if necessary
if patience_lr >= config["patience_lr"]:
patience_lr = 0
del optimizer
lr = lr * config["lr_alpha"]
config["lr_last"] = lr
save_config(config, f"{path_results}/{run_id}_config.yaml")
print(f"Adjusting lr to {lr}...")
optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=config["reg"])
print(f"Val Epoch {epoch:03} Elapsed: {elapsed_total:.2f} s\
\nLoss (Tra): {loss_train[0]:.6f} (I): {loss_train[1]:.6f} (L): {loss_train[2]:.6f} (S): {loss_train[3]:.6f} \t| {elapsed_train:.2f} s \
\nLoss (Val): {loss_val[0]:.6f} (I): {loss_val[1]:.6f} (L): {loss_val[2]:.6f} (S) {loss_val [3]:.6f} \t| {(elapsed_total-elapsed_train):.2f} s \
\nPatience (lr/es) {patience_lr}/{patience_es}")
# Save info
info = dict({
'epoch': epoch,
'lr': get_lr(optimizer),
'loss_train': loss_train[0],
'loss_train_image': loss_train[1],
'loss_train_latent': loss_train[2],
'loss_val': loss_val[0],
'loss_val_image': loss_val[1],
'loss_val_latent':loss_val[2],
'loss_train_sen': loss_train[3],
'loss_val_sen': loss_val[3]
})
log_dict_to_csv(info, path_log)
if patience_es >= config["patience_es"]:
config["epoch_last"] = epoch
print(f"Early stopping at epoch {epoch}, validation loss hasn't decreased for {config['patience_es']} epochs.")
break
torch.save(model, f"{path_results}/{run_id}_last.pth")
save_config(config, f"{path_results}/{run_id}_config.yaml")
print(f"Completed!")
def main():
parser = argparse.ArgumentParser(description="Autoencoder training.")
parser.add_argument("--config", default="config/model/carla_rnn_sensor.yaml", type=str,help="YAML config file path")
parser.add_argument('-n', '--nodes', default=1, type=int, metavar='N')
parser.add_argument('-g', '--gpus', default=1, type=int, help='Number of gpus per node')
parser.add_argument('-nr', '--nr', default=0, type=int, help='Ranking within the nodes')
args = parser.parse_args()
config = load_config(args.config)
# DistributedDataParallel
if config["distributed"]:
os.environ['MASTER_ADDR'] = '127.0.0.1'
os.environ['MASTER_PORT'] = '12355'
args.world_size = args.gpus * args.nodes
mp.spawn(train_model, nprocs=args.gpus, args=(args, config,))
dist.destroy_process_group()
else:
train_model(0, args, config)
if __name__ == "__main__":
main()