深度学习-VGG16原理和代码详解

VGG16 原理和代码详解

VGG16 是由牛津大学的 Visual Geometry Group (VGG) 提出的深度卷积神经网络,发表于 2014 年的论文 "Very Deep Convolutional Networks for Large-Scale Image Recognition"。VGG16 是其中的一种结构,由 16 层网络组成,主要用于图像分类任务。

VGG16 结构

VGG16 主要由以下几部分组成:

  • 卷积层 (Convolutional Layers) :在 VGG16 中,所有卷积层使用 3x3 的滤波器,步长为 1,填充为 1,以确保输出的空间分辨率与输入相同。
  • 池化层 (Pooling Layers) :VGG16 使用 2x2 的最大池化层,步长为 2,减少空间维度。
  • 全连接层 (Fully Connected Layers):最后,网络有三个全连接层,其中前两个有 4096 个节点,最后一个是 1000 个节点(用于 1000 类别分类)。
  • 激活函数 (Activation Functions):VGG16 使用 ReLU 激活函数。

VGG16 网络可以表示为:

  • 输入: 224x224x3
  • 卷积层1: 两个 64 个滤波器的 3x3 卷积层
  • 最大池化层1: 2x2 最大池化
  • 卷积层2: 两个 128 个滤波器的 3x3 卷积层
  • 最大池化层2: 2x2 最大池化
  • 卷积层3: 三个 256 个滤波器的 3x3 卷积层
  • 最大池化层3: 2x2 最大池化
  • 卷积层4: 三个 512 个滤波器的 3x3 卷积层
  • 最大池化层4: 2x2 最大池化
  • 卷积层5: 三个 512 个滤波器的 3x3 卷积层
  • 最大池化层5: 2x2 最大池化
  • 全连接层1: 4096 个节点
  • 全连接层2: 4096 个节点
  • 全连接层3: 1000 个节点(Softmax 输出)

以下是一个 VGG16 的代码实现,并带有逐句解释:

python 复制代码
import tensorflow as tf
from tensorflow.keras import layers, models

# 定义 VGG16 模型
# 导入库:
import tensorflow as tf
from tensorflow.keras import layers, models

# 这部分导入了 TensorFlow 以及 Keras 库中的层和模型模块,方便我们定义神经网络模型。

# 定义 VGG16 模型函数:
def VGG16():
    model = models.Sequential()
    # 这里定义了一个 VGG16 模型的函数 VGG16(),并初始化一个 Sequential 模型,用于按顺序堆叠各层。

    # 第1个卷积块:
    model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3)))
    model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
    model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
    # 第一个卷积层使用 64 个 3x3 的滤波器,padding='same' 表示输出的尺寸与输入相同。
    # input_shape=(224, 224, 3) 指定输入图像的大小为 224x224x3 (RGB 图像)。
    # 第二个卷积层也使用 64 个 3x3 的滤波器。
    # 最后使用 2x2 的最大池化层,将特征图的尺寸减半。

    # 第2个卷积块:
    model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
    model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
    model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
    # 同样地,使用两个 128 个 3x3 的卷积层和一个 2x2 的最大池化层。

    # 第3个卷积块:
    model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
    model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
    model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
    model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
    # 这个卷积块使用了三个 256 个 3x3 的卷积层和一个 2x2 的最大池化层。

    # 第4个卷积块:
    model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
    model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
    model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
    model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
    # 同样地,使用了三个 512 个 3x3 的卷积层和一个 2x2 的最大池化层。

    # 第5个卷积块:
    model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
    model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
    model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
    model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
    # 与第4个卷积块相同,使用三个 512 个 3x3 的卷积层和一个 2x2 的最大池化层。

    # 全连接层:
    model.add(layers.Flatten())
    model.add(layers.Dense(4096, activation='relu'))
    model.add(layers.Dense(4096, activation='relu'))
    model.add(layers.Dense(1000, activation='softmax'))
    # 使用 Flatten() 将多维特征图展平为一维。
    # 添加两个全连接层,每层有 4096 个节点,并使用 ReLU 激活函数。
    # 最后一层是输出层,有 1000 个节点,对应 1000 个类别,使用 softmax 激活函数,输出概率分布。

    return model

# 创建和输出模型:
model = VGG16()
model.summary()
# 调用 VGG16() 函数来创建模型实例。
# 使用 model.summary() 来打印模型的结构和参数数量。
相关推荐
回眸&啤酒鸭1 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智1 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
倒头就睡的小比特1 天前
算法竞赛C++常用的STL
c++·算法
AI的探索之旅1 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein1 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
LaughingZhu1 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
美狐美颜SDK开放平台1 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
wukangjupingbb1 天前
智能网联汽车安全能力框架
人工智能