File size: 3,413 Bytes
9e14838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
import logging
import os
from datetime import datetime
from types import MethodType

import torch
import torch.nn.functional as F
from mmcv.utils import get_logger
from package_utils.utils import make_dir

LOG_DIR = "logs/{}".format(datetime.today().strftime("%d-%m-%Y"))
make_dir(LOG_DIR)


class Logger:
    def __init__(self, task="training", workdir=LOG_DIR):
        super().__init__()
        self.logger = logging.getLogger("")
        self.logger.setLevel(logging.INFO)

        file_handler = logging.FileHandler("{}/{}.log".format(workdir, task))
        stream_handler = logging.StreamHandler()
        self.logger.addHandler(file_handler)
        self.logger.addHandler(stream_handler)
        self.logger.epochInfor = MethodType(self.epochInfor, self.logger)
        self.info = self.logger.info

    def epochInfor(
        self,
        epoch,
        idx,
        length,
        batch_time,
        speed,
        data_time,
        losses,
        acc,
        loss_cls=None,
        **kwargs,
    ):
        msg = (
            "Epoch: [{0}][{1}/{2}]\t"
            "Time {batch_time.val:.3f}s ({batch_time.avg:.3f}s)\t"
            "Speed {speed:.1f} samples/s\t"
            "Data {data_time.val:.3f}s ({data_time.avg:.3f}s)\t"
            "Loss {loss.val:.5f} ({loss.avg:.5f})\t"
            "Accuracy {acc.val:.3f} ({acc.avg:.3f})".format(
                epoch,
                idx,
                length,
                batch_time=batch_time,
                speed=speed,
                data_time=data_time,
                loss=losses,
                acc=acc,
            )
        )
        if loss_cls is not None:
            msg += "\t Cls Loss: {loss_cls:.5f}".format(loss_cls=loss_cls)

        for k, v in kwargs.items():
            if v is not None:
                msg += f"\t {k}: {v:.5f}"

        self.logger.info(msg)


def board_writing(writer, loss, acc, iterations, dataset="Train"):
    writer.add_scalar("{}/loss".format(dataset), loss, iterations)
    writer.add_scalar("{}/acc".format(dataset), acc, iterations)


def debug_writing(writer, outputs, labels, inputs, iterations):
    tmp_tar = torch.unsqueeze(labels.cpu().data[0], dim=1)
    # tmp_out = torch.unsqueeze(outputs.cpu().data[0], dim=1)

    tmp_inp = inputs.cpu().data[0]
    tmp_inp[0] += 0.406
    tmp_inp[1] += 0.457
    tmp_inp[2] += 0.480

    tmp_inp[0] += torch.sum(
        F.interpolate(tmp_tar, scale_factor=4, mode="bilinear"), dim=0
    )[0]
    tmp_inp.clamp_(0, 1)

    writer.add_image("Data/input", tmp_inp, iterations)


def get_root_logger(log_file=None, log_level=logging.INFO):
    """Use `get_logger` method in mmcv to get the root logger.

    The logger will be initialized if it has not been initialized. By default a
    StreamHandler will be added. If `log_file` is specified, a FileHandler will
    also be added. The name of the root logger is the top-level package name,
    e.g., "mmpose".

    Args:
        log_file (str | None): The log filename. If specified, a FileHandler
            will be added to the root logger.
        log_level (int): The root logger level. Note that only the process of
            rank 0 is affected, while other processes will set the level to
            "Error" and be silent most of the time.

    Returns:
        logging.Logger: The root logger.
    """
    return get_logger(LOG_DIR, log_file, log_level)