Last active
June 15, 2018 23:03
-
-
Save Alescontrela/253bd77d68b96b5d2d3d76a89f0a9a03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def maxpool(image, f=2, s=2): | |
``` | |
Downsample input `image` using a kernel size of `f` and a stride of `s` | |
``` | |
n_c, h_prev, w_prev = image.shape | |
# calculate output dimensions after the maxpooling operation. | |
h = int((h_prev - f)/s)+1 | |
w = int((w_prev - f)/s)+1 | |
# create a matrix to hold the values of the maxpooling operation. | |
downsampled = np.zeros((n_c, h, w)) | |
# slide the window over every part of the image using stride s. Take the maximum value at each step. | |
for i in range(n_c): | |
curr_y = out_y = 0 | |
# slide the max pooling window vertically across the image | |
while curr_y + f <= h_prev: | |
curr_x = out_x = 0 | |
# slide the max pooling window horizontally across the image | |
while curr_x + f <= w_prev: | |
# choose the maximum value within the window at each step and store it to the output matrix | |
downsampled[i, out_y, out_x] = np.max(image[i, curr_y:curr_y+f, curr_x:curr_x+f]) | |
curr_x += s | |
out_x += 1 | |
curr_y += s | |
out_y += 1 | |
return downsampled |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment