rec_mobilenet_v3.py 5.5 KB

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