rec_mobilenet_v3.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # copyright (c) 2020 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. from paddle import nn
  15. from ppocr.modeling.backbones.det_mobilenet_v3 import (
  16. ResidualUnit,
  17. ConvBNLayer,
  18. make_divisible,
  19. )
  20. __all__ = ["MobileNetV3"]
  21. class MobileNetV3(nn.Layer):
  22. def __init__(
  23. self,
  24. in_channels=3,
  25. model_name="small",
  26. scale=0.5,
  27. large_stride=None,
  28. small_stride=None,
  29. disable_se=False,
  30. **kwargs,
  31. ):
  32. super(MobileNetV3, self).__init__()
  33. self.disable_se = disable_se
  34. if small_stride is None:
  35. small_stride = [2, 2, 2, 2]
  36. if large_stride is None:
  37. large_stride = [1, 2, 2, 2]
  38. assert isinstance(
  39. large_stride, list
  40. ), "large_stride type must " "be list but got {}".format(type(large_stride))
  41. assert isinstance(
  42. small_stride, list
  43. ), "small_stride type must " "be list but got {}".format(type(small_stride))
  44. assert (
  45. len(large_stride) == 4
  46. ), "large_stride length must be " "4 but got {}".format(len(large_stride))
  47. assert (
  48. len(small_stride) == 4
  49. ), "small_stride length must be " "4 but got {}".format(len(small_stride))
  50. if model_name == "large":
  51. cfg = [
  52. # k, exp, c, se, nl, s,
  53. [3, 16, 16, False, "relu", large_stride[0]],
  54. [3, 64, 24, False, "relu", (large_stride[1], 1)],
  55. [3, 72, 24, False, "relu", 1],
  56. [5, 72, 40, True, "relu", (large_stride[2], 1)],
  57. [5, 120, 40, True, "relu", 1],
  58. [5, 120, 40, True, "relu", 1],
  59. [3, 240, 80, False, "hardswish", 1],
  60. [3, 200, 80, False, "hardswish", 1],
  61. [3, 184, 80, False, "hardswish", 1],
  62. [3, 184, 80, False, "hardswish", 1],
  63. [3, 480, 112, True, "hardswish", 1],
  64. [3, 672, 112, True, "hardswish", 1],
  65. [5, 672, 160, True, "hardswish", (large_stride[3], 1)],
  66. [5, 960, 160, True, "hardswish", 1],
  67. [5, 960, 160, True, "hardswish", 1],
  68. ]
  69. cls_ch_squeeze = 960
  70. elif model_name == "small":
  71. cfg = [
  72. # k, exp, c, se, nl, s,
  73. [3, 16, 16, True, "relu", (small_stride[0], 1)],
  74. [3, 72, 24, False, "relu", (small_stride[1], 1)],
  75. [3, 88, 24, False, "relu", 1],
  76. [5, 96, 40, True, "hardswish", (small_stride[2], 1)],
  77. [5, 240, 40, True, "hardswish", 1],
  78. [5, 240, 40, True, "hardswish", 1],
  79. [5, 120, 48, True, "hardswish", 1],
  80. [5, 144, 48, True, "hardswish", 1],
  81. [5, 288, 96, True, "hardswish", (small_stride[3], 1)],
  82. [5, 576, 96, True, "hardswish", 1],
  83. [5, 576, 96, True, "hardswish", 1],
  84. ]
  85. cls_ch_squeeze = 576
  86. else:
  87. raise NotImplementedError(
  88. "mode[" + model_name + "_model] is not implemented!"
  89. )
  90. supported_scale = [0.35, 0.5, 0.75, 1.0, 1.25]
  91. assert (
  92. scale in supported_scale
  93. ), "supported scales are {} but input scale is {}".format(
  94. supported_scale, scale
  95. )
  96. inplanes = 16
  97. # conv1
  98. self.conv1 = ConvBNLayer(
  99. in_channels=in_channels,
  100. out_channels=make_divisible(inplanes * scale),
  101. kernel_size=3,
  102. stride=2,
  103. padding=1,
  104. groups=1,
  105. if_act=True,
  106. act="hardswish",
  107. )
  108. i = 0
  109. block_list = []
  110. inplanes = make_divisible(inplanes * scale)
  111. for k, exp, c, se, nl, s in cfg:
  112. se = se and not self.disable_se
  113. block_list.append(
  114. ResidualUnit(
  115. in_channels=inplanes,
  116. mid_channels=make_divisible(scale * exp),
  117. out_channels=make_divisible(scale * c),
  118. kernel_size=k,
  119. stride=s,
  120. use_se=se,
  121. act=nl,
  122. )
  123. )
  124. inplanes = make_divisible(scale * c)
  125. i += 1
  126. self.blocks = nn.Sequential(*block_list)
  127. self.conv2 = ConvBNLayer(
  128. in_channels=inplanes,
  129. out_channels=make_divisible(scale * cls_ch_squeeze),
  130. kernel_size=1,
  131. stride=1,
  132. padding=0,
  133. groups=1,
  134. if_act=True,
  135. act="hardswish",
  136. )
  137. self.pool = nn.MaxPool2D(kernel_size=2, stride=2, padding=0)
  138. self.out_channels = make_divisible(scale * cls_ch_squeeze)
  139. def forward(self, x):
  140. x = self.conv1(x)
  141. x = self.blocks(x)
  142. x = self.conv2(x)
  143. x = self.pool(x)
  144. return x