-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
367 lines (298 loc) · 11.5 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
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
import torch
from torch.optim import AdamW
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import numpy as np
import time
import datetime
import os
from depth_estimation.model.model import UDFNet
from depth_estimation.utils.loss import (
SILogLoss,
RMSELoss,
ChamferDistanceLoss,
)
from depth_estimation.utils.visualization import get_tensorboard_grids
# from data.flsea.dataset import get_flsea_dataset
from data.example_dataset.dataset import get_example_dataset
##############################################################
########################## CONFIG ############################
##############################################################
# training parameters
BATCH_SIZE = 6
LEARNING_RATE = 0.0001
LEARNING_RATE_DECAY = 0.90
EPOCHS = 25
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
LOSS_FUNCTIONS = {
"SILog_Loss": SILogLoss(correction=0.85, scaling=10.0),
"Chamfer_Loss": ChamferDistanceLoss(),
"L2_Loss": RMSELoss(),
"L1_Loss": torch.nn.L1Loss(),
}
LOSS_WEIGHTS = {"w_SILog_Loss": 0.6, "w_Chamfer_Loss": 0.1, "w_L2_Loss": 0.3}
TRAINING_LOSS_NAMES = [
"training_loss",
"training_loss/SILog Loss",
"training_loss/Bins Chamfer Loss",
"training_loss/L2 Loss (RMSE)",
"training_loss/L1 Loss (MAE)",
"training_loss/L2 Log Loss (RMSE log)",
"training_loss/L2 Loss [d<5m] (RMSE)",
]
VALIDATION_LOSS_NAMES = [
"validation_loss",
"validation_loss/SILog Loss",
"validation_loss/Bins Chamfer Loss",
"validation_loss/L2 Loss (RMSE)",
"validation_loss/L1 Loss (MAE)",
"validation_loss/L2 Log Loss (RMSE log)",
"validation_loss/L2 Loss [d<5m] (RMSE)",
]
# datasets
# TRAIN_DATASET = get_flsea_dataset(
# split="dataset_with_matched_features",
# train=True,
# shuffle=True,
# device=DEVICE,
# )
# VALIDATION_DATASET = get_flsea_dataset(
# split="test_with_matched_features",
# train=False,
# shuffle=True,
# device=DEVICE,
# )
TRAIN_DATASET = get_example_dataset(train=True, shuffle=True, device=DEVICE)
VALIDATION_DATASET = get_example_dataset(train=False, shuffle=True, device=DEVICE) # you should change this, this should not be the same as training
# tensorboard output frequencies
WRITE_TRAIN_IMG_EVERY_N_BATCHES = 500
WRITE_VALIDATION_IMG_EVERY_N_BATCHES = 300
############################################################
############################################################
############################################################
def train_UDFNet():
"""Train loop to train a UDFNet model."""
# print run infos
run_name = f"udfnet_lr{LEARNING_RATE}_bs{BATCH_SIZE}_lrd{LEARNING_RATE_DECAY}"
print(
f"Training run {run_name} with parameters:\n"
+ f" learning rate: {LEARNING_RATE}\n"
+ f" learning rate decay: {LEARNING_RATE_DECAY}\n"
+ f" batch size: {BATCH_SIZE}\n"
+ f" device: {DEVICE}"
)
# tensorboard summary writer
global summary_writer
summary_writer = SummaryWriter(run_name)
# initialize model
model = UDFNet(n_bins=80).to(DEVICE)
# dataloaders
train_dataloader = DataLoader(TRAIN_DATASET, batch_size=BATCH_SIZE, shuffle=True)
validation_dataloader = DataLoader(VALIDATION_DATASET, batch_size=BATCH_SIZE)
# train epochs
for epoch in range(EPOCHS):
# decayed learning rate
lr = LEARNING_RATE * (LEARNING_RATE_DECAY**epoch)
# epoch info
print("------------------------")
print(f"Epoch {epoch}/{EPOCHS} (lr: {lr}, batch_size: {BATCH_SIZE})")
print("------------------------")
# train epoch
start_time = time.time()
training_losses = train_epoch(
dataloader=train_dataloader,
model=model,
learning_rate=lr,
epoch=epoch,
)
print(
f"Epoch time: {str(datetime.timedelta(seconds=(time.time() - start_time)))}"
)
# validate epoch
validation_losses = validate(
dataloader=validation_dataloader,
model=model,
epoch=epoch,
)
# tensorboard summary for training and validation
for loss, loss_name in zip(training_losses, TRAINING_LOSS_NAMES):
summary_writer.add_scalar(f"{loss_name}", loss, epoch)
for loss, loss_name in zip(validation_losses, VALIDATION_LOSS_NAMES):
summary_writer.add_scalar(f"{loss_name}", loss, epoch)
# save model after every epoch
save_model(model, epoch, run_name)
def train_epoch(
dataloader,
model,
learning_rate,
epoch=0,
):
"""Train a model for one epoch.
- dataloader: the dataloader to use
- model: The model to train
- learning_rate: the learning rate for the optimizer
- epoch: epoch id"""
# set training mode
model.train()
# optimizer
optimizer = AdamW(model.parameters(), lr=learning_rate)
n_batches = len(dataloader)
training_losses = np.zeros(len(TRAINING_LOSS_NAMES))
for batch_id, data in enumerate(dataloader):
# move to device
X = data[0].to(DEVICE) # RGB image
y = data[1].to(DEVICE) # depth image
mask = data[2].to(DEVICE) # mask for valid values
prior = data[3].to(DEVICE) # precomputed features and depth values
# nullprior, for training without any priors
# prior[:, :, :, :] = 0.0
# prediction
pred, bin_edges = model(X, prior)
bin_centers = 0.5 * (bin_edges[:, :-1] + bin_edges[:, 1:])
# individual losses
batch_loss_silog = LOSS_FUNCTIONS["SILog_Loss"](pred, y, mask)
batch_loss_chamfer = LOSS_FUNCTIONS["Chamfer_Loss"](y, bin_centers, mask)
batch_loss_l2 = LOSS_FUNCTIONS["L2_Loss"](pred, y, mask)
batch_loss_l1 = LOSS_FUNCTIONS["L1_Loss"](pred[mask], y[mask]) # , mask)
batch_loss_l2_log = LOSS_FUNCTIONS["L2_Loss"](
torch.log(pred), torch.log(y), mask
)
close_range = y[mask] < 5.0 # close range mask (less than 5m)
batch_loss_l2_close = LOSS_FUNCTIONS["L2_Loss"](
pred[mask][close_range], y[mask][close_range]
)
# guidance signal for points outside of mask (usually points at infinity)
batch_loss_silog = batch_loss_silog + 0.02 * LOSS_FUNCTIONS["SILog_Loss"](
pred, y, ~mask
)
batch_loss_l2 = batch_loss_l2 + 0.02 * LOSS_FUNCTIONS["L2_Loss"](pred, y, ~mask)
# learning objective loss
batch_loss = (
batch_loss_silog * LOSS_WEIGHTS["w_SILog_Loss"]
+ batch_loss_chamfer * LOSS_WEIGHTS["w_Chamfer_Loss"]
+ batch_loss_l2 * LOSS_WEIGHTS["w_L2_Loss"]
)
# backpropagation
optimizer.zero_grad()
batch_loss.backward()
optimizer.step()
# statistics for tensorboard visualization graphs
batch_losses = np.array(
[
batch_loss.item(),
batch_loss_silog.item(),
batch_loss_chamfer.item(),
batch_loss_l2.item(),
batch_loss_l1.item(),
batch_loss_l2_log.item(),
batch_loss_l2_close.item(),
]
)
training_losses += batch_losses
# tensorboard summary grids for visual inspection
if (batch_id % WRITE_TRAIN_IMG_EVERY_N_BATCHES == 0) and (
X.size(0) == BATCH_SIZE
):
with torch.no_grad(): # no gradients for visualization
# get tensorboard grids
grids = get_tensorboard_grids(
X, y, prior, pred, mask, bin_edges, device=DEVICE
)
# write to tensorboard
summary_writer.add_image(
f"train_rgb_target_pred_error/{batch_id}", grids[0], epoch
)
summary_writer.add_image(
f"train_target_parametrization/{batch_id}", grids[1], epoch
)
if batch_id % 50 == 0:
print(f"batch {batch_id}/{n_batches}, batch training loss: {batch_losses}")
avg_batch_losses = training_losses / n_batches
print(f"Average batch training loss: {avg_batch_losses}")
return avg_batch_losses
@torch.no_grad() # no gradients needed during validation
def validate(
dataloader,
model,
epoch=0,
):
"""Validate a model, typically done after each training epoch."""
# set evaluation mode
model.eval()
n_batches = len(dataloader)
validation_losses = np.zeros(len(VALIDATION_LOSS_NAMES))
for batch_id, data in enumerate(dataloader):
# move to device
X = data[0].to(DEVICE) # RGB image
y = data[1].to(DEVICE) # depth image
mask = data[2].to(DEVICE) # mask for valid values
prior = data[3].to(DEVICE) # precomputed features and depth values
# nullprior
# prior[:, :, :, :] = 0.0
# prediction
pred, bin_edges = model(X, prior)
bin_centers = 0.5 * (bin_edges[:, :-1] + bin_edges[:, 1:])
# individual losses
batch_loss_silog = LOSS_FUNCTIONS["SILog_Loss"](pred, y, mask)
batch_loss_chamfer = LOSS_FUNCTIONS["Chamfer_Loss"](y, bin_centers, mask)
batch_loss_l2 = LOSS_FUNCTIONS["L2_Loss"](pred, y, mask)
batch_loss_l1 = LOSS_FUNCTIONS["L1_Loss"](pred[mask], y[mask]) # , mask)
batch_loss_l2_log = LOSS_FUNCTIONS["L2_Loss"](
torch.log(pred), torch.log(y), mask
)
close_range = y[mask] < 5.0 # close range mask (less than 5m)
batch_loss_l2_close = LOSS_FUNCTIONS["L2_Loss"](
pred[mask][close_range], y[mask][close_range]
)
# objective (for reference)
batch_loss = (
batch_loss_silog * LOSS_WEIGHTS["w_SILog_Loss"]
+ batch_loss_chamfer * LOSS_WEIGHTS["w_Chamfer_Loss"]
+ batch_loss_l2 * LOSS_WEIGHTS["w_L2_Loss"]
)
# statistics for tensorboard visualization graphs
batch_losses = np.array(
[
batch_loss.item(),
batch_loss_silog.item(),
batch_loss_chamfer.item(),
batch_loss_l2.item(),
batch_loss_l1.item(),
batch_loss_l2_log.item(),
batch_loss_l2_close.item(),
]
)
validation_losses += batch_losses
# tensorboard summary grids for visual inspection
if (batch_id % WRITE_VALIDATION_IMG_EVERY_N_BATCHES == 0) and (
X.size(0) == BATCH_SIZE
):
# get grids
grids = get_tensorboard_grids(
X, y, prior, pred, mask, bin_edges, device=DEVICE
)
# write to tensorboard
summary_writer.add_image(
f"rgb_target_pred_error/{batch_id}", grids[0], epoch
)
summary_writer.add_image(
f"target_parametrization/{batch_id}", grids[1], epoch
)
if batch_id % 100 == 0:
print(
f"batch {batch_id}/{n_batches}, batch validation losses: {batch_losses}"
)
avg_batch_losses = validation_losses / n_batches
print(f"Average batch validation losses: {avg_batch_losses}")
return avg_batch_losses
def save_model(model, epoch, run_name):
print(f"Saving model after epoch {epoch} ...")
# check if folder exists
folder_name = "saved_models"
if not os.path.isdir(folder_name):
os.mkdir(folder_name)
# save model
model_filename = f"{folder_name}/model_e{epoch}_{run_name}.pth"
torch.save(model.state_dict(), model_filename)
if __name__ == "__main__":
train_UDFNet()