-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
436 lines (322 loc) · 19.6 KB
/
main.py
File metadata and controls
436 lines (322 loc) · 19.6 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
import os
from src.engine.config.config import Config
import torch
Config.set_environment()
import pytorch_lightning as pl
from pytorch_lightning import seed_everything
from src.engine.utils.utils import Utils
from src.engine.logger.wandb import WandbLogger
from src.engine.losses import losses
from src.engine.metrics import metrics
from src.engine.dataloader.dataloaderPL import MultiModalDataModule
import albumentations as alb
import albumentations.pytorch
from src.modtr import ModTr
from src.translator import Translator
from src.detector import Detector
import numpy as np
# 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 = WandbLogger(args)
args.modtr_backbone = args.decoder_backbone if args.decoder_backbone is not None else Config.EncoderDecoder.decoder_backbone
args.modtr_encoder_depth = args.decoder_encoder_depth if args.decoder_encoder_depth is not None else Config.EncoderDecoder.decoder_encoder_depth
args.modtr_encoder_weights = args.decoder_encoder_weights if args.decoder_encoder_weights is not None else Config.EncoderDecoder.decoder_encoder_weights
args.modtr_in_channels = args.decoder_in_channels if args.decoder_in_channels is not None else Config.EncoderDecoder.decoder_in_channels
args.modtr_out_channels = args.decoder_out_channels if args.decoder_out_channels is not None else Config.EncoderDecoder.decoder_out_channels
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='ssd', train_det=False, fuse_data='none', scheduler_on=False, cosine_t_max=1):
super().__init__()
# Exports the hyperparameters to a YAML file, and create "self.hparams" namespace
# self.save_hyperparameters()
self.model_name = model_name
args.modtr_backbone = self.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
self.cosine_t_max = cosine_t_max
self.modtr = ModTr(args=args,
translator=Translator(args=args,
translator=None),
detector=Detector(detector=None,
detector_name=detector_name,
frozen=True,
pretrained=True))
## Detector
self.detector_name = detector_name
self.detector = self.modtr.detector.detector
self.encoder_decoder = self.modtr.translator
self.loss_pixel = losses.Reconstruction.select_loss_pixel(loss_pixel=loss_pixel)
## Metrics
self.train_metrics_detection_map_modtr = metrics.Detection().map
self.valid_metrics_detection_map_modtr = metrics.Detection().map
self.test_metrics_detection_map_modtr = 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.valid_epoch = 0
self.best_valid_map_50 = 0.0
self.best_valid_epoch = 0
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_translated = self.encoder_decoder(imgs_ir_three_channel)
imgs_translated = Utils.fusion_data(imgs_translated, imgs_ir_three_channel, fuse_data=self.fuse_data)
loss_pixel_rgb = 0.0 if self.loss_pixel == None else self.loss_pixel(imgs_rgb, imgs_translated) * Config.Losses.hparams_losses_weights['pixel_rgb']
loss_pixel_ir = 0.0 if self.loss_pixel == None else self.loss_pixel(imgs_ir_three_channel, imgs_translated) * Config.Losses.hparams_losses_weights['pixel_ir']
## Detector Translated
train_det = True if (self.train_det == True and step == 'train') else False
losses_det, detections_trans = Detector.calculate_torchvision_detectors_loss(self.detector, imgs_translated, targets_ir, train_det=train_det, model_name=self.detector_name)
## Detector RGB
_, detections_rgb = Detector.calculate_torchvision_detectors_loss(self.detector, imgs_rgb, targets_rgb, train_det=train_det, model_name=self.detector_name)
## Detector IR
_, detections_ir = Detector.calculate_torchvision_detectors_loss(self.detector, imgs_ir_three_channel, targets_ir, train_det=train_det, 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_ir
if step == 'val':
self.valid_metrics_detection_map_rgb.update(detections_rgb, targets_rgb)
self.valid_metrics_detection_map_modtr.update(detections_trans, targets_ir)
self.valid_metrics_detection_map_ir.update(detections_ir, targets_ir)
# Normalize for plotting
imgs_translated = Utils.normalize_batch_images(imgs_translated.detach().clone())
return {
'loss' : {'total': total_loss,
'pixel_rgb': loss_pixel_rgb,
'pixel_ir': loss_pixel_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_modtr': output_modtr_det,
# 'det_rgb': output_rgb_det,
# 'det_ir': output_ir_det,
'imgs_rgb': imgs_rgb,
'imgs_ir': imgs_ir,
'imgs_translated': imgs_translated,
}
}
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.training_loss_log(forward_return)
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.valid_loss_log(forward_return)
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_per_class'})
map_modtr = Utils.filter_dictionary(self.valid_metrics_detection_map_modtr.compute(), {'map_50', 'map_75', 'map', 'map_per_class'})
map_ir = Utils.filter_dictionary(self.valid_metrics_detection_map_ir.compute(), {'map_50', 'map_75', 'map', 'map_per_class'})
self.wandb_logger.valid_metrics_log(map_rgb, map_modtr, map_ir, self.valid_epoch)
if(self.best_valid_map_50 < map_modtr['map_50'] and self.current_epoch > 0):
self.best_valid_map_50 = map_modtr['map_50']
self.best_valid_epoch = self.current_epoch
self.wandb_logger.valid_summary_update(map_rgb, map_modtr, map_ir,
self.best_valid_epoch,
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_50', map_modtr['map_50'], 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_modtr.reset()
self.valid_metrics_detection_map_ir.reset()
self.valid_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_translated = self.encoder_decoder(imgs_ir_three_channel)
imgs_translated = Utils.fusion_data(imgs_translated, imgs_ir_three_channel, fuse_data=self.fuse_data)
imgs_rgb = imgs_rgb.float()
_, detections_trans = Detector.calculate_torchvision_detectors_loss(self.detector, imgs_translated, targets_ir, train_det=False, model_name=self.detector_name)
## Detector RGB
_, detections_rgb = Detector.calculate_torchvision_detectors_loss(self.detector, imgs_rgb, targets_rgb, train_det=False, model_name=self.detector_name)
## Detector IR
_, detections_ir = Detector.calculate_torchvision_detectors_loss(self.detector, imgs_ir_three_channel, targets_ir, train_det=False, model_name=self.detector_name)
self.test_metrics_detection_map_rgb.update(detections_rgb, targets_rgb)
self.test_metrics_detection_map_modtr.update(detections_trans, targets_ir)
self.test_metrics_detection_map_ir.update(detections_ir, targets_ir)
def on_test_epoch_end(self):
map_rgb = Utils.filter_dictionary(self.test_metrics_detection_map_rgb.compute(), {'map_50', 'map_75', 'map', 'map_per_class'})
map_modtr = Utils.filter_dictionary(self.test_metrics_detection_map_modtr.compute(), {'map_50', 'map_75', 'map', 'map_per_class'})
map_ir = Utils.filter_dictionary(self.test_metrics_detection_map_ir.compute(), {'map_50', 'map_75', 'map', 'map_per_class'})
self.wandb_logger.test_summary_update(map_rgb, map_modtr, map_ir)
def configure_optimizers(self):
optimizer = Config().config_optimizer(optimizer=self.optimizer_name,
params=(list(self.encoder_decoder.parameters())),
lr=self.lr)
sch = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer,
T_max=self.cosine_t_max)
return {
"optimizer": optimizer,
"lr_scheduler" : {
"scheduler" : sch,
"monitor" : "train_loss",
"interval": "step",
"frequency": 1,
}
}
# Set device
device = Config.cuda_or_cpu() if args.device is None else args.device
# 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,
)
# Model
model = EncoderDecoderLit(batch_size=args.batch,
wandb_logger=wandb_logger,
model_name=args.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=args.fuse_data,
scheduler_on=Config.Optimizer.scheduler_on,
cosine_t_max=args.epochs * len(dm.train_dataloader()),
)
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=args.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=args.fuse_data,
scheduler_on=Config.Optimizer.scheduler_on,
strict=False,
cosine_t_max=args.epochs * len(dm.train_dataloader()),
)
# saves best model
checkpoint_best_callback = pl.callbacks.ModelCheckpoint(
save_top_k=1,
monitor="val_map_50",
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,
callbacks=[
pl.callbacks.RichProgressBar(),
pl.callbacks.EarlyStopping(monitor="val_map_50", mode="max", patience=5),
checkpoint_best_callback,
],
accumulate_grad_batches=2,
limit_train_batches=args.limit_train_batches,
num_sanity_val_steps=0, # debug
enable_model_summary=True,
logger=False,
)
trainer.fit(model, dm)
trainer.save_checkpoint(os.path.join(trainer.checkpoint_callback.dirpath,
'encoder_decoder_pl.ckpt'))
torch.save(model.detector.state_dict(),
os.path.join(trainer.checkpoint_callback.dirpath, 'detector.bin')
)
torch.save(model.encoder_decoder.state_dict(),
os.path.join(trainer.checkpoint_callback.dirpath, 'encoder_decoder.bin')
)
trainer.test(model, dm, ckpt_path="best")
wandb_logger.finish(dirpath=trainer.checkpoint_callback.dirpath)