机器学习实操 第二部分 神经网路和深度学习 第14章 使用卷积神经网络进行深度计算机视觉
内容概要
第14章深入探讨了卷积神经网络(CNNs)及其在计算机视觉中的应用。CNNs受大脑视觉皮层的启发,通过局部感受野和权值共享机制,能够高效地处理图像数据。本章从CNN的基本构建块(如卷积层和池化层)讲起,介绍了多种经典的CNN架构(如LeNet-5、AlexNet、GoogLeNet、ResNet等),并探讨了CNN在目标检测和语义分割等任务中的应用。此外,还介绍了如何使用Keras实现CNN模型,以及如何利用预训练模型进行迁移学习。
主要内容
卷积层和池化层
- 卷积层:通过卷积操作提取图像的局部特征。卷积层使用多个滤波器(filters)生成特征图(feature maps),每个滤波器负责检测图像中的特定模式。
- 池化层:用于降低特征图的空间维度,减少计算量和内存使用,同时引入对小平移的不变性。常见的池化方法包括最大池化(max pooling)和平均池化(average pooling)。
CNN架构
- LeNet-5:由Yann LeCun在1998年提出,用于手写数字识别。它包含交替的卷积层和池化层,以及全连接层。
- AlexNet:在2012年ImageNet挑战赛中获胜,引入了更大的网络深度和 Dropout 正则化。
- GoogLeNet:通过引入Inception模块,实现了更深的网络结构,同时减少了参数数量。
- ResNet:通过残差连接(skip connections)解决了深层网络的梯度消失问题,使得训练更深的网络成为可能。
- 其他架构:包括Xception、ResNeXt、DenseNet、MobileNet、CSPNet和EfficientNet等,每种架构都有其独特的设计和优势。
目标检测和语义分割
- 目标检测:不仅需要对图像中的物体进行分类,还需要定位物体的位置。常见的方法包括滑动窗口法和基于区域的CNN(R-CNN)。
- 语义分割:对图像中的每个像素进行分类,确定其所属的物体类别。全卷积网络(FCN)和U-Net等架构在这一任务中表现出色。
迁移学习
- 使用预训练的CNN模型进行迁移学习,通过在新的数据集上微调模型的高层,可以快速适应新的图像分类任务。
数据增强
- 数据增强技术通过生成训练数据的变体(如旋转、翻转、缩放等)来增加训练集的多样性,减少过拟合的风险。
关键代码和算法
14.1 使用Keras实现ResNet-34
DefaultConv2D = partial(tf.keras.layers.Conv2D,
kernel_size=3,
strides=1,
padding="same",
kernel_initializer="he_normal",
use_bias=False)
class ResidualUnit(tf.keras.layers.Layer):
def __init__(self, filters, strides=1, activation="relu", **kwargs):
super().__init__(**kwargs)
self.activation = tf.keras.activations.get(activation)
self.main_layers = [
DefaultConv2D(filters, strides=strides),
tf.keras.layers.BatchNormalization(),
self.activation,
DefaultConv2D(filters),
tf.keras.layers.BatchNormalization()
]
self.skip_layers = []
if strides > 1:
self.skip_layers = [
DefaultConv2D(filters, kernel_size=1, strides=strides),
tf.keras.layers.BatchNormalization()
]
def call(self, inputs):
Z = inputs
for layer in self.main_layers:
Z = layer(Z)
skip_Z = inputs
for layer in self.skip_layers:
skip_Z = layer(skip_Z)
return self.activation(Z + skip_Z)
model = tf.keras.Sequential([
DefaultConv2D(64, kernel_size=7, strides=2, input_shape=[224, 224, 3]),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.MaxPool2D(pool_size=3, strides=2, padding="same"),
])
prev_filters = 64
for filters in [64] * 3 + [128] * 4 + [256] * 6 + [512] * 3:
strides = 1 if filters == prev_filters else 2
model.add(ResidualUnit(filters, strides=strides))
prev_filters = filters
model.add(tf.keras.layers.GlobalAvgPool2D())
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10, activation="softmax"))
model.compile(loss="sparse_categorical_crossentropy",
optimizer=tf.keras.optimizers.SGD(learning_rate=0.1),
metrics=["accuracy"])
精彩语录
中文:卷积神经网络(CNNs)在图像识别任务中表现出色,因为它们能够利用图像的局部相关性。
英文原文:CNNs are particularly well suited for image recognition tasks because they can leverage the local correlations in images.
解释:强调了CNNs在图像处理中的优势。中文:ResNet通过引入残差连接,解决了深层网络的梯度消失问题,使得训练更深的网络成为可能。
英文原文:ResNet introduced residual connections to address the vanishing gradients problem in deep networks, making it possible to train much deeper networks.
解释:介绍了ResNet的关键创新。中文:数据增强技术通过生成训练数据的变体,有效增加了训练集的多样性,减少了过拟合的风险。
英文原文:Data augmentation techniques effectively increase the diversity of the training set by generating variants of the training data, reducing the risk of overfitting.
解释:解释了数据增强的作用。中文:全卷积网络(FCN)通过将全连接层替换为卷积层,能够处理任意大小的输入图像。
英文原文:Fully Convolutional Networks (FCNs) replace dense layers with convolutional layers, allowing them to process images of any size.
解释:介绍了FCN的特点。中文:迁移学习通过利用预训练模型的高层特征,能够快速适应新的图像分类任务。
英文原文:Transfer learning leverages the high-level features learned by pretrained models to quickly adapt to new image classification tasks.
解释:强调了迁移学习的优势。
总结
通过本章的学习,读者将掌握卷积神经网络(CNNs)的基本原理和实现方法。内容涵盖了CNN的构建块、经典架构、目标检测和语义分割等高级应用,以及如何使用Keras实现CNN模型和进行迁移学习。这些知识将帮助读者在计算机视觉领域构建高效、准确的模型。