-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6-pool.py
executable file
·45 lines (42 loc) · 1.74 KB
/
6-pool.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
#!/usr/bin/env python3
"""Function that performs pooling on images"""
import numpy as np
def pool(images, kernel_shape, stride, mode='max'):
"""Performs a convolution on images using multiple kernels
Args:
images: `numpy.ndarray` with shape (m, h, w)
containing multiple grayscale images
m: `int`, is the number of images
h: `int`, is the height in pixels of the images
w: `int`, is the width in pixels of the images
c: `int`, is the number of channels in the image
kernel_shape is a tuple of (kh, kw) containing
the kernel shape for the pooling
kh: `int`, is the height of the kernel
kw: `int`, is the width of the kernel
stride is a `tuple` of (sh, sw)
sh: `int`, is the stride for the height of the image
sw: `int`, is the stride for the width of the image
mode: `str`, indicates the type of pooling
max: indicates max pooling
avg: indicates average pooling
Returns:
output: `numpy.ndarray` containing the convolved images
"""
c = images.shape[3]
m, h, w = images.shape[0], images.shape[1], images.shape[2]
kh, kw = kernel_shape[0], kernel_shape[1]
sh, sw = stride[0], stride[1]
nw = int(((w - kw) / stride[1]) + 1)
nh = int(((h - kh) / stride[0]) + 1)
pooled = np.zeros((m, nh, nw, c))
for i in range(nh):
x = i * stride[0]
for j in range(nw):
y = j * stride[1]
image = images[:, x:x + kh, y:y + kw, :]
if mode == 'max':
pooled[:, i, j, :] = np.max(image, axis=(1, 2))
else:
pooled[:, i, j, :] = np.average(image, axis=(1, 2))
return pooled