File size: 7,635 Bytes
d7e58f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import json
import os
import os.path as osp
import time
import warnings
from collections import OrderedDict
from unittest.mock import patch

import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
from torch.optim import SGD
from torch.utils.data import DataLoader

import mmcv
from mmcv.runner import build_runner
from mmcv.utils import get_logger


def parse_args():
    parser = argparse.ArgumentParser(description='Visualize the given config'
                                     'of learning rate and momentum, and this'
                                     'script will overwrite the log_config')
    parser.add_argument('config', help='train config file path')
    parser.add_argument(
        '--work-dir', default='./', help='the dir to save logs and models')
    parser.add_argument(
        '--num-iters', default=300, help='The number of iters per epoch')
    parser.add_argument(
        '--num-epochs', default=300, help='Only used in EpochBasedRunner')
    parser.add_argument(
        '--window-size',
        default='12*14',
        help='Size of the window to display images, in format of "$W*$H".')
    parser.add_argument(
        '--log-interval', default=10, help='The interval of TextLoggerHook')
    args = parser.parse_args()
    return args


class SimpleModel(nn.Module):

    def __init__(self):
        super().__init__()
        self.conv = nn.Conv2d(1, 1, 1)

    def train_step(self, *args, **kwargs):
        return dict()

    def val_step(self, *args, **kwargs):
        return dict()


def iter_train(self, data_loader, **kwargs):
    self.mode = 'train'
    self.data_loader = data_loader
    self.call_hook('before_train_iter')
    self.call_hook('after_train_iter')
    self._inner_iter += 1
    self._iter += 1


def epoch_train(self, data_loader, **kwargs):
    self.model.train()
    self.mode = 'train'
    self.data_loader = data_loader
    self._max_iters = self._max_epochs * len(self.data_loader)
    self.call_hook('before_train_epoch')
    for i, data_batch in enumerate(self.data_loader):
        self._inner_iter = i
        self.call_hook('before_train_iter')
        self.call_hook('after_train_iter')
        self._iter += 1
    self.call_hook('after_train_epoch')
    self._epoch += 1


def log(self, runner):
    cur_iter = self.get_iter(runner, inner_iter=True)

    log_dict = OrderedDict(
        mode=self.get_mode(runner),
        epoch=self.get_epoch(runner),
        iter=cur_iter)

    # only record lr of the first param group
    cur_lr = runner.current_lr()
    if isinstance(cur_lr, list):
        log_dict['lr'] = cur_lr[0]
    else:
        assert isinstance(cur_lr, dict)
        log_dict['lr'] = {}
        for k, lr_ in cur_lr.items():
            assert isinstance(lr_, list)
            log_dict['lr'].update({k: lr_[0]})

    cur_momentum = runner.current_momentum()
    if isinstance(cur_momentum, list):
        log_dict['momentum'] = cur_momentum[0]
    else:
        assert isinstance(cur_momentum, dict)
        log_dict['momentum'] = {}
        for k, lr_ in cur_momentum.items():
            assert isinstance(lr_, list)
            log_dict['momentum'].update({k: lr_[0]})
    log_dict = dict(log_dict, **runner.log_buffer.output)
    self._log_info(log_dict, runner)
    self._dump_log(log_dict, runner)
    return log_dict


