fpn.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. This code is refer from:
  16. https://github.com/whai362/PSENet/blob/python3/models/neck/fpn.py
  17. """
  18. import paddle.nn as nn
  19. import paddle
  20. import math
  21. import paddle.nn.functional as F
  22. class Conv_BN_ReLU(nn.Layer):
  23. def __init__(self,
  24. in_planes,
  25. out_planes,
  26. kernel_size=1,
  27. stride=1,
  28. padding=0):
  29. super(Conv_BN_ReLU, self).__init__()
  30. self.conv = nn.Conv2D(
  31. in_planes,
  32. out_planes,
  33. kernel_size=kernel_size,
  34. stride=stride,
  35. padding=padding,
  36. bias_attr=False)
  37. self.bn = nn.BatchNorm2D(out_planes, momentum=0.1)
  38. self.relu = nn.ReLU()
  39. for m in self.sublayers():
  40. if isinstance(m, nn.Conv2D):
  41. n = m._kernel_size[0] * m._kernel_size[1] * m._out_channels
  42. m.weight = paddle.create_parameter(
  43. shape=m.weight.shape,
  44. dtype='float32',
  45. default_initializer=paddle.nn.initializer.Normal(
  46. 0, math.sqrt(2. / n)))
  47. elif isinstance(m, nn.BatchNorm2D):
  48. m.weight = paddle.create_parameter(
  49. shape=m.weight.shape,
  50. dtype='float32',
  51. default_initializer=paddle.nn.initializer.Constant(1.0))
  52. m.bias = paddle.create_parameter(
  53. shape=m.bias.shape,
  54. dtype='float32',
  55. default_initializer=paddle.nn.initializer.Constant(0.0))
  56. def forward(self, x):
  57. return self.relu(self.bn(self.conv(x)))
  58. class FPN(nn.Layer):
  59. def __init__(self, in_channels, out_channels):
  60. super(FPN, self).__init__()
  61. # Top layer
  62. self.toplayer_ = Conv_BN_ReLU(
  63. in_channels[3], out_channels, kernel_size=1, stride=1, padding=0)
  64. # Lateral layers
  65. self.latlayer1_ = Conv_BN_ReLU(
  66. in_channels[2], out_channels, kernel_size=1, stride=1, padding=0)
  67. self.latlayer2_ = Conv_BN_ReLU(
  68. in_channels[1], out_channels, kernel_size=1, stride=1, padding=0)
  69. self.latlayer3_ = Conv_BN_ReLU(
  70. in_channels[0], out_channels, kernel_size=1, stride=1, padding=0)
  71. # Smooth layers
  72. self.smooth1_ = Conv_BN_ReLU(
  73. out_channels, out_channels, kernel_size=3, stride=1, padding=1)
  74. self.smooth2_ = Conv_BN_ReLU(
  75. out_channels, out_channels, kernel_size=3, stride=1, padding=1)
  76. self.smooth3_ = Conv_BN_ReLU(
  77. out_channels, out_channels, kernel_size=3, stride=1, padding=1)
  78. self.out_channels = out_channels * 4
  79. for m in self.sublayers():
  80. if isinstance(m, nn.Conv2D):
  81. n = m._kernel_size[0] * m._kernel_size[1] * m._out_channels
  82. m.weight = paddle.create_parameter(
  83. shape=m.weight.shape,
  84. dtype='float32',
  85. default_initializer=paddle.nn.initializer.Normal(
  86. 0, math.sqrt(2. / n)))
  87. elif isinstance(m, nn.BatchNorm2D):
  88. m.weight = paddle.create_parameter(
  89. shape=m.weight.shape,
  90. dtype='float32',
  91. default_initializer=paddle.nn.initializer.Constant(1.0))
  92. m.bias = paddle.create_parameter(
  93. shape=m.bias.shape,
  94. dtype='float32',
  95. default_initializer=paddle.nn.initializer.Constant(0.0))
  96. def _upsample(self, x, scale=1):
  97. return F.upsample(x, scale_factor=scale, mode='bilinear')
  98. def _upsample_add(self, x, y, scale=1):
  99. return F.upsample(x, scale_factor=scale, mode='bilinear') + y
  100. def forward(self, x):
  101. f2, f3, f4, f5 = x
  102. p5 = self.toplayer_(f5)
  103. f4 = self.latlayer1_(f4)
  104. p4 = self._upsample_add(p5, f4, 2)
  105. p4 = self.smooth1_(p4)
  106. f3 = self.latlayer2_(f3)
  107. p3 = self._upsample_add(p4, f3, 2)
  108. p3 = self.smooth2_(p3)
  109. f2 = self.latlayer3_(f2)
  110. p2 = self._upsample_add(p3, f2, 2)
  111. p2 = self.smooth3_(p2)
  112. p3 = self._upsample(p3, 2)
  113. p4 = self._upsample(p4, 4)
  114. p5 = self._upsample(p5, 8)
  115. fuse = paddle.concat([p2, p3, p4, p5], axis=1)
  116. return fuse