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