mobilenet_v1.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # copyright (c) 2021 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, division, print_function
  15. from paddle import ParamAttr
  16. import paddle.nn as nn
  17. from paddle.nn import Conv2D, BatchNorm, Linear, ReLU, Flatten
  18. from paddle.nn import AdaptiveAvgPool2D
  19. from paddle.nn.initializer import KaimingNormal
  20. from paddlex.ppcls.arch.backbone.base.theseus_layer import TheseusLayer
  21. from paddlex.ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url
  22. MODEL_URLS = {
  23. "MobileNetV1_x0_25":
  24. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/MobileNetV1_x0_25_pretrained.pdparams",
  25. "MobileNetV1_x0_5":
  26. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/MobileNetV1_x0_5_pretrained.pdparams",
  27. "MobileNetV1_x0_75":
  28. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/MobileNetV1_x0_75_pretrained.pdparams",
  29. "MobileNetV1":
  30. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/MobileNetV1_pretrained.pdparams"
  31. }
  32. __all__ = MODEL_URLS.keys()
  33. class ConvBNLayer(TheseusLayer):
  34. def __init__(self,
  35. num_channels,
  36. filter_size,
  37. num_filters,
  38. stride,
  39. padding,
  40. num_groups=1):
  41. super().__init__()
  42. self.conv = Conv2D(
  43. in_channels=num_channels,
  44. out_channels=num_filters,
  45. kernel_size=filter_size,
  46. stride=stride,
  47. padding=padding,
  48. groups=num_groups,
  49. weight_attr=ParamAttr(initializer=KaimingNormal()),
  50. bias_attr=False)
  51. self.bn = BatchNorm(num_filters)
  52. self.relu = ReLU()
  53. def forward(self, x):
  54. x = self.conv(x)
  55. x = self.bn(x)
  56. x = self.relu(x)
  57. return x
  58. class DepthwiseSeparable(TheseusLayer):
  59. def __init__(self, num_channels, num_filters1, num_filters2, num_groups,
  60. stride, scale):
  61. super().__init__()
  62. self.depthwise_conv = ConvBNLayer(
  63. num_channels=num_channels,
  64. num_filters=int(num_filters1 * scale),
  65. filter_size=3,
  66. stride=stride,
  67. padding=1,
  68. num_groups=int(num_groups * scale))
  69. self.pointwise_conv = ConvBNLayer(
  70. num_channels=int(num_filters1 * scale),
  71. filter_size=1,
  72. num_filters=int(num_filters2 * scale),
  73. stride=1,
  74. padding=0)
  75. def forward(self, x):
  76. x = self.depthwise_conv(x)
  77. x = self.pointwise_conv(x)
  78. return x
  79. class MobileNet(TheseusLayer):
  80. """
  81. MobileNet
  82. Args:
  83. scale: float=1.0. The coefficient that controls the size of network parameters.
  84. class_num: int=1000. The number of classes.
  85. Returns:
  86. model: nn.Layer. Specific MobileNet model depends on args.
  87. """
  88. def __init__(self, scale=1.0, class_num=1000, return_patterns=None):
  89. super().__init__()
  90. self.scale = scale
  91. self.conv = ConvBNLayer(
  92. num_channels=3,
  93. filter_size=3,
  94. num_filters=int(32 * scale),
  95. stride=2,
  96. padding=1)
  97. #num_channels, num_filters1, num_filters2, num_groups, stride
  98. self.cfg = [[int(32 * scale), 32, 64, 32, 1],
  99. [int(64 * scale), 64, 128, 64, 2],
  100. [int(128 * scale), 128, 128, 128, 1],
  101. [int(128 * scale), 128, 256, 128, 2],
  102. [int(256 * scale), 256, 256, 256, 1],
  103. [int(256 * scale), 256, 512, 256, 2],
  104. [int(512 * scale), 512, 512, 512, 1],
  105. [int(512 * scale), 512, 512, 512, 1],
  106. [int(512 * scale), 512, 512, 512, 1],
  107. [int(512 * scale), 512, 512, 512, 1],
  108. [int(512 * scale), 512, 512, 512, 1],
  109. [int(512 * scale), 512, 1024, 512, 2],
  110. [int(1024 * scale), 1024, 1024, 1024, 1]]
  111. self.blocks = nn.Sequential(*[
  112. DepthwiseSeparable(
  113. num_channels=params[0],
  114. num_filters1=params[1],
  115. num_filters2=params[2],
  116. num_groups=params[3],
  117. stride=params[4],
  118. scale=scale) for params in self.cfg
  119. ])
  120. self.avg_pool = AdaptiveAvgPool2D(1)
  121. self.flatten = Flatten(start_axis=1, stop_axis=-1)
  122. self.fc = Linear(
  123. int(1024 * scale),
  124. class_num,
  125. weight_attr=ParamAttr(initializer=KaimingNormal()))
  126. if return_patterns is not None:
  127. self.update_res(return_patterns)
  128. self.register_forward_post_hook(self._return_dict_hook)
  129. def forward(self, x):
  130. x = self.conv(x)
  131. x = self.blocks(x)
  132. x = self.avg_pool(x)
  133. x = self.flatten(x)
  134. x = self.fc(x)
  135. return x
  136. def _load_pretrained(pretrained, model, model_url, use_ssld):
  137. if pretrained is False:
  138. pass
  139. elif pretrained is True:
  140. load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld)
  141. elif isinstance(pretrained, str):
  142. load_dygraph_pretrain(model, pretrained)
  143. else:
  144. raise RuntimeError(
  145. "pretrained type is not available. Please use `string` or `boolean` type."
  146. )
  147. def MobileNetV1_x0_25(pretrained=False, use_ssld=False, **kwargs):
  148. """
  149. MobileNetV1_x0_25
  150. Args:
  151. pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
  152. If str, means the path of the pretrained model.
  153. use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
  154. Returns:
  155. model: nn.Layer. Specific `MobileNetV1_x0_25` model depends on args.
  156. """
  157. model = MobileNet(scale=0.25, **kwargs)
  158. _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV1_x0_25"],
  159. use_ssld)
  160. return model
  161. def MobileNetV1_x0_5(pretrained=False, use_ssld=False, **kwargs):
  162. """
  163. MobileNetV1_x0_5
  164. Args:
  165. pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
  166. If str, means the path of the pretrained model.
  167. use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
  168. Returns:
  169. model: nn.Layer. Specific `MobileNetV1_x0_5` model depends on args.
  170. """
  171. model = MobileNet(scale=0.5, **kwargs)
  172. _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV1_x0_5"],
  173. use_ssld)
  174. return model
  175. def MobileNetV1_x0_75(pretrained=False, use_ssld=False, **kwargs):
  176. """
  177. MobileNetV1_x0_75
  178. Args:
  179. pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
  180. If str, means the path of the pretrained model.
  181. use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
  182. Returns:
  183. model: nn.Layer. Specific `MobileNetV1_x0_75` model depends on args.
  184. """
  185. model = MobileNet(scale=0.75, **kwargs)
  186. _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV1_x0_75"],
  187. use_ssld)
  188. return model
  189. def MobileNetV1(pretrained=False, use_ssld=False, **kwargs):
  190. """
  191. MobileNetV1
  192. Args:
  193. pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
  194. If str, means the path of the pretrained model.
  195. use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
  196. Returns:
  197. model: nn.Layer. Specific `MobileNetV1` model depends on args.
  198. """
  199. model = MobileNet(scale=1.0, **kwargs)
  200. _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV1"], use_ssld)
  201. return model