darknet.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. import paddle
  15. from paddle import ParamAttr
  16. import paddle.nn as nn
  17. from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
  18. from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
  19. from paddle.nn.initializer import Uniform
  20. import math
  21. __all__ = ["DarkNet53"]
  22. class ConvBNLayer(nn.Layer):
  23. def __init__(self,
  24. input_channels,
  25. output_channels,
  26. filter_size,
  27. stride,
  28. padding,
  29. name=None):
  30. super(ConvBNLayer, self).__init__()
  31. self._conv = Conv2D(
  32. in_channels=input_channels,
  33. out_channels=output_channels,
  34. kernel_size=filter_size,
  35. stride=stride,
  36. padding=padding,
  37. weight_attr=ParamAttr(name=name + ".conv.weights"),
  38. bias_attr=False)
  39. bn_name = name + ".bn"
  40. self._bn = BatchNorm(
  41. num_channels=output_channels,
  42. act="relu",
  43. param_attr=ParamAttr(name=bn_name + ".scale"),
  44. bias_attr=ParamAttr(name=bn_name + ".offset"),
  45. moving_mean_name=bn_name + ".mean",
  46. moving_variance_name=bn_name + ".var")
  47. def forward(self, inputs):
  48. x = self._conv(inputs)
  49. x = self._bn(x)
  50. return x
  51. class BasicBlock(nn.Layer):
  52. def __init__(self, input_channels, output_channels, name=None):
  53. super(BasicBlock, self).__init__()
  54. self._conv1 = ConvBNLayer(
  55. input_channels, output_channels, 1, 1, 0, name=name + ".0")
  56. self._conv2 = ConvBNLayer(
  57. output_channels, output_channels * 2, 3, 1, 1, name=name + ".1")
  58. def forward(self, inputs):
  59. x = self._conv1(inputs)
  60. x = self._conv2(x)
  61. return paddle.add(x=inputs, y=x)
  62. class DarkNet(nn.Layer):
  63. def __init__(self, class_dim=1000):
  64. super(DarkNet, self).__init__()
  65. self.stages = [1, 2, 8, 8, 4]
  66. self._conv1 = ConvBNLayer(3, 32, 3, 1, 1, name="yolo_input")
  67. self._conv2 = ConvBNLayer(
  68. 32, 64, 3, 2, 1, name="yolo_input.downsample")
  69. self._basic_block_01 = BasicBlock(64, 32, name="stage.0.0")
  70. self._downsample_0 = ConvBNLayer(
  71. 64, 128, 3, 2, 1, name="stage.0.downsample")
  72. self._basic_block_11 = BasicBlock(128, 64, name="stage.1.0")
  73. self._basic_block_12 = BasicBlock(128, 64, name="stage.1.1")
  74. self._downsample_1 = ConvBNLayer(
  75. 128, 256, 3, 2, 1, name="stage.1.downsample")
  76. self._basic_block_21 = BasicBlock(256, 128, name="stage.2.0")
  77. self._basic_block_22 = BasicBlock(256, 128, name="stage.2.1")
  78. self._basic_block_23 = BasicBlock(256, 128, name="stage.2.2")
  79. self._basic_block_24 = BasicBlock(256, 128, name="stage.2.3")
  80. self._basic_block_25 = BasicBlock(256, 128, name="stage.2.4")
  81. self._basic_block_26 = BasicBlock(256, 128, name="stage.2.5")
  82. self._basic_block_27 = BasicBlock(256, 128, name="stage.2.6")
  83. self._basic_block_28 = BasicBlock(256, 128, name="stage.2.7")
  84. self._downsample_2 = ConvBNLayer(
  85. 256, 512, 3, 2, 1, name="stage.2.downsample")
  86. self._basic_block_31 = BasicBlock(512, 256, name="stage.3.0")
  87. self._basic_block_32 = BasicBlock(512, 256, name="stage.3.1")
  88. self._basic_block_33 = BasicBlock(512, 256, name="stage.3.2")
  89. self._basic_block_34 = BasicBlock(512, 256, name="stage.3.3")
  90. self._basic_block_35 = BasicBlock(512, 256, name="stage.3.4")
  91. self._basic_block_36 = BasicBlock(512, 256, name="stage.3.5")
  92. self._basic_block_37 = BasicBlock(512, 256, name="stage.3.6")
  93. self._basic_block_38 = BasicBlock(512, 256, name="stage.3.7")
  94. self._downsample_3 = ConvBNLayer(
  95. 512, 1024, 3, 2, 1, name="stage.3.downsample")
  96. self._basic_block_41 = BasicBlock(1024, 512, name="stage.4.0")
  97. self._basic_block_42 = BasicBlock(1024, 512, name="stage.4.1")
  98. self._basic_block_43 = BasicBlock(1024, 512, name="stage.4.2")
  99. self._basic_block_44 = BasicBlock(1024, 512, name="stage.4.3")
  100. self._pool = AdaptiveAvgPool2D(1)
  101. stdv = 1.0 / math.sqrt(1024.0)
  102. self._out = Linear(
  103. 1024,
  104. class_dim,
  105. weight_attr=ParamAttr(
  106. name="fc_weights", initializer=Uniform(-stdv, stdv)),
  107. bias_attr=ParamAttr(name="fc_offset"))
  108. def forward(self, inputs):
  109. x = self._conv1(inputs)
  110. x = self._conv2(x)
  111. x = self._basic_block_01(x)
  112. x = self._downsample_0(x)
  113. x = self._basic_block_11(x)
  114. x = self._basic_block_12(x)
  115. x = self._downsample_1(x)
  116. x = self._basic_block_21(x)
  117. x = self._basic_block_22(x)
  118. x = self._basic_block_23(x)
  119. x = self._basic_block_24(x)
  120. x = self._basic_block_25(x)
  121. x = self._basic_block_26(x)
  122. x = self._basic_block_27(x)
  123. x = self._basic_block_28(x)
  124. x = self._downsample_2(x)
  125. x = self._basic_block_31(x)
  126. x = self._basic_block_32(x)
  127. x = self._basic_block_33(x)
  128. x = self._basic_block_34(x)
  129. x = self._basic_block_35(x)
  130. x = self._basic_block_36(x)
  131. x = self._basic_block_37(x)
  132. x = self._basic_block_38(x)
  133. x = self._downsample_3(x)
  134. x = self._basic_block_41(x)
  135. x = self._basic_block_42(x)
  136. x = self._basic_block_43(x)
  137. x = self._basic_block_44(x)
  138. x = self._pool(x)
  139. x = paddle.squeeze(x, axis=[2, 3])
  140. x = self._out(x)
  141. return x
  142. def DarkNet53(**args):
  143. model = DarkNet(**args)
  144. return model