analyze_logs.py 7.31 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
wuyuefeng's avatar
wuyuefeng committed
2
3
import argparse
import json
4
5
from collections import defaultdict

wuyuefeng's avatar
wuyuefeng committed
6
7
import numpy as np
import seaborn as sns
zhangwenwei's avatar
zhangwenwei committed
8
from matplotlib import pyplot as plt
wuyuefeng's avatar
wuyuefeng committed
9
10
11
12
13
14
15
16
17
18
19


def cal_train_time(log_dicts, args):
    for i, log_dict in enumerate(log_dicts):
        print(f'{"-" * 5}Analyze train time of {args.json_logs[i]}{"-" * 5}')
        all_times = []
        for epoch in log_dict.keys():
            if args.include_outliers:
                all_times.append(log_dict[epoch]['time'])
            else:
                all_times.append(log_dict[epoch]['time'][1:])
20
21
22
23
24
        if not all_times:
            raise KeyError(
                'Please reduce the log interval in the config so that '
                'interval is less than iterations of one epoch.')
        epoch_ave_time = np.array(list(map(lambda x: np.mean(x), all_times)))
wuyuefeng's avatar
wuyuefeng committed
25
26
27
28
        slowest_epoch = epoch_ave_time.argmax()
        fastest_epoch = epoch_ave_time.argmin()
        std_over_epoch = epoch_ave_time.std()
        print(f'slowest epoch {slowest_epoch + 1}, '
29
              f'average time is {epoch_ave_time[slowest_epoch]:.4f} s/iter')
wuyuefeng's avatar
wuyuefeng committed
30
        print(f'fastest epoch {fastest_epoch + 1}, '
31
              f'average time is {epoch_ave_time[fastest_epoch]:.4f} s/iter')
wuyuefeng's avatar
wuyuefeng committed
32
        print(f'time std over epochs is {std_over_epoch:.4f}')
33
        print(f'average iter time: {np.mean(epoch_ave_time):.4f} s/iter\n')
wuyuefeng's avatar
wuyuefeng committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54


def plot_curve(log_dicts, args):
    if args.backend is not None:
        plt.switch_backend(args.backend)
    sns.set_style(args.style)
    # if legend is None, use {filename}_{key} as legend
    legend = args.legend
    if legend is None:
        legend = []
        for json_log in args.json_logs:
            for metric in args.keys:
                legend.append(f'{json_log}_{metric}')
    assert len(legend) == (len(args.json_logs) * len(args.keys))
    metrics = args.keys

    num_metrics = len(metrics)
    for i, log_dict in enumerate(log_dicts):
        epochs = list(log_dict.keys())
        for j, metric in enumerate(metrics):
            print(f'plot curve of {args.json_logs[i]}, metric is {metric}')
55
56
57
58
59
60
61
62
            if metric not in log_dict[epochs[int(args.eval_interval) - 1]]:
                if args.eval:
                    raise KeyError(
                        f'{args.json_logs[i]} does not contain metric '
                        f'{metric}. Please check if "--no-validate" is '
                        'specified when you trained the model. Or check '
                        f'if the eval_interval {args.eval_interval} in args '
                        'is equal to the `eval_interval` during training.')
wuyuefeng's avatar
wuyuefeng committed
63
                raise KeyError(
64
65
66
67
68
69
                    f'{args.json_logs[i]} does not contain metric {metric}. '
                    'Please reduce the log interval in the config so that '
                    'interval is less than iterations of one epoch.')

            if args.eval:
                xs = []
wuyuefeng's avatar
wuyuefeng committed
70
                ys = []
71
                for epoch in epochs:
wuyuefeng's avatar
wuyuefeng committed
72
                    ys += log_dict[epoch][metric]
73
74
                    if log_dict[epoch][metric]:
                        xs += [epoch]
wuyuefeng's avatar
wuyuefeng committed
75
76
77
78
79
                plt.xlabel('epoch')
                plt.plot(xs, ys, label=legend[i * num_metrics + j], marker='o')
            else:
                xs = []
                ys = []
80
81
82
                for epoch in epochs:
                    iters = log_dict[epoch]['step']
                    xs.append(np.array(iters))
