TensorFlow 中文手册

13.2 CNN 经典网络结构

TensorFlow CNN经典网络结构:从LeNet-5到MobileNetV2入门教程

TensorFlow 中文手册

本TensorFlow中文学习手册章节详细讲解CNN经典网络结构,包括基础LeNet-5、深度AlexNet和VGG16、轻量MobileNetV2和ResNet18,提供简单易懂的解释和TensorFlow代码示例,适合初学者快速掌握深度学习基础。

推荐工具
PyCharm专业版开发必备

功能强大的Python IDE,提供智能代码补全、代码分析、调试和测试工具,提高Python开发效率。特别适合处理列表等数据结构的开发工作。

了解更多

CNN经典网络结构入门:从基础到轻量化

卷积神经网络(CNN)是深度学习中处理图像任务的核心技术。本章将介绍几种经典的CNN架构,帮助初学者理解其演变和TensorFlow实现,内容从基础到深度再到轻量化,逐步深入。

基础CNN:LeNet-5

LeNet-5由Yann LeCun于1998年提出,是早期CNN的经典代表,主要用于手写数字识别。它的结构简单,易于理解,非常适合初学者入门。

网络结构特点:

  • 输入:32x32灰度图像
  • 包含2个卷积层和2个池化层,最后是全连接层
  • 使用ReLU激活函数(原版使用Tanh,现代实现常用ReLU)

TensorFlow实现示例:

import tensorflow as tf
from tensorflow.keras import layers, models

def lenet5_model():
    model = models.Sequential()
    model.add(layers.Conv2D(6, (5, 5), activation='relu', input_shape=(32, 32, 1)))  # 第一卷积层
    model.add(layers.MaxPooling2D((2, 2)))  # 池化层
    model.add(layers.Conv2D(16, (5, 5), activation='relu'))  # 第二卷积层
    model.add(layers.MaxPooling2D((2, 2)))  # 池化层
    model.add(layers.Flatten())  # 展平
    model.add(layers.Dense(120, activation='relu'))  # 全连接层
    model.add(layers.Dense(84, activation='relu'))
    model.add(layers.Dense(10, activation='softmax'))  # 输出层,10个类别
    return model

model = lenet5_model()
model.summary()  # 查看模型结构

运行此代码可以快速构建一个LeNet-5模型,并用于MNIST等数据集的训练。

深度CNN:AlexNet和VGG16

随着硬件进步,CNN向更深层次发展。AlexNet和VGG16是深度CNN的里程碑,在图像分类任务中表现出色。

AlexNet

AlexNet在2012年ImageNet竞赛中获胜,引入了ReLU激活、Dropout和数据增强等技巧。

  • 结构: 5个卷积层和3个全连接层,使用ReLU和局部响应归一化(LRN)。
  • TensorFlow示例(简化版):
def alexnet_model(input_shape=(224, 224, 3)):
    model = models.Sequential()
    model.add(layers.Conv2D(96, (11, 11), strides=4, activation='relu', input_shape=input_shape))
    model.add(layers.MaxPooling2D((3, 3), strides=2))
    model.add(layers.Conv2D(256, (5, 5), padding='same', activation='relu'))
    model.add(layers.MaxPooling2D((3, 3), strides=2))
    model.add(layers.Flatten())
    model.add(layers.Dense(4096, activation='relu'))
    model.add(layers.Dense(1000, activation='softmax'))
    return model

VGG16

VGG16以其一致的3x3卷积堆叠闻名,结构更简单,但层数更深(16层)。

  • 特点: 所有卷积层使用3x3核,易于扩展。
  • TensorFlow实现:
from tensorflow.keras.applications import VGG16

# 使用预训练模型(推荐初学者)
vgg16 = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# 自定义构建(理解结构)
model = models.Sequential([
    layers.Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3)),
    layers.Conv2D(64, (3, 3), activation='relu', padding='same'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(128, (3, 3), activation='relu', padding='same'),
    # 继续添加更多层,直到符合VGG16结构
])

轻量CNN:MobileNetV2和ResNet18

针对移动设备或资源受限场景,轻量级CNN减少了计算和参数,同时保持高性能。

MobileNetV2

MobileNetV2基于深度可分离卷积和倒残差结构,高效轻便。

  • 核心思想: 使用深度卷积和点卷积减少计算量。
  • TensorFlow示例:
from tensorflow.keras.applications import MobileNetV2

mobilenetv2 = MobileNetV2(weights='imagenet', input_shape=(224, 224, 3))
# 自定义深度可分离卷积示例
from tensorflow.keras import layers
input_layer = layers.Input(shape=(224, 224, 3))
x = layers.DepthwiseConv2D(kernel_size=(3, 3), padding='same')(input_layer)  # 深度卷积
x = layers.Conv2D(32, (1, 1))(x)  # 点卷积
model = models.Model(inputs=input_layer, outputs=x)

ResNet18

ResNet18引入残差连接,解决深度网络梯度消失问题,是轻量化的常用选择。

  • 结构: 18层,包含残差块。
  • TensorFlow实现:
from tensorflow.keras.applications import ResNet18

resnet18 = ResNet18(weights='imagenet')
# 构建一个残差块示例(理解原理)
def residual_block(input_tensor, filters):
    x = layers.Conv2D(filters, (3, 3), padding='same')(input_tensor)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    x = layers.Conv2D(filters, (3, 3), padding='same')(x)
    x = layers.BatchNormalization()(x)
    # 添加残差连接
    x = layers.add([x, input_tensor])
    return layers.Activation('relu')(x)

总结

本章介绍了从基础到轻量化的CNN经典网络结构,通过TensorFlow代码示例,新手可以轻松上手实现。建议先从LeNet-5开始练习,再逐步尝试深度和轻量化模型,以深入理解CNN的演变和实际应用。

学习建议:

  • 使用TensorFlow的Keras API构建和训练这些模型。
  • 参考官方文档和预训练模型加速学习。
  • 结合图像数据集(如MNIST、CIFAR-10)进行实验。

通过本章学习,您将掌握CNN的基本概念和TensorFlow实现,为更高级的深度学习应用打下坚实基础。

开发工具推荐
Python开发者工具包

包含虚拟环境管理、代码格式化、依赖管理、测试框架等Python开发全流程工具,提高开发效率。特别适合处理复杂数据结构和算法。

获取工具包