@patch('torch.cuda.is_available', lambda: False)
@patch('mmcv.runner.EpochBasedRunner.train', epoch_train)
@patch('mmcv.runner.IterBasedRunner.train', iter_train)
@patch('mmcv.runner.hooks.TextLoggerHook.log', log)
def run(cfg, logger):
    momentum_config = cfg.get('momentum_config')
    lr_config = cfg.get('lr_config')

    model = SimpleModel()
    optimizer = SGD(model.parameters(), 0.1, momentum=0.8)
    cfg.work_dir = cfg.get('work_dir', './')
    workflow = [('train', 1)]

    if cfg.get('runner') is None:
        cfg.runner = {
            'type': 'EpochBasedRunner',
            'max_epochs': cfg.get('total_epochs', cfg.num_epochs)
        }
        warnings.warn(
            'config is now expected to have a `runner` section, '
            'please set `runner` in your config.', UserWarning)
    batch_size = 1
    data = cfg.get('data')
    if data:
        batch_size = data.get('samples_per_gpu')
    fake_dataloader = DataLoader(
        list(range(cfg.num_iters)), batch_size=batch_size)
    runner = build_runner(
        cfg.runner,
        default_args=dict(
            model=model,
            batch_processor=None,
            optimizer=optimizer,
            work_dir=cfg.work_dir,
            logger=logger,
            meta=None))
    log_config = dict(
        interval=cfg.log_interval, hooks=[
            dict(type='TextLoggerHook'),
        ])

    runner.register_training_hooks(lr_config, log_config=log_config)
    runner.register_momentum_hook(momentum_config)
    runner.run([fake_dataloader], workflow)


def plot_lr_curve(json_file, cfg):
    data_dict = dict(LearningRate=[], Momentum=[])
    assert os.path.isfile(json_file)
    with open(json_file) as f:
        for line in f:
            log = json.loads(line.strip())
            data_dict['LearningRate'].append(log['lr'])
            data_dict['Momentum'].append(log['momentum'])

    wind_w, wind_h = (int(size) for size in cfg.window_size.split('*'))
    # if legend is None, use {filename}_{key} as legend
    fig, axes = plt.subplots(2, 1, figsize=(wind_w, wind_h))
    plt.subplots_adjust(hspace=0.5)
    font_size = 20
    for index, (updater_type, data_list) in enumerate(data_dict.items()):
        ax = axes[index]
        if cfg.runner.type == 'EpochBasedRunner':
            ax.plot(data_list, linewidth=1)
            ax.xaxis.tick_top()
            ax.set_xlabel('Iters', fontsize=font_size)
            ax.xaxis.set_label_position('top')
            sec_ax = ax.secondary_xaxis(
                'bottom',
                functions=(lambda x: x / cfg.num_iters * cfg.log_interval,
                           lambda y: y * cfg.num_iters / cfg.log_interval))
            sec_ax.tick_params(labelsize=font_size)
            sec_ax.set_xlabel('Epochs', fontsize=font_size)
        else:
            # plt.subplot(2, 1, index + 1)
            x_list = np.arange(len(data_list)) * cfg.log_interval
            ax.plot(x_list, data_list)
            ax.set_xlabel('Iters', fontsize=font_size)
        ax.set_ylabel(updater_type, fontsize=font_size)
        if updater_type == 'LearningRate':
            if cfg.get('lr_config'):
                title = cfg.lr_config.type
            else:
                title = 'No learning rate scheduler'
        else:
            if cfg.get('momentum_config'):
                title = cfg.momentum_config.type
            else:
                title = 'No momentum scheduler'
        ax.set_title(title, fontsize=font_size)
        ax.grid()
        # set tick font size
        ax.tick_params(labelsize=font_size)
    save_path = osp.join(cfg.work_dir, 'visualization-result')
    plt.savefig(save_path)
    print(f'The learning rate graph is saved at {save_path}.png')
    plt.show()


def main():
    args = parse_args()
    timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime())
    cfg = mmcv.Config.fromfile(args.config)
    cfg['num_iters'] = args.num_iters
    cfg['num_epochs'] = args.num_epochs
    cfg['log_interval'] = args.log_interval
    cfg['window_size'] = args.window_size

    log_path = osp.join(cfg.get('work_dir', './'), f'{timestamp}.log')
    json_path = log_path + '.json'
    logger = get_logger('mmcv', log_path)

    run(cfg, logger)
    plot_lr_curve(json_path, cfg)


if __name__ == '__main__':
    main()