mobilenet_v3.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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. import paddle.fluid as fluid
  15. from paddle.fluid.param_attr import ParamAttr
  16. from paddle.fluid.regularizer import L2Decay
  17. import math
  18. class MobileNetV3():
  19. """
  20. MobileNet v3, see https://arxiv.org/abs/1905.02244
  21. Args:
  22. scale (float): scaling factor for convolution groups proportion of mobilenet_v3.
  23. model_name (str): There are two modes, small and large.
  24. norm_type (str): normalization type, 'bn' and 'sync_bn' are supported.
  25. norm_decay (float): weight decay for normalization layer weights.
  26. conv_decay (float): weight decay for convolution layer weights.
  27. with_extra_blocks (bool): if extra blocks should be added.
  28. extra_block_filters (list): number of filter for each extra block.
  29. """
  30. def __init__(self,
  31. scale=1.0,
  32. model_name='small',
  33. with_extra_blocks=False,
  34. conv_decay=0.0,
  35. norm_type='bn',
  36. norm_decay=0.0,
  37. extra_block_filters=[[256, 512], [128, 256], [128, 256],
  38. [64, 128]],
  39. num_classes=None,
  40. lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0]):
  41. assert len(lr_mult_list) == 5, \
  42. "lr_mult_list length in MobileNetV3 must be 5 but got {}!!".format(
  43. len(lr_mult_list))
  44. self.scale = scale
  45. self.with_extra_blocks = with_extra_blocks
  46. self.extra_block_filters = extra_block_filters
  47. self.conv_decay = conv_decay
  48. self.norm_decay = norm_decay
  49. self.inplanes = 16
  50. self.end_points = []
  51. self.block_stride = 1
  52. self.num_classes = num_classes
  53. self.lr_mult_list = lr_mult_list
  54. self.curr_stage = 0
  55. if model_name == "large":
  56. self.cfg = [
  57. # kernel_size, expand, channel, se_block, act_mode, stride
  58. [3, 16, 16, False, 'relu', 1],
  59. [3, 64, 24, False, 'relu', 2],
  60. [3, 72, 24, False, 'relu', 1],
  61. [5, 72, 40, True, 'relu', 2],
  62. [5, 120, 40, True, 'relu', 1],
  63. [5, 120, 40, True, 'relu', 1],
  64. [3, 240, 80, False, 'hard_swish', 2],
  65. [3, 200, 80, False, 'hard_swish', 1],
  66. [3, 184, 80, False, 'hard_swish', 1],
  67. [3, 184, 80, False, 'hard_swish', 1],
  68. [3, 480, 112, True, 'hard_swish', 1],
  69. [3, 672, 112, True, 'hard_swish', 1],
  70. [5, 672, 160, True, 'hard_swish', 2],
  71. [5, 960, 160, True, 'hard_swish', 1],
  72. [5, 960, 160, True, 'hard_swish', 1],
  73. ]
  74. self.cls_ch_squeeze = 960
  75. self.cls_ch_expand = 1280
  76. self.lr_interval = 3
  77. elif model_name == "small":
  78. self.cfg = [
  79. # kernel_size, expand, channel, se_block, act_mode, stride
  80. [3, 16, 16, True, 'relu', 2],
  81. [3, 72, 24, False, 'relu', 2],
  82. [3, 88, 24, False, 'relu', 1],
  83. [5, 96, 40, True, 'hard_swish', 2],
  84. [5, 240, 40, True, 'hard_swish', 1],
  85. [5, 240, 40, True, 'hard_swish', 1],
  86. [5, 120, 48, True, 'hard_swish', 1],
  87. [5, 144, 48, True, 'hard_swish', 1],
  88. [5, 288, 96, True, 'hard_swish', 2],
  89. [5, 576, 96, True, 'hard_swish', 1],
  90. [5, 576, 96, True, 'hard_swish', 1],
  91. ]
  92. self.cls_ch_squeeze = 576
  93. self.cls_ch_expand = 1280
  94. self.lr_interval = 2
  95. else:
  96. raise NotImplementedError
  97. def _conv_bn_layer(self,
  98. input,
  99. filter_size,
  100. num_filters,
  101. stride,
  102. padding,
  103. num_groups=1,
  104. if_act=True,
  105. act=None,
  106. name=None,
  107. use_cudnn=True):
  108. lr_idx = self.curr_stage // self.lr_interval
  109. lr_idx = min(lr_idx, len(self.lr_mult_list) - 1)
  110. lr_mult = self.lr_mult_list[lr_idx]
  111. if self.num_classes:
  112. regularizer = None
  113. else:
  114. regularizer = L2Decay(self.conv_decay)
  115. conv_param_attr = ParamAttr(
  116. name=name + '_weights',
  117. learning_rate=lr_mult,
  118. regularizer=regularizer)
  119. conv = fluid.layers.conv2d(
  120. input=input,
  121. num_filters=num_filters,
  122. filter_size=filter_size,
  123. stride=stride,
  124. padding=padding,
  125. groups=num_groups,
  126. act=None,
  127. use_cudnn=use_cudnn,
  128. param_attr=conv_param_attr,
  129. bias_attr=False)
  130. bn_name = name + '_bn'
  131. bn_param_attr = ParamAttr(
  132. name=bn_name + "_scale", regularizer=L2Decay(self.norm_decay))
  133. bn_bias_attr = ParamAttr(
  134. name=bn_name + "_offset", regularizer=L2Decay(self.norm_decay))
  135. bn = fluid.layers.batch_norm(
  136. input=conv,
  137. param_attr=bn_param_attr,
  138. bias_attr=bn_bias_attr,
  139. moving_mean_name=bn_name + '_mean',
  140. moving_variance_name=bn_name + '_variance')
  141. if if_act:
  142. if act == 'relu':
  143. bn = fluid.layers.relu(bn)
  144. elif act == 'hard_swish':
  145. bn = self._hard_swish(bn)
  146. elif act == 'relu6':
  147. bn = fluid.layers.relu6(bn)
  148. return bn
  149. def _hard_swish(self, x):
  150. return x * fluid.layers.relu6(x + 3) / 6.
  151. def _se_block(self, input, num_out_filter, ratio=4, name=None):
  152. lr_idx = self.curr_stage // self.lr_interval
  153. lr_idx = min(lr_idx, len(self.lr_mult_list) - 1)
  154. lr_mult = self.lr_mult_list[lr_idx]
  155. num_mid_filter = int(num_out_filter // ratio)
  156. pool = fluid.layers.pool2d(
  157. input=input, pool_type='avg', global_pooling=True, use_cudnn=False)
  158. conv1 = fluid.layers.conv2d(
  159. input=pool,
  160. filter_size=1,
  161. num_filters=num_mid_filter,
  162. act='relu',
  163. param_attr=ParamAttr(
  164. name=name + '_1_weights', learning_rate=lr_mult),
  165. bias_attr=ParamAttr(
  166. name=name + '_1_offset', learning_rate=lr_mult))
  167. conv2 = fluid.layers.conv2d(
  168. input=conv1,
  169. filter_size=1,
  170. num_filters=num_out_filter,
  171. act='hard_sigmoid',
  172. param_attr=ParamAttr(
  173. name=name + '_2_weights', learning_rate=lr_mult),
  174. bias_attr=ParamAttr(
  175. name=name + '_2_offset', learning_rate=lr_mult))
  176. scale = fluid.layers.elementwise_mul(x=input, y=conv2, axis=0)
  177. return scale
  178. def _residual_unit(self,
  179. input,
  180. num_in_filter,
  181. num_mid_filter,
  182. num_out_filter,
  183. stride,
  184. filter_size,
  185. act=None,
  186. use_se=False,
  187. name=None):
  188. input_data = input
  189. conv0 = self._conv_bn_layer(
  190. input=input,
  191. filter_size=1,
  192. num_filters=num_mid_filter,
  193. stride=1,
  194. padding=0,
  195. if_act=True,
  196. act=act,
  197. name=name + '_expand')
  198. if self.block_stride == 16 and stride == 2:
  199. self.end_points.append(conv0)
  200. conv1 = self._conv_bn_layer(
  201. input=conv0,
  202. filter_size=filter_size,
  203. num_filters=num_mid_filter,
  204. stride=stride,
  205. padding=int((filter_size - 1) // 2),
  206. if_act=True,
  207. act=act,
  208. num_groups=num_mid_filter,
  209. use_cudnn=False,
  210. name=name + '_depthwise')
  211. if use_se:
  212. conv1 = self._se_block(
  213. input=conv1, num_out_filter=num_mid_filter, name=name + '_se')
  214. conv2 = self._conv_bn_layer(
  215. input=conv1,
  216. filter_size=1,
  217. num_filters=num_out_filter,
  218. stride=1,
  219. padding=0,
  220. if_act=False,
  221. name=name + '_linear')
  222. if num_in_filter != num_out_filter or stride != 1:
  223. return conv2
  224. else:
  225. return fluid.layers.elementwise_add(
  226. x=input_data, y=conv2, act=None)
  227. def _extra_block_dw(self,
  228. input,
  229. num_filters1,
  230. num_filters2,
  231. stride,
  232. name=None):
  233. pointwise_conv = self._conv_bn_layer(
  234. input=input,
  235. filter_size=1,
  236. num_filters=int(num_filters1),
  237. stride=1,
  238. padding="SAME",
  239. act='relu6',
  240. name=name + "_extra1")
  241. depthwise_conv = self._conv_bn_layer(
  242. input=pointwise_conv,
  243. filter_size=3,
  244. num_filters=int(num_filters2),
  245. stride=stride,
  246. padding="SAME",
  247. num_groups=int(num_filters1),
  248. act='relu6',
  249. use_cudnn=False,
  250. name=name + "_extra2_dw")
  251. normal_conv = self._conv_bn_layer(
  252. input=depthwise_conv,
  253. filter_size=1,
  254. num_filters=int(num_filters2),
  255. stride=1,
  256. padding="SAME",
  257. act='relu6',
  258. name=name + "_extra2_sep")
  259. return normal_conv
  260. def __call__(self, input):
  261. scale = self.scale
  262. inplanes = self.inplanes
  263. cfg = self.cfg
  264. blocks = []
  265. #conv1
  266. conv = self._conv_bn_layer(
  267. input,
  268. filter_size=3,
  269. num_filters=inplanes if scale <= 1.0 else int(inplanes * scale),
  270. stride=2,
  271. padding=1,
  272. num_groups=1,
  273. if_act=True,
  274. act='hard_swish',
  275. name='conv1')
  276. i = 0
  277. for layer_cfg in cfg:
  278. self.block_stride *= layer_cfg[5]
  279. if layer_cfg[5] == 2:
  280. blocks.append(conv)
  281. conv = self._residual_unit(
  282. input=conv,
  283. num_in_filter=inplanes,
  284. num_mid_filter=int(scale * layer_cfg[1]),
  285. num_out_filter=int(scale * layer_cfg[2]),
  286. act=layer_cfg[4],
  287. stride=layer_cfg[5],
  288. filter_size=layer_cfg[0],
  289. use_se=layer_cfg[3],
  290. name='conv' + str(i + 2))
  291. inplanes = int(scale * layer_cfg[2])
  292. i += 1
  293. self.curr_stage = i
  294. blocks.append(conv)
  295. if self.num_classes:
  296. conv = self._conv_bn_layer(
  297. input=conv,
  298. filter_size=1,
  299. num_filters=int(scale * self.cls_ch_squeeze),
  300. stride=1,
  301. padding=0,
  302. num_groups=1,
  303. if_act=True,
  304. act='hard_swish',
  305. name='conv_last')
  306. conv = fluid.layers.pool2d(
  307. input=conv,
  308. pool_type='avg',
  309. global_pooling=True,
  310. use_cudnn=False)
  311. conv = fluid.layers.conv2d(
  312. input=conv,
  313. num_filters=self.cls_ch_expand,
  314. filter_size=1,
  315. stride=1,
  316. padding=0,
  317. act=None,
  318. param_attr=ParamAttr(name='last_1x1_conv_weights'),
  319. bias_attr=False)
  320. conv = self._hard_swish(conv)
  321. drop = fluid.layers.dropout(x=conv, dropout_prob=0.2)
  322. out = fluid.layers.fc(input=drop,
  323. size=self.num_classes,
  324. param_attr=ParamAttr(name='fc_weights'),
  325. bias_attr=ParamAttr(name='fc_offset'))
  326. return out
  327. if not self.with_extra_blocks:
  328. return blocks
  329. # extra block
  330. conv_extra = self._conv_bn_layer(
  331. conv,
  332. filter_size=1,
  333. num_filters=int(scale * cfg[-1][1]),
  334. stride=1,
  335. padding="SAME",
  336. num_groups=1,
  337. if_act=True,
  338. act='hard_swish',
  339. name='conv' + str(i + 2))
  340. self.end_points.append(conv_extra)
  341. i += 1
  342. for block_filter in self.extra_block_filters:
  343. conv_extra = self._extra_block_dw(conv_extra, block_filter[0],
  344. block_filter[1], 2,
  345. 'conv' + str(i + 2))
  346. self.end_points.append(conv_extra)
  347. i += 1
  348. return self.end_points