This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
model_fuse.py
88 lines (69 loc) · 2.48 KB
/
model_fuse.py
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
# -*- coding: utf-8 -*-
"""
@date: 2021/7/28 下午10:10
@file: model_fuse.py
@author: zj
@description: Fuse block for ACBlock/RepVGGBLock/DBBlock
"""
import argparse
import os.path
import torch
from zcls.model.conv_helper import fuse_acblock, fuse_repvgg_block, fuse_dbblock
from zcls.config import cfg
from zcls.model.recognizers.build import build_recognizer
from zcls.util.checkpoint import CheckPointer
def parse_args():
parser = argparse.ArgumentParser(description='Fuse block for ACBlock/RepVGGBLock/DBBlock')
parser.add_argument("config_file",
type=str,
default="",
metavar="CONFIG_FILE",
help="path to config file")
parser.add_argument('output_dir',
type=str,
default="",
metavar="OUTPUT_DIR",
help="path to output")
parser.add_argument('--verbose',
default=False,
action='store_true',
help="Print Model Info")
args = parser.parse_args()
return args
def fuse(cfg, model, verbose=False):
block_name_tuple = cfg.MODEL.CONV.ADD_BLOCKS
if block_name_tuple is not None:
assert isinstance(block_name_tuple, tuple)
# The insertion and fusion operations are in reverse order
for add_block in list(reversed(block_name_tuple)):
if verbose:
print(f'fuse {add_block} ...')
if add_block == 'RepVGGBlock':
fuse_repvgg_block(model)
if add_block == 'ACBlock':
fuse_acblock(model)
if add_block == 'DiverseBranchBlock':
fuse_dbblock(model)
else:
if verbose:
print('No fusion operation is required')
def save_model(model, output_dir, verbose):
checkpoint = CheckPointer(model, save_dir=output_dir, save_to_disk=True)
name = 'model_fused'
checkpoint.save(name)
model_path = os.path.join(output_dir, f'{name}.pth')
if verbose:
print(f'save to {model_path}')
if __name__ == '__main__':
args = parse_args()
cfg.merge_from_file(args.config_file)
model = build_recognizer(cfg, device=torch.device('cpu'))
verbose = args.verbose
if verbose:
print('before fuse')
print(model)
fuse(cfg, model, verbose)
if verbose:
print('after fuse')
print(model)
save_model(model, cfg.OUTPUT_DIR, verbose)