-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxPooling.py
50 lines (44 loc) · 2.03 KB
/
MaxPooling.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
# MaxPooling.py
# https://stackoverflow.com/questions/41699513/how-to-update-the-weights-of-a-deconvolutional-layer
# https://stackoverflow.com/questions/34254679/how-can-i-implement-deconvolution-layer-for-a-cnn-in-numpy
# https://stackoverflow.com/questions/40615034/understanding-scipy-deconvolve
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.deconvolve.html
# https://stackoverflow.com/questions/41699513/how-to-update-the-weights-of-a-deconvolutional-layer
# https://github.com/many-facedgod/Numpy-Atrous-Transposed-CNN
# https://towardsdatascience.com/advanced-numpy-master-stride-tricks-with-25-illustrated-exercises-923a9393ab20
# https://remykarem.medium.com/
# https://medium.com/analytics-vidhya/simple-cnn-using-numpy-part-iii-relu-max-pooling-softmax-c03a3377eaf2
# https://stackoverflow.com/questions/42463172/how-to-perform-max-mean-pooling-on-a-2d-array-using-numpy
# https://nbviewer.org/github/craffel/crucialpython/blob/master/week3/stride_tricks.ipynb
import numpy as np
as_strided = np.lib.stride_tricks.as_strided
"""
# x = np.random.rand(6,6).astype(dtype=np.float16)
x = np.arange(1,37).reshape(6,6).astype(dtype=np.int8)
out = as_strided(x, shape=(3,3,2,2), strides=(12,2,6,1))
print(out.shape)
"""
"""
Kh, Kw = 2, 4 # ksize = 3,3
x = np.random.rand(24,26).astype(dtype=np.float16)
dtypeSize = x.itemsize
Xh, Xw = x.shape
print(dtypeSize)
print(Xh, Xw)
out = as_strided(x, shape=(Xh//Kh, Xw//Kw, Kh,Kw), strides=(Xw*Kh*dtypeSize,Kw*dtypeSize,Xw//dtypeSize,dtypeSize))
print(out.shape)
"""
def pool(x, ksize=(2,2)):
Xh, Xw = x.shape
Kh, Kw = ksize
dtypeSize = x.itemsize # default stride along the first axis (column)
return as_strided(x, shape=(Xh//Kh, Xw//Kw, Kh,Kw),
strides=(Xw*Kh*dtypeSize, Kw*dtypeSize,
Xw//dtypeSize, dtypeSize)).max(axis=(-2,-1))
# x = np.random.rand(24,24)
# x = np.arange(1,37).astype(np.int8).reshape(6,6)
x = np.random.randint(-9,10,(8,6)).astype(np.int8)
pooled = pool(x, (3,2))
print(x)
print(pooled)
# print(pooled.shape)