Search code examples
pythonnumpynumpy-ndarraynumpy-slicing

Maxpooling 2x2 array only using numpy


I want help in maxpooling using numpy. I am learning Python for data science, here I have to do maxpooling and average pooling for 2x2 matrix, the input can be 8x8 or more but I have to do maxpool for every 2x2 matrix. I have created an matrix by using

k = np.random.randint(1,64,64).reshape(8,8)

So hereby I will be getting 8x8 matrix as a random output. Form the result I want to do 2x2 max pooling. Thanks in advancei just want to perform this in numpy coding

lwhat I have done


Solution

  • You don't have to compute the necessary strides yourself, you can just inject two auxiliary dimensions to create a 4d array that's a 2d collection of 2x2 block matrices, then take the elementwise maximum over the blocks:

    import numpy as np
    
    # use 2-by-3 size to prevent some subtle indexing errors
    arr = np.random.randint(1, 64, 6*4).reshape(6, 4)
    
    m, n = arr.shape
    pooled = arr.reshape(m//2, 2, n//2, 2).max((1, 3))
    

    An example instance of the above:

    >>> arr
    array([[40, 24, 61, 60],
           [ 8, 11, 27,  5],
           [17, 41,  7, 41],
           [44,  5, 47, 13],
           [31, 53, 40, 36],
           [31, 23, 39, 26]])
    
    >>> pooled
    array([[40, 61],
           [44, 47],
           [53, 40]])
    

    For a completely general block pooling that doesn't assume 2-by-2 blocks:

    import numpy as np
    
    # again use coprime dimensions for debugging safety
    block_size = (2, 3)
    num_blocks = (7, 5)
    arr_shape = np.array(block_size) * np.array(num_blocks)
    numel = arr_shape.prod()
    arr = np.random.randint(1, numel, numel).reshape(arr_shape)
    
    m, n = arr.shape  # pretend we only have this
    pooled = arr.reshape(m//block_size[0], block_size[0],
                         n//block_size[1], block_size[1]).max((1, 3))