Skip to content

Commit d6fc59f

Browse files
committed
milestone v0.1
1 parent 88739ea commit d6fc59f

6 files changed

Lines changed: 40 additions & 64 deletions

File tree

infCNN/LayerLib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .Core import Layer
2-
from .conv_s import conv
2+
from .conv import conv
33
import numpy as np
44

55

infCNN/conv.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import numpy as np
22
from time import time
3-
from numba import njit
3+
try: from numba import njit
4+
except: njit = None
5+
6+
if njit is None: print('install numba can double speed!')
47

58
def neighbors(shape, core):
69
shp = [slice(0,i) for i in core]
@@ -12,8 +15,7 @@ def neighbors(shape, core):
1215
acc = np.cumprod((1,)+shape[::-1][:-1])
1316
return np.dot(idx.T, acc[::-1])
1417

15-
@njit
16-
def fill_col(pdimg, idx, colimg):
18+
def jit_fill_col(pdimg, idx, colimg):
1719
s = 0
1820
for i in range(len(pdimg)):
1921
if pdimg[i]&1==0: continue
@@ -22,19 +24,25 @@ def fill_col(pdimg, idx, colimg):
2224
s += 1
2325
return colimg
2426

27+
def fill_col(pdimg, idx, colimg):
28+
rc = np.where(pdimg&1)[0]
29+
rc = rc.reshape((-1,1))+idx
30+
colimg[:] = pdimg[rc.ravel()]
31+
return colimg
32+
33+
if not njit is None: fill_col = njit(jit_fill_col)
34+
2535
def conv(img, core, stride=(1,1), buf=[np.zeros(1, dtype=np.int32)]):
2636
# new the col_img, if needed
2737
strh, strw = stride
2838
cimg_w = np.cumprod(core.shape[1:])[-1]
2939
n,c,h,w = img.shape
3040
cimg_h = n*(h//strh)*(w//strw)
31-
3241
if len(buf[0])<cimg_h*cimg_w:
3342
buf[0] = col_img = np.zeros(cimg_h*cimg_w, dtype=np.int32)
3443
else:
3544
col_img = buf[0][:cimg_h*cimg_w]
3645
col_img[:] = 0
37-
3846
# mark where need
3947
iimg = img.view(dtype=np.int32)
4048
iimg &= 0xfffffffe
@@ -71,4 +79,4 @@ def conv(img, core, stride=(1,1), buf=[np.zeros(1, dtype=np.int32)]):
7179

7280
start = time()
7381
rst2 = conv(img, core, (1,1))
74-
print('jit cost:', time()-start)
82+
print('numpy cost:', time()-start)

infCNN/numpy_extend.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

infCNN/upsample.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ def _resize(img, k, ra, rs, _rs, ca, cs, _cs, out):
2020
rcab = rra * _cs[c] + rrb * cs[c]
2121
out[r,c] = rcab
2222

23-
def upsample(img, k=2, out=None):
24-
nc, (h, w), e = img.shape[:-2], img.shape[-2:], 1e-4
23+
def upsample(img, k, out=None):
24+
nc, (h, w) = img.shape[:-2], img.shape[-2:]
2525
if out is None:
2626
out = np.zeros(nc+(h*k, w*k), dtype=img.dtype)
27-
28-
rs = np.linspace(e,h-1-e, h*k, dtype=np.float32)
29-
cs = np.linspace(e,w-1-e, w*k, dtype=np.float32)
27+
rs = np.linspace(-0.5+0.5/k,h-0.5-0.5/k, h*k, dtype=np.float32)
28+
cs = np.linspace(-0.5+0.5/k,w-0.5-0.5/k, w*k, dtype=np.float32)
29+
np.clip(rs, 0, h-1, out=rs)
30+
np.clip(cs, 0, w-1, out=cs)
3031
ra = np.floor(rs).astype(np.uint32)
3132
ca = np.floor(cs).astype(np.uint32)
32-
rs -= ra
33-
cs -= ca
34-
33+
np.clip(ra, 0, h-1.5, out=ra)
34+
np.clip(ca, 0, w-1.5, out=ca)
35+
rs -= ra; cs -= ca;
3536
outcol = out.reshape((-1, h*k, w*k))
3637
imgcol = img.reshape((-1, h, w))
3738
for i, o in zip(imgcol, outcol):
@@ -40,11 +41,20 @@ def upsample(img, k=2, out=None):
4041

4142
if __name__ == '__main__':
4243
from time import time
43-
from skimage.data import astronaut
44+
from skimage.data import astronaut, camera
4445
img = astronaut().transpose(2,0,1).astype(np.float32)
45-
46+
img = camera().astype(np.float32)
47+
img = np.array([[1,2],[3,4]], dtype=np.float32)
4648
# firs time to jit
47-
resize(img, 2)
49+
resize(img, 4)
4850
start = time()
49-
rst = resize(img, 2)
50-
print('\nmine v1', time()-start)
51+
for i in range(10):
52+
rst1 = resize(img, 2)
53+
print('mine v1', time()-start)
54+
55+
import cv2
56+
start = time()
57+
for i in range(10):
58+
rst2 = cv2.resize(img, (4, 4))
59+
print('cv', time()-start)
60+

plgs/net.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
# infCNN layer
44
from infCNN import Conv2d, Dense
55
# infCNN op
6-
from infCNN import ReLU, Flatten, MaxPool, Softmax
6+
from infCNN import relu, flatten, maxpool, softmax
77
from time import time
88

9-
# op
10-
relu = ReLU()
11-
flatten = Flatten()
12-
maxpool = MaxPool()
13-
softmax = Softmax()
14-
159
class LeNet():
1610
def __init__(self):
1711
self.conv1 = Conv2d(1, 6, 5, 1)

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The training on pytorch is regular, which can be seen in ```train/``` folder. Th
1313

1414

1515
#### Plugins for ImagePy
16-
The plugin is in ```plgs/``` folder
16+
The plugin example for mnist is in ```plgs/``` folder
1717

1818
#### References
1919

0 commit comments

Comments
 (0)