mobilenet_v2.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import numpy as np
  18. import paddle
  19. from paddle import ParamAttr
  20. import paddle.nn as nn
  21. import paddle.nn.functional as F
  22. from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
  23. from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
  24. import math
  25. __all__ = [
  26. "MobileNetV2_x0_25", "MobileNetV2_x0_5", "MobileNetV2_x0_75",
  27. "MobileNetV2", "MobileNetV2_x1_5", "MobileNetV2_x2_0"
  28. ]
  29. class ConvBNLayer(nn.Layer):
  30. def __init__(self,
  31. num_channels,
  32. filter_size,
  33. num_filters,
  34. stride,
  35. padding,
  36. channels=None,
  37. num_groups=1,
  38. name=None,
  39. use_cudnn=True):
  40. super(ConvBNLayer, self).__init__()
  41. self._conv = Conv2D(
  42. in_channels=num_channels,
  43. out_channels=num_filters,
  44. kernel_size=filter_size,
  45. stride=stride,
  46. padding=padding,
  47. groups=num_groups,
  48. weight_attr=ParamAttr(name=name + "_weights"),
  49. bias_attr=False)
  50. self._batch_norm = BatchNorm(
  51. num_filters,
  52. param_attr=ParamAttr(name=name + "_bn_scale"),
  53. bias_attr=ParamAttr(name=name + "_bn_offset"),
  54. moving_mean_name=name + "_bn_mean",
  55. moving_variance_name=name + "_bn_variance")
  56. def forward(self, inputs, if_act=True):
  57. y = self._conv(inputs)
  58. y = self._batch_norm(y)
  59. if if_act:
  60. y = F.relu6(y)
  61. return y
  62. class InvertedResidualUnit(nn.Layer):
  63. def __init__(self, num_channels, num_in_filter, num_filters, stride,
  64. filter_size, padding, expansion_factor, name):
  65. super(InvertedResidualUnit, self).__init__()
  66. num_expfilter = int(round(num_in_filter * expansion_factor))
  67. self._expand_conv = ConvBNLayer(
  68. num_channels=num_channels,
  69. num_filters=num_expfilter,
  70. filter_size=1,
  71. stride=1,
  72. padding=0,
  73. num_groups=1,
  74. name=name + "_expand")
  75. self._bottleneck_conv = ConvBNLayer(
  76. num_channels=num_expfilter,
  77. num_filters=num_expfilter,
  78. filter_size=filter_size,
  79. stride=stride,
  80. padding=padding,
  81. num_groups=num_expfilter,
  82. use_cudnn=False,
  83. name=name + "_dwise")
  84. self._linear_conv = ConvBNLayer(
  85. num_channels=num_expfilter,
  86. num_filters=num_filters,
  87. filter_size=1,
  88. stride=1,
  89. padding=0,
  90. num_groups=1,
  91. name=name + "_linear")
  92. def forward(self, inputs, ifshortcut):
  93. y = self._expand_conv(inputs, if_act=True)
  94. y = self._bottleneck_conv(y, if_act=True)
  95. y = self._linear_conv(y, if_act=False)
  96. if ifshortcut:
  97. y = paddle.add(inputs, y)
  98. return y
  99. class InvresiBlocks(nn.Layer):
  100. def __init__(self, in_c, t, c, n, s, name):
  101. super(InvresiBlocks, self).__init__()
  102. self._first_block = InvertedResidualUnit(
  103. num_channels=in_c,
  104. num_in_filter=in_c,
  105. num_filters=c,
  106. stride=s,
  107. filter_size=3,
  108. padding=1,
  109. expansion_factor=t,
  110. name=name + "_1")
  111. self._block_list = []
  112. for i in range(1, n):
  113. block = self.add_sublayer(
  114. name + "_" + str(i + 1),
  115. sublayer=InvertedResidualUnit(
  116. num_channels=c,
  117. num_in_filter=c,
  118. num_filters=c,
  119. stride=1,
  120. filter_size=3,
  121. padding=1,
  122. expansion_factor=t,
  123. name=name + "_" + str(i + 1)))
  124. self._block_list.append(block)
  125. def forward(self, inputs):
  126. y = self._first_block(inputs, ifshortcut=False)
  127. for block in self._block_list:
  128. y = block(y, ifshortcut=True)
  129. return y
  130. class MobileNet(nn.Layer):
  131. def __init__(self, class_dim=1000, scale=1.0, prefix_name="", **args):
  132. super(MobileNet, self).__init__()
  133. self.scale = scale
  134. self.class_dim = class_dim
  135. bottleneck_params_list = [
  136. (1, 16, 1, 1),
  137. (6, 24, 2, 2),
  138. (6, 32, 3, 2),
  139. (6, 64, 4, 2),
  140. (6, 96, 3, 1),
  141. (6, 160, 3, 2),
  142. (6, 320, 1, 1),
  143. ]
  144. self.conv1 = ConvBNLayer(
  145. num_channels=3,
  146. num_filters=int(32 * scale),
  147. filter_size=3,
  148. stride=2,
  149. padding=1,
  150. name=prefix_name + "conv1_1")
  151. self.block_list = []
  152. i = 1
  153. in_c = int(32 * scale)
  154. for layer_setting in bottleneck_params_list:
  155. t, c, n, s = layer_setting
  156. i += 1
  157. block = self.add_sublayer(
  158. prefix_name + "conv" + str(i),
  159. sublayer=InvresiBlocks(
  160. in_c=in_c,
  161. t=t,
  162. c=int(c * scale),
  163. n=n,
  164. s=s,
  165. name=prefix_name + "conv" + str(i)))
  166. self.block_list.append(block)
  167. in_c = int(c * scale)
  168. self.out_c = int(1280 * scale) if scale > 1.0 else 1280
  169. self.conv9 = ConvBNLayer(
  170. num_channels=in_c,
  171. num_filters=self.out_c,
  172. filter_size=1,
  173. stride=1,
  174. padding=0,
  175. name=prefix_name + "conv9")
  176. self.pool2d_avg = AdaptiveAvgPool2D(1)
  177. self.out = Linear(
  178. self.out_c,
  179. class_dim,
  180. weight_attr=ParamAttr(name=prefix_name + "fc10_weights"),
  181. bias_attr=ParamAttr(name=prefix_name + "fc10_offset"))
  182. def forward(self, inputs):
  183. y = self.conv1(inputs, if_act=True)
  184. for block in self.block_list:
  185. y = block(y)
  186. y = self.conv9(y, if_act=True)
  187. y = self.pool2d_avg(y)
  188. y = paddle.flatten(y, start_axis=1, stop_axis=-1)
  189. y = self.out(y)
  190. return y
  191. def MobileNetV2_x0_25(**args):
  192. model = MobileNet(scale=0.25, **args)
  193. return model
  194. def MobileNetV2_x0_5(**args):
  195. model = MobileNet(scale=0.5, **args)
  196. return model
  197. def MobileNetV2_x0_75(**args):
  198. model = MobileNet(scale=0.75, **args)
  199. return model
  200. def MobileNetV2(**args):
  201. model = MobileNet(scale=1.0, **args)
  202. return model
  203. def MobileNetV2_x1_5(**args):
  204. model = MobileNet(scale=1.5, **args)
  205. return model
  206. def MobileNetV2_x2_0(**args):
  207. model = MobileNet(scale=2.0, **args)
  208. return model