-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_model.py
79 lines (63 loc) · 2.32 KB
/
export_model.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
import os
import argparse
import paddle
from models.resnet import ResNet
from models.heads.tsn_clshead import TSNClsHead
from models.recognizers.recognizer2d import Recognizer2D
from utils import load_pretrained_model
def parse_args():
parser = argparse.ArgumentParser(description='Model export.')
parser.add_argument(
'--save_dir',
dest='save_dir',
help='The directory for saving the exported model',
type=str,
default='./output')
parser.add_argument(
'--model_path',
dest='model_path',
help='The path of model for export',
type=str,
default=None)
return parser.parse_args()
def main(args):
backbone = ResNet(depth=50, out_indices=(3,), norm_eval=False, partial_norm=False)
head = TSNClsHead(spatial_size=-1, spatial_type='avg',
with_avg_pool=False,
temporal_feature_size=1,
spatial_feature_size=1,
dropout_ratio=0.5,
in_channels=2048,
init_std=0.01,
num_classes=101)
net = Recognizer2D(backbone=backbone, cls_head=head,
module_cfg=dict(type='MVF', n_segment=16, alpha=0.125, mvf_freq=(0, 0, 1, 1), mode='THW'))
if args.model_path:
para_state_dict = paddle.load(args.model_path)
net.set_dict(para_state_dict)
print('Loaded trained params of model successfully.')
shape = [-1, 16, 3, 224, 224]
new_net = net
new_net.eval()
new_net = paddle.jit.to_static(
new_net,
input_spec=[paddle.static.InputSpec(shape=shape, dtype='float32'), None, False, False])
save_path = os.path.join(args.save_dir, 'model')
paddle.jit.save(new_net, save_path)
# yml_file = os.path.join(args.save_dir, 'deploy.yaml')
# with open(yml_file, 'w') as file:
# transforms = cfg.export_config.get('transforms', [{
# 'type': 'Normalize'
# }])
# data = {
# 'Deploy': {
# 'transforms': transforms,
# 'model': 'model.pdmodel',
# 'params': 'model.pdiparams'
# }
# }
# yaml.dump(data, file)
print(f'Model is saved in {args.save_dir}.')
if __name__ == '__main__':
args = parse_args()
main(args)