In [3]:
import numpy as np
In [18]:
def pooling_strided_naive(image, pool_size, stride, function):
output_x = int((image.shape[0] - pool_size) / stride + 1)
output_y = int((image.shape[1] - pool_size) / stride + 1)
output = np.zeros((output_x, output_y))
# walk over output rows
for i in range(output.shape[0]):
# walk over output columns
for j in range(output.shape[1]):
image_x = i * stride
image_y = j * stride
image_x_end = image_x + pool_size
image_y_end = image_y + pool_size
output[i, j] = function(image[image_x:image_x_end,
image_y:image_y_end])
return output
In [19]:
image = np.array([
[1, 3, 2, 1],
[2, 9, 1, 1],
[1, 3, 2, 3],
[5, 6, 1, 2]
])
In [20]:
# Max pooling
pooling_strided_naive(image, 2, 2, np.max)
Out[20]:
In [21]:
# Average pooling
pooling_strided_naive(image, 2, 2, np.mean)
Out[21]:
In [22]:
image = np.array([
[1, 3, 2, 1, 3],
[2, 9, 1, 1, 5],
[1, 3, 2, 3, 2],
[8, 3, 5, 1, 0],
[5, 6, 1, 2, 9]
])
In [23]:
pooling_strided_naive(image, 3, 1, np.max)
Out[23]: