Keras - Pre-Trained Models
Keras - 预训练模型概述
Section titled “Keras - 预训练模型概述”本章简要概述 tf.keras.applications 中一些常用的预训练模型。这些模型已经在大型 ImageNet 数据集(140 万张标注图像,1000 个类别)上训练过,可以直接用于各种计算机视觉任务。
加载这些模型时请记住以下关键参数:
weights: 通常设置为'imagenet'以加载预训练权重,或设置为None进行随机初始化。include_top: 设置为True以包含最终分类层(用于直接进行 ImageNet 分类),设置为False以排除它(用于特征提取或迁移学习)。input_shape: 可选的元组(例如(224, 224, 3)),指定预期的输入尺寸,如果include_top=False并且你需要固定尺寸,则需要提供。classes: 输出类别数量(仅当include_top=True且weights=None时相关)。
每个模型在其模块内还有一个对应的 preprocess_input 函数(例如,tf.keras.applications.vgg16.preprocess_input),应使用此函数在将图像输入模型之前对其进行预处理。
VGG16 / VGG19
Section titled “VGG16 / VGG19”由牛津大学可视化几何组 (Visual Geometry Group) 开发。其特点是简单,仅使用堆叠的 3x3 卷积核,后跟最大池化层。VGG19 是 VGG16 的更深版本。
- 默认输入尺寸: 224x224
- 优点: 简单、统一的架构。良好的特征提取器。
- 缺点: 参数数量非常庞大,计算开销大。
import tensorflow as tf
# 加载 VGG16vgg16_model = tf.keras.applications.VGG16(weights='imagenet', include_top=True)
# 加载用于特征提取的 VGG16vgg16_base = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))ResNet (例如,ResNet50, ResNet101, ResNet152V2)
Section titled “ResNet (例如,ResNet50, ResNet101, ResNet152V2)”残差网络 (Residual Networks) 引入了“跳跃连接”(‘skip connections’)或“残差块”(‘residual blocks’)的概念。这些连接允许梯度更容易地流经更深的网络,从而能够在不降低性能的情况下训练具有数百甚至数千层的模型。
- 默认输入尺寸: 224x224
- 优点: 可以训练非常深的网络,性能强大,广泛使用的基线模型。
- 缺点: 根据深度不同,计算开销可能仍然很大。
import tensorflow as tf
# 加载 ResNet50V2 (V2 通常优于 V1)resnet_model = tf.keras.applications.ResNet50V2(weights='imagenet', include_top=True)MobileNet / MobileNetV2 / MobileNetV3
Section titled “MobileNet / MobileNetV2 / MobileNetV3”专为移动和嵌入式视觉应用设计,这些应用计算资源有限。它们使用深度可分离卷积(depthwise separable convolutions)显著减少参数数量和计算量,同时保持良好的准确率。
- 默认输入尺寸: 224x224
- 优点: 轻量、快速、高效,适用于移动设备。
- 缺点: 在复杂任务上准确率可能略低于大型模型。
- 参数
alpha: 控制网络的宽度(卷积核数量)。alpha < 1.0减少尺寸/计算,alpha > 1.0增加尺寸/计算。
import tensorflow as tf
# 加载 MobileNetV2mobilenet_model = tf.keras.applications.MobileNetV2(weights='imagenet', include_top=True)
# 使用 alpha 加载一个较小版本smaller_mobilenet = tf.keras.applications.MobileNetV2(weights='imagenet', alpha=0.5, include_top=False)Inception (例如,InceptionV3, InceptionResNetV2)
Section titled “Inception (例如,InceptionV3, InceptionResNetV2)”Inception 架构(也称为 GoogLeNet)使用“Inception 模块”,这些模块并行执行不同卷积核尺寸(1x1、3x3、5x5)的卷积,并将其输出连接起来。这使得网络能够有效地捕获不同尺度的特征。InceptionResNetV2 将 Inception 模块与残差连接相结合。
- 默认输入尺寸: 299x299
- 优点: 与 VGG 相比,参数较少,性能良好。
- 缺点: 架构更复杂。
import tensorflow as tf
# 加载 InceptionV3inception_model = tf.keras.applications.InceptionV3(weights='imagenet', include_top=True)
# 加载 InceptionResNetV2inc_resnet_model = tf.keras.applications.InceptionResNetV2(weights='imagenet', include_top=True)EfficientNet (B0-B7, V2)
Section titled “EfficientNet (B0-B7, V2)”EfficientNet 模型以显著更少的参数和计算量实现了最先进的准确率。它们使用一种称为“复合缩放”(‘compound scaling’)的技术,根据可用资源统一缩放网络的深度、宽度和分辨率。EfficientNetV2 代表了进一步的改进。
- 默认输入尺寸: 不同尺寸(例如,B0 为 224x224,B7 最高可达 600x600)
- 优点: 出色的准确率-效率比。
- 缺点: 在较高尺度(B5+)时可能仍然较大/较慢。
import tensorflow as tf
# 加载 EfficientNetB0efficientnet_b0 = tf.keras.applications.EfficientNetB0(weights='imagenet', include_top=True)
# 加载 EfficientNetV2Sefficientnet_v2s = tf.keras.applications.EfficientNetV2S(weights='imagenet', include_top=True)最佳模型取决于你的具体需求:
- 需要高准确率(资源充足): ResNet、InceptionResNetV2、EfficientNet(较高的 B 型号)。
- 良好平衡(准确率/效率): ResNet50、InceptionV3、EfficientNet(较低的 B 型号)。
- 资源受限(移动/边缘设备): MobileNetV2/V3、EfficientNetB0。
- 特征提取基线: VGG16、ResNet50 通常是很好的起点。
使用这些预训练模型,特别是用于迁移学习或特征提取时,可以节省大量的训练时间和计算成本,即使只有有限的自定义数据,也能实现高性能。