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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
552
553
# -*- coding: utf-8 -*-
import os
import time
from typing import Union

import torch
from lib.metrics import bin_calculate_auc_ap_ar, get_acc_mesure_func
from logs.logger import board_writing
from numpy import arange
from package_utils.utils import debugging_panel
from tqdm import tqdm


class AverageMeter(object):
    """Computes and stores the average and current value"""

    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count if self.count != 0 else 0


def get_batch_data(batch_data: Union[dict]):
    """
    Parsing data for model consumption (train/val/test)
    """
    inputs = batch_data["img"] if isinstance(batch_data, dict) else batch_data[0]
    labels = batch_data["label"] if isinstance(batch_data, dict) else batch_data[1]
    heatmaps = None
    cstency_heatmaps = None
    offsets = None
    targets = None
    temp_locs = None
    maskout_pes = None

    if "heatmap" in batch_data:
        heatmaps = batch_data["heatmap"]

    if "target" in batch_data:
        targets = batch_data["target"]

    if "cstency" in batch_data:
        cstency_heatmaps = batch_data["cstency"]

    if "offset" in batch_data:
        offsets = batch_data["offset"]

    if "temp_loc" in batch_data:
        temp_locs = batch_data["temp_loc"]

    if "mask_out_p" in batch_data:
        maskout_pes = batch_data["mask_out_p"]

    return (
        inputs,
        labels,
        targets,
        heatmaps,
        cstency_heatmaps,
        offsets,
        temp_locs,
        maskout_pes,
    )


