网站制作流程分为哪三步,室内设计培训学校哪个好,长沙网站排名报价,哪些网站是响应式经典的卷积神经网络模型 - VGGNet
flyfish
VGG网络的名称来源于其开发团队——牛津大学的视觉几何组#xff08;Visual Geometry Group#xff09; 在2014年#xff0c;牛津大学的视觉几何组和Google DeepMind公司的研究人员也不例外#xff0c;研发了一个名为VGG的网络Visual Geometry Group 在2014年牛津大学的视觉几何组和Google DeepMind公司的研究人员也不例外研发了一个名为VGG的网络 VGG网络的一个主要贡献是展示了网络的深度即层数在提高图像识别性能方面的重要性。他们证明了通过增加网络的层数可以显著提高模型的性能。 他们使用了一种非常简单却有效的设计方式所有的卷积层都使用相同的小卷积核3x3这使得网络结构更为一致和简单。 VGG网络由多个卷积层和池化层堆叠而成卷积层负责提取图像的特征池化层则用于缩小特征图的尺寸。 这些层之后网络还有几个全连接层用于对提取的特征进行分类。 具体来说VGG16模型有16个权重层其中包含13个卷积层和3个全连接层。 VGG网络有多个变体例如VGG11、VGG13、VGG16、VGG19等这些变体的数字表示网络中权重层卷积层和全连接层的总数。例如VGG16有16个权重层VGG19有19个权重层。
import torchvision.models as models
vgg16 models.vgg16()
print(vgg16)VGG((features): Sequential((0): Conv2d(3, 64, kernel_size(3, 3), stride(1, 1), padding(1, 1))(1): ReLU(inplaceTrue)(2): Conv2d(64, 64, kernel_size(3, 3), stride(1, 1), padding(1, 1))(3): ReLU(inplaceTrue)(4): MaxPool2d(kernel_size2, stride2, padding0, dilation1, ceil_modeFalse)(5): Conv2d(64, 128, kernel_size(3, 3), stride(1, 1), padding(1, 1))(6): ReLU(inplaceTrue)(7): Conv2d(128, 128, kernel_size(3, 3), stride(1, 1), padding(1, 1))(8): ReLU(inplaceTrue)(9): MaxPool2d(kernel_size2, stride2, padding0, dilation1, ceil_modeFalse)(10): Conv2d(128, 256, kernel_size(3, 3), stride(1, 1), padding(1, 1))(11): ReLU(inplaceTrue)(12): Conv2d(256, 256, kernel_size(3, 3), stride(1, 1), padding(1, 1))(13): ReLU(inplaceTrue)(14): Conv2d(256, 256, kernel_size(3, 3), stride(1, 1), padding(1, 1))(15): ReLU(inplaceTrue)(16): MaxPool2d(kernel_size2, stride2, padding0, dilation1, ceil_modeFalse)(17): Conv2d(256, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))(18): ReLU(inplaceTrue)(19): Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))(20): ReLU(inplaceTrue)(21): Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))(22): ReLU(inplaceTrue)(23): MaxPool2d(kernel_size2, stride2, padding0, dilation1, ceil_modeFalse)(24): Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))(25): ReLU(inplaceTrue)(26): Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))(27): ReLU(inplaceTrue)(28): Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))(29): ReLU(inplaceTrue)(30): MaxPool2d(kernel_size2, stride2, padding0, dilation1, ceil_modeFalse))(avgpool): AdaptiveAvgPool2d(output_size(7, 7))(classifier): Sequential((0): Linear(in_features25088, out_features4096, biasTrue)(1): ReLU(inplaceTrue)(2): Dropout(p0.5, inplaceFalse)(3): Linear(in_features4096, out_features4096, biasTrue)(4): ReLU(inplaceTrue)(5): Dropout(p0.5, inplaceFalse)(6): Linear(in_features4096, out_features1000, biasTrue))
)手工用函数实现VGGNet
import torch
import torch.nn as nnclass VGG(nn.Module):def __init__(self):super(VGG, self).__init__()self.features nn.Sequential(nn.Conv2d(3, 64, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.Conv2d(64, 64, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.MaxPool2d(kernel_size2, stride2),nn.Conv2d(64, 128, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.Conv2d(128, 128, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.MaxPool2d(kernel_size2, stride2),nn.Conv2d(128, 256, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.Conv2d(256, 256, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.Conv2d(256, 256, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.MaxPool2d(kernel_size2, stride2),nn.Conv2d(256, 512, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.Conv2d(512, 512, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.Conv2d(512, 512, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.MaxPool2d(kernel_size2, stride2),nn.Conv2d(512, 512, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.Conv2d(512, 512, kernel_size3, stride1, padding1),nn.ReLU(inplaceTrue),nn.Conv2d(512, 512, kernel_size3, stride1, 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(p0.5),nn.Linear(4096, 4096),nn.ReLU(inplaceTrue),nn.Dropout(p0.5),nn.Linear(4096, 1000),)def forward(self, x):x self.features(x)x self.avgpool(x)x torch.flatten(x, 1)x self.classifier(x)return xvgg16 VGG()
print(vgg16)特点
特征图尺寸单调递减是的从输入的224x224开始经过每一个MaxPooling层特征图的尺寸都在减小224 - 112 - 56 - 28 - 14 - 7。 特征图数量单调递增是的从输入的3个通道开始特征图的数量随着网络的加深不断增加3 - 64 - 128 - 256 - 512。
假设输入图像的大小为 224 × 224 224 \times 224 224×224RGB图像3个通道。
第一段卷积层和池化层 输入 224 × 224 × 3 224 \times 224 \times 3 224×224×3 Conv2d(3, 64, kernel_size3, stride1, padding1) - 输出 224 × 224 × 64 224 \times 224 \times 64 224×224×64 ReLU(inplaceTrue) 输入 224 × 224 × 64 224 \times 224 \times 64 224×224×64 Conv2d(64, 64, kernel_size3, stride1, padding1) - 输出 224 × 224 × 64 224 \times 224 \times 64 224×224×64 ReLU(inplaceTrue) 输入 224 × 224 × 64 224 \times 224 \times 64 224×224×64 MaxPool2d(kernel_size2, stride2) - 输出 112 × 112 × 64 112 \times 112 \times 64 112×112×64
第二段卷积层和池化层 输入 112 × 112 × 64 112 \times 112 \times 64 112×112×64 Conv2d(64, 128, kernel_size3, stride1, padding1) - 输出 112 × 112 × 128 112 \times 112 \times 128 112×112×128 ReLU(inplaceTrue) 输入 112 × 112 × 128 112 \times 112 \times 128 112×112×128 Conv2d(128, 128, kernel_size3, stride1, padding1) - 输出 112 × 112 × 128 112 \times 112 \times 128 112×112×128 ReLU(inplaceTrue) 输入 112 × 112 × 128 112 \times 112 \times 128 112×112×128 MaxPool2d(kernel_size2, stride2) - 输出 56 × 56 × 128 56 \times 56 \times 128 56×56×128
第三段卷积层和池化层 输入 56 × 56 × 128 56 \times 56 \times 128 56×56×128 Conv2d(128, 256, kernel_size3, stride1, padding1) - 输出 56 × 56 × 256 56 \times 56 \times 256 56×56×256 ReLU(inplaceTrue) 输入 56 × 56 × 256 56 \times 56 \times 256 56×56×256 Conv2d(256, 256, kernel_size3, stride1, padding1) - 输出 56 × 56 × 256 56 \times 56 \times 256 56×56×256 ReLU(inplaceTrue) 输入 56 × 56 × 256 56 \times 56 \times 256 56×56×256 Conv2d(256, 256, kernel_size3, stride1, padding1) - 输出 56 × 56 × 256 56 \times 56 \times 256 56×56×256 ReLU(inplaceTrue) 输入 56 × 56 × 256 56 \times 56 \times 256 56×56×256 MaxPool2d(kernel_size2, stride2) - 输出 28 × 28 × 256 28 \times 28 \times 256 28×28×256
第四段卷积层和池化层 输入 28 × 28 × 256 28 \times 28 \times 256 28×28×256 Conv2d(256, 512, kernel_size3, stride1, padding1) - 输出 28 × 28 × 512 28 \times 28 \times 512 28×28×512 ReLU(inplaceTrue) 输入 28 × 28 × 512 28 \times 28 \times 512 28×28×512 Conv2d(512, 512, kernel_size3, stride1, padding1) - 输出 28 × 28 × 512 28 \times 28 \times 512 28×28×512 ReLU(inplaceTrue) 输入 28 × 28 × 512 28 \times 28 \times 512 28×28×512 Conv2d(512, 512, kernel_size3, stride1, padding1) - 输出 28 × 28 × 512 28 \times 28 \times 512 28×28×512 ReLU(inplaceTrue) 输入 28 × 28 × 512 28 \times 28 \times 512 28×28×512 MaxPool2d(kernel_size2, stride2) - 输出 14 × 14 × 512 14 \times 14 \times 512 14×14×512
第五段卷积层和池化层 输入 14 × 14 × 512 14 \times 14 \times 512 14×14×512 Conv2d(512, 512, kernel_size3, stride1, padding1) - 输出 14 × 14 × 512 14 \times 14 \times 512 14×14×512 ReLU(inplaceTrue) 输入 14 × 14 × 512 14 \times 14 \times 512 14×14×512 Conv2d(512, 512, kernel_size3, stride1, padding1) - 输出 14 × 14 × 512 14 \times 14 \times 512 14×14×512 ReLU(inplaceTrue) 输入 14 × 14 × 512 14 \times 14 \times 512 14×14×512 Conv2d(512, 512, kernel_size3, stride1, padding1) - 输出 14 × 14 × 512 14 \times 14 \times 512 14×14×512 ReLU(inplaceTrue) 输入 14 × 14 × 512 14 \times 14 \times 512 14×14×512 MaxPool2d(kernel_size2, stride2) - 输出 7 × 7 × 512 7 \times 7 \times 512 7×7×512
平均池化层
输入 7 × 7 × 512 7 \times 7 \times 512 7×7×512 AdaptiveAvgPool2d(output_size(7, 7)) - 输出 7 × 7 × 512 7 \times 7 \times 512 7×7×512
全连接层 输入 7 × 7 × 512 25088 7 \times 7 \times 512 25088 7×7×51225088 展平 Linear(25088, 4096) - 输出 4096 ReLU(inplaceTrue) Dropout(p0.5) 输入 4096 Linear(4096, 4096) - 输出 4096 ReLU(inplaceTrue) Dropout(p0.5) 输入 4096 Linear(4096, 1000) - 输出 1000
特征图的尺寸都在减小224 - 112 - 56 - 28 - 14 - 7。 特征图数量单调递增3 - 64 - 128 - 256 - 512。
如何数层数(数权重层)
卷积层 (Convolutional Layers)
1. Conv2d(3, 64, kernel_size(3, 3), stride(1, 1), padding(1, 1))
2. Conv2d(64, 64, kernel_size(3, 3), stride(1, 1), padding(1, 1))
3. Conv2d(64, 128, kernel_size(3, 3), stride(1, 1), padding(1, 1))
4. Conv2d(128, 128, kernel_size(3, 3), stride(1, 1), padding(1, 1))
5. Conv2d(128, 256, kernel_size(3, 3), stride(1, 1), padding(1, 1))
6. Conv2d(256, 256, kernel_size(3, 3), stride(1, 1), padding(1, 1))
7. Conv2d(256, 256, kernel_size(3, 3), stride(1, 1), padding(1, 1))
8. Conv2d(256, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))
9. Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))
10. Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))
11. Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))
12. Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))
13. Conv2d(512, 512, kernel_size(3, 3), stride(1, 1), padding(1, 1))全连接层 (Fully Connected Layers)
1. Linear(in_features25088, out_features4096, biasTrue)
2. Linear(in_features4096, out_features4096, biasTrue)
3. Linear(in_features4096, out_features1000, biasTrue)统计权重层
卷积层和全连接层总共13316层因此命名为VGG16。
注意ReLU和MaxPooling层不算作权重层因为它们不包含可训练的参数。 在计算神经网络的层数时ReLU和MaxPooling层不计入可训练层。可训练层指的是那些在训练过程中具有可调节权重的层如卷积层Conv2d和全连接层Linear。
ReLU 只对输入进行非线性变换MaxPooling 只对输入进行下采样数
ReLU层
ReLURectified Linear Unit是一种激活函数定义为 f ( x ) max ( 0 , x ) f(x) \max(0, x) f(x)max(0,x) 它对输入值进行逐元素的非线性变换将所有负值设为0正值保持不变。 ReLU层本质上只是一个数学操作不涉及任何参数。因此在训练过程中没有任何权重需要更新。
MaxPooling层
MaxPooling是一种池化操作用于减小特征图的尺寸同时保留最重要的特征。常见的最大池化操作是2x2窗口每个窗口选取其中的最大值。 MaxPooling层通过固定的规则选择每个窗口中的最大值来操作输入特征图没有任何需要学习的参数。