Skip to content

Commit bfbf0ff

Browse files
committed
adding resnet18, gap
1 parent a0b8633 commit bfbf0ff

9 files changed

Lines changed: 1284 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ __pycache__
44
*egg*
55
build/*
66
*.npy
7+
*.onnx

cnnumpy/io.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def parse(matched):
2626

2727
conv = re.compile(r'.*%(.+?) .+?(Conv).+?dilations=(\[\d+?, \d+?\]).+?strides=(\[\d+?, \d+?\]).+?(\(%.+?, %.+?, %.+?\)).+?\n')
2828
relu = re.compile(r'.*%(.+?) .+?(Relu)\(%(.+?)\).+?\n')
29+
gap = re.compile(r'.*%(.+?) .+?(GlobalAveragePool)\(%(.+?)\).+?\n')
2930
sigmoid = re.compile(r'.*%(.+?) .+?(Sigmoid)\(%(.+?)\).+?\n')
3031
maxpool = re.compile(r'.*%(.+?) .+?(MaxPool).+?kernel_shape=(\[\d+?, \d+?\]).+?strides=(\[\d+?, \d+?\]).+?\(%(.+?)\).+?\n')
3132
upsample = re.compile(r'.*%.+? .+?Constant\[value=.+?(\d+\.?\d*) \[.+?\n.+?%(.+?) .+?(Upsample).+?\(%(.+?),.+?\n')
@@ -36,7 +37,7 @@ def parse(matched):
3637
add = re.compile(r'.*%(.+?) .+?(Add)(\(%.+?\)).+?\n')
3738
weight = re.compile(r'.*%(.+?) .+?(\(.*?\)).*\n')
3839

39-
res = (flatten, upsample, conv, relu, sigmoid, maxpool, dense, concat, add, batchnorm, weight)
40+
res = (flatten, upsample, conv, relu, gap, sigmoid, maxpool, dense, concat, add, batchnorm, weight)
4041

4142
def read_onnx(path):
4243
with open(path+'.txt') as f:
@@ -58,7 +59,7 @@ def read_onnx(path):
5859
flow.append((i[4][0], ['conv_%s'%num], i[0]))
5960
elif i[1]=='Gemm':
6061
num = len(body)
61-
body.append(('dense_%s'%num, 'dense', key[i[2][1][::-1]]))
62+
body.append(('dense_%s'%num, 'dense', key[i[2][1]][::-1]))
6263
flow.append((i[2][0], ['dense_%s'%num], i[0]))
6364
elif i[1]=='Sigmoid':
6465
num = len(body)
@@ -68,6 +69,10 @@ def read_onnx(path):
6869
num = len(body)
6970
body.append(('relu_%s'%num, 'relu', None))
7071
flow.append((i[2], ['relu_%s'%num], i[0]))
72+
elif i[1]=='GlobalAveragePool':
73+
num = len(body)
74+
body.append(('gap_%s'%num, 'gap', None))
75+
flow.append((i[2], ['gap_%s'%num], i[0]))
7176
elif i[1]=='Add':
7277
num = len(body)
7378
body.append(('add_%s'%num, 'add', None))
@@ -93,8 +98,9 @@ def read_onnx(path):
9398
body.append(('flatten_%s'%num, 'flatten', None))
9499
flow.append((i[2], ['flatten_%s'%num], i[0]))
95100

96-
#for i in body: print(i)
97-
#for i in flow: print(i)
101+
# for i in body: print(i)
102+
# print("=============== ")
103+
# for i in flow: print(i)
98104

99105
net = Net()
100106
net.load_json(body, flow)

cnnumpy/layer.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,18 @@ def __init__(self, w=2, stride=2):
9494
self.w = w
9595
self.stride = stride
9696

97-
def para(self): return (self.stride,)
97+
def para(self): return (self.w, self.stride)
9898

9999
def forward(self, x):
100100
return maxpool(x, (self.w, self.w), (self.stride, self.stride))
101101

102+
class GlobalAveragePool(Layer):
103+
name = 'gap'
104+
def __init__(self): pass
105+
106+
def forward(self, x):
107+
return x.mean(axis=(-2, -1))
108+
102109
class UpSample(Layer):
103110
name = 'upsample'
104111
def __init__(self, k):
@@ -150,7 +157,7 @@ def load(self, buf):
150157

151158
layerkey = {'dense':Dense, 'conv':Conv2d, 'relu':ReLU, 'batchnorm':BatchNorm,
152159
'flatten':Flatten, 'sigmoid':Sigmoid, 'softmax': Softmax,
153-
'maxpool':Maxpool, 'upsample':UpSample, 'concat':Concatenate, 'add':Add}
160+
'maxpool':Maxpool, 'upsample':UpSample, 'concat':Concatenate, 'add':Add, 'gap':GlobalAveragePool}
154161

155162
if __name__ == "__main__":
156163
pass

demo/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ Demo for edge detector with model HED. Run ```python main.py``` inside folder ``
1212
The detected result is shown as following:
1313
![](https://raw.githubusercontent.com/Image-Py/cnnumpy/master/demo/hed_edge_detector/rst.png)
1414

15+
## Resnet18 trained on ImageNet
16+
Demo for image classification with resnet18. ```python main.py``` inside folder ```resnet18```. 2 files (same level folder with ```main.py```) are needed (res.txt, [res.npy]())
17+
The detected result is shown as following:
18+
![](https://raw.githubusercontent.com/Image-Py/cnnumpy/master/demo/resnet18/rst.png)
19+

0 commit comments

Comments
 (0)