建简单网站,老薛主机wordpress慢,安阳做网站,阿里云 个人网站 名称在前面的文章中#xff0c;我们介绍了如何使用 Keras 构建和训练全连接神经网络#xff08;MLP#xff09;、卷积神经网络#xff08;CNN#xff09;和循环神经网络#xff08;RNN#xff09;。本文将带你深入学习如何使用 迁移学习#xff08;Transfer Learning#…在前面的文章中我们介绍了如何使用 Keras 构建和训练全连接神经网络MLP、卷积神经网络CNN和循环神经网络RNN。本文将带你深入学习如何使用 迁移学习Transfer Learning 来加速和提升模型性能。我们将使用 Keras 和预训练的卷积神经网络如 VGG16来完成一个图像分类任务。
目录
什么是迁移学习环境准备导入必要的库加载和预处理数据加载预训练模型构建迁移学习模型编译模型训练模型评估模型保存和加载模型总结 1. 什么是迁移学习
迁移学习 是一种机器学习技术它利用在一个任务上训练的模型来解决另一个相关任务。通过迁移学习我们可以
加速训练: 利用预训练模型的特征提取能力减少训练时间。提高性能: 在数据量有限的情况下迁移学习可以显著提高模型的泛化能力。减少数据需求: 预训练模型已经在大规模数据集上训练过可以减少对新数据的需求。
在图像分类任务中迁移学习通常涉及使用在 ImageNet 等大型数据集上预训练的卷积神经网络如 VGG16、ResNet、Inception 等并将其应用到新的图像分类任务中。
2. 环境准备
确保你已经安装了 Python推荐 3.6 及以上版本和 TensorFlowKeras 已集成在 TensorFlow 中。如果尚未安装请运行以下命令
pip install tensorflow3. 导入必要的库
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, models, applications
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import numpy as nptensorflow: 深度学习框架Keras 已集成其中。ImageDataGenerator: 用于数据增强和预处理。applications: 预训练模型模块包含 VGG16、ResNet 等。
4. 加载和预处理数据
我们将使用 猫狗数据集Cats vs Dogs这是一个二分类图像数据集包含 25,000 张猫和狗的图片。我们将使用 Keras 的 ImageDataGenerator 进行数据增强和预处理。
# 数据集路径
train_dir data/train
validation_dir data/validation# 图像参数
img_height, img_width 150, 150
batch_size 32# 训练数据生成器数据增强
train_datagen ImageDataGenerator(rescale1./255, # 归一化rotation_range40, # 随机旋转width_shift_range0.2, # 随机水平平移height_shift_range0.2, # 随机垂直平移shear_range0.2, # 随机剪切zoom_range0.2, # 随机缩放horizontal_flipTrue, # 随机水平翻转fill_modenearest # 填充方式
)# 测试数据生成器仅归一化
test_datagen ImageDataGenerator(rescale1./255)# 加载训练数据
train_generator train_datagen.flow_from_directory(train_dir,target_size(img_height, img_width),batch_sizebatch_size,class_modebinary # 二分类
)# 加载验证数据
validation_generator test_datagen.flow_from_directory(validation_dir,target_size(img_height, img_width),batch_sizebatch_size,class_modebinary
)说明
使用 ImageDataGenerator 进行数据增强可以提高模型的泛化能力。flow_from_directory 方法从目录中加载数据目录结构应为 train_dir/class1/ 和 train_dir/class2/。
5. 加载预训练模型
我们将使用预训练的 VGG16 模型并冻结其卷积基convolutional base只训练顶部的全连接层。
# 加载预训练的 VGG16 模型不包括顶部的全连接层
conv_base applications.VGG16(weightsimagenet,include_topFalse,input_shape(img_height, img_width, 3))# 冻结卷积基
conv_base.trainable False# 查看模型结构
conv_base.summary()说明
weightsimagenet: 使用在 ImageNet 数据集上预训练的权重。include_topFalse: 不包括顶部的全连接层以便我们添加自己的分类器。conv_base.trainable False: 冻结卷积基防止其权重在训练过程中被更新。
6. 构建迁移学习模型
我们将添加自己的全连接层来进行分类。
model models.Sequential([conv_base, # 预训练的卷积基layers.Flatten(), # 展平层layers.Dense(256, activationrelu), # 全连接层layers.Dropout(0.5), # Dropout 层防止过拟合layers.Dense(1, activationsigmoid) # 输出层二分类
])# 查看模型结构
model.summary()说明
添加 Flatten 层将多维输出展平。添加 Dense 层和 Dropout 层进行分类。输出层使用 sigmoid 激活函数进行二分类。
7. 编译模型
model.compile(optimizerkeras.optimizers.Adam(),lossbinary_crossentropy,metrics[accuracy])说明
使用 Adam 优化器和二元交叉熵损失函数。评估指标为准确率。
8. 训练模型
# 设置训练参数
epochs 10# 训练模型
history model.fit(train_generator,steps_per_epochtrain_generator.samples // batch_size,epochsepochs,validation_datavalidation_generator,validation_stepsvalidation_generator.samples // batch_size
)说明
steps_per_epoch: 每个 epoch 的步数通常为训练样本数除以批量大小。validation_steps: 每个 epoch 的验证步数通常为验证样本数除以批量大小。
9. 评估模型
test_loss, test_acc model.evaluate(validation_generator, stepsvalidation_generator.samples // batch_size)
print(f\n测试准确率: {test_acc:.4f})10. 保存和加载模型
# 保存模型
model.save(cats_vs_dogs_transfer_learning.h5)# 加载模型
new_model keras.models.load_model(cats_vs_dogs_transfer_learning.h5)11. 可视化训练过程
# 绘制训练 验证的准确率和损失值
plt.figure(figsize(12,4))# 准确率
plt.subplot(1,2,1)
plt.plot(history.history[accuracy], label训练准确率)
plt.plot(history.history[val_accuracy], label验证准确率)
plt.xlabel(Epoch)
plt.ylabel(准确率)
plt.legend(loclower right)
plt.title(训练与验证准确率)# 损失值
plt.subplot(1,2,2)
plt.plot(history.history[loss], label训练损失)
plt.plot(history.history[val_loss], label验证损失)
plt.xlabel(Epoch)
plt.ylabel(损失)
plt.legend(locupper right)
plt.title(训练与验证损失)plt.show()12. 解冻部分卷积基进行微调
为了进一步提高模型性能可以解冻部分卷积基进行微调。
# 解冻最后几个卷积层
conv_base.trainable True# 查看可训练的参数
for layer in conv_base.layers:if layer.name block5_conv1:breaklayer.trainable False# 重新编译模型
model.compile(optimizerkeras.optimizers.Adam(1e-5), # 使用较低的学习率lossbinary_crossentropy,metrics[accuracy])# 继续训练模型
history_fine model.fit(train_generator,steps_per_epochtrain_generator.samples // batch_size,epochs5,validation_datavalidation_generator,validation_stepsvalidation_generator.samples // batch_size
)说明
解冻部分卷积层并使用较低的学习率进行微调。继续训练模型以微调预训练模型的权重。
13. 课程回顾
本文其实不算什么知识点只是利用迁移学习来加速训练的一个实际操作的例子。
作者简介
前腾讯电子签的前端负责人现 whentimes tech CTO专注于前端技术的大咖一枚一路走来从小屏到大屏从 Web 到移动什么前端难题都见过。热衷于用技术打磨产品带领团队把复杂的事情做到极简体验做到极致。喜欢探索新技术也爱分享一些实战经验帮助大家少走弯路
温馨提示可搜老码小张公号联系导师