Skip to content

Commit 7278401

Browse files
author
yxdragon
committed
io
1 parent 6000d29 commit 7278401

3 files changed

Lines changed: 93 additions & 13 deletions

File tree

cnnumpy/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .layer import *
2-
from .net import Net, read_net
2+
from .net import Net
3+
from .io import read_net, read_onnx

cnnumpy/io.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

cnnumpy/net.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
from .layer import layerkey as key
2-
import numpy as np
3-
import json
42

53
class Net:
64
def __init__(self):
@@ -19,7 +17,7 @@ def forward(self, x):
1917
for x, ls, y in self.cmds:
2018
for l in ls:
2119
out = x if l == ls[0] else y
22-
if isinstance(out, list):
20+
if not isinstance(out, str):
2321
p = [rst[i] for i in out]
2422
else: p = rst[out]
2523
rst[y] = dic[l](p)
@@ -75,13 +73,5 @@ def load_weights(self, data):
7573
def __call__(self, x):
7674
return self.forward(x)
7775

78-
def read_net(path):
79-
net = Net()
80-
with open(path+'.lay') as f: lay = json.load(f)
81-
with open(path+'.flw') as f: flw = json.load(f)
82-
net.load_json(lay, flw)
83-
net.load_weights(np.load(path+'.npy'))
84-
return net
85-
8676
if __name__ == '__main__':
87-
pass
77+
pass

0 commit comments

Comments
 (0)