卷积运算和相关运算

卷积运算和相关运算在深度学习领域属于常见的运算,本文将从二者计算的公式和实现代码进行分析。

卷积运算

给定一个图像$X\in \mathbb{R}^{M\times N}$,和滤波器$W\in \mathbb{R}^{m\times n}$,一般$m \ll M $,$n \ll N $,其卷积为:
$$y_{ij} = \sum_{u=1}^m \sum_{v=1}^n w_{uv} \cdot x_{i-u+1, j-v+1}$$

  • 代码实现
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    def conv(image, kernel):
    """ An implementation of convolution filter. This function uses element-wise multiplication and np.sum() to efficiently compute weighted sum of neighborhood at each
    pixel.
    Args:
    image: numpy array of shape (Hi, Wi)
    kernel: numpy array of shape (Hk, Wk)
    Returns:
    out: numpy array of shape (Hi, Wi)
    """
    Hi, Wi = image.shape
    Hk, Wk = kernel.shape
    out = np.zeros((Hi, Wi))
    image = np.pad(image, (Hk//2, Wk//2), 'constant', constant_values=0)
    print(image.shape)
    kernel = np.flip(np.flip(kernel, 1), 0)
    for i in range(Hi):
    for j in range(Wi):
    out[i, j] = sum(
    sum(kernel * image[i:i + Hk, j:j + Wk]))
    return out

相关运算

因为卷积运算需要对卷积核进行翻转,然而翻转对于深度卷积神经网络来说并不必须,所以通常深度学习框架,如Pytorch,Tensorflow采用相关运算取代卷积运算来避免不必要的运算复杂度的增加。
同样给定一个图像$X\in \mathbb{R}^{M\times N}$,和滤波器$W\in \mathbb{R}^{m\times n}$,一般$m \ll M $,$n \ll N $,其互相关为:
$$y_{ij} = \sum_{u=1}^m \sum_{v=1}^n w_{uv} \cdot x_{i+u-1, j+v-1}$$

  • 代码实现
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    def cross_correlation(f, g):
    """ Cross-correlation of f and g
    Args:
    f: numpy array of shape (Hf, Wf)
    g: numpy array of shape (Hg, Wg)
    Returns:
    out: numpy array of shape (Hf, Wf)
    """
    out = None
    g = np.flip(np.flip(g, 0), 1)
    out = conv(f, g)
    return out
    上面的代码中,我利用前面的卷积运算,经过两轮的翻转,卷积核与最初的输入值保持一致,这样也就实现了相关系数的计算。
非常感谢各位老板投喂!