Skip to content

Commit 4e7aff3

Browse files
author
yxdragon
committed
1 npy
1 parent 5c2db58 commit 4e7aff3

4 files changed

Lines changed: 13 additions & 22 deletions

File tree

cnnumpy/io.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,5 @@ def read_onnx(path):
9898

9999
net = Net()
100100
net.load_json(body, flow)
101-
if os.path.exists(path+'_bn.npy'):
102-
bn = np.load(path+'_bn.npy')
103-
else:
104-
bn = []
105-
net.load_weights(np.load(path+'.npy'), bn)
101+
net.load_weights(np.load(path+'.npy'))
106102
return net

cnnumpy/layer.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,19 @@ def __init__(self, c):
126126
self.v = np.zeros(c, dtype=np.float32)
127127

128128
def forward(self, x):
129-
130-
x = (x - self.m.reshape(1, -1, 1, 1))/np.sqrt(self.v.reshape(1, -1, 1, 1)) * self.k.reshape(1, -1, 1, 1) + self.b.reshape(1, -1, 1, 1)
131-
129+
x = (x - self.m.reshape(1, -1, 1, 1))
130+
x /= np.sqrt(self.v.reshape(1, -1, 1, 1))
131+
x *= self.k.reshape(1, -1, 1, 1)
132+
x += self.b.reshape(1, -1, 1, 1)
132133
return x
133134

134135
def load(self, buf):
135136
c = self.c
136-
self.k = buf[:c]
137-
self.b = buf[c:2*c]
138-
return self.c * 2
139-
140-
def load_bn(self, buf):
141-
c = self.c
142-
self.m = buf[:c]
143-
self.v = buf[c:2*c]
144-
return self.c * 2
137+
self.k[:] = buf[0*c:1*c]
138+
self.b[:] = buf[1*c:2*c]
139+
self.m[:] = buf[2*c:3*c]
140+
self.v[:] = buf[3*c:4*c]
141+
return self.c * 4
145142

146143
layerkey = {'dense':Dense, 'conv':Conv2d, 'relu':ReLU, 'batchnorm':BatchNorm,
147144
'flatten':Flatten, 'sigmoid':Sigmoid, 'softmax': Softmax,

cnnumpy/net.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,9 @@ def flw2code(self, style='list'):
6666
body.append('')
6767
return '\n'.join(body)
6868

69-
def load_weights(self, data, data_bn):
69+
def load_weights(self, data):
7070
s = 0
71-
b = 0
7271
for i in self.body:
73-
if 'batchnorm' in i[0]:
74-
# print(i[0])
75-
b += i[1].load_bn(data_bn[b:])
7672
s += i[1].load(data[s:])
7773
# print(data.shape, s)
7874
# print(data_bn.shape, b)

demo/craft_text_detector/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
sys.path.append('../../')
13
from cnnumpy import read_onnx
24
import numpy as np
35
from matplotlib import pyplot as plt

0 commit comments

Comments
 (0)