def train(
    cfg,
    model,
    critetion,
    optimizer,
    epoch,
    data_loader,
    logger,
    writer,
    devices,
    trainIters,
    metrics_base="combine",
    scaler=None,
):
    calculate_acc = get_acc_mesure_func(metrics_base)
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    acc = AverageMeter()

    # Switch to train mode
    model.train()
    data_loader = tqdm(data_loader, dynamic_ncols=True)
    accumulation_steps = cfg.TRAIN.accumulation_steps if cfg.TRAIN.use_amp else 1
    start = time.time()
    optimizer.zero_grad()
    for i, batch_data in enumerate(data_loader):
        (
            inputs,
            labels,
            targets,
            heatmaps,
            cstency_heatmaps,
            offsets,
            temp_locs,
            maskout_pes,
        ) = get_batch_data(batch_data)
        inputs = inputs.cuda().to(non_blocking=True, dtype=torch.float)
        labels = labels.cuda().to(non_blocking=True, dtype=torch.float)
        maskout_pes = (
            maskout_pes.cuda().to(non_blocking=True)
            if maskout_pes is not None
            else None
        )
        # additional_targets = {"maskout_pes": maskout_pes}

        # Measuring data loading time
        data_time.update(time.time() - start)
        loop = arange(1) if cfg.TRAIN.optimizer != "SAM" else arange(2)

        for idx in loop:
            with torch.cuda.amp.autocast(enabled=cfg.TRAIN.use_amp):
                # outputs = model(inputs, **additional_targets)
                outputs = model(inputs)
                if isinstance(outputs, list):
                    outputs = outputs[0]

                # In case outputs contain a dict key
                if isinstance(outputs, dict):
                    outputs_cls = outputs["cls"]
                    outputs_hm = outputs["hm"] if "hm" in outputs.keys() else None
                    outputs_offset = (
                        outputs["offset"] if "offset" in outputs.keys() else None
                    )
                    outputs_cstency = (
                        outputs["cstency"] if "cstency" in outputs.keys() else None
                    )
                    outputs_temp_loc = (
                        outputs["temp_loc"] if "temp_loc" in outputs.keys() else None
                    )

                    if idx == 0:
                        first_outputs_hm = outputs_hm
                        first_outputs_cls = outputs_cls

                if "Combined" in cfg.TRAIN.loss.type:
                    # labels = labels.cuda().to(non_blocking=True).long()

                    if offsets is not None:
                        offsets = offsets.cuda().to(non_blocking=True)

                    if cstency_heatmaps is not None:
                        cstency_heatmaps = cstency_heatmaps.cuda().to(non_blocking=True)

                    if temp_locs is not None:
                        temp_locs = temp_locs.cuda().to(non_blocking=True)

                    if cfg.TRAIN.loss.type != "CombinedHeatmapBinaryLoss":
                        heatmaps = heatmaps.cuda().to(non_blocking=True)
                    else:
                        heatmaps = targets.cuda().to(non_blocking=True)

                    loss_ = critetion(
                        outputs_hm,
                        heatmaps,
                        outputs_cls,
                        labels,
                        offset_preds=outputs_offset,
                        offset_gts=offsets,
                        cstency_preds=outputs_cstency,
                        cstency_gts=cstency_heatmaps,
                        temp_loc_preds=outputs_temp_loc,
                        temp_loc_gts=temp_locs,
                        hm_mask=maskout_pes,
                    )
                    loss = loss_["hm"]
                    if "cls" in loss_.keys():
                        loss += loss_["cls"]
                    if "dst_hm_cls" in loss_.keys():
                        loss += loss_["dst_hm_cls"]
                    if "offset" in loss_.keys():
                        loss += loss_["offset"]
                    if "cstency" in loss_.keys():
                        loss += loss_["cstency"]
                    if "temp_loc" in loss_.keys():
                        loss += loss_["temp_loc"]
                else:
                    loss = critetion(outputs_cls, labels)

            loss /= accumulation_steps

            # gradients accumulation for larger batch
            if cfg.TRAIN.use_amp:
                scaler(
                    cfg,
                    loss,
                    optimizer,
                    parameters=model.parameters(),
                    step=idx,
                    update_grad=(i + 1) % accumulation_steps == 0,
                )
                if (i + 1) % accumulation_steps == 0:
                    optimizer.zero_grad()
            else:
                loss.backward()

                if cfg.TRAIN.optimizer != "SAM":
                    optimizer.step()
                else:
                    if idx == 0:
                        optimizer.first_step(zero_grad=True)
                    else:
                        optimizer.second_step(zero_grad=True)
                optimizer.zero_grad()

        if cfg.TRAIN.use_amp:
            torch.cuda.synchronize()

        if cfg.TRAIN.debug.active:
            debugging_panel(
                cfg.TRAIN.debug,
                inputs,
                heatmaps,
                first_outputs_hm,
                i,
                batch_cls_pred=first_outputs_cls,
            )

        if metrics_base == "binary":
            acc_ = calculate_acc(first_outputs_cls, targets=targets, labels=labels)
        elif metrics_base == "heatmap":
            acc_ = calculate_acc(first_outputs_hm, targets=targets, labels=labels)
        else:
            acc_ = calculate_acc(
                first_outputs_hm,
                first_outputs_cls,
                targets=targets,
                labels=labels,
                cls_lamda=critetion.cls_lmda,
            )

        if isinstance(inputs, list):
            batch_size = inputs[0].size(0)
        else:
            batch_size = inputs.size(0)

        # Measure accuracy and record loss
        losses.update(loss.item() * accumulation_steps, n=batch_size)
        acc.update(acc_, n=batch_size)

        batch_time.update(time.time() - start)
        start = time.time()

        # Logging
        if i % 5 == 0:
            params = {}
            if "Combined" in cfg.TRAIN.loss.type:
                if (
                    hasattr(critetion, "dst_hm_cls_lmda")
                    and critetion.dst_hm_cls_lmda > 0
                ):
                    params["loss_dst"] = loss_["dst_hm_cls"].item()
                if hasattr(critetion, "offset_lmda") and critetion.offset_lmda > 0:
                    params["loss_offset"] = loss_["offset"].item()
                if "cstency" in loss_.keys():
                    params["loss_cstency"] = loss_["cstency"].item()
                if "temp_loc" in loss_.keys():
                    params["loss_temp_loc"] = loss_["temp_loc"].item()
                logger.epochInfor(
                    epoch,
                    i,
                    len(data_loader),
                    batch_time=batch_time,
                    data_time=data_time,
                    losses=losses,
                    acc=acc,
                    speed=batch_size / batch_time.val,
                    loss_cls=loss_["cls"].item(),
                    **params,
                )
            else:
                logger.epochInfor(
                    epoch,
                    i,
                    len(data_loader),
                    batch_time=batch_time,
                    data_time=data_time,
                    losses=losses,
                    acc=acc,
                    speed=batch_size / batch_time.val,
                )

        trainIters += 1
        if cfg.TRAIN.tensorboard:
            board_writing(writer, losses.avg, acc.avg, trainIters, "Train")
    return losses, acc, trainIters


