1. VGG简介

VGGNet是由牛津大学视觉几何小组(Visual Geometry Group, VGG)提出的一种深层卷积网络结构,他们以7.32%的错误率赢得了2014年ILSVRC分类任务的亚军(冠军由GoogLeNet以6.65%的错误率夺得)和25.32%的错误率夺得定位任务(Localization)的第一名(GoogLeNet错误率为26.44%),网络名称VGGNet取自该小组名缩写。VGGNet是首批把图像分类的错误率降低到10%以内模型,同时该网络所采用的3×33\times33×3卷积核的思想是后来许多模型的基础,该模型发表在2015年国际学习表征会议(International Conference On Learning Representations, ICLR)后至今被引用的次数已经超过1万4千余次。

2. 模型结构


在原论文中的VGGNet包含了6个版本的演进,分别对应VGG11、VGG11-LRN、VGG13、VGG16-1、VGG16-3和VGG19,不同的后缀数值表示不同的网络层数(VGG11-LRN表示在第一层中采用了LRN的VGG11,VGG16-1表示后三组卷积块中最后一层卷积采用卷积核尺寸为1×11\times11×1,相应的VGG16-3表示卷积核尺寸为3×33\times33×3),本节介绍的VGG16为VGG16-3。图中的VGG16体现了VGGNet的核心思路,使用3×33\times33×3的卷积组合代替大尺寸的卷积(2个3×3卷积即可与3\times3卷积即可与3×3卷积即可与5×55\times55×5卷积拥有相同的感受视野),网络参数设置如表所示。

网络层 输入尺寸 核尺寸 输出尺寸 参数个数
卷积层C11C_{11}C11​ 224×224×3224\times224\times3224×224×3 3×3×64/13\times3\times64/13×3×64/1 224×224×64224\times224\times64224×224×64 (3×3×3+1)×64(3\times3\times3+1)\times64(3×3×3+1)×64
卷积层C12C_{12}C12​ 224×224×64224\times224\times64224×224×64 3×3×64/13\times3\times64/13×3×64/1 224×224×64224\times224\times64224×224×64 (3×3×64+1)×64(3\times3\times64+1)\times64(3×3×64+1)×64
下采样层Smax1S_{max1}Smax1​ 224×224×64224\times224\times64224×224×64 2×2/22\times2/22×2/2 112×112×64112\times112\times64112×112×64 000
卷积层C21C_{21}C21​ 112×112×64112\times112\times64112×112×64 3×3×128/13\times3\times128/13×3×128/1 112×112×128112\times112\times128112×112×128 (3×3×64+1)×128(3\times3\times64+1)\times128(3×3×64+1)×128
卷积层C22C_{22}C22​ 112×112×128112\times112\times128112×112×128 3×3×128/13\times3\times128/13×3×128/1 112×112×128112\times112\times128112×112×128 (3×3×128+1)×128(3\times3\times128+1)\times128(3×3×128+1)×128
下采样层Smax2S_{max2}Smax2​ 112×112×128112\times112\times128112×112×128 2×2/22\times2/22×2/2 56×56×12856\times56\times12856×56×128 000
卷积层C31C_{31}C31​ 56×56×12856\times56\times12856×56×128 3×3×256/13\times3\times256/13×3×256/1 56×56×25656\times56\times25656×56×256 (3×3×128+1)×256(3\times3\times128+1)\times256(3×3×128+1)×256
卷积层C32C_{32}C32​ 56×56×25656\times56\times25656×56×256 3×3×256/13\times3\times256/13×3×256/1 56×56×25656\times56\times25656×56×256 (3×3×256+1)×256(3\times3\times256+1)\times256(3×3×256+1)×256
卷积层C33C_{33}C33​ 56×56×25656\times56\times25656×56×256 3×3×256/13\times3\times256/13×3×256/1 56×56×25656\times56\times25656×56×256 (3×3×256+1)×256(3\times3\times256+1)\times256(3×3×256+1)×256
下采样层Smax3S_{max3}Smax3​ 56×56×25656\times56\times25656×56×256 2×2/22\times2/22×2/2 28×28×25628\times28\times25628×28×256 000
卷积层C41C_{41}C41​ 28×28×25628\times28\times25628×28×256 3×3×512/13\times3\times512/13×3×512/1 28×28×51228\times28\times51228×28×512 (3×3×256+1)×512(3\times3\times256+1)\times512(3×3×256+1)×512
卷积层C42C_{42}C42​ 28×28×51228\times28\times51228×28×512 3×3×512/13\times3\times512/13×3×512/1 28×28×51228\times28\times51228×28×512 (3×3×512+1)×512(3\times3\times512+1)\times512(3×3×512+1)×512
卷积层C43C_{43}C43​ 28×28×51228\times28\times51228×28×512 3×3×512/13\times3\times512/13×3×512/1 28×28×51228\times28\times51228×28×512 (3×3×512+1)×512(3\times3\times512+1)\times512(3×3×512+1)×512
下采样层Smax4S_{max4}Smax4​ 28×28×51228\times28\times51228×28×512 2×2/22\times2/22×2/2 14×14×51214\times14\times51214×14×512 000
卷积层C51C_{51}C51​ 14×14×51214\times14\times51214×14×512 3×3×512/13\times3\times512/13×3×512/1 14×14×51214\times14\times51214×14×512 (3×3×512+1)×512(3\times3\times512+1)\times512(3×3×512+1)×512
卷积层C52C_{52}C52​ 14×14×51214\times14\times51214×14×512 3×3×512/13\times3\times512/13×3×512/1 14×14×51214\times14\times51214×14×512 (3×3×512+1)×512(3\times3\times512+1)\times512(3×3×512+1)×512
卷积层C53C_{53}C53​ 14×14×51214\times14\times51214×14×512 3×3×512/13\times3\times512/13×3×512/1 14×14×51214\times14\times51214×14×512 (3×3×512+1)×512(3\times3\times512+1)\times512(3×3×512+1)×512
下采样层Smax5S_{max5}Smax5​ 14×14×51214\times14\times51214×14×512 2×2/22\times2/22×2/2 7×7×5127\times7\times5127×7×512 000
全连接层FC1FC_{1}FC1​ 7×7×5127\times7\times5127×7×512 (7×7×512)×4096(7\times7\times512)\times4096(7×7×512)×4096 1×40961\times40961×4096 (7×7×512+1)×4096(7\times7\times512+1)\times4096(7×7×512+1)×4096
全连接层FC2FC_{2}FC2​ 1×40961\times40961×4096 4096×40964096\times40964096×4096 1×40961\times40961×4096 (4096+1)×4096(4096+1)\times4096(4096+1)×4096
全连接层FC3FC_{3}FC3​ 1×40961\times40961×4096 4096×10004096\times10004096×1000 1×10001\times10001×1000 (4096+1)×1000(4096+1)\times1000(4096+1)×1000

3. 模型特性

  • 整个网络都使用了同样大小的卷积核尺寸3×33\times33×3和最大池化尺寸2×22\times22×2。
  • 1×11\times11×1卷积的意义主要在于线性变换,而输入通道数和输出通道数不变,没有发生降维。
  • 两个3×33\times33×3的卷积层串联相当于1个5×55\times55×5的卷积层,感受野大小为5×55\times55×5。同样地,3个3×33\times33×3的卷积层串联的效果则相当于1个7×77\times77×7的卷积层。这样的连接方式使得网络参数量更小,而且多层的激活函数令网络对特征的学习能力更强。
  • VGGNet在训练时有一个小技巧,先训练浅层的的简单网络VGG11,再复用VGG11的权重来初始化VGG13,如此反复训练并初始化VGG19,能够使训练时收敛的速度更快。
  • 在训练过程中使用多尺度的变换对原始数据做数据增强,使得模型不易过拟合。

4. VGG16模型PyTorch代码实现

