LeNet-5 到 ResNet-506个经典CNN模型PyTorch复现与参数量/FLOPs对比卷积神经网络CNN在计算机视觉领域的发展历程中LeNet-5、AlexNet、VGGNet、GoogLeNet和ResNet等经典模型构成了深度学习演进的里程碑。本文将从工程实践角度使用PyTorch框架完整复现这6个代表性模型并通过参数量、FLOPs浮点运算次数和内存占用等量化指标进行横向对比帮助开发者直观理解模型复杂度的演进规律。1. 实验环境与评估方法1.1 环境配置推荐使用以下环境运行本文代码import torch import torch.nn as nn from torchsummary import summary from thop import profile # 安装: pip install thop # 检查设备 device torch.device(cuda if torch.cuda.is_available() else cpu) print(fUsing {device} device)1.2 评估指标定义我们主要关注三个核心指标参数量Params模型可训练参数的总数FLOPs处理单张224x224输入图像所需的浮点运算次数内存占用Memory前向传播时的显存消耗提示实际评估时需统一输入尺寸为224x224LeNet-5调整为32x32batch_size12. LeNet-5实现与分析2.1 PyTorch实现class LeNet5(nn.Module): def __init__(self, num_classes10): super().__init__() self.features nn.Sequential( nn.Conv2d(1, 6, kernel_size5, stride1), nn.Tanh(), nn.AvgPool2d(kernel_size2), nn.Conv2d(6, 16, kernel_size5, stride1), nn.Tanh(), nn.AvgPool2d(kernel_size2) ) self.classifier nn.Sequential( nn.Linear(16*5*5, 120), nn.Tanh(), nn.Linear(120, 84), nn.Tanh(), nn.Linear(84, num_classes) ) def forward(self, x): x self.features(x) x torch.flatten(x, 1) x self.classifier(x) return x2.2 性能评估指标数值参数量61,706FLOPs0.43 M内存占用0.12 MB结构特点首个成功应用的CNN架构1998年交替使用卷积层和平均池化层全连接层占比超过90%参数量3. AlexNet实现与分析3.1 PyTorch实现class AlexNet(nn.Module): def __init__(self, num_classes1000): super().__init__() self.features nn.Sequential( nn.Conv2d(3, 64, kernel_size11, stride4, padding2), nn.ReLU(inplaceTrue), nn.MaxPool2d(kernel_size3, stride2), nn.Conv2d(64, 192, kernel_size5, padding2), nn.ReLU(inplaceTrue), nn.MaxPool2d(kernel_size3, stride2), nn.Conv2d(192, 384, kernel_size3, padding1), nn.ReLU(inplaceTrue), nn.Conv2d(384, 256, kernel_size3, padding1), nn.ReLU(inplaceTrue), nn.Conv2d(256, 256, kernel_size3, padding1), nn.ReLU(inplaceTrue), nn.MaxPool2d(kernel_size3, stride2), ) self.classifier nn.Sequential( nn.Dropout(), nn.Linear(256*6*6, 4096), nn.ReLU(inplaceTrue), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(inplaceTrue), nn.Linear(4096, num_classes), ) def forward(self, x): x self.features(x) x torch.flatten(x, 1) x self.classifier(x) return x3.2 性能评估指标数值参数量61.1 MFLOPs1.5 G内存占用3.8 MB关键创新首次使用ReLU激活函数引入Dropout防止过拟合使用局部响应归一化(LRN)双GPU并行训练设计4. VGGNet实现与分析4.1 VGG-16实现class VGG16(nn.Module): def __init__(self, num_classes1000): super().__init__() self.features nn.Sequential( nn.Conv2d(3, 64, kernel_size3, padding1), nn.ReLU(inplaceTrue), nn.Conv2d(64, 64, kernel_size3, padding1), nn.ReLU(inplaceTrue), nn.MaxPool2d(kernel_size2, stride2), # 后续类似结构省略... ) self.avgpool nn.AdaptiveAvgPool2d((7, 7)) self.classifier nn.Sequential( nn.Linear(512*7*7, 4096), nn.ReLU(inplaceTrue), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(inplaceTrue), nn.Dropout(), nn.Linear(4096, num_classes), ) def forward(self, x): x self.features(x) x self.avgpool(x) x torch.flatten(x, 1) x self.classifier(x) return x4.2 性能对比模型变体参数量FLOPs内存占用VGG-16138 M15.5 G7.2 MBVGG-19144 M19.6 G7.5 MB设计哲学全部使用3x3小卷积核堆叠通过增加深度提升性能全连接层参数量占比过大约90%5. GoogLeNet实现与分析5.1 Inception模块实现class Inception(nn.Module): def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_proj): super().__init__() self.branch1 nn.Sequential( nn.Conv2d(in_channels, ch1x1, kernel_size1), nn.BatchNorm2d(ch1x1), nn.ReLU(inplaceTrue) ) # 其他分支类似实现... def forward(self, x): branch1 self.branch1(x) branch2 self.branch2(x) branch3 self.branch3(x) branch4 self.branch4(x) return torch.cat([branch1, branch2, branch3, branch4], 1)5.2 性能特点指标数值参数量6.8 MFLOPs3.0 G内存占用4.2 MB创新点引入Inception模块并行结构使用1x1卷积降维全局平均池化替代全连接层辅助分类器辅助训练6. ResNet实现与分析6.1 残差块实现class BasicBlock(nn.Module): expansion 1 def __init__(self, inplanes, planes, stride1, downsampleNone): super().__init__() self.conv1 nn.Conv2d(inplanes, planes, kernel_size3, stridestride, padding1, biasFalse) self.bn1 nn.BatchNorm2d(planes) self.relu nn.ReLU(inplaceTrue) self.conv2 nn.Conv2d(planes, planes, kernel_size3, stride1, padding1, biasFalse) self.bn2 nn.BatchNorm2d(planes) self.downsample downsample self.stride stride def forward(self, x): identity x out self.conv1(x) out self.bn1(out) out self.relu(out) out self.conv2(out) out self.bn2(out) if self.downsample is not None: identity self.downsample(x) out identity out self.relu(out) return out6.2 不同深度ResNet对比模型参数量FLOPsTop-1错误率ResNet-1811.7 M3.6 G30.2%ResNet-3421.8 M7.3 G26.7%ResNet-5025.6 M8.2 G23.9%ResNet-10144.5 M15.6 G22.0%突破性设计残差连接解决梯度消失瓶颈结构(Bottleneck)减少计算量批量归一化加速训练全卷积设计提升灵活性7. 综合对比与选型建议7.1 量化指标对比表模型参数量FLOPs内存占用输入尺寸LeNet-561.7 K0.43 M0.12 MB32x32AlexNet61.1 M1.5 G3.8 MB224x224VGG-16138 M15.5 G7.2 MB224x224GoogLeNet6.8 M3.0 G4.2 MB224x224ResNet-5025.6 M8.2 G5.1 MB224x2247.2 实际应用建议轻量级场景考虑MobileNet或ShuffleNet变体平衡型任务ResNet-34/50在精度和效率间取得较好平衡高精度需求ResNet-152或EfficientNet-large实时系统采用通道剪枝后的ResNet或知识蒸馏模型在具体实现时发现ResNet的残差连接虽然增加了少量计算量但显著提升了训练稳定性和模型性能。实际部署时可以通过TensorRT等工具对模型进行进一步优化。
LeNet-5 到 ResNet-50:6个经典CNN模型PyTorch复现与参数量/FLOPs对比
LeNet-5 到 ResNet-506个经典CNN模型PyTorch复现与参数量/FLOPs对比卷积神经网络CNN在计算机视觉领域的发展历程中LeNet-5、AlexNet、VGGNet、GoogLeNet和ResNet等经典模型构成了深度学习演进的里程碑。本文将从工程实践角度使用PyTorch框架完整复现这6个代表性模型并通过参数量、FLOPs浮点运算次数和内存占用等量化指标进行横向对比帮助开发者直观理解模型复杂度的演进规律。1. 实验环境与评估方法1.1 环境配置推荐使用以下环境运行本文代码import torch import torch.nn as nn from torchsummary import summary from thop import profile # 安装: pip install thop # 检查设备 device torch.device(cuda if torch.cuda.is_available() else cpu) print(fUsing {device} device)1.2 评估指标定义我们主要关注三个核心指标参数量Params模型可训练参数的总数FLOPs处理单张224x224输入图像所需的浮点运算次数内存占用Memory前向传播时的显存消耗提示实际评估时需统一输入尺寸为224x224LeNet-5调整为32x32batch_size12. LeNet-5实现与分析2.1 PyTorch实现class LeNet5(nn.Module): def __init__(self, num_classes10): super().__init__() self.features nn.Sequential( nn.Conv2d(1, 6, kernel_size5, stride1), nn.Tanh(), nn.AvgPool2d(kernel_size2), nn.Conv2d(6, 16, kernel_size5, stride1), nn.Tanh(), nn.AvgPool2d(kernel_size2) ) self.classifier nn.Sequential( nn.Linear(16*5*5, 120), nn.Tanh(), nn.Linear(120, 84), nn.Tanh(), nn.Linear(84, num_classes) ) def forward(self, x): x self.features(x) x torch.flatten(x, 1) x self.classifier(x) return x2.2 性能评估指标数值参数量61,706FLOPs0.43 M内存占用0.12 MB结构特点首个成功应用的CNN架构1998年交替使用卷积层和平均池化层全连接层占比超过90%参数量3. AlexNet实现与分析3.1 PyTorch实现class AlexNet(nn.Module): def __init__(self, num_classes1000): super().__init__() self.features nn.Sequential( nn.Conv2d(3, 64, kernel_size11, stride4, padding2), nn.ReLU(inplaceTrue), nn.MaxPool2d(kernel_size3, stride2), nn.Conv2d(64, 192, kernel_size5, padding2), nn.ReLU(inplaceTrue), nn.MaxPool2d(kernel_size3, stride2), nn.Conv2d(192, 384, kernel_size3, padding1), nn.ReLU(inplaceTrue), nn.Conv2d(384, 256, kernel_size3, padding1), nn.ReLU(inplaceTrue), nn.Conv2d(256, 256, kernel_size3, padding1), nn.ReLU(inplaceTrue), nn.MaxPool2d(kernel_size3, stride2), ) self.classifier nn.Sequential( nn.Dropout(), nn.Linear(256*6*6, 4096), nn.ReLU(inplaceTrue), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(inplaceTrue), nn.Linear(4096, num_classes), ) def forward(self, x): x self.features(x) x torch.flatten(x, 1) x self.classifier(x) return x3.2 性能评估指标数值参数量61.1 MFLOPs1.5 G内存占用3.8 MB关键创新首次使用ReLU激活函数引入Dropout防止过拟合使用局部响应归一化(LRN)双GPU并行训练设计4. VGGNet实现与分析4.1 VGG-16实现class VGG16(nn.Module): def __init__(self, num_classes1000): super().__init__() self.features nn.Sequential( nn.Conv2d(3, 64, kernel_size3, padding1), nn.ReLU(inplaceTrue), nn.Conv2d(64, 64, kernel_size3, padding1), nn.ReLU(inplaceTrue), nn.MaxPool2d(kernel_size2, stride2), # 后续类似结构省略... ) self.avgpool nn.AdaptiveAvgPool2d((7, 7)) self.classifier nn.Sequential( nn.Linear(512*7*7, 4096), nn.ReLU(inplaceTrue), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(inplaceTrue), nn.Dropout(), nn.Linear(4096, num_classes), ) def forward(self, x): x self.features(x) x self.avgpool(x) x torch.flatten(x, 1) x self.classifier(x) return x4.2 性能对比模型变体参数量FLOPs内存占用VGG-16138 M15.5 G7.2 MBVGG-19144 M19.6 G7.5 MB设计哲学全部使用3x3小卷积核堆叠通过增加深度提升性能全连接层参数量占比过大约90%5. GoogLeNet实现与分析5.1 Inception模块实现class Inception(nn.Module): def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_proj): super().__init__() self.branch1 nn.Sequential( nn.Conv2d(in_channels, ch1x1, kernel_size1), nn.BatchNorm2d(ch1x1), nn.ReLU(inplaceTrue) ) # 其他分支类似实现... def forward(self, x): branch1 self.branch1(x) branch2 self.branch2(x) branch3 self.branch3(x) branch4 self.branch4(x) return torch.cat([branch1, branch2, branch3, branch4], 1)5.2 性能特点指标数值参数量6.8 MFLOPs3.0 G内存占用4.2 MB创新点引入Inception模块并行结构使用1x1卷积降维全局平均池化替代全连接层辅助分类器辅助训练6. ResNet实现与分析6.1 残差块实现class BasicBlock(nn.Module): expansion 1 def __init__(self, inplanes, planes, stride1, downsampleNone): super().__init__() self.conv1 nn.Conv2d(inplanes, planes, kernel_size3, stridestride, padding1, biasFalse) self.bn1 nn.BatchNorm2d(planes) self.relu nn.ReLU(inplaceTrue) self.conv2 nn.Conv2d(planes, planes, kernel_size3, stride1, padding1, biasFalse) self.bn2 nn.BatchNorm2d(planes) self.downsample downsample self.stride stride def forward(self, x): identity x out self.conv1(x) out self.bn1(out) out self.relu(out) out self.conv2(out) out self.bn2(out) if self.downsample is not None: identity self.downsample(x) out identity out self.relu(out) return out6.2 不同深度ResNet对比模型参数量FLOPsTop-1错误率ResNet-1811.7 M3.6 G30.2%ResNet-3421.8 M7.3 G26.7%ResNet-5025.6 M8.2 G23.9%ResNet-10144.5 M15.6 G22.0%突破性设计残差连接解决梯度消失瓶颈结构(Bottleneck)减少计算量批量归一化加速训练全卷积设计提升灵活性7. 综合对比与选型建议7.1 量化指标对比表模型参数量FLOPs内存占用输入尺寸LeNet-561.7 K0.43 M0.12 MB32x32AlexNet61.1 M1.5 G3.8 MB224x224VGG-16138 M15.5 G7.2 MB224x224GoogLeNet6.8 M3.0 G4.2 MB224x224ResNet-5025.6 M8.2 G5.1 MB224x2247.2 实际应用建议轻量级场景考虑MobileNet或ShuffleNet变体平衡型任务ResNet-34/50在精度和效率间取得较好平衡高精度需求ResNet-152或EfficientNet-large实时系统采用通道剪枝后的ResNet或知识蒸馏模型在具体实现时发现ResNet的残差连接虽然增加了少量计算量但显著提升了训练稳定性和模型性能。实际部署时可以通过TensorRT等工具对模型进行进一步优化。