-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxpooling_test.py
More file actions
39 lines (32 loc) · 897 Bytes
/
maxpooling_test.py
File metadata and controls
39 lines (32 loc) · 897 Bytes
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
from timeit import default_timer as timer
import numpy as np
import torch
def dim(x):
if isinstance(x, np.ndarray):
s = list(x.shape)
else:
s = []
while isinstance(x, list):
s.append(len(x))
x = x[0]
return s
def maxpooling(X):
# maxpooling 2 * 2 squares in images of size m * n with stride 2
m, n = dim(X)
Y = [None] * (m // 2)
for i in range(0, m - 1, 2):
Y[int(i / 2)] = [None] * (n // 2)
for j in range(0, n - 1, 2):
Y[int(i / 2)][int(j / 2)] = max(
X[i][j], X[i][j + 1], X[i + 1][j], X[i + 1][j + 1]
)
return np.array(Y)
m = torch.nn.MaxPool2d(2, stride=2)
info = []
for size in range(10, 251, 10):
image = np.random.randn(size, size)
start = timer()
result = maxpooling(image)
end = timer()
info.append(end - start)
print(info)