前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于Pytorch构建VGG-16Net网络对cifar-10进行分类

基于Pytorch构建VGG-16Net网络对cifar-10进行分类

作者头像
python与大数据分析
发布2023-09-03 21:21:42
2300
发布2023-09-03 21:21:42
举报

VGGNet发布于 2014 年,作者是 Karen Simonyan 和 Andrew Zisserman,该网络表明堆叠多个层是提升计算机视觉性能的关键因素。

VGGNet 包含 11层或13层或16层 或 19 层,主要由小型的 3×3 卷积操作和 2×2 池化操作组成。基本组成部分是:1. 带填充以保持分辨率的卷积层;2. 非线性激活函 数,如ReLU;3. 汇聚层,如最大池化层。

VGG 的优点在于,堆叠多个小的卷积核而不使用池化操作可以增加网络的表征深度,同时限制参数的数量。例如,通过堆叠 3 个 3×3 卷积层而不是使用单个的 7×7 层,可以克服一些限制。首先,这样做组合了三个非线性函数,而不只是一个,使得决策函数更有判别力和表征能力。第二,参数量减少了 81%,而感受野保持不变。另外,小卷积核的使用也扮演了正则化器的角色,并提高了不同卷积核的有效性。

VGG 的缺点在于,其评估的开销比浅层网络更加昂贵,内存和参数(140M)也更多。这些参数的大部分都可以归因于第一个全连接层。结果表明,这些层可以在不降低性能的情况下移除,同时显著减少了必要参数的数量。

VGG突出的贡献是:用较小的卷积滤波器(3×3)代替之前较大的卷积滤波器(7×7/5×5),通过增加深度至16-19个权重层,有效提高网络的整体性能。

VGG网络结构图

VGG网络结构细图

VGG模型分为4个深度,即:11、13、16、19 weight layers。其中,较为经典的是深度为16和19的VGG16、VGG19。

VGG模型各类子模型对比:

代码仍然遵循之前的代码框架,只是定义了一下VGG_16Net模型,只需要把前文中的AlexNet替换为VGG_16Net即可。

代码语言:javascript
复制
  1. #定义VGG_16Net网络
  2. class VGG_16Net(nn.Module):
  3. def __init__(self):
  4. super(VGG_16Net, self).__init__()
  5. # 定义每一个就卷积层
  6. self.conv1 = nn.Conv2d(3, 64, 3, padding=1)
  7. self.conv2 = nn.Conv2d(64, 64, 3, padding=1)
  8. self.pool1 = nn.MaxPool2d(2, 2)
  9. self.bn1 = nn.BatchNorm2d(64)
  10. # nn.BatchNorm2d是 PyTorch中的一个函数,用于创建二维批量归一化层。
  11. # 批量归一化是一种用于提高深度学习模型性能和稳定性的技术。
  12. # 它通过在小批量数据上减去平均值并除以激活值的标准差来对每个神经元的输出进行归一化。
  13. # 这样可以降低内部协变量变化,即训练期间由于权重的更新而引起的层输入分布的变化。
  14. # 通过减少内部协变量变化,批量归一化可以帮助模型更快地学习并更好地推广到新数据
  15. # BatchNorm2d参数:
  16. # num_features:输入数据的shape一般为[batch_size, channel, height, width], num_features为其中的channel;
  17. # eps: 分母中添加的一个值,目的是为了计算的稳定性,默认:1e-5;
  18. # momentum: 一个用于运行过程中均值和方差的一个估计参数,默认值为0.1.
  19. # affine:当设为true时,给定可以学习的系数矩阵γ 和β
  20. self.relu1 = nn.ReLU()
  21. self.conv3 = nn.Conv2d(64, 128, 3, padding=1)
  22. self.conv4 = nn.Conv2d(128, 128, 3, padding=1)
  23. self.pool2 = nn.MaxPool2d(2, 2, padding=1)
  24. self.bn2 = nn.BatchNorm2d(128)
  25. self.relu2 = nn.ReLU()
  26. self.conv5 = nn.Conv2d(128, 128, 3, padding=1)
  27. self.conv6 = nn.Conv2d(128, 128, 3, padding=1)
  28. self.conv7 = nn.Conv2d(128, 128, 1, padding=1)
  29. self.pool3 = nn.MaxPool2d(2, 2, padding=1)
  30. self.bn3 = nn.BatchNorm2d(128)
  31. self.relu3 = nn.ReLU()
  32. self.conv8 = nn.Conv2d(128, 256, 3, padding=1)
  33. self.conv9 = nn.Conv2d(256, 256, 3, padding=1)
  34. self.conv10 = nn.Conv2d(256, 256, 1, padding=1)
  35. self.pool4 = nn.MaxPool2d(2, 2, padding=1)
  36. self.bn4 = nn.BatchNorm2d(256)
  37. self.relu4 = nn.ReLU()
  38. self.conv11 = nn.Conv2d(256, 512, 3, padding=1)
  39. self.conv12 = nn.Conv2d(512, 512, 3, padding=1)
  40. self.conv13 = nn.Conv2d(512, 512, 1, padding=1)
  41. self.pool5 = nn.MaxPool2d(2, 2, padding=1)
  42. self.bn5 = nn.BatchNorm2d(512)
  43. self.relu5 = nn.ReLU()
  44. self.fc14 = nn.Linear(512* 4* 4, 1024)
  45. self.drop1 = nn.Dropout2d()
  46. self.fc15 = nn.Linear(1024, 1024)
  47. self.drop2 = nn.Dropout2d()
  48. self.fc16 = nn.Linear(1024, 10)
  49. def forward(self, x):
  50. x = self.conv1(x)
  51. x = self.conv2(x)
  52. x = self.pool1(x)
  53. x = self.bn1(x)
  54. x = self.relu1(x)
  55. x = self.conv3(x)
  56. x = self.conv4(x)
  57. x = self.pool2(x)
  58. x = self.bn2(x)
  59. x = self.relu2(x)
  60. x = self.conv5(x)
  61. x = self.conv6(x)
  62. x = self.conv7(x)
  63. x = self.pool3(x)
  64. x = self.bn3(x)
  65. x = self.relu3(x)
  66. x = self.conv8(x)
  67. x = self.conv9(x)
  68. x = self.conv10(x)
  69. x = self.pool4(x)
  70. x = self.bn4(x)
  71. x = self.relu4(x)
  72. x = self.conv11(x)
  73. x = self.conv12(x)
  74. x = self.conv13(x)
  75. x = self.pool5(x)
  76. x = self.bn5(x)
  77. x = self.relu5(x)
  78. x = x.view(-1, 512* 4* 4)
  79. x = F.relu(self.fc14(x))
  80. x = self.drop1(x)
  81. x = F.relu(self.fc15(x))
  82. x = self.drop2(x)
  83. x = self.fc16(x)
  84. return x

