rec_mobilenet_v3.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, MyConvBNLayer, \
  16. MyResidualUnit
  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. if model_name == "large":
  40. print("Mobilenet V3 is 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', 2],
  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. print("Mobilenet V3 is small")
  62. cfg = [
  63. # k, exp, c, se, nl, s,
  64. [3, 16, 16, True, 'relu', (small_stride[0], 1)],
  65. [3, 72, 24, False, 'relu', (small_stride[1], 1)],
  66. [3, 88, 24, False, 'relu', 1],
  67. [5, 96, 40, True, 'hardswish', (small_stride[2], 1)],
  68. [5, 240, 40, True, 'hardswish', 1],
  69. [5, 240, 40, True, 'hardswish', 1],
  70. [5, 120, 48, True, 'hardswish', 1],
  71. [5, 144, 48, True, 'hardswish', 1],
  72. [5, 288, 96, True, 'hardswish', (small_stride[3], 1)],
  73. [5, 576, 96, True, 'hardswish', 1],
  74. [5, 576, 96, True, 'hardswish', 1],
  75. ]
  76. cls_ch_squeeze = 576
  77. else:
  78. raise NotImplementedError("mode[" + model_name +
  79. "_model] is not implemented!")
  80. supported_scale = [0.35, 0.5, 0.75, 1.0, 1.25]
  81. assert scale in supported_scale, \
  82. "supported scales are {} but input scale is {}".format(supported_scale, scale)
  83. inplanes = 16
  84. # conv1
  85. # ConvBNLayer 改 MyConvBNLayer,可调整是否训练该层参数
  86. self.conv1 = MyConvBNLayer(
  87. in_channels=in_channels,
  88. out_channels=make_divisible(inplanes * scale),
  89. kernel_size=3,
  90. stride=2,
  91. padding=1,
  92. groups=1,
  93. if_act=True,
  94. act='hardswish',
  95. name='conv1',
  96. trainable=True
  97. )
  98. # blocks
  99. # 残差CNN
  100. i = 0
  101. block_list = []
  102. inplanes = make_divisible(inplanes * scale)
  103. # ResidualUnit 改 MyResidualUnit,可调整是否训练该层参数
  104. for (k, exp, c, se, nl, s) in cfg:
  105. # 前6层不训练参数
  106. if i < 6:
  107. block_list.append(
  108. MyResidualUnit(
  109. in_channels=inplanes,
  110. mid_channels=make_divisible(scale * exp),
  111. out_channels=make_divisible(scale * c),
  112. kernel_size=k,
  113. stride=s,
  114. use_se=se,
  115. act=nl,
  116. name='conv' + str(i + 2),
  117. trainable=True
  118. )
  119. )
  120. inplanes = make_divisible(scale * c)
  121. i += 1
  122. continue
  123. block_list.append(
  124. ResidualUnit(
  125. in_channels=inplanes,
  126. mid_channels=make_divisible(scale * exp),
  127. out_channels=make_divisible(scale * c),
  128. kernel_size=k,
  129. stride=s,
  130. use_se=se,
  131. act=nl,
  132. name='conv' + str(i + 2)))
  133. inplanes = make_divisible(scale * c)
  134. i += 1
  135. self.blocks = nn.Sequential(*block_list)
  136. # conv2
  137. self.conv2 = ConvBNLayer(
  138. in_channels=inplanes,
  139. out_channels=make_divisible(scale * cls_ch_squeeze),
  140. kernel_size=1,
  141. stride=1,
  142. padding=0,
  143. groups=1,
  144. if_act=True,
  145. act='hardswish',
  146. name='conv_last')
  147. # pool
  148. self.pool = nn.MaxPool2D(kernel_size=2, stride=2, padding=0)
  149. self.out_channels = make_divisible(scale * cls_ch_squeeze)
  150. def forward(self, x):
  151. x = self.conv1(x)
  152. x = self.blocks(x)
  153. x = self.conv2(x)
  154. x = self.pool(x)
  155. # print("mobile-net-v3 output shape ", x.shape)
  156. return x