-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_hallucidet.py
More file actions
551 lines (430 loc) · 29.1 KB
/
train_hallucidet.py
File metadata and controls
551 lines (430 loc) · 29.1 KB
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import sys
sys.path.append("./src/")
sys.path.append("./src/segmentation_models/")
import os
from src.config.config import Config
import torch
Config.set_environment()
import pytorch_lightning as pl
from pytorch_lightning import seed_everything
from src.utils.utils import Utils
import wandb
import torch.nn as nn
from src.metrics import metrics
from src.losses import losses
import numpy as np
from src.dataloader.dataloaderPL import MultiModalDataModule
import torchvision
from src.models.detector import Detector
from train_detector import DetectorLit
import albumentations as alb
import albumentations.pytorch
from src.models.encoder_decoder import EncoderDecoder
# True = Speed-up but not deterministic
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
args = Config.argument_parser()
seed_everything(args.seed)
dataset = args.dataset if args.dataset is not None else Config.Dataset.dataset
Config.set_dataset_path(dataset)
detector = args.detector if args.detector is not None else Config.Detector.name
Config.set_detector(detector, train_det=False, pretrained=args.directly_coco)
Config.set_loss_weights(args)
ext = args.ext if args.ext is not None else Config.Dataset.ext
wandb_logger = wandb.init(project=args.wandb_project, name=args.wandb_name)
fuse_data = args.fuse_data
decoder_backbone = args.decoder_backbone
ext = args.ext if args.ext is not None else Config.Dataset.ext
pre_train_path = None
if Config.EncoderDecoder.load_encoder_decoder:
pre_train_path = args.pre_train_path if args.pre_train_path is not None else Config.EncoderDecoder.encoder_decoder_load_path
LR = 0.0001 if args.lr is None else args.lr
class EncoderDecoderLit(pl.LightningModule):
def __init__(self, batch_size=4,
wandb_logger=None, model_name='resnet34',
in_channels=1, output_channels=3, lr=0.0001,
loss_pixel='mse', loss_perceptual='lpips_alexnet',
detector_name='fasterrcnn', train_det=False, fuse_data='none', scheduler_on=False):
super().__init__()
self.model_name = model_name
self.wandb_logger = wandb_logger
self.in_channels = in_channels
self.output_channels = output_channels
self.lr = lr
self.batch_size = batch_size
self.train_det = train_det
self.fuse_data = fuse_data
self.optimizer_name = Config.Optimizer.name
self.segmentation_head = Config.EncoderDecoder.decoder_head
self.scheduler_on = scheduler_on
## EncoderDecoder
self.encoder_decoder = EncoderDecoder(name=self.model_name,
encoder_depth=args.encoder_depth,
encoder_weights='imagenet',
decoder_attention_type=None,
in_channels=self.in_channels,
output_channels=self.output_channels,
segmentation_head=Config.EncoderDecoder.decoder_head,
).encoder_decoder
## Detector
self.detector_name = detector_name
# Model
detectorLit = DetectorLit(batch_size=self.batch_size,
wandb_logger=self.wandb_logger,
lr=LR, detector_name=self.detector_name,
pretrained=True, optimizer_name=self.optimizer_name,
modality=args.modality,
directly_coco=args.directly_coco)
self.detector = detectorLit.detector
if not Config.Detector.train_det:
self.detector = self.detector.eval()
for param in self.detector.parameters():
param.requires_grad = False
self.detector = detectorLit.load_from_checkpoint(checkpoint_path=args.detector_path,
batch_size=args.batch,
wandb_logger=wandb_logger,
lr=LR,
detector_name=detector_name,
pretrained=True,
optimizer_name=self.optimizer_name,
modality=args.modality,
).detector
self.loss_pixel = losses.Reconstruction.select_loss_pixel(loss_pixel=loss_pixel)
self.loss_perceptual = losses.Reconstruction.select_loss_perceptual(loss_perceptual=loss_perceptual)
## Metrics
self.train_metrics_detection_map_hall = metrics.Detection().map
self.valid_metrics_detection_map_hall = metrics.Detection().map
self.test_metrics_detection_map_hall = metrics.Detection().map
self.train_metrics_detection_map_rgb = metrics.Detection().map
self.valid_metrics_detection_map_rgb = metrics.Detection().map
self.test_metrics_detection_map_rgb = metrics.Detection().map
self.train_metrics_detection_map_ir = metrics.Detection().map
self.valid_metrics_detection_map_ir = metrics.Detection().map
self.test_metrics_detection_map_ir = metrics.Detection().map
self.train_epoch = 0
self.valid_epoch = 0
self.train_media_step = 0
self.valid_media_step = 0
self.train_loss_step = 0
self.valid_loss_step = 0
self.best_valid_map_50 = 0.0
self.best_valid_epoch = 0
self.wandb_logger.define_metric("train/loss/step")
self.wandb_logger.define_metric("train/loss/*", step_metric="train/loss/step")
self.wandb_logger.define_metric("train/media/step")
self.wandb_logger.define_metric("train/media/*", step_metric="train/media/step")
self.wandb_logger.define_metric("valid/loss/step")
self.wandb_logger.define_metric("valid/loss/*", step_metric="valid/loss/step")
self.wandb_logger.define_metric("valid/media/step")
self.wandb_logger.define_metric("valid/media/*", step_metric="valid/media/step")
self.wandb_logger.define_metric("valid/metrics/step")
self.wandb_logger.define_metric("valid/metrics/*", step_metric="valid/metrics/step")
def forward_step(self, imgs_rgb, targets_rgb, imgs_ir, targets_ir, batch_idx, step='train'):
imgs_ir = Utils.batch_images_for_encoder_decoder(imgs=imgs_ir, device=device, ablation_flag=args.ablation_flag)
imgs_rgb = Utils.batch_images_for_encoder_decoder(imgs=imgs_rgb, device=device, ablation_flag=args.ablation_flag)
targets_rgb = Utils.batch_targets_for_detector(targets=targets_rgb, device=device, detector_name=self.detector_name)
targets_ir = Utils.batch_targets_for_detector(targets=targets_ir, device=device, detector_name=self.detector_name)
## Encoder / Decoder
imgs_ir_three_channel = Utils.expand_one_channel_to_output_channels(imgs_ir, self.output_channels)
imgs_hallucinated = self.encoder_decoder(imgs_ir_three_channel)
loss_pixel_rgb = 0.0 if self.loss_pixel == None else self.loss_pixel(imgs_rgb, imgs_hallucinated) * Config.Losses.hparams_losses_weights['pixel_rgb']
loss_perceptual_rgb = 0.0 if self.loss_perceptual == None else torch.mean(self.loss_perceptual(imgs_rgb, imgs_hallucinated)) * Config.Losses.hparams_losses_weights['perceptual_rgb']
loss_pixel_ir = 0.0 if self.loss_pixel == None else self.loss_pixel(imgs_ir_three_channel, imgs_hallucinated) * Config.Losses.hparams_losses_weights['pixel_ir']
loss_perceptual_ir = 0.0 if self.loss_perceptual == None else torch.mean(self.loss_perceptual(imgs_ir_three_channel, imgs_hallucinated)) * Config.Losses.hparams_losses_weights['perceptual_ir']
## Detector Hallucinated
train_det = True if (self.train_det == True and step == 'train') else False
losses_det, detections_hall = Detector.calculate_loss(self.detector, imgs_hallucinated, targets_ir, train_det=train_det, model_name=self.detector_name)
## Detector RGB
_, detections_rgb = Detector.calculate_loss(self.detector, imgs_rgb, targets_rgb, train_det=False, model_name=self.detector_name)
## Detector IR
_, detections_ir = Detector.calculate_loss(self.detector, imgs_ir_three_channel, targets_ir, train_det=False, model_name=self.detector_name)
if 'fasterrcnn' in self.detector_name:
losses_det['classification'] = losses_det['loss_classifier']
losses_det['bbox_regression'] = losses_det['loss_box_reg']
losses_det['bbox_regression'] = losses_det['bbox_regression'] * Config.Losses.hparams_losses_weights['det_regression']
losses_det['classification'] = losses_det['classification'] * Config.Losses.hparams_losses_weights['det_classification']
losses_det['loss_objectness'] = (losses_det['loss_objectness'] * Config.Losses.hparams_losses_weights['det_objectness']
if 'fasterrcnn' in self.detector_name else 0.0)
losses_det['loss_rpn_box_reg'] = (losses_det['loss_rpn_box_reg'] * Config.Losses.hparams_losses_weights['det_rpn_box_reg']
if 'fasterrcnn' in self.detector_name else 0.0)
losses_det['bbox_ctrness'] = (losses_det['bbox_ctrness'] * Config.Losses.hparams_losses_weights['det_bbox_ctrness']
if 'fcos' in self.detector_name else 0.0)
loss_det_total = losses_det['bbox_regression'] + losses_det['classification'] + \
losses_det['loss_objectness'] + \
losses_det['loss_rpn_box_reg'] + losses_det['bbox_ctrness']
## Total Loss
total_loss = loss_det_total + loss_pixel_rgb + loss_perceptual_rgb + loss_pixel_ir + loss_perceptual_ir
if step == 'val':
self.valid_metrics_detection_map_rgb.update(detections_rgb, targets_rgb)
self.valid_metrics_detection_map_hall.update(detections_hall, targets_ir)
self.valid_metrics_detection_map_ir.update(detections_ir, targets_ir)
# Normalize for plotting
imgs_hallucinated = Utils.normalize_batch_images(imgs_hallucinated.detach().clone())
return {
'loss' : {'total': total_loss,
'pixel_rgb': loss_pixel_rgb,
'perceptual_rgb': loss_perceptual_rgb,
'pixel_ir': loss_pixel_ir,
'perceptual_ir': loss_perceptual_ir,
'det_regression': losses_det['bbox_regression'],
'det_classification': losses_det['classification'],
'det_objectness': losses_det['loss_objectness'],
'det_rpn_box_reg': losses_det['loss_rpn_box_reg'],
'det_bbox_ctrness': losses_det['bbox_ctrness'],
'det_total': loss_det_total,
},
'output': { #'det_hal': output_hal_det,
# 'det_rgb': output_rgb_det,
# 'det_ir': output_ir_det,
'imgs_rgb': imgs_rgb,
'imgs_ir': imgs_ir,
'imgs_hallucinated': imgs_hallucinated,
}
}
def training_step(self, train_batch, batch_idx):
imgs_rgb, targets_rgb, imgs_ir, targets_ir = train_batch
forward_return = self.forward_step(imgs_rgb, targets_rgb, imgs_ir, targets_ir, batch_idx, step='train')
self.log('train_loss', forward_return['loss']['total'], on_step=True, on_epoch=True, prog_bar=True, logger=True, batch_size=self.batch_size)
self.wandb_logger.log({ 'train/loss/pixel_rgb': forward_return['loss']['pixel_rgb'],
'train/loss/perceptual_rgb': forward_return['loss']['perceptual_rgb'],
'train/loss/pixel_ir': forward_return['loss']['pixel_ir'],
'train/loss/perceptual_ir': forward_return['loss']['perceptual_ir'],
'train/loss/det_reg': forward_return['loss']['det_regression'],
'train/loss/det_class': forward_return['loss']['det_classification'],
'train/loss/det_objectness': forward_return['loss']['det_objectness'],
'train/loss/det_rpn_box_reg': forward_return['loss']['det_rpn_box_reg'],
'train/loss/det_bbox_ctrness': forward_return['loss']['det_bbox_ctrness'],
'train/loss/det_total': forward_return['loss']['det_total'],
'train/loss/total': forward_return['loss']['total'],
'train/loss/step': self.train_loss_step,
})
self.train_loss_step = self.train_loss_step + 1
# if((batch_idx % 100) == 1):
# self.wandb_logger.log({"train/media/input_ir": [wandb.Image(forward_return['output']['imgs_ir'], caption="train/input_ir")],
# "train/media/input_rgb": [wandb.Image(forward_return['output']['imgs_rgb'], caption="train/input_rgb")],
# "train/media/output_hal": [wandb.Image(forward_return['output']['imgs_hallucinated'], caption="train/output")],
# "train/media/output_hal_det": [wandb.Image(forward_return['output']['det_hal'], caption="train/output_hal_det")],
# "train/media/output_rgb_det": [wandb.Image(forward_return['output']['det_rgb'], caption="train/output_rgb_det")],
# "train/media/output_ir_det": [wandb.Image(forward_return['output']['det_ir'], caption="train/output_ir_det")],
# "train/media/input_ir_samples": [wandb.Image(im) for im in forward_return['output']['imgs_ir']],
# "train/media/input_rgb_samples": [wandb.Image(im) for im in forward_return['output']['imgs_rgb']],
# "train/media/output_hal_samples": [wandb.Image(im) for im in forward_return['output']['imgs_hallucinated']],
# "train/media/output_hal_det_samples": [wandb.Image(im) for im in forward_return['output']['det_hal']],
# "train/media/output_rgb_det_samples": [wandb.Image(im) for im in forward_return['output']['det_rgb']],
# "train/media/output_ir_det_samples": [wandb.Image(im) for im in forward_return['output']['det_ir']],
# "train/media/step" : self.train_media_step,
# })
# self.train_media_step = self.train_media_step + 1
return forward_return['loss']['total']
def validation_step(self, val_batch, batch_idx):
imgs_rgb, targets_rgb, imgs_ir, targets_ir = val_batch
forward_return = self.forward_step(imgs_rgb, targets_rgb, imgs_ir, targets_ir, batch_idx, step='val')
self.log('val_loss', forward_return['loss']['total'], on_step=True, on_epoch=True, prog_bar=True, logger=True, batch_size=self.batch_size)
self.wandb_logger.log({ 'valid/loss/pixel_rgb': forward_return['loss']['pixel_rgb'],
'valid/loss/perceptual_rgb': forward_return['loss']['perceptual_rgb'],
'valid/loss/pixel_ir': forward_return['loss']['pixel_ir'],
'valid/loss/perceptual_ir': forward_return['loss']['perceptual_ir'],
'valid/loss/det_reg': forward_return['loss']['det_regression'],
'valid/loss/det_class': forward_return['loss']['det_classification'],
'valid/loss/det_objectness': forward_return['loss']['det_objectness'],
'valid/loss/det_rpn_box_reg': forward_return['loss']['det_rpn_box_reg'],
'valid/loss/det_bbox_ctrness': forward_return['loss']['det_bbox_ctrness'],
'valid/loss/det_total': forward_return['loss']['det_total'],
'valid/loss/total': forward_return['loss']['total'],
'valid/loss/step': self.valid_loss_step,
})
self.valid_loss_step = self.valid_loss_step + 1
# if((batch_idx % 100) == 1):
# self.wandb_logger.log({"valid/media/input_ir": [wandb.Image(forward_return['output']['imgs_ir'], caption="valid/input_ir")],
# "valid/media/input_rgb": [wandb.Image(forward_return['output']['imgs_rgb'], caption="valid/input_rgb")],
# "valid/media/output_hal": [wandb.Image(forward_return['output']['imgs_hallucinated'], caption="valid/output_hal")],
# "valid/media/output_hal_det": [wandb.Image(forward_return['output']['det_hal'], caption="valid/output_hal_det")],
# "valid/media/output_rgb_det": [wandb.Image(forward_return['output']['det_rgb'], caption="valid/output_rgb_det")],
# "valid/media/output_ir_det": [wandb.Image(forward_return['output']['det_ir'], caption="valid/output_ir_det")],
# "valid/media/input_ir_samples": [wandb.Image(im) for im in forward_return['output']['imgs_ir']],
# "valid/media/input_rgb_samples": [wandb.Image(im) for im in forward_return['output']['imgs_rgb']],
# "valid/media/output_hal_samples": [wandb.Image(im) for im in forward_return['output']['imgs_hallucinated']],
# "valid/media/output_hal_det_samples": [wandb.Image(im) for im in forward_return['output']['det_hal']],
# "valid/media/output_rgb_det_samples": [wandb.Image(im) for im in forward_return['output']['det_rgb']],
# "valid/media/output_ir_det_samples": [wandb.Image(im) for im in forward_return['output']['det_ir']],
# "valid/media/step" : self.valid_media_step,
# })
# self.valid_media_step = self.valid_media_step + 1
return forward_return['loss']['total']
def on_validation_epoch_end(self):
map_rgb = Utils.filter_dictionary(self.valid_metrics_detection_map_rgb.compute(), {'map_50', 'map_75', 'map'})
map_hall = Utils.filter_dictionary(self.valid_metrics_detection_map_hall.compute(), {'map_50', 'map_75', 'map'})
map_ir = Utils.filter_dictionary(self.valid_metrics_detection_map_ir.compute(), {'map_50', 'map_75', 'map'})
self.wandb_logger.log({
'valid/metrics/map_rgb': map_rgb,
'valid/metrics/map_hall': map_hall,
'valid/metrics/map_ir': map_ir,
'valid/metrics/step': self.valid_epoch,
})
if(self.best_valid_map_50 < map_hall['map_50'] and self.current_epoch > 0):
self.best_valid_map_50 = map_hall['map_50']
self.best_valid_epoch = self.current_epoch
self.wandb_logger.summary["valid/metrics/map_rgb"] = map_rgb
self.wandb_logger.summary["valid/metrics/map_hall"] = map_hall
self.wandb_logger.summary["valid/metrics/map_ir"] = map_ir
self.wandb_logger.summary["valid/metrics/best_epoch"] = self.best_valid_epoch
self.wandb_logger.summary["checkpoint_dirpath"] = self.trainer.checkpoint_callback.dirpath
ckpt_path = os.path.join(
self.trainer.checkpoint_callback.dirpath, 'best_encoder_decoder_pl.ckpt'
)
self.trainer.save_checkpoint(ckpt_path)
self.log('val_map', map_hall['map'], on_step=False, on_epoch=True, prog_bar=False, logger=True, batch_size=self.batch_size)
self.valid_metrics_detection_map_rgb.reset()
self.valid_metrics_detection_map_hall.reset()
self.valid_metrics_detection_map_ir.reset()
self.valid_epoch += 1
def on_train_epoch_end(self):
self.train_epoch += 1
def test_step(self, test_batch, batch_idx):
imgs_rgb, targets_rgb, imgs_ir, targets_ir = test_batch
imgs_ir = Utils.batch_images_for_encoder_decoder(imgs=imgs_ir, device=device, ablation_flag=args.ablation_flag)
imgs_rgb = Utils.batch_images_for_encoder_decoder(imgs=imgs_rgb, device=device, ablation_flag=args.ablation_flag)
targets_rgb = Utils.batch_targets_for_detector(targets=targets_rgb, device=device, detector_name=self.detector_name)
targets_ir = Utils.batch_targets_for_detector(targets=targets_ir, device=device, detector_name=self.detector_name)
imgs_ir_three_channel = Utils.expand_one_channel_to_output_channels(imgs_ir, self.output_channels)
imgs_hallucinated = self.encoder_decoder(imgs_ir_three_channel)
imgs_rgb = imgs_rgb.float() # To handle problems with double to float conversion
_, detections_hall = Detector.calculate_loss(self.detector, imgs_hallucinated, targets_ir, train_det=False, model_name=self.detector_name)
# output_hal_det = torch.Tensor(np.asarray([Utils().plot_each_image(imgs_hallucinated[idx], det, targets_ir[idx], threshold=args.threshold)
# for idx, det in enumerate(detections_hall)]))
## Detector RGB
_, detections_rgb = Detector.calculate_loss(self.detector, imgs_rgb, targets_rgb, train_det=False, model_name=self.detector_name)
# output_rgb_det = torch.Tensor(np.asarray([Utils().plot_each_image(imgs_rgb[idx], det, targets_rgb[idx], threshold=args.threshold)
# for idx, det in enumerate(detections_rgb)]))
## Detector IR
_, detections_ir = Detector.calculate_loss(self.detector, imgs_ir_three_channel, targets_ir, train_det=False, model_name=self.detector_name)
# output_ir_det = torch.Tensor(np.asarray([Utils().plot_each_image(imgs_ir_three_channel[idx], det, targets_ir[idx], threshold=args.threshold)
# for idx, det in enumerate(detections_ir)]))
self.test_metrics_detection_map_rgb.update(detections_rgb, targets_rgb)
self.test_metrics_detection_map_hall.update(detections_hall, targets_ir)
self.test_metrics_detection_map_ir.update(detections_ir, targets_ir)
# if((batch_idx % 100) == 1):
# self.wandb_logger.log({"test/media/input_ir": [wandb.Image(imgs_ir, caption="test/input_ir")],
# "test/media/input_rgb": [wandb.Image(imgs_rgb, caption="test/input_rgb")],
# "test/media/output": [wandb.Image(outs, caption="test/output")],
# "test/media/input_ir_samples": [wandb.Image(im) for im in imgs_ir],
# "test/media/input_rgb_samples": [wandb.Image(im) for im in imgs_rgb],
# "test/media/output_samples": [wandb.Image(im) for im in outs],
# })
def on_test_epoch_end(self):
map_rgb = Utils.filter_dictionary(self.test_metrics_detection_map_rgb.compute(), {'map_50', 'map_75', 'map'})
map_hall = Utils.filter_dictionary(self.test_metrics_detection_map_hall.compute(), {'map_50', 'map_75', 'map'})
map_ir = Utils.filter_dictionary(self.test_metrics_detection_map_ir.compute(), {'map_50', 'map_75', 'map'})
self.wandb_logger.summary["test/metrics/map_rgb"] = map_rgb
self.wandb_logger.summary["test/metrics/map_hall"] = map_hall
self.wandb_logger.summary["test/metrics/map_ir"] = map_ir
self.wandb_logger.log({
'test/metrics/map_rgb': map_rgb,
'test/metrics/map_hall': map_hall,
'test/metrics/map_ir': map_ir,
})
def configure_optimizers(self):
optimizer = Config().config_optimizer(optimizer=self.optimizer_name,
params=(list(self.encoder_decoder.parameters()) + list(self.detector.parameters())
if self.train_det else self.encoder_decoder.parameters()),
#list(self.detector.parameters()),
lr=self.lr)
sch = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min')
return {
"optimizer": optimizer,
"lr_scheduler" : {
"scheduler" : sch,
"monitor" : "val_loss",
}
}
# Set device
device = Config.cuda_or_cpu() if args.device is None else args.device
# Model
model = EncoderDecoderLit(batch_size=args.batch,
wandb_logger=wandb_logger,
model_name=decoder_backbone,
in_channels=Config.EncoderDecoder.in_channels_encoder,
output_channels=Config.EncoderDecoder.out_channels_decoder,
lr=LR,
loss_pixel=Config.Losses.pixel,
loss_perceptual=Config.Losses.perceptual,
detector_name=Config.Detector.name,
train_det=Config.Detector.train_det,
fuse_data=fuse_data,
scheduler_on=Config.Optimizer.scheduler_on,
)
if(Config.EncoderDecoder.load_encoder_decoder):
model = EncoderDecoderLit.load_from_checkpoint(checkpoint_path=Config.EncoderDecoder.encoder_decoder_load_path,
batch_size=args.batch,
wandb_logger=wandb_logger,
model_name=decoder_backbone,
in_channels=Config.EncoderDecoder.in_channels_encoder,
output_channels=Config.EncoderDecoder.out_channels_decoder,
lr=LR,
loss_pixel=Config.Losses.pixel,
loss_perceptual=Config.Losses.perceptual,
detector_name=Config.Detector.name,
train_det=Config.Detector.train_det,
fuse_data=fuse_data,
scheduler_on=Config.Optimizer.scheduler_on,
strict=False
)
# saves best model
checkpoint_best_callback = pl.callbacks.ModelCheckpoint(
save_top_k=1,
monitor="val_map",
mode="max",
dirpath=os.path.join('lightning_logs', args.wandb_project, args.wandb_name, "_".join([args.dataset, args.modality, Config.Detector.name])),
filename="best",
)
# Training
trainer = pl.Trainer(
gpus=Config.Environment.N_GPUS,
accelerator="gpu",
max_epochs=args.epochs,
gradient_clip_val=Config.Optimizer.gradient_clip_val,
gradient_clip_algorithm="value",
callbacks=[
pl.callbacks.RichProgressBar(),
checkpoint_best_callback,
],
deterministic=False,
limit_train_batches=args.limit_train_batches,
num_sanity_val_steps=0,
precision=args.precision, # 32 default
enable_model_summary=True,
logger=False,
)
# Fixed transformations
fixed_transformations = alb.Compose(
[
alb.pytorch.ToTensorV2(),
]
)
# data augmentation
data_augmentation = alb.Compose(
[fixed_transformations],
bbox_params=alb.BboxParams(format='pascal_voc', label_fields=['labels']),
additional_targets={'image1': 'image', 'bboxes1': 'bboxes', 'labels1': 'labels'}, p=1.0
)
dm = MultiModalDataModule(
dataset=dataset,
path_images_train_rgb=Config.Dataset.train_path,
path_images_train_ir=Config.Dataset.train_path,
path_images_test_rgb=Config.Dataset.test_path,
path_images_test_ir=Config.Dataset.test_path,
batch_size=args.batch,
num_workers=args.num_workers,
ext=ext,
seed=args.seed,
split_ratio_train_valid=Config.Dataset.train_valid_split,
data_augmentation=data_augmentation,
fixed_transformations=fixed_transformations,
ablation_flag=args.ablation_flag,
)
trainer.fit(model, dm)
trainer.save_checkpoint(os.path.join(trainer.checkpoint_callback.dirpath,
'encoder_decoder_pl.ckpt'))
trainer.test(model, dm, ckpt_path="best")
wandb_logger.summary["checkpoint_dirpath"] = trainer.checkpoint_callback.dirpath
wandb_logger.finish()