卷积神经网络(LeNet)

:label:sec_lenet

通过之前几节,我们学习了构建一个完整卷积神经网络的所需组件。 回想一下,之前我们将softmax回归模型( :numref:sec_softmax_scratch)和多层感知机模型( :numref:sec_mlp_scratch)应用于Fashion-MNIST数据集中的服装图片。 为了能够应用softmax回归和多层感知机,我们首先将每个大小为$28\times28$的图像展平为一个784维的固定长度的一维向量,然后用全连接层对其进行处理。 而现在,我们已经掌握了卷积层的处理方法,我们可以在图像中保留空间结构。 同时,用卷积层代替全连接层的另一个好处是:模型更简洁、所需的参数更少。

在本节中,我们将介绍LeNet,它是最早发布的卷积神经网络之一,因其在计算机视觉任务中的高效性能而受到广泛关注。 这个模型是由AT&T贝尔实验室的研究员Yann LeCun在1989年提出的(并以其命名),目的是识别图像 :cite:LeCun.Bottou.Bengio.ea.1998中的手写数字。 当时,Yann LeCun发表了第一篇通过反向传播成功训练卷积神经网络的研究,这项工作代表了十多年来神经网络研究开发的成果。

当时,LeNet取得了与支持向量机(support vector machines)性能相媲美的成果,成为监督学习的主流方法。 LeNet被广泛用于自动取款机(ATM)机中,帮助识别处理支票的数字。 时至今日,一些自动取款机仍在运行Yann LeCun和他的同事Leon Bottou在上世纪90年代写的代码呢!

LeNet

总体来看,(LeNet(LeNet-5)由两个部分组成:)(卷积编码器和全连接层密集块)

  • 卷积编码器:由两个卷积层组成;
  • 全连接层密集块:由三个全连接层组成。

该架构如 :numref:img_lenet所示。

LeNet中的数据流。输入是手写数字,输出为10种可能结果的概率。 :label:img_lenet

每个卷积块中的基本单元是一个卷积层、一个sigmoid激活函数和平均汇聚层。请注意,虽然ReLU和最大汇聚层更有效,但它们在20世纪90年代还没有出现。每个卷积层使用$5\times 5$卷积核和一个sigmoid激活函数。这些层将输入映射到多个二维特征输出,通常同时增加通道的数量。第一卷积层有6个输出通道,而第二个卷积层有16个输出通道。每个$2\times2$池操作(步幅2)通过空间下采样将维数减少4倍。卷积的输出形状由批量大小、通道数、高度、宽度决定。

为了将卷积块的输出传递给稠密块,我们必须在小批量中展平每个样本。换言之,我们将这个四维输入转换成全连接层所期望的二维输入。这里的二维表示的第一个维度索引小批量中的样本,第二个维度给出每个样本的平面向量表示。LeNet的稠密块有三个全连接层,分别有120、84和10个输出。因为我们在执行分类任务,所以输出层的10维对应于最后输出结果的数量。

通过下面的LeNet代码,你会相信用深度学习框架实现此类模型非常简单。我们只需要实例化一个Sequential块并将需要的层连接在一起。

```{.python .input} from d2l import mxnet as d2l from mxnet import autograd, gluon, init, np, npx from mxnet.gluon import nn npx.set_np()

net = nn.Sequential() net.add(nn.Conv2D(channels=6, kernel_size=5, padding=2, activation=’sigmoid’), nn.AvgPool2D(pool_size=2, strides=2), nn.Conv2D(channels=16, kernel_size=5, activation=’sigmoid’), nn.AvgPool2D(pool_size=2, strides=2),

  1. # 默认情况下,“Dense”会自动将形状为(批量大小,通道数,高度,宽度)的输入,
  2. # 转换为形状为(批量大小,通道数*高度*宽度)的输入
  3. nn.Dense(120, activation='sigmoid'),
  4. nn.Dense(84, activation='sigmoid'),
  5. nn.Dense(10))
  1. ```{.python .input}
  2. #@tab pytorch
  3. from d2l import torch as d2l
  4. import torch
  5. from torch import nn
  6. net = nn.Sequential(
  7. nn.Conv2d(1, 6, kernel_size=5, padding=2), nn.Sigmoid(),
  8. nn.AvgPool2d(kernel_size=2, stride=2),
  9. nn.Conv2d(6, 16, kernel_size=5), nn.Sigmoid(),
  10. nn.AvgPool2d(kernel_size=2, stride=2),
  11. nn.Flatten(),
  12. nn.Linear(16 * 5 * 5, 120), nn.Sigmoid(),
  13. nn.Linear(120, 84), nn.Sigmoid(),
  14. nn.Linear(84, 10))

