.. _sec_conv_layer:
图像卷积
========
上节我们解析了卷积层的原理,现在我们看看它的实际应用。由于卷积神经网络的设计是用于探索图像数据,本节我们将以图像为例。
互相关运算
----------
严格来说,卷积层是个错误的叫法,因为它所表达的运算其实是\ *互相关运算*\ (cross-correlation),而不是卷积运算。
根据
:numref:`sec_why-conv`\ 中的描述,在卷积层中,输入张量和核张量通过互相关运算产生输出张量。
首先,我们暂时忽略通道(第三维)这一情况,看看如何处理二维图像数据和隐藏表示。在
:numref:`fig_correlation`\ 中,输入是高度为\ :math:`3`\ 、宽度为\ :math:`3`\ 的二维张量(即形状为\ :math:`3 \times 3`\ )。卷积核的高度和宽度都是\ :math:`2`\ ,而卷积核窗口(或卷积窗口)的形状由内核的高度和宽度决定(即\ :math:`2 \times 2`\ )。
.. _fig_correlation:
.. figure:: ../img/correlation.svg
二维互相关运算。阴影部分是第一个输出元素,以及用于计算输出的输入张量元素和核张量元素:\ :math:`0\times0+1\times1+3\times2+4\times3=19`.
在二维互相关运算中,卷积窗口从输入张量的左上角开始,从左到右、从上到下滑动。
当卷积窗口滑动到新一个位置时,包含在该窗口中的部分张量与卷积核张量进行按元素相乘,得到的张量再求和得到一个单一的标量值,由此我们得出了这一位置的输出张量值。
在如上例子中,输出张量的四个元素由二维互相关运算得到,这个输出高度为\ :math:`2`\ 、宽度为\ :math:`2`\ ,如下所示:
.. math::
0\times0+1\times1+3\times2+4\times3=19,\\
1\times0+2\times1+4\times2+5\times3=25,\\
3\times0+4\times1+6\times2+7\times3=37,\\
4\times0+5\times1+7\times2+8\times3=43.
注意,输出大小略小于输入大小。这是因为卷积核的宽度和高度大于1,
而卷积核只与图像中每个大小完全适合的位置进行互相关运算。
所以,输出大小等于输入大小\ :math:`n_h \times n_w`\ 减去卷积核大小\ :math:`k_h \times k_w`\ ,即:
.. math:: (n_h-k_h+1) \times (n_w-k_w+1).
这是因为我们需要足够的空间在图像上“移动”卷积核。稍后,我们将看到如何通过在图像边界周围填充零来保证有足够的空间移动卷积核,从而保持输出大小不变。
接下来,我们在\ ``corr2d``\ 函数中实现如上过程,该函数接受输入张量\ ``X``\ 和卷积核张量\ ``K``\ ,并返回输出张量\ ``Y``\ 。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
import mindspore
from mindspore import Parameter, nn, ops
def corr2d(X, K):
"""计算二维互相关运算。"""
h, w = K.shape
Y = ops.zeros((X.shape[0] - h + 1, X.shape[1] - w + 1))
for i in range(Y.shape[0]):
for j in range(Y.shape[1]):
Y[i, j] = (X[i:i + h, j:j + w] * K).sum()
return Y
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
import torch
from torch import nn
from d2l import torch as d2l
def corr2d(X, K): #@save
"""计算二维互相关运算"""
h, w = K.shape
Y = torch.zeros((X.shape[0] - h + 1, X.shape[1] - w + 1))
for i in range(Y.shape[0]):
for j in range(Y.shape[1]):
Y[i, j] = (X[i:i + h, j:j + w] * K).sum()
return Y
.. raw:: html
.. raw:: html
通过
:numref:`fig_correlation`\ 的输入张量\ ``X``\ 和卷积核张量\ ``K``\ ,我们来验证上述二维互相关运算的输出。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
X = ops.Tensor([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]])
K = ops.Tensor([[0.0, 1.0], [2.0, 3.0]])
corr2d(X, K)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
Tensor(shape=[2, 2], dtype=Float32, value=
[[ 1.90000000e+01, 2.50000000e+01],
[ 3.70000000e+01, 4.30000000e+01]])
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
X = torch.tensor([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]])
K = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
corr2d(X, K)
corr2d(X, K)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
tensor([[19., 25.],
[37., 43.]])
.. raw:: html
.. raw:: html
卷积层
------
卷积层对输入和卷积核权重进行互相关运算,并在添加标量偏置之后产生输出。
所以,卷积层中的两个被训练的参数是卷积核权重和标量偏置。
就像我们之前随机初始化全连接层一样,在训练基于卷积层的模型时,我们也随机初始化卷积核权重。
基于上面定义的\ ``corr2d``\ 函数实现二维卷积层。在\ ``__init__``\ 构造函数中,将\ ``weight``\ 和\ ``bias``\ 声明为两个模型参数。前向传播函数调用\ ``corr2d``\ 函数并添加偏置。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
class Conv2D(nn.Cell):
def __init__(self, kernel_size):
super().__init__()
self.weight = Parameter(ops.normal((kernel_size), 0, 1))
self.bias = Parameter(ops.zeros(1))
def construct(self, x):
return corr2d(x, self.weight) + self.bias
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
class Conv2D(nn.Module):
def __init__(self, kernel_size):
super().__init__()
self.weight = nn.Parameter(torch.rand(kernel_size))
self.bias = nn.Parameter(torch.zeros(1))
def forward(self, x):
return corr2d(x, self.weight) + self.bias
.. raw:: html
.. raw:: html
高度和宽度分别为\ :math:`h`\ 和\ :math:`w`\ 的卷积核可以被称为\ :math:`h \times w`\ 卷积或\ :math:`h \times w`\ 卷积核。
我们也将带有\ :math:`h \times w`\ 卷积核的卷积层称为\ :math:`h \times w`\ 卷积层。
图像中目标的边缘检测
--------------------
如下是卷积层的一个简单应用:通过找到像素变化的位置,来检测图像中不同颜色的边缘。
首先,我们构造一个\ :math:`6\times 8`\ 像素的黑白图像。中间四列为黑色(\ :math:`0`\ ),其余像素为白色(\ :math:`1`\ )。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
X = ops.ones((6, 8))
X[:, 2:6] = 0
X
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
Tensor(shape=[6, 8], dtype=Float32, value=
[[ 1.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, 1.00000000e+00, 1.00000000e+00],
[ 1.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, 1.00000000e+00, 1.00000000e+00],
[ 1.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, 1.00000000e+00, 1.00000000e+00],
[ 1.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, 1.00000000e+00, 1.00000000e+00],
[ 1.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, 1.00000000e+00, 1.00000000e+00],
[ 1.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, 1.00000000e+00, 1.00000000e+00]])
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
X = torch.ones((6, 8))
X[:, 2:6] = 0
X
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
tensor([[1., 1., 0., 0., 0., 0., 1., 1.],
[1., 1., 0., 0., 0., 0., 1., 1.],
[1., 1., 0., 0., 0., 0., 1., 1.],
[1., 1., 0., 0., 0., 0., 1., 1.],
[1., 1., 0., 0., 0., 0., 1., 1.],
[1., 1., 0., 0., 0., 0., 1., 1.]])
.. raw:: html
.. raw:: html
接下来,我们构造一个高度为\ :math:`1`\ 、宽度为\ :math:`2`\ 的卷积核\ ``K``\ 。当进行互相关运算时,如果水平相邻的两元素相同,则输出为零,否则输出为非零。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
K = ops.Tensor([[1.0, -1.0]])
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
K = torch.tensor([[1.0, -1.0]])
.. raw:: html
.. raw:: html
现在,我们对参数\ ``X``\ (输入)和\ ``K``\ (卷积核)执行互相关运算。
如下所示,输出\ ``Y``\ 中的1代表从白色到黑色的边缘,-1代表从黑色到白色的边缘,其他情况的输出为\ :math:`0`\ 。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
Y = corr2d(X, K)
Y
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
Tensor(shape=[6, 7], dtype=Float32, value=
[[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, -1.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, -1.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, -1.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, -1.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, -1.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00 ... 0.00000000e+00, -1.00000000e+00, 0.00000000e+00]])
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
Y = corr2d(X, K)
Y
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
tensor([[ 0., 1., 0., 0., 0., -1., 0.],
[ 0., 1., 0., 0., 0., -1., 0.],
[ 0., 1., 0., 0., 0., -1., 0.],
[ 0., 1., 0., 0., 0., -1., 0.],
[ 0., 1., 0., 0., 0., -1., 0.],
[ 0., 1., 0., 0., 0., -1., 0.]])
.. raw:: html
.. raw:: html
现在我们将输入的二维图像转置,再进行如上的互相关运算。
其输出如下,之前检测到的垂直边缘消失了。
不出所料,这个卷积核\ ``K``\ 只可以检测垂直边缘,无法检测水平边缘。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
corr2d(X.transpose(), K)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
Tensor(shape=[8, 5], dtype=Float32, value=
[[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
...
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]])
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
corr2d(X.t(), K)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
tensor([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
.. raw:: html
.. raw:: html
学习卷积核
----------
如果我们只需寻找黑白边缘,那么以上\ ``[1, -1]``\ 的边缘检测器足以。然而,当有了更复杂数值的卷积核,或者连续的卷积层时,我们不可能手动设计滤波器。那么我们是否可以学习由\ ``X``\ 生成\ ``Y``\ 的卷积核呢?
现在让我们看看是否可以通过仅查看“输入-输出”对来学习由\ ``X``\ 生成\ ``Y``\ 的卷积核。
我们先构造一个卷积层,并将其卷积核初始化为随机张量。接下来,在每次迭代中,我们比较\ ``Y``\ 与卷积层输出的平方误差,然后计算梯度来更新卷积核。为了简单起见,我们在此使用内置的二维卷积层,并忽略偏置。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
# 构造一个二维卷积层,它具有1个输出通道和形状为(1,2)的卷积核
conv2d = nn.Conv2d(1,1, kernel_size=(1, 2), has_bias=False, pad_mode='valid')
# 这个二维卷积层使用四维输入和输出格式(批量大小、通道、高度、宽度),
# 其中批量大小和通道数都为1
X = X.reshape((1, 1, 6, 8))
Y = Y.reshape((1, 1, 6, 7))
lr = 3e-2
loss = nn.MSELoss(reduction='sum')
optim = nn.SGD(conv2d.trainable_params(), lr)
def forward_fn(x, y):
Y_hat = conv2d(x)
return loss(Y_hat, y)
grad_fn = mindspore.value_and_grad(forward_fn, None, weights=optim.parameters)
for i in range(10):
l, grads = grad_fn(X, Y)
optim(grads)
if (i + 1) % 2 == 0:
print(f'batch {i+1}, loss {l.asnumpy():.3f}')
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
batch 2, loss 7.033
batch 4, loss 1.675
batch 6, loss 0.484
batch 8, loss 0.164
batch 10, loss 0.062
[WARNING] UTILS(2900662,799e7558a740,python3.11):2026-05-21-02:12:46.603.257 [mindspore/ccsrc/utils/signal_util.cc:51] RegisterGlobalSignalHandler] MSCONTEXT_REGISTER_INIT_FUNC register int handler
[WARNING] UTILS(2900662,799e7558a740,python3.11):2026-05-21-02:12:46.603.270 [mindspore/ccsrc/utils/signal_util.cc:57] RegisterGlobalSignalHandler] The signal has been registered
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
# 构造一个二维卷积层,它具有1个输出通道和形状为(1,2)的卷积核
conv2d = nn.Conv2d(1,1, kernel_size=(1, 2), bias=False)
# 这个二维卷积层使用四维输入和输出格式(批量大小、通道、高度、宽度),
# 其中批量大小和通道数都为1
X = X.reshape((1, 1, 6, 8))
Y = Y.reshape((1, 1, 6, 7))
lr = 3e-2 # 学习率
for i in range(10):
Y_hat = conv2d(X)
l = (Y_hat - Y) ** 2
conv2d.zero_grad()
l.sum().backward()
# 迭代卷积核
conv2d.weight.data[:] -= lr * conv2d.weight.grad
if (i + 1) % 2 == 0:
print(f'epoch {i+1}, loss {l.sum():.3f}')
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
epoch 2, loss 12.944
epoch 4, loss 3.388
epoch 6, loss 1.067
epoch 8, loss 0.383
epoch 10, loss 0.148
.. raw:: html
.. raw:: html
在\ :math:`10`\ 次迭代之后,误差已经降到足够低。现在我们来看看我们所学的卷积核的权重张量。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
conv2d.weight.data.reshape((1, 2))
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
Tensor(shape=[1, 2], dtype=Float32, value=
[[ 1.01318908e+00, -9.63589489e-01]])
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
conv2d.weight.data.reshape((1, 2))
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
tensor([[ 1.0242, -0.9465]])
.. raw:: html
.. raw:: html
细心的读者一定会发现,我们学习到的卷积核权重非常接近我们之前定义的卷积核\ ``K``\ 。
互相关和卷积
------------
回想一下我们在
:numref:`sec_why-conv`\ 中观察到的互相关和卷积运算之间的对应关系。
为了得到正式的\ *卷积*\ 运算输出,我们需要执行
:eq:`eq_2d-conv-discrete`\ 中定义的严格卷积运算,而不是互相关运算。
幸运的是,它们差别不大,我们只需水平和垂直翻转二维卷积核张量,然后对输入张量执行\ *互相关*\ 运算。
值得注意的是,由于卷积核是从数据中学习到的,因此无论这些层执行严格的卷积运算还是互相关运算,卷积层的输出都不会受到影响。
为了说明这一点,假设卷积层执行\ *互相关*\ 运算并学习
:numref:`fig_correlation`\ 中的卷积核,该卷积核在这里由矩阵\ :math:`\mathbf{K}`\ 表示。
假设其他条件不变,当这个层执行严格的\ *卷积*\ 时,学习的卷积核\ :math:`\mathbf{K}'`\ 在水平和垂直翻转之后将与\ :math:`\mathbf{K}`\ 相同。
也就是说,当卷积层对
:numref:`fig_correlation`\ 中的输入和\ :math:`\mathbf{K}'`\ 执行严格\ *卷积*\ 运算时,将得到与互相关运算
:numref:`fig_correlation`\ 中相同的输出。
为了与深度学习文献中的标准术语保持一致,我们将继续把“互相关运算”称为卷积运算,尽管严格地说,它们略有不同。
此外,对于卷积核张量上的权重,我们称其为\ *元素*\ 。
特征映射和感受野
----------------
如在 :numref:`subsec_why-conv-channels`\ 中所述,
:numref:`fig_correlation`\ 中输出的卷积层有时被称为\ *特征映射*\ (feature
map),因为它可以被视为一个输入映射到下一层的空间维度的转换器。
在卷积神经网络中,对于某一层的任意元素\ :math:`x`\ ,其\ *感受野*\ (receptive
field)是指在前向传播期间可能影响\ :math:`x`\ 计算的所有元素(来自所有先前层)。
请注意,感受野可能大于输入的实际大小。让我们用
:numref:`fig_correlation`\ 为例来解释感受野:
给定\ :math:`2 \times 2`\ 卷积核,阴影输出元素值\ :math:`19`\ 的感受野是输入阴影部分的四个元素。
假设之前输出为\ :math:`\mathbf{Y}`\ ,其大小为\ :math:`2 \times 2`\ ,现在我们在其后附加一个卷积层,该卷积层以\ :math:`\mathbf{Y}`\ 为输入,输出单个元素\ :math:`z`\ 。
在这种情况下,\ :math:`\mathbf{Y}`\ 上的\ :math:`z`\ 的感受野包括\ :math:`\mathbf{Y}`\ 的所有四个元素,而输入的感受野包括最初所有九个输入元素。
因此,当一个特征图中的任意元素需要检测更广区域的输入特征时,我们可以构建一个更深的网络。
小结
----
- 二维卷积层的核心计算是二维互相关运算。最简单的形式是,对二维输入数据和卷积核执行互相关操作,然后添加一个偏置。
- 我们可以设计一个卷积核来检测图像的边缘。
- 我们可以从数据中学习卷积核的参数。
- 学习卷积核时,无论用严格卷积运算或互相关运算,卷积层的输出不会受太大影响。
- 当需要检测输入特征中更广区域时,我们可以构建一个更深的卷积网络。
练习
----
1. 构建一个具有对角线边缘的图像\ ``X``\ 。
1. 如果将本节中举例的卷积核\ ``K``\ 应用于\ ``X``\ ,会发生什么情况?
2. 如果转置\ ``X``\ 会发生什么?
3. 如果转置\ ``K``\ 会发生什么?
2. 在我们创建的\ ``Conv2D``\ 自动求导时,有什么错误消息?
3. 如何通过改变输入张量和卷积核张量,将互相关运算表示为矩阵乘法?
4. 手工设计一些卷积核。
1. 二阶导数的核的形式是什么?
2. 积分的核的形式是什么?
3. 得到\ :math:`d`\ 次导数的最小核的大小是多少?
.. raw:: html
.. raw:: html
`讨论 `__
.. raw:: html
.. raw:: html
`讨论 `__
.. raw:: html
.. raw:: html