-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaxpool.py
181 lines (155 loc) · 7.44 KB
/
maxpool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import cv2
import numpy as np
import time
import math
wantDebug = False
def maxpool2d(img, G=4):
# Max Pooling
H, W = img.shape
filterMaxpool = G
cellMaxpool = int(math.sqrt(filterMaxpool)) # 4 --> 2
Nh = H // cellMaxpool
Nw = W // cellMaxpool
# img size = 345 x 518 --> 86 block plus 1 pixel x 129 block plus 2 pixel
# image size = 348 x 520 (multiple of filterMaxpool)
if (H % filterMaxpool) == 0:
maxpoolMaxRow = math.floor(H // filterMaxpool)
else:
maxpoolMaxRow = math.floor(H // filterMaxpool) + 1
if (W % filterMaxpool) == 0:
maxpoolMaxCol = math.floor(W // filterMaxpool)
else:
maxpoolMaxCol = math.floor(W // filterMaxpool) + 1
totalRow = maxpoolMaxRow * filterMaxpool
totalCol = maxpoolMaxCol * filterMaxpool
image = np.zeros((totalRow, totalCol))
image[0:H, 0:W] = img
temp = np.zeros((totalRow//2, totalCol//2))
m = 0
for maxpoolRow in range(maxpoolMaxRow):
for maxpoolCol in range(maxpoolMaxCol):
if wantDebug and (maxpoolCol == 129):
print("\nLAST COLUMN!")
# Get a maxpool consists 4x4
sourceMaxpoolRow = maxpoolRow * filterMaxpool
sourceMaxpoolCol = maxpoolCol * filterMaxpool
lastPart = image[sourceMaxpoolRow:sourceMaxpoolRow+filterMaxpool, sourceMaxpoolCol: sourceMaxpoolCol+filterMaxpool]
blr, blc = np.shape(lastPart) # get the shape of the lastPart
if (blr != filterMaxpool) or (blc != filterMaxpool): # Check if we are the last block
oneMaxpool = np.zeros((filterMaxpool, filterMaxpool))
oneMaxpool[0:blr, 0:blc] = lastPart # copy the lastPart into 4x4
else:
oneMaxpool = lastPart
if wantDebug:
print("\nmaxpool #{}, maxpoolRow {}, maxpoolCol {}\noneMaxpool[{}, {}] = \n{}".format(m, maxpoolRow, maxpoolCol, sourceMaxpoolRow, sourceMaxpoolCol, oneMaxpool))
# Take oneCell, find itsMax
for cellRow in range(cellMaxpool):
for cellCol in range(cellMaxpool):
sourceRow = (maxpoolRow * filterMaxpool) + (cellMaxpool * cellRow)
sourceRow2 = (maxpoolRow * filterMaxpool) + (cellMaxpool * (cellRow+1))
sourceCol = (maxpoolCol * filterMaxpool) + (cellMaxpool * cellCol)
sourceCol2 = (maxpoolCol * filterMaxpool) + (cellMaxpool * (cellCol+1))
oneCell= image[sourceRow:sourceRow2, sourceCol:sourceCol2]
targetRow = maxpoolRow * cellMaxpool + cellRow
targetCol = maxpoolCol * cellMaxpool + cellCol
itsMax = np.max(image[sourceRow:sourceRow2, sourceCol:sourceCol2])
if wantDebug:
print("oneCell [{},{}] = \n {}\nitsMax = {}".format(cellRow, cellCol, oneCell, itsMax))
temp[targetRow, targetCol] = itsMax
m = m + 1
out1 = temp[0:Nh, 0:Nw]
print("\ntotal maxPool2d = ", m)
print(out1.shape)
return out1
def pool2dt5d_16x16(img, tileMaxpool=16):
'''
:img = 426 x 426 (Yolvov3 input resolution 416x416)
:out2 = 213 x 213
:param img: input image
:param tileMaxpool: window size
:return:
'''
H, W = img.shape
filterMaxpool = int(math.sqrt(tileMaxpool)) # 16 --> 4
cellMaxpool = int(math.sqrt(filterMaxpool)) # 4 --> 2
Nh = H // cellMaxpool # Nh = NewHeight
Nw = W // cellMaxpool # Nw = NewWidth
# img size = 345 x 518 divide by 16 --> 21 tileBlockRow plus 9 pixel x 32 tileBlockCol plus 6 pixel
# image size = 348 x 520 (multiple of filterMaxpool)
if (H % tileMaxpool) == 0:
maxpoolMaxRow = math.floor(H // tileMaxpool)
else:
maxpoolMaxRow = math.floor(H // tileMaxpool) + 1
if (W % tileMaxpool) == 0:
maxpoolMaxCol = math.floor(W // tileMaxpool)
else:
maxpoolMaxCol = math.floor(W // tileMaxpool) + 1
totalRow = maxpoolMaxRow * tileMaxpool
totalCol = maxpoolMaxCol * tileMaxpool
image = np.zeros((totalRow, totalCol))
image[0:H, 0:W] = img
temp = np.zeros((totalRow//2+filterMaxpool, totalCol//2+filterMaxpool))
m = 0
for maxpoolRow in range(0, totalRow, tileMaxpool):
for maxpoolCol in range(0, totalCol, tileMaxpool):
if maxpoolCol == 516:
print ("\n LAST COLUMN!!")
# Get a tileBlock consists 16x16
lastPart = image[maxpoolRow:maxpoolRow + tileMaxpool, maxpoolCol:maxpoolCol + tileMaxpool]
blr, blc = np.shape(lastPart) # get the shape of the lastPart
if (blr != tileMaxpool) or (blc != tileMaxpool): # Check if we are the last block
aBlock = np.zeros((tileMaxpool,tileMaxpool))
aBlock[0:blr, 0:blc] = lastPart # copy the lastPart into 16x16
else:
aBlock = lastPart
if wantDebug:
print("\nmaxPool #{}, maxpoolRow {}, maxpoolCol {}, aBlock = \n {}".format(m, maxpoolRow, maxpoolCol, aBlock))
# We process each cell. 16 values are calculated at a time
for cellRow in range(cellMaxpool):
for cellCol in range(cellMaxpool):
if wantDebug:
print("\nmaxpoolRow {}, maxpoolCol {}, cellRow {}, cellCol {}".format(maxpoolRow, maxpoolCol, cellRow, cellCol))
for row in range(filterMaxpool):
for col in range(filterMaxpool):
# Take oneCell, find itsMax
sourceRow = (row * filterMaxpool) + (cellMaxpool * cellRow)
sourceRow2 = (row * filterMaxpool) + (cellMaxpool * (cellRow+1))
sourceCol = (col * filterMaxpool) + (cellMaxpool * cellCol) # 0, 4, 8, 12
sourceCol2 = (col * filterMaxpool) + (cellMaxpool * (cellCol+1)) # 0, 4, 8, 12
oneCell = aBlock[sourceRow:sourceRow2, sourceCol:sourceCol2]
targetRow = maxpoolRow//2 + (row * cellMaxpool + cellRow)
targetCol = maxpoolCol//2 + (col * cellMaxpool + cellCol)
itsMax = np.max(aBlock[sourceRow:sourceRow2, sourceCol:sourceCol2])
if wantDebug:
print("maxPool #{}, oneCell[{},{}] = \n {} \ntemp[{},{}] = {}".format(m, sourceRow, sourceCol, oneCell, targetRow, targetCol, itsMax))
temp[targetRow, targetCol] = itsMax
m = m + 1
out2 = temp[0:Nh, 0:Nw]
print("\ntotal maxPool2dt5d = ", m)
print(out2.shape)
return out2
# Main Program
#read image
img = cv2.imread("Image_416x416.jpeg", 0)
print("Image size (WxH) = ", img.shape)
# Current methods
# Window size 4x4
start = time.time()
out1 = maxpool2d(img)
cv2.imwrite("maxpool2d4x4.jpeg", out1)
end = time.time()
total1 = end - start
print("Process pool2d (filter4x4) = {:6.2f} miliseconds".format(total1*1000))
# cv2.imwrite('2dpool.jpg',out)
# Our methods
oldimg = cv2.imread("Image_416x416.jpeg",0)
start = time.time()
out2 = pool2dt5d_16x16(oldimg)
cv2.imwrite("maxpool2dt5d.jpeg", out2)
end = time.time()
total2 = end - start
print("Process maxpool2dt5d (filter4x4) = {:6.2f} miliseconds".format(total2*1000))
total3 = total1 - total2
print("maxpool2dt5d faster = {:6.2f} miliseconds".format(total3*1000))
diff = out2-out1
print ("\nDifference = ", diff)