当前位置:   article > 正文

Resnet代码实现+为什么使用全局平均池化​​​​​​​_resnet接全局平均池化层

resnet接全局平均池化层

1.ResNet直接使用stride=2的卷积做下采样,并且用global average pool层替换了全连接层

GAP的真正意义是:对整个网路在结构上做正则化防止过拟合。但是值得我们注意的是,使用gap可能会造成收敛速度减慢。用一个GAP将N个feature map降维成1*N大小的feature map,再用class个1*1卷积核将1*N的feature map卷成1*class的向量。

①摘取重点于:为什么使用全局平均池化

在卷积神经网络的初期,卷积层通过池化层(一般是MaxPooling)后总是要一个或n个全连接层,全连接网络可以使feature map的维度减少,进而输入到softmax分类。其特征就是全连接层的参数超多,模型本身非常臃肿,又会造成过拟合。(现在已经很少大量使用fc层),用pooling来代替全连接。就解决了之前的问题:要不要在fc层使用dropout。使用AVP就不要了。

(作者自己思考的部分,不知对错)全局平均池化层代替全连接层虽然有好处,但是不利于迁移学习。因为参数较为“固化”在卷积的诸层网络中。增加新的分类,那就意味着相当数量的卷积特征要做调整。而全连接层模型则可以更好的迁移学习,因为它的参数很大一部分调整在全连接层,迁移的时候卷积层可能也会调整,但是相对来讲要小的多了。

再对比:论文R-FCN(全卷积+位置敏感型“Score Map”) (目标检测)(two-stage)(深度学习)(NIPS 2016)中的Position-sensitive score map

小例子1  ③④

2.ResNet的一个重要设计原则是:当feature map大小降低一半时,feature map的数量增加一倍,这保持了网络层的复杂度。

那么我是否也要一致呢?

  1. import torch.nn as nn
  2. import math
  3. import torch.utils.model_zoo as model_zoo
  4. def conv3x3(in_planes, out_planes, stride=1):
  5. return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
  6. padding=1, bias=False)
  7. class BasicBlock(nn.Module):
  8. expansion = 1
  9. def __init__(self, inplanes, planes, stride=1, downsample=None):
  10. super(BasicBlock, self).__init__()
  11. self.conv1 = conv3x3(inplanes, planes, stride)
  12. self.bn1 = nn.BatchNorm2d(planes)
  13. self.relu = nn.ReLU(inplace=True)
  14. self.conv2 = conv3x3(planes, planes)
  15. self.bn2 = nn.BatchNorm2d(planes)
  16. self.downsample = downsample
  17. self.stride = stride
  18. def forward(self, x):
  19. residual = x
  20. out = self.conv1(x)
  21. out = self.bn1(out)
  22. out = self.relu(out)
  23. out = self.conv2(out)
  24. out = self.bn2(out)
  25. if self.downsample is not None:
  26. residual = self.downsample(x)
  27. out += residual
  28. out = self.relu(out)
  29. return out
  30. class ResNet(nn.Module):
  31. def __init__(self, block, layers, num_classes=1000):
  32. self.inplanes = 64
  33. super(ResNet, self).__init__()
  34. self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
  35. bias=False)
  36. self.bn1 = nn.BatchNorm2d(64)
  37. self.relu = nn.ReLU(inplace=True)
  38. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  39. self.layer1 = self._make_layer(block, 64, layers[0])
  40. self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
  41. self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
  42. self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
  43. self.avgpool = nn.AvgPool2d(7, stride=1)
  44. self.fc = nn.Linear(512 * block.expansion, num_classes)
  45. for m in self.modules():
  46. if isinstance(m, nn.Conv2d):
  47. n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
  48. m.weight.data.normal_(0, math.sqrt(2. / n))
  49. elif isinstance(m, nn.BatchNorm2d):
  50. m.weight.data.fill_(1)
  51. m.bias.data.zero_()
  52. def _make_layer(self, block, planes, blocks, stride=1):
  53. downsample = None
  54. if stride != 1 or self.inplanes != planes * block.expansion:
  55. downsample = nn.Sequential(
  56. nn.Conv2d(self.inplanes, planes * block.expansion,
  57. kernel_size=1, stride=stride, bias=False),
  58. nn.BatchNorm2d(planes * block.expansion),
  59. )
  60. layers = []
  61. layers.append(block(self.inplanes, planes, stride, downsample))
  62. self.inplanes = planes * block.expansion
  63. for i in range(1, blocks):
  64. layers.append(block(self.inplanes, planes))
  65. return nn.Sequential(*layers)
  66. def forward(self, x):
  67. x = self.conv1(x)
  68. x = self.bn1(x)
  69. x = self.relu(x)
  70. x = self.maxpool(x)
  71. x = self.layer1(x)
  72. x = self.layer2(x)
  73. x = self.layer3(x)
  74. x = self.layer4(x)
  75. x = self.avgpool(x)
  76. x = x.view(x.size(0), -1)
  77. x = self.fc(x)
  78. return x
  79. def resnet18(pretrained=False, **kwargs):
  80. trained (bool): If True, returns a model pre-trained on ImageNet
  81. model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
  82. if pretrained:
  83. model.load_state_dict(model_zoo.load_url(model_urls['resnet18']))
  84. return model

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/92546
推荐阅读
  

闽ICP备14008679号