dilated_encoder.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  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. import paddle
  15. import paddle.nn as nn
  16. from paddle import ParamAttr
  17. from paddle.regularizer import L2Decay
  18. from paddle.nn.initializer import KaimingUniform, Constant, Normal
  19. from ppdet.core.workspace import register, serializable
  20. from ..shape_spec import ShapeSpec
  21. __all__ = ['DilatedEncoder']
  22. class Bottleneck(nn.Layer):
  23. def __init__(self, in_channels, mid_channels, dilation):
  24. super(Bottleneck, self).__init__()
  25. self.conv1 = nn.Sequential(* [
  26. nn.Conv2D(
  27. in_channels,
  28. mid_channels,
  29. 1,
  30. padding=0,
  31. weight_attr=ParamAttr(initializer=Normal(
  32. mean=0, std=0.01)),
  33. bias_attr=ParamAttr(initializer=Constant(0.0))),
  34. nn.BatchNorm2D(
  35. mid_channels,
  36. weight_attr=ParamAttr(regularizer=L2Decay(0.0)),
  37. bias_attr=ParamAttr(regularizer=L2Decay(0.0))),
  38. nn.ReLU(),
  39. ])
  40. self.conv2 = nn.Sequential(* [
  41. nn.Conv2D(
  42. mid_channels,
  43. mid_channels,
  44. 3,
  45. padding=dilation,
  46. dilation=dilation,
  47. weight_attr=ParamAttr(initializer=Normal(
  48. mean=0, std=0.01)),
  49. bias_attr=ParamAttr(initializer=Constant(0.0))),
  50. nn.BatchNorm2D(
  51. mid_channels,
  52. weight_attr=ParamAttr(regularizer=L2Decay(0.0)),
  53. bias_attr=ParamAttr(regularizer=L2Decay(0.0))),
  54. nn.ReLU(),
  55. ])
  56. self.conv3 = nn.Sequential(* [
  57. nn.Conv2D(
  58. mid_channels,
  59. in_channels,
  60. 1,
  61. padding=0,
  62. weight_attr=ParamAttr(initializer=Normal(
  63. mean=0, std=0.01)),
  64. bias_attr=ParamAttr(initializer=Constant(0.0))),
  65. nn.BatchNorm2D(
  66. in_channels,
  67. weight_attr=ParamAttr(regularizer=L2Decay(0.0)),
  68. bias_attr=ParamAttr(regularizer=L2Decay(0.0))),
  69. nn.ReLU(),
  70. ])
  71. def forward(self, x):
  72. identity = x
  73. y = self.conv3(self.conv2(self.conv1(x)))
  74. return y + identity
  75. @register
  76. class DilatedEncoder(nn.Layer):
  77. """
  78. DilatedEncoder used in YOLOF
  79. """
  80. def __init__(self,
  81. in_channels=[2048],
  82. out_channels=[512],
  83. block_mid_channels=128,
  84. num_residual_blocks=4,
  85. block_dilations=[2, 4, 6, 8]):
  86. super(DilatedEncoder, self).__init__()
  87. self.in_channels = in_channels
  88. self.out_channels = out_channels
  89. assert len(self.in_channels) == 1, "YOLOF only has one level feature."
  90. assert len(self.out_channels) == 1, "YOLOF only has one level feature."
  91. self.block_mid_channels = block_mid_channels
  92. self.num_residual_blocks = num_residual_blocks
  93. self.block_dilations = block_dilations
  94. out_ch = self.out_channels[0]
  95. self.lateral_conv = nn.Conv2D(
  96. self.in_channels[0],
  97. out_ch,
  98. 1,
  99. weight_attr=ParamAttr(initializer=KaimingUniform(
  100. negative_slope=1, nonlinearity='leaky_relu')),
  101. bias_attr=ParamAttr(initializer=Constant(value=0.0)))
  102. self.lateral_norm = nn.BatchNorm2D(
  103. out_ch,
  104. weight_attr=ParamAttr(regularizer=L2Decay(0.0)),
  105. bias_attr=ParamAttr(regularizer=L2Decay(0.0)))
  106. self.fpn_conv = nn.Conv2D(
  107. out_ch,
  108. out_ch,
  109. 3,
  110. padding=1,
  111. weight_attr=ParamAttr(initializer=KaimingUniform(
  112. negative_slope=1, nonlinearity='leaky_relu')))
  113. self.fpn_norm = nn.BatchNorm2D(
  114. out_ch,
  115. weight_attr=ParamAttr(regularizer=L2Decay(0.0)),
  116. bias_attr=ParamAttr(regularizer=L2Decay(0.0)))
  117. encoder_blocks = []
  118. for i in range(self.num_residual_blocks):
  119. encoder_blocks.append(
  120. Bottleneck(
  121. out_ch,
  122. self.block_mid_channels,
  123. dilation=block_dilations[i]))
  124. self.dilated_encoder_blocks = nn.Sequential(*encoder_blocks)
  125. def forward(self, inputs, for_mot=False):
  126. out = self.lateral_norm(self.lateral_conv(inputs[0]))
  127. out = self.fpn_norm(self.fpn_conv(out))
  128. out = self.dilated_encoder_blocks(out)
  129. return [out]
  130. @classmethod
  131. def from_config(cls, cfg, input_shape):
  132. return {'in_channels': [i.channels for i in input_shape], }
  133. @property
  134. def out_shape(self):
  135. return [ShapeSpec(channels=c) for c in self.out_channels]