mobilenet_v1.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. from paddle import fluid
  18. from paddle.fluid.param_attr import ParamAttr
  19. from paddle.fluid.regularizer import L2Decay
  20. class MobileNetV1(object):
  21. """
  22. MobileNet v1, see https://arxiv.org/abs/1704.04861
  23. Args:
  24. norm_type (str): normalization type, 'bn' and 'sync_bn' are supported
  25. norm_decay (float): weight decay for normalization layer weights
  26. conv_group_scale (int): scaling factor for convolution groups
  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. norm_type='bn',
  32. norm_decay=0.,
  33. conv_group_scale=1,
  34. conv_learning_rate=1.0,
  35. with_extra_blocks=False,
  36. extra_block_filters=[[256, 512], [128, 256], [128, 256],
  37. [64, 128]],
  38. weight_prefix_name='',
  39. num_classes=None):
  40. self.norm_type = norm_type
  41. self.norm_decay = norm_decay
  42. self.conv_group_scale = conv_group_scale
  43. self.conv_learning_rate = conv_learning_rate
  44. self.with_extra_blocks = with_extra_blocks
  45. self.extra_block_filters = extra_block_filters
  46. self.prefix_name = weight_prefix_name
  47. self.num_classes = num_classes
  48. def _conv_norm(self,
  49. input,
  50. filter_size,
  51. num_filters,
  52. stride,
  53. padding,
  54. num_groups=1,
  55. act='relu',
  56. use_cudnn=True,
  57. name=None):
  58. parameter_attr = ParamAttr(
  59. learning_rate=self.conv_learning_rate,
  60. initializer=fluid.initializer.MSRA(),
  61. name=name + "_weights")
  62. conv = fluid.layers.conv2d(
  63. input=input,
  64. num_filters=num_filters,
  65. filter_size=filter_size,
  66. stride=stride,
  67. padding=padding,
  68. groups=num_groups,
  69. act=None,
  70. use_cudnn=use_cudnn,
  71. param_attr=parameter_attr,
  72. bias_attr=False)
  73. bn_name = name + "_bn"
  74. norm_decay = self.norm_decay
  75. bn_param_attr = ParamAttr(
  76. regularizer=L2Decay(norm_decay), name=bn_name + '_scale')
  77. bn_bias_attr = ParamAttr(
  78. regularizer=L2Decay(norm_decay), name=bn_name + '_offset')
  79. return fluid.layers.batch_norm(
  80. input=conv,
  81. act=act,
  82. param_attr=bn_param_attr,
  83. bias_attr=bn_bias_attr,
  84. moving_mean_name=bn_name + '_mean',
  85. moving_variance_name=bn_name + '_variance')
  86. def depthwise_separable(self,
  87. input,
  88. num_filters1,
  89. num_filters2,
  90. num_groups,
  91. stride,
  92. scale,
  93. name=None):
  94. depthwise_conv = self._conv_norm(
  95. input=input,
  96. filter_size=3,
  97. num_filters=int(num_filters1 * scale),
  98. stride=stride,
  99. padding=1,
  100. num_groups=int(num_groups * scale),
  101. use_cudnn=False,
  102. name=name + "_dw")
  103. pointwise_conv = self._conv_norm(
  104. input=depthwise_conv,
  105. filter_size=1,
  106. num_filters=int(num_filters2 * scale),
  107. stride=1,
  108. padding=0,
  109. name=name + "_sep")
  110. return pointwise_conv
  111. def _extra_block(self,
  112. input,
  113. num_filters1,
  114. num_filters2,
  115. num_groups,
  116. stride,
  117. name=None):
  118. pointwise_conv = self._conv_norm(
  119. input=input,
  120. filter_size=1,
  121. num_filters=int(num_filters1),
  122. stride=1,
  123. num_groups=int(num_groups),
  124. padding=0,
  125. name=name + "_extra1")
  126. normal_conv = self._conv_norm(
  127. input=pointwise_conv,
  128. filter_size=3,
  129. num_filters=int(num_filters2),
  130. stride=2,
  131. num_groups=int(num_groups),
  132. padding=1,
  133. name=name + "_extra2")
  134. return normal_conv
  135. def __call__(self, input):
  136. scale = self.conv_group_scale
  137. blocks = []
  138. # input 1/1
  139. out = self._conv_norm(
  140. input, 3, int(32 * scale), 2, 1, name=self.prefix_name + "conv1")
  141. # 1/2
  142. out = self.depthwise_separable(
  143. out, 32, 64, 32, 1, scale, name=self.prefix_name + "conv2_1")
  144. out = self.depthwise_separable(
  145. out, 64, 128, 64, 2, scale, name=self.prefix_name + "conv2_2")
  146. # 1/4
  147. out = self.depthwise_separable(
  148. out, 128, 128, 128, 1, scale, name=self.prefix_name + "conv3_1")
  149. out = self.depthwise_separable(
  150. out, 128, 256, 128, 2, scale, name=self.prefix_name + "conv3_2")
  151. # 1/8
  152. blocks.append(out)
  153. out = self.depthwise_separable(
  154. out, 256, 256, 256, 1, scale, name=self.prefix_name + "conv4_1")
  155. out = self.depthwise_separable(
  156. out, 256, 512, 256, 2, scale, name=self.prefix_name + "conv4_2")
  157. # 1/16
  158. blocks.append(out)
  159. for i in range(5):
  160. out = self.depthwise_separable(
  161. out,
  162. 512,
  163. 512,
  164. 512,
  165. 1,
  166. scale,
  167. name=self.prefix_name + "conv5_" + str(i + 1))
  168. module11 = out
  169. out = self.depthwise_separable(
  170. out, 512, 1024, 512, 2, scale, name=self.prefix_name + "conv5_6")
  171. # 1/32
  172. out = self.depthwise_separable(
  173. out, 1024, 1024, 1024, 1, scale, name=self.prefix_name + "conv6")
  174. module13 = out
  175. blocks.append(out)
  176. if self.num_classes:
  177. out = fluid.layers.pool2d(
  178. input=out, pool_type='avg', global_pooling=True)
  179. output = fluid.layers.fc(
  180. input=out,
  181. size=self.num_classes,
  182. param_attr=ParamAttr(
  183. initializer=fluid.initializer.MSRA(), name="fc7_weights"),
  184. bias_attr=ParamAttr(name="fc7_offset"))
  185. return output
  186. if not self.with_extra_blocks:
  187. return blocks
  188. num_filters = self.extra_block_filters
  189. module14 = self._extra_block(module13, num_filters[0][0],
  190. num_filters[0][1], 1, 2,
  191. self.prefix_name + "conv7_1")
  192. module15 = self._extra_block(module14, num_filters[1][0],
  193. num_filters[1][1], 1, 2,
  194. self.prefix_name + "conv7_2")
  195. module16 = self._extra_block(module15, num_filters[2][0],
  196. num_filters[2][1], 1, 2,
  197. self.prefix_name + "conv7_3")
  198. module17 = self._extra_block(module16, num_filters[3][0],
  199. num_filters[3][1], 1, 2,
  200. self.prefix_name + "conv7_4")
  201. return module11, module13, module14, module15, module16, module17