|
| 1 | +# Source : https://computersciencewiki.org/index.php/Max-pooling_/_Pooling |
| 2 | +# Importing the libraries |
| 3 | +import numpy as np |
| 4 | +from PIL import Image |
| 5 | + |
| 6 | + |
| 7 | +# Maxpooling Function |
| 8 | +def maxpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: |
| 9 | + """ |
| 10 | + This function is used to perform maxpooling on the input array of 2D matrix(image) |
| 11 | + Args: |
| 12 | + arr: numpy array |
| 13 | + size: size of pooling matrix |
| 14 | + stride: the number of pixels shifts over the input matrix |
| 15 | + Returns: |
| 16 | + numpy array of maxpooled matrix |
| 17 | + Sample Input Output: |
| 18 | + >>> maxpooling([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], 2, 2) |
| 19 | + array([[ 6., 8.], |
| 20 | + [14., 16.]]) |
| 21 | + >>> maxpooling([[147, 180, 122],[241, 76, 32],[126, 13, 157]], 2, 1) |
| 22 | + array([[241., 180.], |
| 23 | + [241., 157.]]) |
| 24 | + """ |
| 25 | + arr = np.array(arr) |
| 26 | + if arr.shape[0] != arr.shape[1]: |
| 27 | + raise ValueError("The input array is not a square matrix") |
| 28 | + i = 0 |
| 29 | + j = 0 |
| 30 | + mat_i = 0 |
| 31 | + mat_j = 0 |
| 32 | + |
| 33 | + # compute the shape of the output matrix |
| 34 | + maxpool_shape = (arr.shape[0] - size) // stride + 1 |
| 35 | + # initialize the output matrix with zeros of shape maxpool_shape |
| 36 | + updated_arr = np.zeros((maxpool_shape, maxpool_shape)) |
| 37 | + |
| 38 | + while i < arr.shape[0]: |
| 39 | + if i + size > arr.shape[0]: |
| 40 | + # if the end of the matrix is reached, break |
| 41 | + break |
| 42 | + while j < arr.shape[1]: |
| 43 | + # if the end of the matrix is reached, break |
| 44 | + if j + size > arr.shape[1]: |
| 45 | + break |
| 46 | + # compute the maximum of the pooling matrix |
| 47 | + updated_arr[mat_i][mat_j] = np.max(arr[i : i + size, j : j + size]) |
| 48 | + # shift the pooling matrix by stride of column pixels |
| 49 | + j += stride |
| 50 | + mat_j += 1 |
| 51 | + |
| 52 | + # shift the pooling matrix by stride of row pixels |
| 53 | + i += stride |
| 54 | + mat_i += 1 |
| 55 | + |
| 56 | + # reset the column index to 0 |
| 57 | + j = 0 |
| 58 | + mat_j = 0 |
| 59 | + |
| 60 | + return updated_arr |
| 61 | + |
| 62 | + |
| 63 | +# Averagepooling Function |
| 64 | +def avgpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray: |
| 65 | + """ |
| 66 | + This function is used to perform avgpooling on the input array of 2D matrix(image) |
| 67 | + Args: |
| 68 | + arr: numpy array |
| 69 | + size: size of pooling matrix |
| 70 | + stride: the number of pixels shifts over the input matrix |
| 71 | + Returns: |
| 72 | + numpy array of avgpooled matrix |
| 73 | + Sample Input Output: |
| 74 | + >>> avgpooling([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], 2, 2) |
| 75 | + array([[ 3., 5.], |
| 76 | + [11., 13.]]) |
| 77 | + >>> avgpooling([[147, 180, 122],[241, 76, 32],[126, 13, 157]], 2, 1) |
| 78 | + array([[161., 102.], |
| 79 | + [114., 69.]]) |
| 80 | + """ |
| 81 | + arr = np.array(arr) |
| 82 | + if arr.shape[0] != arr.shape[1]: |
| 83 | + raise ValueError("The input array is not a square matrix") |
| 84 | + i = 0 |
| 85 | + j = 0 |
| 86 | + mat_i = 0 |
| 87 | + mat_j = 0 |
| 88 | + |
| 89 | + # compute the shape of the output matrix |
| 90 | + avgpool_shape = (arr.shape[0] - size) // stride + 1 |
| 91 | + # initialize the output matrix with zeros of shape avgpool_shape |
| 92 | + updated_arr = np.zeros((avgpool_shape, avgpool_shape)) |
| 93 | + |
| 94 | + while i < arr.shape[0]: |
| 95 | + # if the end of the matrix is reached, break |
| 96 | + if i + size > arr.shape[0]: |
| 97 | + break |
| 98 | + while j < arr.shape[1]: |
| 99 | + # if the end of the matrix is reached, break |
| 100 | + if j + size > arr.shape[1]: |
| 101 | + break |
| 102 | + # compute the average of the pooling matrix |
| 103 | + updated_arr[mat_i][mat_j] = int(np.average(arr[i : i + size, j : j + size])) |
| 104 | + # shift the pooling matrix by stride of column pixels |
| 105 | + j += stride |
| 106 | + mat_j += 1 |
| 107 | + |
| 108 | + # shift the pooling matrix by stride of row pixels |
| 109 | + i += stride |
| 110 | + mat_i += 1 |
| 111 | + # reset the column index to 0 |
| 112 | + j = 0 |
| 113 | + mat_j = 0 |
| 114 | + |
| 115 | + return updated_arr |
| 116 | + |
| 117 | + |
| 118 | +# Main Function |
| 119 | +if __name__ == "__main__": |
| 120 | + from doctest import testmod |
| 121 | + |
| 122 | + testmod(name="avgpooling", verbose=True) |
| 123 | + |
| 124 | + # Loading the image |
| 125 | + image = Image.open("path_to_image") |
| 126 | + |
| 127 | + # Converting the image to numpy array and maxpooling, displaying the result |
| 128 | + # Ensure that the image is a square matrix |
| 129 | + |
| 130 | + Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show() |
| 131 | + |
| 132 | + # Converting the image to numpy array and averagepooling, displaying the result |
| 133 | + # Ensure that the image is a square matrix |
| 134 | + |
| 135 | + Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show() |
0 commit comments