wuyuefeng's avatar
wuyuefeng committed
83
84
85
86
87
88
                    ys.append(np.array(log_dict[epoch][metric][:len(iters)]))
                xs = np.concatenate(xs)
                ys = np.concatenate(ys)
                plt.xlabel('iter')
                plt.plot(
                    xs, ys, label=legend[i * num_metrics + j], linewidth=0.5)
89
                plt.legend()
wuyuefeng's avatar
wuyuefeng committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        if args.title is not None:
            plt.title(args.title)
    if args.out is None:
        plt.show()
    else:
        print(f'save curve to: {args.out}')
        plt.savefig(args.out)
        plt.cla()


def add_plot_parser(subparsers):
    parser_plt = subparsers.add_parser(
        'plot_curve', help='parser for plotting curves')
    parser_plt.add_argument(
        'json_logs',
        type=str,
        nargs='+',
        help='path of train log in json format')
    parser_plt.add_argument(
        '--keys',
        type=str,
        nargs='+',
        default=['mAP_0.25'],
        help='the metric that you want to plot')
114
115
116
117
118
119
120
121
122
    parser_plt.add_argument(
        '--eval',
        action='store_true',
        help='whether to plot evaluation metric')
    parser_plt.add_argument(
        '--eval-interval',
        type=str,
        default='1',
        help='the eval interval when training')
wuyuefeng's avatar
wuyuefeng committed
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
    parser_plt.add_argument('--title', type=str, help='title of figure')
    parser_plt.add_argument(
        '--legend',
        type=str,
        nargs='+',
        default=None,
        help='legend of each plot')
    parser_plt.add_argument(
        '--backend', type=str, default=None, help='backend of plt')
    parser_plt.add_argument(
        '--style', type=str, default='dark', help='style of plt')
    parser_plt.add_argument('--out', type=str, default=None)


def add_time_parser(subparsers):
    parser_time = subparsers.add_parser(
        'cal_train_time',
        help='parser for computing the average time per training iteration')
    parser_time.add_argument(
        'json_logs',
        type=str,
        nargs='+',
        help='path of train log in json format')
    parser_time.add_argument(
        '--include-outliers',
        action='store_true',
        help='include the first value of every epoch when computing '
        'the average time')


def parse_args():
    parser = argparse.ArgumentParser(description='Analyze Json Log')
    # currently only support plot curve and calculate average train time
    subparsers = parser.add_subparsers(dest='task', help='task parser')
    add_plot_parser(subparsers)
    add_time_parser(subparsers)
    args = parser.parse_args()
    return args


def load_json_logs(json_logs):
    # load and convert json_logs to log_dict, key is epoch, value is a sub dict
    # keys of sub dict is different metrics, e.g. memory, bbox_mAP
    # value of sub dict is a list of corresponding values of all iterations
    log_dicts = [dict() for _ in json_logs]
    for json_log, log_dict in zip(json_logs, log_dicts):
        with open(json_log, 'r') as log_file:
170
            epoch = 1
171
            for i, line in enumerate(log_file):
wuyuefeng's avatar
wuyuefeng committed
172
                log = json.loads(line.strip())
173
                val_flag = False
174
175
                # skip lines only contains one key
                if not len(log) > 1:
wuyuefeng's avatar
wuyuefeng committed
176
                    continue
177

wuyuefeng's avatar
wuyuefeng committed
178
179
                if epoch not in log_dict:
                    log_dict[epoch] = defaultdict(list)
180

wuyuefeng's avatar
wuyuefeng committed
181
                for k, v in log.items():
182
183
184
185
186
187
188
189
                    if '/' in k:
                        log_dict[epoch][k.split('/')[-1]].append(v)
                        val_flag = True
                    elif val_flag:
                        continue
                    else:
                        log_dict[epoch][k].append(v)

190
                if 'epoch' in log.keys():
191
192
                    epoch = log['epoch']

wuyuefeng's avatar
wuyuefeng committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    return log_dicts


def main():
    args = parse_args()

    json_logs = args.json_logs
    for json_log in json_logs:
        assert json_log.endswith('.json')

    log_dicts = load_json_logs(json_logs)

    eval(args.task)(log_dicts, args)


if __name__ == '__main__':
    main()