```{.python .input}

@tab tensorflow

from d2l import tensorflow as d2l import tensorflow as tf

def net(): return tf.keras.models.Sequential([ tf.keras.layers.Conv2D(filters=6, kernel_size=5, activation=’sigmoid’, padding=’same’), tf.keras.layers.AvgPool2D(pool_size=2, strides=2), tf.keras.layers.Conv2D(filters=16, kernel_size=5, activation=’sigmoid’), tf.keras.layers.AvgPool2D(pool_size=2, strides=2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(120, activation=’sigmoid’), tf.keras.layers.Dense(84, activation=’sigmoid’), tf.keras.layers.Dense(10)])

  1. 我们对原始模型做了一点小改动,去掉了最后一层的高斯激活。除此之外,这个网络与最初的LeNet-5一致。
  2. 下面,我们将一个大小为$28 \times 28$的单通道(黑白)图像通过LeNet。通过在每一层打印输出的形状,我们可以[**检查模型**],以确保其操作与我们期望的 :numref:`img_lenet_vert`一致。
  3. ![LeNet 的简化版。](/uploads/projects/d2l-ai-CN/img/lenet-vert.svg)
  4. :label:`img_lenet_vert`
  5. ```{.python .input}
  6. X = np.random.uniform(size=(1, 1, 28, 28))
  7. net.initialize()
  8. for layer in net:
  9. X = layer(X)
  10. print(layer.name, 'output shape:\t', X.shape)

```{.python .input}

@tab pytorch

X = torch.rand(size=(1, 1, 28, 28), dtype=torch.float32) for layer in net: X = layer(X) print(layer.class.name,’output shape: \t’,X.shape)

  1. ```{.python .input}
  2. #@tab tensorflow
  3. X = tf.random.uniform((1, 28, 28, 1))
  4. for layer in net().layers:
  5. X = layer(X)
  6. print(layer.__class__.__name__, 'output shape: \t', X.shape)

请注意,在整个卷积块中,与上一层相比,每一层特征的高度和宽度都减小了。 第一个卷积层使用2个像素的填充,来补偿$5 \times 5$卷积核导致的特征减少。 相反,第二个卷积层没有填充,因此高度和宽度都减少了4个像素。 随着层叠的上升,通道的数量从输入时的1个,增加到第一个卷积层之后的6个,再到第二个卷积层之后的16个。 同时,每个汇聚层的高度和宽度都减半。最后,每个全连接层减少维数,最终输出一个维数与结果分类数相匹配的输出。

模型训练

现在我们已经实现了LeNet,让我们看看[LeNet在Fashion-MNIST数据集上的表现]。

```{.python .input}

@tab all

batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size)

  1. 虽然卷积神经网络的参数较少,但与深度的多层感知机相比,它们的计算成本仍然很高,因为每个参数都参与更多的乘法。
  2. 如果你有机会使用GPU,可以用它加快训练。
  3. :begin_tab:`mxnet, pytorch`
  4. 为了进行评估,我们需要[**对**] :numref:`sec_softmax_scratch`中描述的(**`evaluate_accuracy`函数进行轻微的修改**)。
  5. 由于完整的数据集位于内存中,因此在模型使用GPU计算数据集之前,我们需要将其复制到显存中。
  6. :end_tab:
  7. ```{.python .input}
  8. def evaluate_accuracy_gpu(net, data_iter, device=None): #@save
  9. """使用GPU计算模型在数据集上的精度"""
  10. if not device: # 查询第一个参数所在的第一个设备
  11. device = list(net.collect_params().values())[0].list_ctx()[0]
  12. metric = d2l.Accumulator(2) # 正确预测的数量,总预测的数量
  13. for X, y in data_iter:
  14. X, y = X.as_in_ctx(device), y.as_in_ctx(device)
  15. metric.add(d2l.accuracy(net(X), y), d2l.size(y))
  16. return metric[0] / metric[1]

