"docs/en/model_support.md" did not exist on "d9fccfefe27c9d63212698578084aaf9f688a066"
reparameterize_repvgg.py 1.78 KB
Newer Older
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
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import warnings
from pathlib import Path

import torch

from mmcls.apis import init_model

bright_style, reset_style = '\x1b[1m', '\x1b[0m'
red_text, blue_text = '\x1b[31m', '\x1b[34m'
white_background = '\x1b[107m'

msg = bright_style + red_text
msg += 'DeprecationWarning: This tool will be deprecated in future. '
msg += red_text + 'Welcome to use the '
msg += white_background
msg += '"tools/convert_models/reparameterize_model.py"'
msg += reset_style
warnings.warn(msg)


def convert_repvggblock_param(config_path, checkpoint_path, save_path):
    model = init_model(config_path, checkpoint=checkpoint_path)
    print('Converting...')

    model.backbone.switch_to_deploy()
    torch.save(model.state_dict(), save_path)

    print('Done! Save at path "{}"'.format(save_path))


def main():
    parser = argparse.ArgumentParser(
        description='Convert the parameters of the repvgg block '
        'from training mode to deployment mode.')
    parser.add_argument(
        'config_path',
        help='The path to the configuration file of the network '
        'containing the repvgg block.')
    parser.add_argument(
        'checkpoint_path',
        help='The path to the checkpoint file corresponding to the model.')
    parser.add_argument(
        'save_path',
        help='The path where the converted checkpoint file is stored.')
    args = parser.parse_args()

    save_path = Path(args.save_path)
    if save_path.suffix != '.pth':
        print('The path should contain the name of the pth format file.')
        exit(1)
    save_path.parent.mkdir(parents=True, exist_ok=True)

    convert_repvggblock_param(args.config_path, args.checkpoint_path,
                              args.save_path)


if __name__ == '__main__':
    main()