mobilenet_v2.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. from paddlex.ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url
  26. MODEL_URLS = {
  27. "MobileNetV2_x0_25":
  28. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_x0_25_pretrained.pdparams",
  29. "MobileNetV2_x0_5":
  30. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_x0_5_pretrained.pdparams",
  31. "MobileNetV2_x0_75":
  32. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_x0_75_pretrained.pdparams",
  33. "MobileNetV2":
  34. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_pretrained.pdparams",
  35. "MobileNetV2_x1_5":
  36. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_x1_5_pretrained.pdparams",
  37. "MobileNetV2_x2_0":
  38. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_x2_0_pretrained.pdparams"
  39. }
  40. __all__ = list(MODEL_URLS.keys())
  41. class ConvBNLayer(nn.Layer):
  42. def __init__(self,
  43. num_channels,
  44. filter_size,
  45. num_filters,
  46. stride,
  47. padding,
  48. channels=None,
  49. num_groups=1,
  50. name=None,
  51. use_cudnn=True):
  52. super(ConvBNLayer, self).__init__()
  53. self._conv = Conv2D(
  54. in_channels=num_channels,
  55. out_channels=num_filters,
  56. kernel_size=filter_size,
  57. stride=stride,
  58. padding=padding,
  59. groups=num_groups,
  60. weight_attr=ParamAttr(name=name + "_weights"),
  61. bias_attr=False)
  62. self._batch_norm = BatchNorm(
  63. num_filters,
  64. param_attr=ParamAttr(name=name + "_bn_scale"),
  65. bias_attr=ParamAttr(name=name + "_bn_offset"),
  66. moving_mean_name=name + "_bn_mean",
  67. moving_variance_name=name + "_bn_variance")
  68. def forward(self, inputs, if_act=True):
  69. y = self._conv(inputs)
  70. y = self._batch_norm(y)
  71. if if_act:
  72. y = F.relu6(y)
  73. return y
  74. class InvertedResidualUnit(nn.Layer):
  75. def __init__(self, num_channels, num_in_filter, num_filters, stride,
  76. filter_size, padding, expansion_factor, name):
  77. super(InvertedResidualUnit, self).__init__()
  78. num_expfilter = int(round(num_in_filter * expansion_factor))
  79. self._expand_conv = ConvBNLayer(
  80. num_channels=num_channels,
  81. num_filters=num_expfilter,
  82. filter_size=1,
  83. stride=1,
  84. padding=0,
  85. num_groups=1,
  86. name=name + "_expand")
  87. self._bottleneck_conv = ConvBNLayer(
  88. num_channels=num_expfilter,
  89. num_filters=num_expfilter,
  90. filter_size=filter_size,
  91. stride=stride,
  92. padding=padding,
  93. num_groups=num_expfilter,
  94. use_cudnn=False,
  95. name=name + "_dwise")
  96. self._linear_conv = ConvBNLayer(
  97. num_channels=num_expfilter,
  98. num_filters=num_filters,
  99. filter_size=1,
  100. stride=1,
  101. padding=0,
  102. num_groups=1,
  103. name=name + "_linear")
  104. def forward(self, inputs, ifshortcut):
  105. y = self._expand_conv(inputs, if_act=True)
  106. y = self._bottleneck_conv(y, if_act=True)
  107. y = self._linear_conv(y, if_act=False)
  108. if ifshortcut:
  109. y = paddle.add(inputs, y)
  110. return y
  111. class InvresiBlocks(nn.Layer):
  112. def __init__(self, in_c, t, c, n, s, name):
  113. super(InvresiBlocks, self).__init__()
  114. self._first_block = InvertedResidualUnit(
  115. num_channels=in_c,
  116. num_in_filter=in_c,
  117. num_filters=c,
  118. stride=s,
  119. filter_size=3,
  120. padding=1,
  121. expansion_factor=t,
  122. name=name + "_1")
  123. self._block_list = []
  124. for i in range(1, n):
  125. block = self.add_sublayer(
  126. name + "_" + str(i + 1),
  127. sublayer=InvertedResidualUnit(
  128. num_channels=c,
  129. num_in_filter=c,
  130. num_filters=c,
  131. stride=1,
  132. filter_size=3,
  133. padding=1,
  134. expansion_factor=t,
  135. name=name + "_" + str(i + 1)))
  136. self._block_list.append(block)
  137. def forward(self, inputs):
  138. y = self._first_block(inputs, ifshortcut=False)
  139. for block in self._block_list:
  140. y = block(y, ifshortcut=True)
  141. return y
  142. class MobileNet(nn.Layer):
  143. def __init__(self, class_num=1000, scale=1.0, prefix_name=""):
  144. super(MobileNet, self).__init__()
  145. self.scale = scale
  146. self.class_num = class_num
  147. bottleneck_params_list = [
  148. (1, 16, 1, 1),
  149. (6, 24, 2, 2),
  150. (6, 32, 3, 2),
  151. (6, 64, 4, 2),
  152. (6, 96, 3, 1),
  153. (6, 160, 3, 2),
  154. (6, 320, 1, 1),
  155. ]
  156. self.conv1 = ConvBNLayer(
  157. num_channels=3,
  158. num_filters=int(32 * scale),
  159. filter_size=3,
  160. stride=2,
  161. padding=1,
  162. name=prefix_name + "conv1_1")
  163. self.block_list = []
  164. i = 1
  165. in_c = int(32 * scale)
  166. for layer_setting in bottleneck_params_list:
  167. t, c, n, s = layer_setting
  168. i += 1
  169. block = self.add_sublayer(
  170. prefix_name + "conv" + str(i),
  171. sublayer=InvresiBlocks(
  172. in_c=in_c,
  173. t=t,
  174. c=int(c * scale),
  175. n=n,
  176. s=s,
  177. name=prefix_name + "conv" + str(i)))
  178. self.block_list.append(block)
  179. in_c = int(c * scale)
  180. self.out_c = int(1280 * scale) if scale > 1.0 else 1280
  181. self.conv9 = ConvBNLayer(
  182. num_channels=in_c,
  183. num_filters=self.out_c,
  184. filter_size=1,
  185. stride=1,
  186. padding=0,
  187. name=prefix_name + "conv9")
  188. self.pool2d_avg = AdaptiveAvgPool2D(1)
  189. self.out = Linear(
  190. self.out_c,
  191. class_num,
  192. weight_attr=ParamAttr(name=prefix_name + "fc10_weights"),
  193. bias_attr=ParamAttr(name=prefix_name + "fc10_offset"))
  194. def forward(self, inputs):
  195. y = self.conv1(inputs, if_act=True)
  196. for block in self.block_list:
  197. y = block(y)
  198. y = self.conv9(y, if_act=True)
  199. y = self.pool2d_avg(y)
  200. y = paddle.flatten(y, start_axis=1, stop_axis=-1)
  201. y = self.out(y)
  202. return y
  203. def _load_pretrained(pretrained, model, model_url, use_ssld=False):
  204. if pretrained is False:
  205. pass
  206. elif pretrained is True:
  207. load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld)
  208. elif isinstance(pretrained, str):
  209. load_dygraph_pretrain(model, pretrained)
  210. else:
  211. raise RuntimeError(
  212. "pretrained type is not available. Please use `string` or `boolean` type."
  213. )
  214. def MobileNetV2_x0_25(pretrained=False, use_ssld=False, **kwargs):
  215. model = MobileNet(scale=0.25, **kwargs)
  216. _load_pretrained(
  217. pretrained, model, MODEL_URLS["MobileNetV2_x0_25"], use_ssld=use_ssld)
  218. return model
  219. def MobileNetV2_x0_5(pretrained=False, use_ssld=False, **kwargs):
  220. model = MobileNet(scale=0.5, **kwargs)
  221. _load_pretrained(
  222. pretrained, model, MODEL_URLS["MobileNetV2_x0_5"], use_ssld=use_ssld)
  223. return model
  224. def MobileNetV2_x0_75(pretrained=False, use_ssld=False, **kwargs):
  225. model = MobileNet(scale=0.75, **kwargs)
  226. _load_pretrained(
  227. pretrained, model, MODEL_URLS["MobileNetV2_x0_75"], use_ssld=use_ssld)
  228. return model
  229. def MobileNetV2(pretrained=False, use_ssld=False, **kwargs):
  230. model = MobileNet(scale=1.0, **kwargs)
  231. _load_pretrained(
  232. pretrained, model, MODEL_URLS["MobileNetV2"], use_ssld=use_ssld)
  233. return model
  234. def MobileNetV2_x1_5(pretrained=False, use_ssld=False, **kwargs):
  235. model = MobileNet(scale=1.5, **kwargs)
  236. _load_pretrained(
  237. pretrained, model, MODEL_URLS["MobileNetV2_x1_5"], use_ssld=use_ssld)
  238. return model
  239. def MobileNetV2_x2_0(pretrained=False, use_ssld=False, **kwargs):
  240. model = MobileNet(scale=2.0, **kwargs)
  241. _load_pretrained(
  242. pretrained, model, MODEL_URLS["MobileNetV2_x2_0"], use_ssld=use_ssld)
  243. return model