def validate(
    cfg,
    model,
    critetion,
    epoch,
    data_loader,
    logger,
    writer,
    devices,
    valIters,
    metrics_base="combine",
):
    calculate_acc = get_acc_mesure_func(metrics_base)
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    acc = AverageMeter()

    # Switch to test mode
    model.eval()
    data_loader = tqdm(data_loader, dynamic_ncols=True)
    start = time.time()
    with torch.no_grad():
        for i, batch_data in enumerate(data_loader):
            (
                inputs,
                labels,
                targets,
                heatmaps,
                cstency_heatmaps,
                offsets,
                temp_locs,
                maskout_pes,
            ) = get_batch_data(batch_data)
            inputs = inputs.to(devices, non_blocking=True, dtype=torch.float).cuda()
            labels = labels.cuda().to(non_blocking=True, dtype=torch.float)
            maskout_pes = (
                maskout_pes.cuda().to(non_blocking=True)
                if maskout_pes is not None
                else None
            )
            # additional_targets = {"maskout_pes": maskout_pes}

            # Measuring data loading time
            data_time.update(time.time() - start)

            # outputs = model(inputs, **additional_targets)
            outputs = model(inputs)
            if isinstance(outputs, list):
                outputs = outputs[0]

            # In case outputs contain a dict key
            if isinstance(outputs, dict):
                outputs_cls = outputs["cls"]
                outputs_hm = outputs["hm"] if "hm" in outputs.keys() else None
                outputs_offset = (
                    outputs["offset"] if "offset" in outputs.keys() else None
                )
                outputs_cstency = (
                    outputs["cstency"] if "cstency" in outputs.keys() else None
                )
                outputs_temp_loc = (
                    outputs["temp_loc"] if "temp_loc" in outputs.keys() else None
                )

            if "Combined" in cfg.TRAIN.loss.type:
                # labels = labels.cuda().to(non_blocking=True).long()

                if offsets is not None:
                    offsets = offsets.cuda().to(non_blocking=True)

                if cstency_heatmaps is not None:
                    cstency_heatmaps = cstency_heatmaps.cuda().to(non_blocking=True)

                if temp_locs is not None:
                    temp_locs = temp_locs.cuda().to(non_blocking=True)

                if cfg.TRAIN.loss.type != "CombinedHeatmapBinaryLoss":
                    heatmaps = heatmaps.cuda().to(non_blocking=True)
                else:
                    heatmaps = targets.cuda().to(non_blocking=True)

                loss_ = critetion(
                    outputs_hm,
                    heatmaps,
                    outputs_cls,
                    labels,
                    offset_preds=outputs_offset,
                    offset_gts=offsets,
                    cstency_preds=outputs_cstency,
                    cstency_gts=cstency_heatmaps,
                    temp_loc_preds=outputs_temp_loc,
                    temp_loc_gts=temp_locs,
                    hm_mask=maskout_pes,
                )
                loss = loss_["hm"]
                if "cls" in loss_.keys():
                    loss += loss_["cls"]
                if "dst_hm_cls" in loss_.keys():
                    loss += loss_["dst_hm_cls"]
                if "offset" in loss_.keys():
                    loss += loss_["offset"]
                if "cstency" in loss_.keys():
                    loss += loss_["cstency"]
                if "temp_loc" in loss_.keys():
                    loss += loss_["temp_loc"]
            else:
                loss = critetion(outputs_cls, labels)

            if cfg.TRAIN.debug.active:
                debugging_panel(
                    cfg.TRAIN.debug,
                    inputs,
                    heatmaps,
                    outputs_hm,
                    i,
                    batch_cls_pred=outputs_cls,
                    split="val",
                )

            if metrics_base == "binary":
                acc_ = calculate_acc(outputs_cls, targets=targets, labels=labels)
            elif metrics_base == "heatmap":
                acc_ = calculate_acc(outputs_hm, targets=targets, labels=labels)
            else:
                acc_ = calculate_acc(
                    outputs_hm,
                    outputs_cls,
                    targets=targets,
                    labels=labels,
                    cls_lamda=critetion.cls_lmda,
                )

            if isinstance(inputs, list):
                batch_size = inputs[0].size(0)
            else:
                batch_size = inputs.size(0)

            # Measure accuracy and record loss
            losses.update(loss.item(), n=batch_size)
            acc.update(acc_, n=batch_size)

            batch_time.update(time.time() - start)
            start = time.time()

            valIters += 1
            if cfg.TRAIN.tensorboard:
                board_writing(writer, losses.avg, acc.avg, valIters, "Val")

            # Logging
            params = {}
            if "Combined" in cfg.TRAIN.loss.type:
                if (
                    hasattr(critetion, "dst_hm_cls_lmda")
                    and critetion.dst_hm_cls_lmda > 0
                ):
                    params["loss_dst"] = loss_["dst_hm_cls"].item()
                if hasattr(critetion, "offset_lmda") and critetion.offset_lmda > 0:
                    params["loss_offset"] = loss_["offset"].item()
                if "cstency" in loss_.keys():
                    params["loss_cstency"] = loss_["cstency"].item()
                if "temp_loc" in loss_.keys():
                    params["loss_temp_loc"] = loss_["temp_loc"].item()
                logger.epochInfor(
                    epoch,
                    i,
                    len(data_loader),
                    batch_time=batch_time,
                    data_time=data_time,
                    losses=losses,
                    acc=acc,
                    speed=batch_size / batch_time.val,
                    loss_cls=loss_["cls"].item(),
                    **params,
                )
            else:
                logger.epochInfor(
                    epoch,
                    i,
                    len(data_loader),
                    batch_time=batch_time,
                    data_time=data_time,
                    losses=losses,
                    acc=acc,
                    speed=batch_size / batch_time.val,
                )
    return losses, acc, valIters


