2-D convolution as a matrix-matrix multiplication

前端 未结 4 1124
你的背包
你的背包 2020-11-29 17:07

I know that, in the 1D case, the convolution between two vectors, a and b, can be computed as conv(a, b), but also as the product betw

4条回答
  •  天命终不由人
    2020-11-29 18:05

    Yes, it is possible and you should also use a doubly block circulant matrix (which is a special case of Toeplitz matrix). I will give you an example with a small size of kernel and the input, but it is possible to construct Toeplitz matrix for any kernel. So you have a 2d input x and 2d kernel k and you want to calculate the convolution x * k. Also let's assume that k is already flipped. Let's also assume that x is of size n×n and k is m×m.

    So you unroll k into a sparse matrix of size (n-m+1)^2 × n^2, and unroll x into a long vector n^2 × 1. You compute a multiplication of this sparse matrix with a vector and convert the resulting vector (which will have a size (n-m+1)^2 × 1) into a n-m+1 square matrix.

    I am pretty sure this is hard to understand just from reading. So here is an example for 2×2 kernel and 3×3 input.

    *

    Here is a constructed matrix with a vector:

    which is equal to .

    And this is the same result you would have got by doing a sliding window of k over x.

提交回复
热议问题