```{.python .input}

@tab pytorch

def evaluate_accuracy_gpu(net, data_iter, device=None): #@save “””使用GPU计算模型在数据集上的精度””” if isinstance(net, nn.Module): net.eval() # 设置为评估模式 if not device: device = next(iter(net.parameters())).device

  1. # 正确预测的数量,总预测的数量
  2. metric = d2l.Accumulator(2)
  3. with torch.no_grad():
  4. for X, y in data_iter:
  5. if isinstance(X, list):
  6. # BERT微调所需的(之后将介绍)
  7. X = [x.to(device) for x in X]
  8. else:
  9. X = X.to(device)
  10. y = y.to(device)
  11. metric.add(d2l.accuracy(net(X), y), d2l.size(y))
  12. return metric[0] / metric[1]
  1. [**为了使用GPU,我们还需要一点小改动**]。
  2. :numref:`sec_softmax_scratch`中定义的`train_epoch_ch3`不同,在进行正向和反向传播之前,我们需要将每一小批量数据移动到我们指定的设备(例如GPU)上。
  3. 如下所示,训练函数`train_ch6`也类似于 :numref:`sec_softmax_scratch`中定义的`train_ch3`
  4. 由于我们将实现多层神经网络,因此我们将主要使用高级API
  5. 以下训练函数假定从高级API创建的模型作为输入,并进行相应的优化。
  6. 我们使用在 :numref:`subsec_xavier`中介绍的Xavier随机初始化模型参数。
  7. 与全连接层一样,我们使用交叉熵损失函数和小批量随机梯度下降。
  8. ```{.python .input}
  9. #@save
  10. def train_ch6(net, train_iter, test_iter, num_epochs, lr, device):
  11. """用GPU训练模型(在第六章定义)"""
  12. net.initialize(force_reinit=True, ctx=device, init=init.Xavier())
  13. loss = gluon.loss.SoftmaxCrossEntropyLoss()
  14. trainer = gluon.Trainer(net.collect_params(),
  15. 'sgd', {'learning_rate': lr})
  16. animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs],
  17. legend=['train loss', 'train acc', 'test acc'])
  18. timer, num_batches = d2l.Timer(), len(train_iter)
  19. for epoch in range(num_epochs):
  20. metric = d2l.Accumulator(3) # 训练损失之和,训练准确率之和,样本数
  21. for i, (X, y) in enumerate(train_iter):
  22. timer.start()
  23. # 下面是与“d2l.train_epoch_ch3”的主要不同
  24. X, y = X.as_in_ctx(device), y.as_in_ctx(device)
  25. with autograd.record():
  26. y_hat = net(X)
  27. l = loss(y_hat, y)
  28. l.backward()
  29. trainer.step(X.shape[0])
  30. metric.add(l.sum(), d2l.accuracy(y_hat, y), X.shape[0])
  31. timer.stop()
  32. train_l = metric[0] / metric[2]
  33. train_acc = metric[1] / metric[2]
  34. if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
  35. animator.add(epoch + (i + 1) / num_batches,
  36. (train_l, train_acc, None))
  37. test_acc = evaluate_accuracy_gpu(net, test_iter)
  38. animator.add(epoch + 1, (None, None, test_acc))
  39. print(f'loss {train_l:.3f}, train acc {train_acc:.3f}, '
  40. f'test acc {test_acc:.3f}')
  41. print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec '
  42. f'on {str(device)}')