def test(
    cfg,
    model,
    critetion,
    epoch,
    data_loader,
    logger,
    writer,
    devices,
    valIters,
    metrics_base="combine",
):
    calculate_acc = get_acc_mesure_func(metrics_base)
    total_preds = torch.tensor([]).cuda().to(dtype=torch.float)
    total_labels = torch.tensor([]).cuda().to(dtype=torch.float)

    # Switch to test mode
    model.eval()
    test_dataloader = tqdm(data_loader, dynamic_ncols=True)
    with torch.no_grad():
        for b, (inputs, labels, vid_ids) in enumerate(test_dataloader):
            inputs = inputs.to(dtype=torch.float).cuda()
            labels = labels.to(dtype=torch.float).cuda()

            outputs = model(inputs)
            # Applying Flip test
            if isinstance(outputs, list):
                outputs = outputs[0]

            # In case outputs contain a dict key
            if isinstance(outputs, dict):
                # hm_outputs = outputs['hm'] if 'hm' in outputs.keys() else None
                cls_outputs = outputs["cls"]
                # outputs_temp_loc = outputs['temp_loc'] if 'temp_loc' in outputs.keys() else None

            total_preds = torch.cat((total_preds, cls_outputs), 0)
            total_labels = torch.cat((total_labels, labels), 0)

        acc_ = calculate_acc(
            total_preds, targets=None, labels=total_labels, threshold=cfg.TEST.threshold
        )
        metrics = bin_calculate_auc_ap_ar(
            total_preds,
            total_labels,
            metrics_base=metrics_base,
            threshold=cfg.TEST.threshold,
        )
        auc_, ap_, ar_, mf1_ = (
            metrics["auc"],
            metrics["ap"],
            metrics["ar"],
            metrics["mf1"],
        )

        logger.info(
            f"Current ACC, AUC, AP, AR, mF1 for {cfg.DATASET.DATA.TEST.FAKETYPE} --- {cfg.DATASET.DATA.TEST.LABEL_FOLDER} -- \
            {acc_*100} -- {auc_*100} -- {ap_*100} -- {ar_*100} -- {mf1_*100}"
        )

    return acc_, auc_, ap_, ar_