rec_mobilenet_v3.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 ResidualUnit, ConvBNLayer, make_divisible
  16. __all__ = ['MobileNetV3']
  17. class MobileNetV3(nn.Layer):
  18. def __init__(self,
  19. in_channels=3,
  20. model_name='small',
  21. scale=0.5,
  22. large_stride=None,
  23. small_stride=None,
  24. disable_se=False,
  25. **kwargs):
  26. super(MobileNetV3, self).__init__()
  27. self.disable_se = disable_se
  28. if small_stride is None:
  29. small_stride = [2, 2, 2, 2]
  30. if large_stride is None:
  31. large_stride = [1, 2, 2, 2]
  32. assert isinstance(large_stride, list), "large_stride type must " \
  33. "be list but got {}".format(type(large_stride))
  34. assert isinstance(small_stride, list), "small_stride type must " \
  35. "be list but got {}".format(type(small_stride))
  36. assert len(large_stride) == 4, "large_stride length must be " \
  37. "4 but got {}".format(len(large_stride))
  38. assert len(small_stride) == 4, "small_stride length must be " \
  39. "4 but got {}".format(len(small_stride))
  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. i = 0
  94. block_list = []
  95. inplanes = make_divisible(inplanes * scale)
  96. for (k, exp, c, se, nl, s) in cfg:
  97. se = se and not self.disable_se
  98. block_list.append(
  99. ResidualUnit(
  100. in_channels=inplanes,
  101. mid_channels=make_divisible(scale * exp),
  102. out_channels=make_divisible(scale * c),
  103. kernel_size=k,
  104. stride=s,
  105. use_se=se,
  106. act=nl))
  107. inplanes = make_divisible(scale * c)
  108. i += 1
  109. self.blocks = nn.Sequential(*block_list)
  110. self.conv2 = ConvBNLayer(
  111. in_channels=inplanes,
  112. out_channels=make_divisible(scale * cls_ch_squeeze),
  113. kernel_size=1,
  114. stride=1,
  115. padding=0,
  116. groups=1,
  117. if_act=True,
  118. act='hardswish')
  119. self.pool = nn.MaxPool2D(kernel_size=2, stride=2, padding=0)
  120. self.out_channels = make_divisible(scale * cls_ch_squeeze)
  121. def forward(self, x):
  122. x = self.conv1(x)
  123. x = self.blocks(x)
  124. x = self.conv2(x)
  125. x = self.pool(x)
  126. return x