1+ import json , re , numpy as np
2+ from .net import Net
3+
4+ def read_net (path ):
5+ net = Net ()
6+ with open (path + '.lay' ) as f : lay = json .load (f )
7+ with open (path + '.flw' ) as f : flw = json .load (f )
8+ net .load_json (lay , flw )
9+ net .load_weights (np .load (path + '.npy' ))
10+ return net
11+
12+ def parse (matched ):
13+ gps = list (matched .groups ())
14+ if len (matched .groups ())== 0 : return ''
15+ for i in range (len (gps )):
16+ if not '%' in gps [i ]: continue
17+ gps [i ] = gps [i ].replace ('%' ,"'" )
18+ gps [i ] = gps [i ].replace (')' ,"')" )
19+ gps [i ] = gps [i ].replace (',' ,"'," )
20+ return str (gps )+ '\n '
21+
22+ pt = re .compile (r'.*%(.+?) .+(\(.+\))' )
23+ conv = re .compile (r'.*%(.+?) .+?(Conv).+?strides=(\[\d+?, \d+?\]).+?(\(%.+?, %.+?, %.+?\)).+?\n' )
24+ relu = re .compile (r'.*%(.+?) .+?(Relu)\(%(.+?)\).+?\n' )
25+ sigmoid = re .compile (r'.*%(.+?) .+?(Sigmoid)\(%(.+?)\).+?\n' )
26+ maxpool = re .compile (r'.*%(.+?) .+?(MaxPool).+?strides=(\[\d+?, \d+?\]).+?\(%(.+?)\).+?\n' )
27+ upsample = re .compile (r'.*%.+? .+?Constant\[value=.+?(\d+\.?\d*) \[.+?\n.+?%(.+?) .+?(Upsample).+?\(%(.+?),.+?\n' )
28+ flatten = re .compile (r'.*%.+?Constant.+?\n.+?Shape.+?\n.+?Gather.+?\n.+?Constant.+?\n.+?Unsqueeze.+?\n.+?Unsqueeze.+?\n.+?Concat.+?\n.+?%(.+?) .+?(Reshape)\(%(.+?),.+?\n' )
29+ dense = re .compile (r'.*%(.+?) .+?(Gemm).+(\(%.+?, %.+?, %.+?\)).+?\n' )
30+ concat = re .compile (r'.*%(.+?) .+?(Concat).+(\(%.+?\)).+?\n' )
31+ add = re .compile (r'.*%(.+?) .+?(Add)(\(%.+?\)).+?\n' )
32+ weight = re .compile (r'.*%(.+?) .+?(\(.+?\)).*\n' )
33+
34+ res = (flatten , upsample , conv , relu , sigmoid , maxpool , dense , concat , add , weight )
35+
36+ def read_onnx (path ):
37+ with open (path + '.onnx' ) as f :
38+ cont = f .read ()
39+ for i in res : cont = i .sub (parse , cont )
40+ #for i in cont.split('\n'): print(i)
41+ cont = [eval (i ) for i in cont .split ('\n ' ) if len (i )> 0 and i [0 ]== '[' ]
42+ cont = [[eval (j ) if ',' in j else j for j in i ] for i in cont ]
43+
44+ body = []
45+ flow = []
46+ key = {}
47+ for i in cont :
48+ if len (i )== 2 : key [i [0 ]] = i [1 ]
49+ elif i [1 ]== 'Conv' :
50+ num = len (body )
51+ shp = [key [i [3 ][1 ]][j ] for j in (1 ,0 ,2 )] + [i [2 ][0 ]]
52+ body .append (('conv_%s' % num , 'conv' , shp ))
53+ flow .append ((i [3 ][0 ], ['conv_%s' % num ], i [0 ]))
54+ elif i [1 ]== 'Gemm' :
55+ num = len (body )
56+ body .append (('dense_%s' % num , 'dense' , key [i [2 ][1 ][::- 1 ]]))
57+ flow .append ((i [2 ][0 ], ['dense_%s' % num ], i [0 ]))
58+ elif i [1 ]== 'Sigmoid' :
59+ num = len (body )
60+ body .append (('sigmoid_%s' % num , 'sigmoid' , None ))
61+ flow .append ((i [2 ], ['sigmoid_%s' % num ], i [0 ]))
62+ elif i [1 ]== 'Relu' :
63+ num = len (body )
64+ body .append (('relu_%s' % num , 'relu' , None ))
65+ flow .append ((i [2 ], ['relu_%s' % num ], i [0 ]))
66+ elif i [1 ]== 'Add' :
67+ num = len (body )
68+ body .append (('add_%s' % num , 'add' , None ))
69+ flow .append ((i [2 ], ['add_%s' % num ], i [0 ]))
70+ elif i [1 ]== 'Concat' :
71+ num = len (body )
72+ body .append (('concat_%s' % num , 'concat' , None ))
73+ flow .append ((i [2 ], ['concat_%s' % num ], i [0 ]))
74+ elif i [1 ]== 'MaxPool' :
75+ num = len (body )
76+ body .append (('maxpool_%s' % num , 'maxpool' , [i [2 ][0 ]]))
77+ flow .append ((i [3 ], ['maxpool_%s' % num ], i [0 ]))
78+ elif i [2 ]== 'Upsample' :
79+ num = len (body )
80+ body .append (('upsample_%s' % num , 'upsample' , [int (float (i [0 ]))]))
81+ flow .append ((i [3 ], ['upsample_%s' % num ], i [1 ]))
82+ elif i [1 ]== 'Reshape' :
83+ num = len (body )
84+ body .append (('flatten_%s' % num , 'flatten' , None ))
85+ flow .append ((i [2 ], ['flatten_%s' % num ], i [0 ]))
86+ net = Net ()
87+ net .load_json (body , flow )
88+ net .load_weights (np .load (path + '.npy' ))
89+ return net
0 commit comments