Search code examples
pythonnumpygaussianpooling

How to perform Gaussian pooling on a 2d array using numpy


Given a 2D(M x N) matrix, and a 2D Gaussian Mask(K x K), how do i return a matrix that is the result of Gaussian pooling using the given kernel over the image?

I'd like to use numpy if possible(Not Using "for", only use numpy) eg of 2x2 Gaussian pooling:(It could be wrong answer)

matrix:
array([[  20,  200,   -5,   23,  10, -50],
       [ -13,  134,  119,  100,  45, -79],
       [ 120,   32,   49,   25,  13,   0],
       [  40,   12,   59,   23,  32,  -1],
       [  75,  121,   69,   67,  64,  -7],
       [  39,   12,   79,   -8,  16,  -9]])
kernel:
array([[ 1/16, 1/8, 1/16],
       [  1/8, 1/4,  1/8],
       [ 1/16, 1/8, 1/16]])
soln:
array([[   87.25,   16.625],
       [ 64.8125,  29.8125]])

Solution

  • First transform you M x N matrix into a (M//K) x K x (N//K) x K array, then pointwise multiply with the kernel at the second and fourth dimensions, then sum at the second and fourth dimensions.

    np.sum(
        matrix.reshape((
            matrix.shape[-2] // kernel.shape[-2], kernel.shape[-2],
            matrix.shape[-1] // kernel.shape[-1], kernel.shape[-1],
        ))
        * kernel[np.newaxis, :, np.newaxis, :],
        axis=(-3, -1),
    )
    

    You can also replace the pointwise-multiply-then-sum by a np.tensordot call.

    np.tensordot(
        matrix.reshape((
            matrix.shape[-2] // kernel.shape[-2], kernel.shape[-2],
            matrix.shape[-1] // kernel.shape[-1], kernel.shape[-1],
        )),
        kernel,
        axes=(
            (-3, -1),
            (-2, -1),
        )
    )