-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathdump.py
More file actions
80 lines (65 loc) · 1.97 KB
/
dump.py
File metadata and controls
80 lines (65 loc) · 1.97 KB
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
# -*- coding: utf-8 -*-
# MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import argparse
import sys
# pylint: disable=import-error
import resnet.model as resnet_model
# pylint: disable=import-error
import shufflenet.model as snet_model
import numpy as np
import megengine as mge
from megengine import jit
def dump_static_graph(model, graph_name, shape):
model.eval()
data = mge.Tensor(np.random.random(shape))
@jit.trace(capture_as_const=True)
def pred_func(data):
outputs = model(data)
return outputs
pred_func(data)
pred_func.dump(
graph_name,
arg_names=["data"],
optimize_for_inference=True,
enable_fuse_conv_bias_nonlinearity=True,
)
def main():
parser = argparse.ArgumentParser(description="MegEngine Classification Dump .mge")
parser.add_argument(
"-a",
"--arch",
default="resnet18",
help="model architecture (default: resnet18)",
)
parser.add_argument(
"-s",
"--shape",
type=int,
nargs=4,
default=(1, 3, 224, 224),
help="input shape (default: 1 3 224 224)"
)
parser.add_argument(
"-o",
"--output",
type=str,
default="model.mge",
help="output filename"
)
args = parser.parse_args()
if 'resnet' in args.arch:
model = getattr(resnet_model, args.arch)(pretrained=True)
elif 'shufflenet' in args.arch:
model = getattr(snet_model, args.arch)(pretrained=True)
else:
print('unavailable arch {}'.format(args.arch))
sys.exit()
dump_static_graph(model, args.output, tuple(args.shape))
if __name__ == "__main__":
main()