国家工程建设标准化信息网站,在淘宝介绍里边怎么做网站链接,自己做的网页怎么上传网站吗,wordpress自动识别手机文为「365天深度学习训练营」内部文章 参考本文所写记录性文章#xff0c;请在文章开头带上「#x1f449;声明」 #x1f37a; 要求#xff1a;
自己搭建VGG-16网络框架【达成√】调用官方的VGG-16网络框架【达成√】如何查看模型的参数量以及相关指标【达成√】
#…文为「365天深度学习训练营」内部文章 参考本文所写记录性文章请在文章开头带上「声明」 要求
自己搭建VGG-16网络框架【达成√】调用官方的VGG-16网络框架【达成√】如何查看模型的参数量以及相关指标【达成√】 拔高可选
验证集准确率达到100%【98.61%】使用PPT画出VGG-16算法框架图发论文需要这项技能 探索难度有点大
在不影响准确率的前提下轻量化模型【达成√】
目前VGG16的Total params是134,272,835 我的环境
语言环境Python3.11.9编译器Jupyter Lab深度学习环境 torch2.3.1 torchvision0.18.1 这次我们使用的是马铃薯病害数据集该数据集包含表现出各种疾病的马铃薯植物的高分辨率图像包括早期疫病、晚期疫病和健康叶子。它旨在帮助开发和测试图像识别模型以实现准确的疾病检测和分类从而促进农业诊断的进步。
一、 前期准备
1. 设置GPU
如果设备上支持GPU就使用GPU,否则使用CPU
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
from torchvision import transforms, datasets
import os, PIL, pathlib, warningswarnings.filterwarnings(ignore) # 忽略警告信息# 检查硬件加速支持
if torch.backends.mps.is_available():device torch.device(mps) # 使用 Metal 后端适用于 M 系列芯片print(Using Metal Performance Shaders (MPS) backend for acceleration)
else:device torch.device(cuda if torch.cuda.is_available() else cpu)if device.type cuda:print(Using CUDA for acceleration)else:print(Using CPU (no hardware acceleration available))device2. 导入数据
import os, PIL, random, pathlibdata_dir ./PotatoPlants
data_dir pathlib.Path(data_dir)# 获取子目录路径
data_paths list(data_dir.glob(*))# 提取子目录名称使用 path.name 更安全
classeNames [path.name for path in data_paths]print(classeNames)# 关于transforms.Compose的更多介绍可以参考https://blog.csdn.net/qq_38251616/article/details/124878863
train_transforms transforms.Compose([transforms.Resize([224, 224]), # 将输入图片resize成统一尺寸# transforms.RandomHorizontalFlip(), # 随机水平翻转transforms.ToTensor(), # 将PIL Image或numpy.ndarray转换为tensor并归一化到[0,1]之间transforms.Normalize( # 标准化处理--转换为标准正太分布高斯分布使模型更容易收敛mean[0.485, 0.456, 0.406], std[0.229, 0.224, 0.225]) # 其中 mean[0.485,0.456,0.406]与std[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。
])test_transform transforms.Compose([transforms.Resize([224, 224]), # 将输入图片resize成统一尺寸transforms.ToTensor(), # 将PIL Image或numpy.ndarray转换为tensor并归一化到[0,1]之间transforms.Normalize( # 标准化处理--转换为标准正太分布高斯分布使模型更容易收敛mean[0.485, 0.456, 0.406], std[0.229, 0.224, 0.225]) # 其中 mean[0.485,0.456,0.406]与std[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。
])total_data datasets.ImageFolder(./PotatoPlants/,transformtrain_transforms)
total_data total_data.class_to_idx 3. 划分数据集
train_size int(0.8 * len(total_data))
test_size len(total_data) - train_size
train_dataset, test_dataset torch.utils.data.random_split(total_data, [train_size, test_size])
train_dataset, test_dataset batch_size 32train_dl torch.utils.data.DataLoader(train_dataset,batch_sizebatch_size,shuffleTrue,num_workers1)
test_dl torch.utils.data.DataLoader(test_dataset,batch_sizebatch_size,shuffleTrue,num_workers1)
for X, y in test_dl:print(Shape of X [N, C, H, W]: , X.shape)print(Shape of y: , y.shape, y.dtype)break 二、手动搭建VGG-16模型
VVG-16结构说明
13个卷积层Convolutional Layer分别用blockX_convX表示3个全连接层Fully connected Layer分别用fcX与predictions表示5个池化层Pool layer分别用blockX_pool表示
VGG-16包含了16个隐藏层13个卷积层和3个全连接层故称为VGG-16 1. 搭建模型
import torch
import torch.nn as nn
import torch.nn.functional as Fclass vgg16(nn.Module):def __init__(self):super(vgg16, self).__init__()# 卷积块1self.block1 nn.Sequential(nn.Conv2d(3, 64, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.Conv2d(64, 64, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.MaxPool2d(kernel_size(2, 2), stride(2, 2)))# 卷积块2self.block2 nn.Sequential(nn.Conv2d(64, 128, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.Conv2d(128, 128, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.MaxPool2d(kernel_size(2, 2), stride(2, 2)))# 卷积块3self.block3 nn.Sequential(nn.Conv2d(128, 256, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.Conv2d(256, 256, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.Conv2d(256, 256, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.MaxPool2d(kernel_size(2, 2), stride(2, 2)))# 卷积块4self.block4 nn.Sequential(nn.Conv2d(256, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.MaxPool2d(kernel_size(2, 2), stride(2, 2)))# 卷积块5self.block5 nn.Sequential(nn.Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1)),nn.ReLU(),nn.MaxPool2d(kernel_size(2, 2), stride(2, 2)))# 全连接网络层用于分类self.classifier nn.Sequential(nn.Linear(in_features512*7*7, out_features4096),nn.ReLU(),nn.Linear(in_features4096, out_features4096),nn.ReLU(),nn.Linear(in_features4096, out_features3))def forward(self, x):x self.block1(x)x self.block2(x)x self.block3(x)x self.block4(x)x self.block5(x)x torch.flatten(x, start_dim1)x self.classifier(x)return x# 检查是否支持 MPS 后端
if torch.backends.mps.is_available():device torch.device(mps) # 使用 Metal 后端加速print(Using MPS device for acceleration)
else:device torch.device(cuda if torch.cuda.is_available() else cpu)print(fUsing {device} device)# 将模型加载到指定设备
model vgg16().to(device)
print(Model loaded to:, device)
model2. 查看模型详情
from torchsummary import summary# 检查设备
device torch.device(mps) if torch.backends.mps.is_available() else torch.device(cpu)# 初始化模型并加载到 MPS
model vgg16().to(device)# 将模型移到 CPU 以使用 torchsummary
model_cpu model.to(cpu)
summary(model_cpu, (3, 224, 224), devicecpu)# 完成后将模型移回 MPS
model model.to(device)三、 训练模型
1. 编写训练函数
# 训练循环
def train(dataloader, model, loss_fn, optimizer):size len(dataloader.dataset) # 训练集的大小num_batches len(dataloader) # 批次数目, (size/batch_size向上取整)train_loss, train_acc 0, 0 # 初始化训练损失和正确率for X, y in dataloader: # 获取图片及其标签X, y X.to(device), y.to(device)# 计算预测误差pred model(X) # 网络输出loss loss_fn(pred, y) # 计算网络输出和真实值之间的差距targets为真实值计算二者差值即为损失# 反向传播optimizer.zero_grad() # grad属性归零loss.backward() # 反向传播optimizer.step() # 每一步自动更新# 记录acc与losstrain_acc (pred.argmax(1) y).type(torch.float).sum().item()train_loss loss.item()train_acc / sizetrain_loss / num_batchesreturn train_acc, train_loss
3. 编写测试函数
测试函数和训练函数大致相同但是由于不进行梯度下降对网络权重进行更新所以不需要传入优化器
def test (dataloader, model, loss_fn):size len(dataloader.dataset) # 测试集的大小num_batches len(dataloader) # 批次数目, (size/batch_size向上取整)test_loss, test_acc 0, 0# 当不进行训练时停止梯度更新节省计算内存消耗with torch.no_grad():for imgs, target in dataloader:imgs, target imgs.to(device), target.to(device)# 计算losstarget_pred model(imgs)loss loss_fn(target_pred, target)test_loss loss.item()test_acc (target_pred.argmax(1) target).type(torch.float).sum().item()test_acc / sizetest_loss / num_batchesreturn test_acc, test_loss
4. 正式训练
model.train()、model.eval()训练营往期文章中有详细的介绍。
如果将优化器换成 SGD 会发生什么呢请自行探索接下来发生的诡异事件的原因。 import copyoptimizer torch.optim.Adam(model.parameters(), lr 1e-4)
loss_fn nn.CrossEntropyLoss() # 创建损失函数epochs 40train_loss []
train_acc []
test_loss []
test_acc []best_acc 0 # 设置一个最佳准确率作为最佳模型的判别指标for epoch in range(epochs):model.train()epoch_train_acc, epoch_train_loss train(train_dl, model, loss_fn, optimizer)model.eval()epoch_test_acc, epoch_test_loss test(test_dl, model, loss_fn)# 保存最佳模型到 best_modelif epoch_test_acc best_acc:best_acc epoch_test_accbest_model copy.deepcopy(model)train_acc.append(epoch_train_acc)train_loss.append(epoch_train_loss)test_acc.append(epoch_test_acc)test_loss.append(epoch_test_loss)# 获取当前的学习率lr optimizer.state_dict()[param_groups][0][lr]template (Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%, Test_loss:{:.3f}, Lr:{:.2E})print(template.format(epoch1, epoch_train_acc*100, epoch_train_loss, epoch_test_acc*100, epoch_test_loss, lr))# 保存最佳模型到文件中
PATH ./best_model.pth # 保存的参数文件名
torch.save(model.state_dict(), PATH)print(Done) 四、 结果可视化
1. Loss与Accuracy图
import matplotlib.pyplot as plt
#隐藏警告
import warnings
warnings.filterwarnings(ignore) #忽略警告信息
plt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签
plt.rcParams[axes.unicode_minus] False # 用来正常显示负号
plt.rcParams[figure.dpi] 100 #分辨率epochs_range range(epochs)plt.figure(figsize(12, 3))
plt.subplot(1, 2, 1)plt.plot(epochs_range, train_acc, labelTraining Accuracy)
plt.plot(epochs_range, test_acc, labelTest Accuracy)
plt.legend(loclower right)
plt.title(Training and Validation Accuracy)plt.subplot(1, 2, 2)
plt.plot(epochs_range, train_loss, labelTraining Loss)
plt.plot(epochs_range, test_loss, labelTest Loss)
plt.legend(locupper right)
plt.title(Training and Validation Loss)
plt.show() 2. 指定图片进行预测
from PIL import Image classes list(total_data.class_to_idx)def predict_one_image(image_path, model, transform, classes):test_img Image.open(image_path).convert(RGB)plt.imshow(test_img) # 展示预测的图片test_img transform(test_img)img test_img.to(device).unsqueeze(0)model.eval()output model(img)_,pred torch.max(output,1)pred_class classes[pred]print(f预测结果是{pred_class})
# 预测训练集中的某张照片
predict_one_image(image_path./PotatoPlants/Early_blight/1.JPG, modelmodel, transformtrain_transforms, classesclasses) 3. 模型评估
best_model.eval()
epoch_test_acc, epoch_test_loss test(test_dl, best_model, loss_fn)
epoch_test_acc, epoch_test_loss 五、调用官方的VGG-16网络框架、如何查看模型的参数量以及相关指标【使用torchvision.models以及torchsummary】
import torch
import torchvision.models as models
from torchsummary import summary# 检查设备
device torch.device(mps) if torch.backends.mps.is_available() else torch.device(cpu)
print(fUsing device: {device})# 加载官方 VGG-16 模型
# pretrainedTrue 表示加载在 ImageNet 上预训练的权重改为 False 则为随机初始化
model models.vgg16(pretrainedFalse).to(device)# 将模型移到 CPU 以使用 torchsummary
model_cpu model.to(cpu)
summary(model_cpu, (3, 224, 224), devicecpu)# 完成后将模型移回原设备
model model.to(device)六、优化模型
数据预处理与加载
# 数据增强和标准化
train_transforms transforms.Compose([transforms.RandomResizedCrop(224), # 随机裁剪到指定大小transforms.RandomHorizontalFlip(), # 随机水平翻转增强鲁棒性transforms.ToTensor(),transforms.Normalize(mean[0.485, 0.456, 0.406], std[0.229, 0.224, 0.225])
])test_transforms transforms.Compose([transforms.Resize(256),transforms.CenterCrop(224), # 中心裁剪到 224x224transforms.ToTensor(),transforms.Normalize(mean[0.485, 0.456, 0.406], std[0.229, 0.224, 0.225])
])# 数据加载
total_data datasets.ImageFolder(./PotatoPlants/, transformtrain_transforms)print(fClass-to-Index Mapping: {total_data.class_to_idx})增加数据增强 在训练集上添加随机裁剪和翻转提升模型的泛化能力。精细化测试集预处理 测试集使用中心裁剪保证评估时一致性。增强输出信息 输出类别到索引的映射信息便于调试。
VGG16 模型定义
from torchvision.models import vgg16# 加载预定义的 VGG16 模型
model vgg16(pretrainedFalse)
model.classifier[6] nn.Linear(4096, 3) # 修改最后一层为 3 类输出
model model.to(device)print(fModel loaded to: {device})使用官方 VGG16 使用 torchvision.models 提供的标准 VGG16提高模型可靠性。定制最后一层 修改全连接层的输出大小为 3适配当前分类任务。
优化超参数
import copy# 优化器参数调整
learning_rate 3e-4 # 提高初始学习率
weight_decay 1e-5 # 添加 L2 正则化防止过拟合optimizer torch.optim.Adam(model.parameters(), lrlearning_rate, weight_decayweight_decay)# 损失函数保持不变
loss_fn nn.CrossEntropyLoss()# 调整训练轮次和提前停止机制
epochs 100 # 增加训练轮次
patience 10 # 提前停止当验证集准确率不再提升时停止训练
no_improve_epochs 0# 记录最佳模型
best_acc 0
best_model Nonefor epoch in range(epochs):# 调整学习率调度器余弦退火调度器lr_scheduler torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_maxepochs)# 训练和测试train_acc, train_loss train(train_dl, model, loss_fn, optimizer)test_acc, test_loss test(test_dl, model, loss_fn)# 检查是否是最佳准确率if test_acc best_acc:best_acc test_accbest_model copy.deepcopy(model)no_improve_epochs 0 # 重置提前停止计数else:no_improve_epochs 1# 更新学习率lr_scheduler.step()# 打印日志print(fEpoch {epoch1}: fTrain Acc: {train_acc*100:.2f}%, Train Loss: {train_loss:.4f}, fTest Acc: {test_acc*100:.2f}%, Test Loss: {test_loss:.4f}, fLearning Rate: {optimizer.param_groups[0][lr]:.6f})# 提前停止if no_improve_epochs patience:print(fEarly stopping at epoch {epoch1}. Best Test Accuracy: {best_acc*100:.2f}%)break# 保存最佳模型
torch.save(best_model.state_dict(), ./best_model.pth)
print(Training complete. Best model saved.)1. 学习率调整
将学习率从 1e-4 增加到 3e-4帮助更快收敛。也可以尝试 1e-3。使用 余弦退火调度器 (CosineAnnealingLR)动态调整学习率使得后期优化更稳定。
2. 正则化
添加 weight_decay1e-5L2 正则化到优化器中限制权重过大减少过拟合风险。
3. 训练轮次
将 epochs 从 40 提高到 100确保模型有足够时间学习。增加了 提前停止early stopping避免浪费计算资源。如果验证集准确率在连续 10 个 epoch 没有提升训练提前结束。
4. 学习率调度器
CosineAnnealingLR 在训练过程中动态调整学习率前期快速下降后期缓慢收敛提高最终模型的效果。
5. 日志改进
在打印中添加当前学习率便于跟踪学习率变化。 七、在不影响准确率的前提下轻量化模型
方法 1减少全连接层的参数
VGG-16 的全连接层占了大部分参数超过 90%。我们可以通过以下方法减少参数量
减小全连接层的单元数量。去掉全连接层改用全局平均池化Global Average Pooling, GAP。
from torchvision.models import vgg16
import torch.nn as nn# 加载预定义的 VGG16 模型
model vgg16(pretrainedFalse)# 替换全连接层
model.classifier nn.Sequential(nn.Linear(512 * 7 * 7, 1024), # 从 4096 缩减到 1024nn.ReLU(),nn.Dropout(0.5),nn.Linear(1024, 256), # 第二层从 4096 缩减到 256nn.ReLU(),nn.Dropout(0.5),nn.Linear(256, 3) # 输出层保持不变
)方法 2引入深度可分离卷积
将标准卷积替换为深度可分离卷积Depthwise Separable Convolution减少参数量和计算量同时保持模型性能。
import torch.nn as nnclass DepthwiseSeparableConv(nn.Module):def __init__(self, in_channels, out_channels, kernel_size, stride1, padding0):super(DepthwiseSeparableConv, self).__init__()self.depthwise nn.Conv2d(in_channels, in_channels, kernel_sizekernel_size, stridestride, paddingpadding, groupsin_channels)self.pointwise nn.Conv2d(in_channels, out_channels, kernel_size1)def forward(self, x):x self.depthwise(x)x self.pointwise(x)return x# 替换 VGG-16 的卷积层为深度可分离卷积
class LightweightVGG16(nn.Module):def __init__(self):super(LightweightVGG16, self).__init__()self.features nn.Sequential(DepthwiseSeparableConv(3, 64, kernel_size3, stride1, padding1),nn.ReLU(),DepthwiseSeparableConv(64, 64, kernel_size3, stride1, padding1),nn.ReLU(),nn.MaxPool2d(kernel_size2, stride2),DepthwiseSeparableConv(64, 128, kernel_size3, stride1, padding1),nn.ReLU(),DepthwiseSeparableConv(128, 128, kernel_size3, stride1, padding1),nn.ReLU(),nn.MaxPool2d(kernel_size2, stride2),# 继续添加其他层)self.classifier nn.Sequential(nn.Linear(512 * 7 * 7, 1024),nn.ReLU(),nn.Dropout(0.5),nn.Linear(1024, 256),nn.ReLU(),nn.Dropout(0.5),nn.Linear(256, 3))def forward(self, x):x self.features(x)x torch.flatten(x, 1)x self.classifier(x)return x方法 3使用剪枝Pruning
剪枝可以通过删除冗余的神经元或通道来减少模型大小同时保持性能。
from torch.nn.utils import prune# 对卷积层和全连接层进行剪枝
def prune_model(model, amount0.3):for name, module in model.named_modules():if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear):prune.l1_unstructured(module, nameweight, amountamount)return model# 加载 VGG-16 模型
model vgg16(pretrainedFalse)
model.classifier[6] nn.Linear(4096, 3) # 调整最后一层
model prune_model(model, amount0.3)方法 4量化Quantization
将浮点权重32 位量化为 8 位整数显著减少模型的存储需求和计算量。
import torch.quantization# 模型静态量化
model vgg16(pretrainedFalse)
model.classifier[6] nn.Linear(4096, 3) # 调整最后一层
model.qconfig torch.quantization.get_default_qconfig(fbgemm)
torch.quantization.prepare(model, inplaceTrue)
torch.quantization.convert(model, inplaceTrue)方法 5知识蒸馏Knowledge Distillation
使用一个较大的 VGG-16 模型作为教师模型训练一个较小的学生模型。
class StudentModel(nn.Module):def __init__(self):super(StudentModel, self).__init__()self.features nn.Sequential(nn.Conv2d(3, 32, kernel_size3, stride1, padding1),nn.ReLU(),nn.MaxPool2d(kernel_size2, stride2),nn.Conv2d(32, 64, kernel_size3, stride1, padding1),nn.ReLU(),nn.MaxPool2d(kernel_size2, stride2),)self.classifier nn.Sequential(nn.Linear(64 * 56 * 56, 256),nn.ReLU(),nn.Linear(256, 3))def forward(self, x):x self.features(x)x torch.flatten(x, 1)x self.classifier(x)return x# 蒸馏过程
teacher_model vgg16(pretrainedTrue)
student_model StudentModel()
# 使用交叉熵 KL 散度进行蒸馏对比
方法参数量减少优点缺点减少全连接层大幅减少易于实现对准确率影响较小可能稍微降低表达能力深度可分离卷积中等减少显著减少计算量对准确率影响较小需替换所有卷积层剪枝根据需求减少不改变结构对性能影响小剪枝率需调优量化大幅减少存储效率高对推理速度影响大训练可能较复杂知识蒸馏可灵活调整小模型高效适合嵌入式设备需额外训练教师模型
八、个人学习总结
首先通过手动搭建VGG-16网络框架我深入理解了卷积神经网络的构造原理。VGG-16的模块化设计让我明白了深度网络的构造并不复杂只要按照合理的层叠规则结合激活函数、池化操作等就能有效提取图像的特征。手动实现的过程中我熟悉了PyTorch的nn.Module以及各类层的使用方法这种从零开始搭建的方式让我真正理解了每一层的作用。比如在实现第一个卷积块时我理解了小尺寸卷积核3x3在捕捉局部特征方面的优势。
其次通过调用官方的VGG-16网络框架我学会了如何快速使用预定义模型并进行定制化。具体来说我尝试了将VGG-16的全连接层调整为适配我使用的马铃薯病害数据集的3分类输出并验证了迁移学习在特定任务中的高效性。这让我认识到在实际项目中与其从头开始训练一个模型不如充分利用已经在大型数据集如ImageNet上预训练的模型从而节省时间和计算资源。
在模型训练与优化方面我深刻体会到学习率和正则化的重要性。例如尝试将学习率从1e-4提升到3e-4并结合余弦退火调度器时我观察到模型的收敛速度明显加快。同时添加weight_decay作为L2正则化后模型的泛化能力得到了提升这让我理解了过拟合问题的解决思路。此外我还实践了提前停止early stopping避免了不必要的计算浪费并学会了如何在训练过程中保存最佳模型这些都是非常实用的经验。
轻量化模型的探索是本次学习的一个亮点。通过减少全连接层的参数、引入深度可分离卷积、剪枝、量化以及知识蒸馏等方法我了解到在不影响模型性能的前提下如何显著减少参数量和计算量。这种思路对于部署在嵌入式设备上的模型尤其重要比如使用深度可分离卷积替代标准卷积时模型计算量减少的同时分类准确率几乎没有下降。此外尝试剪枝和量化让我对模型压缩技术的实际效果有了直观感受也认识到这些技术在实际应用中的潜力和局限性。