二维卷积作为矩阵矩阵乘法
我知道,在一维情况下,两个向量a
和b
之间的卷积可以计算为conv(a, b)
,也可以计算为T_a
和b
之间的乘积,其中T_a
是a
对应的Toeplitz矩阵.
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 between the T_a
and b
, where T_a
is the corresponding Toeplitz matrix for a
.
是否可以将此想法扩展到2D?
Is it possible to extend this idea to 2D?
给出a = [5 1 3; 1 1 2; 2 1 3]
和b=[4 3; 1 2]
,是否可以像一维情况一样在Toeplitz矩阵中转换a
并计算T_a
和b
之间的矩阵矩阵乘积?
Given a = [5 1 3; 1 1 2; 2 1 3]
and b=[4 3; 1 2]
, is it possible to convert a
in a Toeplitz matrix and compute the matrix-matrix product between T_a
and b
as in the 1-D case?
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
.
因此,将k
展开为大小为(n-m+1)^2 × n^2
的稀疏矩阵,然后将x
展开为长向量n^2 × 1
.您可以将此稀疏矩阵与向量相乘,然后将结果向量(大小为(n-m+1)^2 × 1
)转换为n-m+1
方阵.
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.
我敢肯定,仅从阅读中很难理解这一点.因此,这里是2×2内核和3×3输入的示例.
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:
等于.
这与在x
上滑动k
的滑动窗口所获得的结果相同.
And this is the same result you would have got by doing a sliding window of k
over x
.