rec_mobilenet_v3.py 4.7 KB

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