代码之家  ›  专栏  ›  技术社区  ›  teclnol

用numpy实现max/mean池(使用stride)

  •  0
  • teclnol  · 技术社区  · 6 年前

    我想知道如何用numpy实现一个简单的max/mean池。我在看书 Max and mean pooling with numpy ,但不幸的是,它假设步幅与内核大小相同。有没有什么新的方法可以做到这一点?如果这对任何维度都有用的话也会很好,但当然不是必要的。

    0 回复  |  直到 6 年前
        1
  •  5
  •   Andreas K.    5 年前

    下面是一个使用 stride_tricks :

    import numpy as np
    from numpy.lib.stride_tricks import as_strided
    
    def pool2d(A, kernel_size, stride, padding, pool_mode='max'):
        '''
        2D Pooling
    
        Parameters:
            A: input 2D array
            kernel_size: int, the size of the window
            stride: int, the stride of the window
            padding: int, implicit zero paddings on both sides of the input
            pool_mode: string, 'max' or 'avg'
        '''
        # Padding
        A = np.pad(A, padding, mode='constant')
    
        # Window view of A
        output_shape = ((A.shape[0] - kernel_size)//stride + 1,
                        (A.shape[1] - kernel_size)//stride + 1)
        kernel_size = (kernel_size, kernel_size)
        A_w = as_strided(A, shape = output_shape + kernel_size, 
                            strides = (stride*A.strides[0],
                                       stride*A.strides[1]) + A.strides)
        A_w = A_w.reshape(-1, *kernel_size)
    
        # Return the result of pooling
        if pool_mode == 'max':
            return A_w.max(axis=(1,2)).reshape(output_shape)
        elif pool_mode == 'avg':
            return A_w.mean(axis=(1,2)).reshape(output_shape)
    

    例子:

    >>> A = np.array([[1, 1, 2, 4],
                      [5, 6, 7, 8],
                      [3, 2, 1, 0],
                      [1, 2, 3, 4]])
    
    >>> pool2d(A, kernel_size=2, stride=2, padding=0, pool_mode='max')
    
    array([[6, 8],
           [3, 4]])
    

    enter image description here

    https://cs231n.github.io/convolutional-networks/