Skip to content

Commit d45bd7a

Browse files
committed
adding batch norm
1 parent 4e7d912 commit d45bd7a

3 files changed

Lines changed: 33 additions & 5 deletions

File tree

cnnumpy/io.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json, re, numpy as np
22
from .net import Net
3+
import os
34

45
def read_net(path):
56
net = Net()
@@ -38,7 +39,7 @@ def parse(matched):
3839
res = (flatten, upsample, conv, relu, sigmoid, maxpool, dense, concat, add, batchnorm, weight)
3940

4041
def read_onnx(path):
41-
with open(path+'.onnx') as f:
42+
with open(path+'.txt') as f:
4243
cont = f.read()
4344
for i in res: cont = i.sub(parse, cont)
4445
#for i in cont.split('\n'): print(i)
@@ -97,5 +98,9 @@ def read_onnx(path):
9798

9899
net = Net()
99100
net.load_json(body, flow)
100-
net.load_weights(np.load(path+'.npy'))
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)
101106
return net

cnnumpy/layer.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Dense(Layer):
2121
name = 'dense'
2222
def __init__(self, c, n):
2323
self.K = np.zeros((n, c), dtype=np.float32)
24-
self.bias = np.zeros(c, dtype=np.float32)
24+
self.bias = np.zeros(n, dtype=np.float32)
2525

2626
def para(self): return self.K.shape
2727

@@ -91,6 +91,7 @@ def forward(self, x):
9191
class Maxpool(Layer):
9292
name = 'maxpool'
9393
def __init__(self, w=2, stride=2):
94+
self.w = w
9495
self.stride = stride
9596

9697
def para(self): return (self.stride,)
@@ -114,16 +115,32 @@ def __init__(self): pass
114115

115116
def forward(self, x):
116117
return np.concatenate(x, axis=1)
117-
118+
118119
class BatchNorm(Layer):
119120
name = 'batchnorm'
120121
def __init__(self, c):
121122
self.c = c
123+
self.k = np.zeros(c, dtype=np.float32)
124+
self.b = np.zeros(c, dtype=np.float32)
125+
self.m = np.zeros(c, dtype=np.float32)
126+
self.v = np.zeros(c, dtype=np.float32)
122127

123128
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+
124132
return x
125133

126134
def load(self, buf):
135+
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]
127144
return self.c * 2
128145

129146
layerkey = {'dense':Dense, 'conv':Conv2d, 'relu':ReLU, 'batchnorm':BatchNorm,

cnnumpy/net.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,16 @@ def flw2code(self, style='list'):
6666
body.append('')
6767
return '\n'.join(body)
6868

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

7480
def __call__(self, x):
7581
return self.forward(x)

0 commit comments

Comments
 (0)