最后的VGG-16模型输出为:

  1. VGG_16Net(
  2. (conv1): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  3. (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  4. (pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  5. (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  6. (relu1): ReLU()
  7. (conv3): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  8. (conv4): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  9. (pool2): MaxPool2d(kernel_size=2, stride=2, padding=1, dilation=1, ceil_mode=False)
  10. (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  11. (relu2): ReLU()
  12. (conv5): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  13. (conv6): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  14. (conv7): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), padding=(1, 1))
  15. (pool3): MaxPool2d(kernel_size=2, stride=2, padding=1, dilation=1, ceil_mode=False)
  16. (bn3): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  17. (relu3): ReLU()
  18. (conv8): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  19. (conv9): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  20. (conv10): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), padding=(1, 1))
  21. (pool4): MaxPool2d(kernel_size=2, stride=2, padding=1, dilation=1, ceil_mode=False)
  22. (bn4): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  23. (relu4): ReLU()
  24. (conv11): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  25. (conv12): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  26. (conv13): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1), padding=(1, 1))
  27. (pool5): MaxPool2d(kernel_size=2, stride=2, padding=1, dilation=1, ceil_mode=False)
  28. (bn5): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  29. (relu5): ReLU()
  30. (fc14): Linear(in_features=8192, out_features=1024, bias=True)
  31. (drop1): Dropout2d(p=0.5, inplace=False)
  32. (fc15): Linear(in_features=1024, out_features=1024, bias=True)
  33. (drop2): Dropout2d(p=0.5, inplace=False)
  34. (fc16): Linear(in_features=1024, out_features=10, bias=True)
  35. )

训练和验证过程,在6G显卡上运行不到十分钟,在CPU电脑上运行4个多小时,最后验证准确率为82%左右,较AlexNet网络提升了7%。

  1. Train Epoch1 Loss: 2.307108, accuracy: 17.187500%
  2. test_avarage_loss: 0.019403, accuracy: 56.260000%
  3. end_time: 2023-08-16 01:38:04
  4. ...
  5. start_time 2023-08-16 01:46:14
  6. Train Epoch20 Loss: 0.090851, accuracy: 95.312500%
  7. test_avarage_loss: 0.014590, accuracy: 81.850000%
  8. end_time: 2023-08-16 01:46:41

经过测试,前64张图片为例

  1. GroundTruth: deer bird truck ship frog deer plane frog cat frog plane ship car car ship car bird deer horse ship cat bird car frog horse cat dog frog frog cat cat cat horse ship car frog deer ship dog frog frog bird cat ship plane deer horse plane cat car bird horse horse deer car truck bird bird ship truck frog frog car dog
  2. Predicted: deer bird truck ship frog deer plane frog dog frog plane ship car car truck car bird deer horse ship cat bird car frog horse cat dog frog cat plane cat bird horse ship car frog deer truck bird frog frog bird deer ship plane deer horse plane cat car bird horse horse horse car truck bird bird ship truck deer frog car cat

训练的过程数据,包括损失率和准确率,

这是基于深度学习开展图像识别的第三个模型,较之前的AlexNet又有了一定的提升,后续继续朝着新网络努力。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-08-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 python与大数据分析 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码仍然遵循之前的代码框架,只是定义了一下VGG_16Net模型,只需要把前文中的AlexNet替换为VGG_16Net即可。
  • 最后的VGG-16模型输出为:
  • 训练和验证过程,在6G显卡上运行不到十分钟,在CPU电脑上运行4个多小时,最后验证准确率为82%左右,较AlexNet网络提升了7%。
  • 经过测试,前64张图片为例
  • 训练的过程数据,包括损失率和准确率,
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档