Skip to content

Commit 40611f6

Browse files
committed
adding setup.py, imagepy plg
1 parent ac2d9cf commit 40611f6

5 files changed

Lines changed: 98 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
__pycache__
22
*.pyc
33
.DS_Store
4+
*egg*
5+
build/*

plgs/infcnn_plg.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from os.path import join as pjoin
2+
import numpy as np
3+
import os.path as osp
4+
from imagepy.core.engine import Simple
5+
from imagepy.core.mark import GeometryMark
6+
from .net import LeNet
7+
8+
path = osp.abspath(osp.dirname(__file__))
9+
10+
class Plugin(Simple):
11+
title = 'mnist'
12+
note = ['8-bit', 'auto_snap', 'not_channel', 'preview']
13+
14+
net = LeNet()
15+
net.load_mat(pjoin(path, 'lenet.mat'))
16+
17+
def run(self, ips, imgs, para = None):
18+
marks = {'type':'layers', 'body':{}}
19+
for i, img in enumerate(imgs):
20+
tmp = img.copy().astype('float32')/255.0
21+
out = self.net(tmp[None, None, :, :])
22+
pred = np.argmax(out, -1)
23+
marks['body'][i] = {'type':'text', 'color':(255,0,0), 'body':(2, -2, str(pred))}
24+
ips.mark = GeometryMark(marks)

plgs/lenet.mat

422 KB
Binary file not shown.

plgs/net.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import numpy as np
2+
from scipy.io import loadmat
3+
# infCNN layer
4+
from infCNN import Conv2d, Dense
5+
# infCNN op
6+
from infCNN import ReLU, Flatten, MaxPool, Softmax
7+
from time import time
8+
9+
# op
10+
relu = ReLU()
11+
flatten = Flatten()
12+
maxpool = MaxPool()
13+
softmax = Softmax()
14+
15+
class LeNet():
16+
def __init__(self):
17+
self.conv1 = Conv2d(1, 6, 5, 1)
18+
self.conv2 = Conv2d(6, 16, 5, 1)
19+
self.fc1 = Dense(784, 120)
20+
self.fc2 = Dense(120, 84)
21+
self.fc3 = Dense(84, 10)
22+
23+
self.weights = [self.conv1, self.conv2, self.fc1, self.fc2, self.fc3]
24+
25+
def forward(self, x):
26+
out = relu(self.conv1(x))
27+
# print(out.shape)
28+
out = maxpool(out)
29+
# print(out.shape)
30+
out = relu(self.conv2(out))
31+
# print(out.shape)
32+
out = maxpool(out)
33+
# print(out.shape)
34+
out = flatten(out)
35+
# print(out.shape)
36+
out = relu(self.fc1(out))
37+
# print(out.shape)
38+
out = relu(self.fc2(out))
39+
# print(out.shape)
40+
out = self.fc3(out)
41+
# print(out.shape)
42+
return softmax(out)
43+
44+
def __call__(self, x):
45+
return self.forward(x)
46+
47+
def load_mat(self, fn):
48+
data = loadmat(fn)
49+
w_len = len(data)-4
50+
print('load num of weights:', w_len)
51+
# minus 4 to skip the header
52+
for i in range(w_len//2+1):
53+
w = data[str(2*i)]
54+
b = data[str(2*i+1)]
55+
print(i, w.shape, b.shape)
56+
self.weights[i].load_from_torch(data[str(2*i)], data[str(2*i+1)][0, :])
57+
print('load done!')
58+
59+
if __name__ == "__main__":
60+
net = LeNet()
61+
data = np.ones((1, 1, 28, 28))
62+
net.load_mat('train/lenet.mat')
63+
start = time()
64+
print(net(data))
65+
print('infCNN LeNet time:', time()-start)
66+

setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name='infCNN',
5+
packages=find_packages()
6+
)

0 commit comments

Comments
 (0)