mobilenet_v3.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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.functional import hardswish, hardsigmoid
  23. from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
  24. from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
  25. from paddle.regularizer import L2Decay
  26. import math
  27. __all__ = [
  28. "MobileNetV3_small_x0_35", "MobileNetV3_small_x0_5",
  29. "MobileNetV3_small_x0_75", "MobileNetV3_small_x1_0",
  30. "MobileNetV3_small_x1_25", "MobileNetV3_large_x0_35",
  31. "MobileNetV3_large_x0_5", "MobileNetV3_large_x0_75",
  32. "MobileNetV3_large_x1_0", "MobileNetV3_large_x1_25"
  33. ]
  34. def make_divisible(v, divisor=8, min_value=None):
  35. if min_value is None:
  36. min_value = divisor
  37. new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
  38. if new_v < 0.9 * v:
  39. new_v += divisor
  40. return new_v
  41. class MobileNetV3(nn.Layer):
  42. def __init__(self,
  43. scale=1.0,
  44. model_name="small",
  45. dropout_prob=0.2,
  46. class_dim=1000):
  47. super(MobileNetV3, self).__init__()
  48. inplanes = 16
  49. if model_name == "large":
  50. self.cfg = [
  51. # k, exp, c, se, nl, s,
  52. [3, 16, 16, False, "relu", 1],
  53. [3, 64, 24, False, "relu", 2],
  54. [3, 72, 24, False, "relu", 1],
  55. [5, 72, 40, True, "relu", 2],
  56. [5, 120, 40, True, "relu", 1],
  57. [5, 120, 40, True, "relu", 1],
  58. [3, 240, 80, False, "hardswish", 2],
  59. [3, 200, 80, False, "hardswish", 1],
  60. [3, 184, 80, False, "hardswish", 1],
  61. [3, 184, 80, False, "hardswish", 1],
  62. [3, 480, 112, True, "hardswish", 1],
  63. [3, 672, 112, True, "hardswish", 1],
  64. [5, 672, 160, True, "hardswish", 2],
  65. [5, 960, 160, True, "hardswish", 1],
  66. [5, 960, 160, True, "hardswish", 1],
  67. ]
  68. self.cls_ch_squeeze = 960
  69. self.cls_ch_expand = 1280
  70. elif model_name == "small":
  71. self.cfg = [
  72. # k, exp, c, se, nl, s,
  73. [3, 16, 16, True, "relu", 2],
  74. [3, 72, 24, False, "relu", 2],
  75. [3, 88, 24, False, "relu", 1],
  76. [5, 96, 40, True, "hardswish", 2],
  77. [5, 240, 40, True, "hardswish", 1],
  78. [5, 240, 40, True, "hardswish", 1],
  79. [5, 120, 48, True, "hardswish", 1],
  80. [5, 144, 48, True, "hardswish", 1],
  81. [5, 288, 96, True, "hardswish", 2],
  82. [5, 576, 96, True, "hardswish", 1],
  83. [5, 576, 96, True, "hardswish", 1],
  84. ]
  85. self.cls_ch_squeeze = 576
  86. self.cls_ch_expand = 1280
  87. else:
  88. raise NotImplementedError(
  89. "mode[{}_model] is not implemented!".format(model_name))
  90. self.conv1 = ConvBNLayer(
  91. in_c=3,
  92. out_c=make_divisible(inplanes * scale),
  93. filter_size=3,
  94. stride=2,
  95. padding=1,
  96. num_groups=1,
  97. if_act=True,
  98. act="hardswish",
  99. name="conv1")
  100. self.block_list = []
  101. i = 0
  102. inplanes = make_divisible(inplanes * scale)
  103. for (k, exp, c, se, nl, s) in self.cfg:
  104. block = self.add_sublayer(
  105. "conv" + str(i + 2),
  106. ResidualUnit(
  107. in_c=inplanes,
  108. mid_c=make_divisible(scale * exp),
  109. out_c=make_divisible(scale * c),
  110. filter_size=k,
  111. stride=s,
  112. use_se=se,
  113. act=nl,
  114. name="conv" + str(i + 2)))
  115. self.block_list.append(block)
  116. inplanes = make_divisible(scale * c)
  117. i += 1
  118. self.last_second_conv = ConvBNLayer(
  119. in_c=inplanes,
  120. out_c=make_divisible(scale * self.cls_ch_squeeze),
  121. filter_size=1,
  122. stride=1,
  123. padding=0,
  124. num_groups=1,
  125. if_act=True,
  126. act="hardswish",
  127. name="conv_last")
  128. self.pool = AdaptiveAvgPool2D(1)
  129. self.last_conv = Conv2D(
  130. in_channels=make_divisible(scale * self.cls_ch_squeeze),
  131. out_channels=self.cls_ch_expand,
  132. kernel_size=1,
  133. stride=1,
  134. padding=0,
  135. weight_attr=ParamAttr(name="last_1x1_conv_weights"),
  136. bias_attr=False)
  137. self.dropout = Dropout(p=dropout_prob, mode="downscale_in_infer")
  138. self.out = Linear(
  139. self.cls_ch_expand,
  140. class_dim,
  141. weight_attr=ParamAttr("fc_weights"),
  142. bias_attr=ParamAttr(name="fc_offset"))
  143. def forward(self, inputs, label=None):
  144. x = self.conv1(inputs)
  145. for block in self.block_list:
  146. x = block(x)
  147. x = self.last_second_conv(x)
  148. x = self.pool(x)
  149. x = self.last_conv(x)
  150. x = hardswish(x)
  151. x = self.dropout(x)
  152. x = paddle.flatten(x, start_axis=1, stop_axis=-1)
  153. x = self.out(x)
  154. return x
  155. class ConvBNLayer(nn.Layer):
  156. def __init__(self,
  157. in_c,
  158. out_c,
  159. filter_size,
  160. stride,
  161. padding,
  162. num_groups=1,
  163. if_act=True,
  164. act=None,
  165. use_cudnn=True,
  166. name=""):
  167. super(ConvBNLayer, self).__init__()
  168. self.if_act = if_act
  169. self.act = act
  170. self.conv = Conv2D(
  171. in_channels=in_c,
  172. out_channels=out_c,
  173. kernel_size=filter_size,
  174. stride=stride,
  175. padding=padding,
  176. groups=num_groups,
  177. weight_attr=ParamAttr(name=name + "_weights"),
  178. bias_attr=False)
  179. self.bn = BatchNorm(
  180. num_channels=out_c,
  181. act=None,
  182. param_attr=ParamAttr(
  183. name=name + "_bn_scale", regularizer=L2Decay(0.0)),
  184. bias_attr=ParamAttr(
  185. name=name + "_bn_offset", regularizer=L2Decay(0.0)),
  186. moving_mean_name=name + "_bn_mean",
  187. moving_variance_name=name + "_bn_variance")
  188. def forward(self, x):
  189. x = self.conv(x)
  190. x = self.bn(x)
  191. if self.if_act:
  192. if self.act == "relu":
  193. x = F.relu(x)
  194. elif self.act == "hardswish":
  195. x = hardswish(x)
  196. else:
  197. print("The activation function is selected incorrectly.")
  198. exit()
  199. return x
  200. class ResidualUnit(nn.Layer):
  201. def __init__(self,
  202. in_c,
  203. mid_c,
  204. out_c,
  205. filter_size,
  206. stride,
  207. use_se,
  208. act=None,
  209. name=''):
  210. super(ResidualUnit, self).__init__()
  211. self.if_shortcut = stride == 1 and in_c == out_c
  212. self.if_se = use_se
  213. self.expand_conv = ConvBNLayer(
  214. in_c=in_c,
  215. out_c=mid_c,
  216. filter_size=1,
  217. stride=1,
  218. padding=0,
  219. if_act=True,
  220. act=act,
  221. name=name + "_expand")
  222. self.bottleneck_conv = ConvBNLayer(
  223. in_c=mid_c,
  224. out_c=mid_c,
  225. filter_size=filter_size,
  226. stride=stride,
  227. padding=int((filter_size - 1) // 2),
  228. num_groups=mid_c,
  229. if_act=True,
  230. act=act,
  231. name=name + "_depthwise")
  232. if self.if_se:
  233. self.mid_se = SEModule(mid_c, name=name + "_se")
  234. self.linear_conv = ConvBNLayer(
  235. in_c=mid_c,
  236. out_c=out_c,
  237. filter_size=1,
  238. stride=1,
  239. padding=0,
  240. if_act=False,
  241. act=None,
  242. name=name + "_linear")
  243. def forward(self, inputs):
  244. x = self.expand_conv(inputs)
  245. x = self.bottleneck_conv(x)
  246. if self.if_se:
  247. x = self.mid_se(x)
  248. x = self.linear_conv(x)
  249. if self.if_shortcut:
  250. x = paddle.add(inputs, x)
  251. return x
  252. class SEModule(nn.Layer):
  253. def __init__(self, channel, reduction=4, name=""):
  254. super(SEModule, self).__init__()
  255. self.avg_pool = AdaptiveAvgPool2D(1)
  256. self.conv1 = Conv2D(
  257. in_channels=channel,
  258. out_channels=channel // reduction,
  259. kernel_size=1,
  260. stride=1,
  261. padding=0,
  262. weight_attr=ParamAttr(name=name + "_1_weights"),
  263. bias_attr=ParamAttr(name=name + "_1_offset"))
  264. self.conv2 = Conv2D(
  265. in_channels=channel // reduction,
  266. out_channels=channel,
  267. kernel_size=1,
  268. stride=1,
  269. padding=0,
  270. weight_attr=ParamAttr(name + "_2_weights"),
  271. bias_attr=ParamAttr(name=name + "_2_offset"))
  272. def forward(self, inputs):
  273. outputs = self.avg_pool(inputs)
  274. outputs = self.conv1(outputs)
  275. outputs = F.relu(outputs)
  276. outputs = self.conv2(outputs)
  277. outputs = hardsigmoid(outputs, slope=0.2, offset=0.5)
  278. return paddle.multiply(x=inputs, y=outputs)
  279. def MobileNetV3_small_x0_35(**args):
  280. model = MobileNetV3(model_name="small", scale=0.35, **args)
  281. return model
  282. def MobileNetV3_small_x0_5(**args):
  283. model = MobileNetV3(model_name="small", scale=0.5, **args)
  284. return model
  285. def MobileNetV3_small_x0_75(**args):
  286. model = MobileNetV3(model_name="small", scale=0.75, **args)
  287. return model
  288. def MobileNetV3_small_x1_0(**args):
  289. model = MobileNetV3(model_name="small", scale=1.0, **args)
  290. return model
  291. def MobileNetV3_small_x1_25(**args):
  292. model = MobileNetV3(model_name="small", scale=1.25, **args)
  293. return model
  294. def MobileNetV3_large_x0_35(**args):
  295. model = MobileNetV3(model_name="large", scale=0.35, **args)
  296. return model
  297. def MobileNetV3_large_x0_5(**args):
  298. model = MobileNetV3(model_name="large", scale=0.5, **args)
  299. return model
  300. def MobileNetV3_large_x0_75(**args):
  301. model = MobileNetV3(model_name="large", scale=0.75, **args)
  302. return model
  303. def MobileNetV3_large_x1_0(**args):
  304. model = MobileNetV3(model_name="large", scale=1.0, **args)
  305. return model
  306. def MobileNetV3_large_x1_25(**args):
  307. model = MobileNetV3(model_name="large", scale=1.25, **args)
  308. return model