import torch
import torch.nn as nn
from torchvision.models.utils import load_state_dict_from_url#--------------------------------------#
#   VGG16的结构
#--------------------------------------#
class VGG(nn.Module):def __init__(self, features, num_classes=1000, init_weights=True):super(VGG, self).__init__()self.features = features#--------------------------------------##   平均池化到7x7大小#--------------------------------------#self.avgpool = nn.AdaptiveAvgPool2d((7, 7))#--------------------------------------##   分类部分#--------------------------------------#self.classifier = nn.Sequential(nn.Linear(512 * 7 * 7, 4096),nn.ReLU(True),nn.Dropout(),nn.Linear(4096, 4096),nn.ReLU(True),nn.Dropout(),nn.Linear(4096, num_classes),)if init_weights:self._initialize_weights()def forward(self, x):#--------------------------------------##   特征提取#--------------------------------------#x = self.features(x)#--------------------------------------##   平均池化#--------------------------------------#x = self.avgpool(x)#--------------------------------------##   平铺后#--------------------------------------#x = torch.flatten(x, 1)#--------------------------------------##   分类部分#--------------------------------------#x = self.classifier(x)return xdef _initialize_weights(self):for m in self.modules():if isinstance(m, nn.Conv2d):nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')if m.bias is not None:nn.init.constant_(m.bias, 0)elif isinstance(m, nn.BatchNorm2d):nn.init.constant_(m.weight, 1)nn.init.constant_(m.bias, 0)elif isinstance(m, nn.Linear):nn.init.normal_(m.weight, 0, 0.01)nn.init.constant_(m.bias, 0)'''
假设输入图像为(600, 600, 3),随着cfg的循环,特征层变化如下:
600,600,3 -> 600,600,64 -> 600,600,64 -> 300,300,64 -> 300,300,128 -> 300,300,128 -> 150,150,128 -> 150,150,256 -> 150,150,256 -> 150,150,256
-> 75,75,256 -> 75,75,512 -> 75,75,512 -> 75,75,512 -> 37,37,512 ->  37,37,512 -> 37,37,512 -> 37,37,512
到cfg结束,我们获得了一个37,37,512的特征层
'''cfg = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M']#--------------------------------------#
#   特征提取部分
#--------------------------------------#
def make_layers(cfg, batch_norm=False):layers = []in_channels = 3for v in cfg:if v == 'M':layers += [nn.MaxPool2d(kernel_size=2, stride=2)]else:conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)if batch_norm:layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]else:layers += [conv2d, nn.ReLU(inplace=True)]in_channels = vreturn nn.Sequential(*layers)def decom_vgg16(pretrained = False):model = VGG(make_layers(cfg))if pretrained:state_dict = load_state_dict_from_url("https://download.pytorch.org/models/vgg16-397923af.pth", model_dir="./model_data")model.load_state_dict(state_dict)#----------------------------------------------------------------------------##   获取特征提取部分,最终获得一个37,37,1024的特征层#----------------------------------------------------------------------------#features    = list(model.features)[:30]#----------------------------------------------------------------------------##   获取分类部分,需要除去Dropout部分#----------------------------------------------------------------------------#classifier  = list(model.classifier)del classifier[6]del classifier[5]del classifier[2]features    = nn.Sequential(*features)classifier  = nn.Sequential(*classifier)return features, classifier

VGG-传统神经网络之巅峰相关推荐

  1. 降低百倍时间步,精度媲美传统神经网络:上交等机构提出ANN-SNN转换框架

    ©作者 | 刘方鑫,赵文博,蒋力 来源 | 机器之心 脉冲神经网络(Spiking Neural Network, SNN)被誉为第三代的神经网络,以其丰富的时空领域的神经动力学特性.多样的编码机制. ...

  2. 手撕VGG卷积神经网络-pytorch-详细注释版(可以直接替换自己数据集)-直接放置自己的数据集就能直接跑。跑的代码有问题的可以在评论区指出,看到了会回复。训练代码和预测代码均有。

    Alexnet网络详解代码:手撕Alexnet卷积神经网络-pytorch-详细注释版(可以直接替换自己数据集)-直接放置自己的数据集就能直接跑.跑的代码有问题的可以在评论区指出,看到了会回复.训练代 ...

  3. 【原创 深度学习与TensorFlow 动手实践系列 - 2】第二课:传统神经网络

    第二课 传统神经网络 <深度学习>整体结构: 线性回归 -> 神经网络 -> 卷积神经网络(CNN)-> 循环神经网络(RNN)- LSTM 目标分类(人脸识别,物品识别 ...

  4. DeepLearning六脉神剑第1式--传统神经网络

    ** DeepLearning六脉神剑第1式–传统神经网络 ** #本次笔记简述 阔别近一个月之久,小川最近也在机器学习的道路行进了一步,虽然速度不算太快,但是至少还是有所感悟,各位看官们有没有想念我 ...

  5. 深度神经网络和传统神经网络区别

    深度学习和神经网络的区别是什么? 从广义上说深度学习的网络结构也是多层神经网络的一种.传统意义上的多层神经网络是只有输入层.隐藏层.输出层.其中隐藏层的层数根据需要而定,没有明确的理论推导来说明到底多 ...

  6. 传统神经网络ANN训练算法总结

    学习/训练算法分类 神经网络类型的不同,对应了不同类型的训练/学习算法.因而根据神经网络的分类,总结起来,传统神经网络的学习算法也可以主要分为以下三类: 1)前馈型神经网络学习算法-----(前馈型神 ...

  7. 基于卷积神经网络VGG的猫狗识别

    !有需要本项目的实验源码的可以私信博主! 摘要:随着大数据时代的到来,深度学习.数据挖掘.图像处理等已经成为了一个热门研究方向.深度学习是一个复杂的机器学习算法,在语音和图像识别方面取得的效果,远远超 ...

  8. 基于VGG深度学习神经网络的猫狗数据集分类

    摘要:VGG网络是由牛津大学视觉几何组完成的基于深度卷积神经网络的大规模图像识别架构,该网络参考了AlexNet.ZFNet.OverFeat等经典的网络架构,从而得出的.这个架构参加了ILSVRC- ...

  9. 卷积神经网络之(使用重复元素的网络)VGG

    卷积神经网络之VGG VGG提出了可以通过重复使用简单的基础块来构建深度模型的思路. VGG模型 VGG块的组成规律是: 连续使用数个相同的填充为1,.窗口形状为3*3的卷积层 后接一个步幅为2.窗口 ...

最新文章

  1. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
  2. RemoteIE 开发者可跨平台使用IE测试网页
  3. JavaScript点击事件-一个按钮触发另一个按钮
  4. 了解一下JavaScript的未来——ECMAScript5
  5. logback实践笔记
  6. ubuntu16.04下ROS操作系统学习笔记(九)Moveit
  7. java 类 date_第十九回:Java常用类之Date
  8. Java进阶之路——从初级程序员到架构师,从小工到专家(转)
  9. 最短路应用 —— 解决某些计数、数论问题
  10. 张宇真题全解(纯题目)
  11. 字符常量和字符串常量
  12. bm3d matlab,Note --- BM3D
  13. 基于成熟网管平台的网管软件开发模式
  14. Git环境傻瓜式讲解
  15. 认知入门之经济学通识
  16. css里的英文翻译,css常见英文翻译
  17. 一张图让你了解五险一金
  18. 三维重建算法综述|传统+深度学习
  19. 分享112个JS特效动画效果,总有一款适合您
  20. matlab之常用命令整理(持续更新中...)

热门文章

  1. android 和风图标字体移植显示墨迹天气图标
  2. 解决《使命召唤:战区2.0》遇到的Windows10版本号不匹配问题,一并解决Windows10更新失败:UEFI固件受限问题
  3. C语言统计数字出现的次数
  4. 即日起,申请美国签证需要提供社交媒体账户
  5. win10应用商店打不开,错误代码0x80131500
  6. 智慧街道智慧社区一体化综合管理平台Java商业源码
  7. 实话实说 —— 心理模型vs实现模型
  8. 在 Power BI 中筛选每类产品的前三名
  9. Htmlayout早期的一个摸索作品
  10. 20201203英语单词学习(仅供自己记录)