```{.python .input}

@tab pytorch

@save

def trainch6(net, train_iter, test_iter, num_epochs, lr, device): “””用GPU训练模型(在第六章定义)””” def init_weights(m): if type(m) == nn.Linear or type(m) == nn.Conv2d: nn.init.xavier_uniform(m.weight) net.apply(init_weights) print(‘training on’, device) net.to(device) optimizer = torch.optim.SGD(net.parameters(), lr=lr) loss = nn.CrossEntropyLoss() animator = d2l.Animator(xlabel=’epoch’, xlim=[1, num_epochs], legend=[‘train loss’, ‘train acc’, ‘test acc’]) timer, num_batches = d2l.Timer(), len(train_iter) for epoch in range(num_epochs):

  1. # 训练损失之和,训练准确率之和,样本数
  2. metric = d2l.Accumulator(3)
  3. net.train()
  4. for i, (X, y) in enumerate(train_iter):
  5. timer.start()
  6. optimizer.zero_grad()
  7. X, y = X.to(device), y.to(device)
  8. y_hat = net(X)
  9. l = loss(y_hat, y)
  10. l.backward()
  11. optimizer.step()
  12. with torch.no_grad():
  13. metric.add(l * X.shape[0], d2l.accuracy(y_hat, y), X.shape[0])
  14. timer.stop()
  15. train_l = metric[0] / metric[2]
  16. train_acc = metric[1] / metric[2]
  17. if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
  18. animator.add(epoch + (i + 1) / num_batches,
  19. (train_l, train_acc, None))
  20. test_acc = evaluate_accuracy_gpu(net, test_iter)
  21. animator.add(epoch + 1, (None, None, test_acc))
  22. print(f'loss {train_l:.3f}, train acc {train_acc:.3f}, '
  23. f'test acc {test_acc:.3f}')
  24. print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec '
  25. f'on {str(device)}')
  1. ```{.python .input}
  2. #@tab tensorflow
  3. class TrainCallback(tf.keras.callbacks.Callback): #@save
  4. """一个以可视化的训练进展的回调"""
  5. def __init__(self, net, train_iter, test_iter, num_epochs, device_name):
  6. self.timer = d2l.Timer()
  7. self.animator = d2l.Animator(
  8. xlabel='epoch', xlim=[1, num_epochs], legend=[
  9. 'train loss', 'train acc', 'test acc'])
  10. self.net = net
  11. self.train_iter = train_iter
  12. self.test_iter = test_iter
  13. self.num_epochs = num_epochs
  14. self.device_name = device_name
  15. def on_epoch_begin(self, epoch, logs=None):
  16. self.timer.start()
  17. def on_epoch_end(self, epoch, logs):
  18. self.timer.stop()
  19. test_acc = self.net.evaluate(
  20. self.test_iter, verbose=0, return_dict=True)['accuracy']
  21. metrics = (logs['loss'], logs['accuracy'], test_acc)
  22. self.animator.add(epoch + 1, metrics)
  23. if epoch == self.num_epochs - 1:
  24. batch_size = next(iter(self.train_iter))[0].shape[0]
  25. num_examples = batch_size * tf.data.experimental.cardinality(
  26. self.train_iter).numpy()
  27. print(f'loss {metrics[0]:.3f}, train acc {metrics[1]:.3f}, '
  28. f'test acc {metrics[2]:.3f}')
  29. print(f'{num_examples / self.timer.avg():.1f} examples/sec on '
  30. f'{str(self.device_name)}')
  31. #@save
  32. def train_ch6(net_fn, train_iter, test_iter, num_epochs, lr, device):
  33. """用GPU训练模型(在第六章定义)"""
  34. device_name = device._device_name
  35. strategy = tf.distribute.OneDeviceStrategy(device_name)
  36. with strategy.scope():
  37. optimizer = tf.keras.optimizers.SGD(learning_rate=lr)
  38. loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
  39. net = net_fn()
  40. net.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
  41. callback = TrainCallback(net, train_iter, test_iter, num_epochs,
  42. device_name)
  43. net.fit(train_iter, epochs=num_epochs, verbose=0, callbacks=[callback])
  44. return net

现在,我们[训练和评估LeNet-5模型]。

```{.python .input}

@tab all

lr, num_epochs = 0.9, 10 train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu()) ```

小结

  • 卷积神经网络(CNN)是一类使用卷积层的网络。
  • 在卷积神经网络中,我们组合使用卷积层、非线性激活函数和汇聚层。
  • 为了构造高性能的卷积神经网络,我们通常对卷积层进行排列,逐渐降低其表示的空间分辨率,同时增加通道数。
  • 在传统的卷积神经网络中,卷积块编码得到的表征在输出之前需由一个或多个全连接层进行处理。
  • LeNet是最早发布的卷积神经网络之一。

练习

  1. 将平均汇聚层替换为最大汇聚层,会发生什么?
  2. 尝试构建一个基于LeNet的更复杂的网络,以提高其准确性。
    1. 调整卷积窗口大小。
    2. 调整输出通道的数量。
    3. 调整激活函数(如ReLU)。
    4. 调整卷积层的数量。
    5. 调整全连接层的数量。
    6. 调整学习率和其他训练细节(例如,初始化和轮数)。
  3. 在MNIST数据集上尝试以上改进的网络。
  4. 显示不同输入(例如毛衣和外套)时,LeNet第一层和第二层的激活值。

:begin_tab:mxnet Discussions :end_tab:

:begin_tab:pytorch Discussions :end_tab:

:begin_tab:tensorflow Discussions :end_tab: