source https computersciencewiki orgindex phpmaxpoolingpooling importing the libraries maxpooling function this function is used to perform maxpooling on the input array of 2d matriximage args arr numpy array size size of pooling matrix stride the number of pixels shifts over the input matrix returns numpy array of maxpooled matrix sample input output maxpooling1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 2 array 6 8 14 16 maxpooling147 180 122 241 76 32 126 13 157 2 1 array241 180 241 157 compute the shape of the output matrix initialize the output matrix with zeros of shape maxpoolshape if the end of the matrix is reached break if the end of the matrix is reached break compute the maximum of the pooling matrix shift the pooling matrix by stride of column pixels shift the pooling matrix by stride of row pixels reset the column index to 0 averagepooling function this function is used to perform avgpooling on the input array of 2d matriximage args arr numpy array size size of pooling matrix stride the number of pixels shifts over the input matrix returns numpy array of avgpooled matrix sample input output avgpooling1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 2 array 3 5 11 13 avgpooling147 180 122 241 76 32 126 13 157 2 1 array161 102 114 69 compute the shape of the output matrix initialize the output matrix with zeros of shape avgpoolshape if the end of the matrix is reached break if the end of the matrix is reached break compute the average of the pooling matrix shift the pooling matrix by stride of column pixels shift the pooling matrix by stride of row pixels reset the column index to 0 main function loading the image converting the image to numpy array and maxpooling displaying the result ensure that the image is a square matrix converting the image to numpy array and averagepooling displaying the result ensure that the image is a square matrix source https computersciencewiki org index php max pooling_ _pooling importing the libraries maxpooling function this function is used to perform maxpooling on the input array of 2d matrix image args arr numpy array size size of pooling matrix stride the number of pixels shifts over the input matrix returns numpy array of maxpooled matrix sample input output maxpooling 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 2 array 6 8 14 16 maxpooling 147 180 122 241 76 32 126 13 157 2 1 array 241 180 241 157 compute the shape of the output matrix initialize the output matrix with zeros of shape maxpool_shape if the end of the matrix is reached break if the end of the matrix is reached break compute the maximum of the pooling matrix shift the pooling matrix by stride of column pixels shift the pooling matrix by stride of row pixels reset the column index to 0 averagepooling function this function is used to perform avgpooling on the input array of 2d matrix image args arr numpy array size size of pooling matrix stride the number of pixels shifts over the input matrix returns numpy array of avgpooled matrix sample input output avgpooling 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 2 array 3 5 11 13 avgpooling 147 180 122 241 76 32 126 13 157 2 1 array 161 102 114 69 compute the shape of the output matrix initialize the output matrix with zeros of shape avgpool_shape if the end of the matrix is reached break if the end of the matrix is reached break compute the average of the pooling matrix shift the pooling matrix by stride of column pixels shift the pooling matrix by stride of row pixels reset the column index to 0 main function loading the image converting the image to numpy array and maxpooling displaying the result ensure that the image is a square matrix converting the image to numpy array and averagepooling displaying the result ensure that the image is a square matrix
import numpy as np
from PIL import Image



def maxpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray:
    
    arr = np.array(arr)
    if arr.shape[0] != arr.shape[1]:
        raise ValueError("The input array is not a square matrix")
    i = 0
    j = 0
    mat_i = 0
    mat_j = 0

    
    maxpool_shape = (arr.shape[0] - size) // stride + 1
    
    updated_arr = np.zeros((maxpool_shape, maxpool_shape))

    while i < arr.shape[0]:
        if i + size > arr.shape[0]:
            
            break
        while j < arr.shape[1]:
            
            if j + size > arr.shape[1]:
                break
            
            updated_arr[mat_i][mat_j] = np.max(arr[i : i + size, j : j + size])
            
            j += stride
            mat_j += 1

        
        i += stride
        mat_i += 1

        
        j = 0
        mat_j = 0

    return updated_arr



def avgpooling(arr: np.ndarray, size: int, stride: int) -> np.ndarray:
    
    arr = np.array(arr)
    if arr.shape[0] != arr.shape[1]:
        raise ValueError("The input array is not a square matrix")
    i = 0
    j = 0
    mat_i = 0
    mat_j = 0

    
    avgpool_shape = (arr.shape[0] - size) // stride + 1
    
    updated_arr = np.zeros((avgpool_shape, avgpool_shape))

    while i < arr.shape[0]:
        
        if i + size > arr.shape[0]:
            break
        while j < arr.shape[1]:
            
            if j + size > arr.shape[1]:
                break
            
            updated_arr[mat_i][mat_j] = int(np.average(arr[i : i + size, j : j + size]))
            
            j += stride
            mat_j += 1

        
        i += stride
        mat_i += 1
        
        j = 0
        mat_j = 0

    return updated_arr



if __name__ == "__main__":
    from doctest import testmod

    testmod(name="avgpooling", verbose=True)

    
    image = Image.open("path_to_image")

    
    

    Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

    
    

    Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()