22from time import time
33from numba import njit
44
5+
56def neighbors (shape , core ):
6- shp = [slice (0 ,i ) for i in core ]
7+ shp = [slice (0 , i ) for i in core ]
78 idx = np .mgrid [tuple (shp )]
8- idx = idx .reshape ((len (core ),- 1 ))
9+ idx = idx .reshape ((len (core ), - 1 ))
910 offset = np .array (core )// 2
1011 offset [0 ] = 0
11- idx -= offset .reshape ((- 1 ,1 ))
12+ idx -= offset .reshape ((- 1 , 1 ))
1213 acc = np .cumprod ((1 ,)+ shape [::- 1 ][:- 1 ])
1314 return np .dot (idx .T , acc [::- 1 ])
1415
16+
1517@njit
1618def fill_col (pdimg , idx , colimg ):
1719 s = 0
1820 for i in range (len (pdimg )):
19- if pdimg [i ]& 1 == 0 : continue
21+ if pdimg [i ] & 1 == 0 :
22+ continue
2023 for j in idx :
2124 colimg [s ] = pdimg [i + j ]
2225 s += 1
2326 return colimg
2427
25- def img2col (img , core , stride = (1 ,1 ), buf = [np .zeros (1 , dtype = np .int32 )]):
28+
29+ def conv (img , core , stride = (1 , 1 ), buf = [np .zeros (1 , dtype = np .int32 )]):
2630 # new the col_img, if needed
2731 strh , strw = stride
2832 cimg_w = np .cumprod (core .shape [1 :])[- 1 ]
29- print (img .shape )
30- n ,c , h , w = img .shape
33+ # print(img.shape)
34+ n , c , h , w = img .shape
3135 cimg_h = n * (h // strh )* (w // strw )
32-
33- if len (buf [0 ])< cimg_h * cimg_w :
36+
37+ if len (buf [0 ]) < cimg_h * cimg_w :
3438 buf [0 ] = col_img = np .zeros (cimg_h * cimg_w , dtype = np .int32 )
3539 else :
3640 col_img = buf [0 ][:cimg_h * cimg_w ]
3741 col_img [:] = 0
38-
42+
3943 # mark where need
4044 iimg = img .view (dtype = np .int32 )
41- #iimg.view(dtype=np.uint8).ravel()[::4]&=0xfe
45+ # iimg.view(dtype=np.uint8).ravel()[::4]&=0xfe
4246 iimg &= 0xfffffffe
43- iimg [:,0 , ::strh ,::strw ] |= 1
44-
47+ iimg [:, 0 , ::strh , ::strw ] |= 1
48+
4549 # ravel the image
46- n ,c , h , w = np .array (core .shape )
47- shp = ((0 ,0 ),(0 ,0 ),(h // 2 ,h // 2 ),(w // 2 ,w // 2 ))
50+ n , c , h , w = np .array (core .shape )
51+ shp = ((0 , 0 ), (0 , 0 ), (h // 2 , h // 2 ), (w // 2 , w // 2 ))
4852 pdimg = np .pad (iimg , shp , 'constant' , constant_values = 0 )
4953 nbs = neighbors (pdimg .shape [1 :], core .shape [1 :])
5054 fill_col (pdimg .ravel (), nbs , col_img )
5155 col_img = col_img .view (np .float32 )
5256 col_img = col_img .reshape ((cimg_h , cimg_w ))
53-
57+
5458 # dot
55- col_core = core .reshape ((core .shape [0 ],- 1 ))
59+ col_core = core .reshape ((core .shape [0 ], - 1 ))
5660 rst = col_core .dot (col_img .T )
5761 ni , ci , hi , wi = img .shape
5862 return rst .reshape ((ni , n , hi // strh , wi // strw ))
5963
64+
6065if __name__ == '__main__' :
6166 from skimage .data import camera
6267 import matplotlib .pyplot as plt
@@ -65,16 +70,16 @@ def img2col(img, core, stride=(1,1), buf=[np.zeros(1, dtype=np.int32)]):
6570 img .ravel ()[:] = np .arange (3 * 512 * 512 )
6671
6772 iimg = img .view (dtype = np .int32 )
68- iimg .view (dtype = np .uint8 ).ravel ()[::4 ]&= 0xfe
69- iimg [:,0 ,:, :] |= 1
70-
73+ iimg .view (dtype = np .uint8 ).ravel ()[::4 ] &= 0xfe
74+ iimg [:, 0 , :, :] |= 1
75+
7176 core = np .zeros ((32 , 3 , 3 , 3 ), dtype = np .float32 )
7277 core .ravel ()[:] = np .arange (3 * 3 * 3 * 32 )
73-
78+
7479 start = time ()
75- rst1 = img2col (img , core , (1 ,1 ))
80+ rst1 = img2col (img , core , (1 , 1 ))
7681 print (time ()- start )
7782
7883 start = time ()
79- rst2 = img2col (img , core , (2 ,2 ))
84+ rst2 = img2col (img , core , (2 , 2 ))
8085 print ('jit cost: x10' , time ()- start )
0 commit comments