shufflenet_v2.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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
  15. from __future__ import division
  16. from __future__ import print_function
  17. import paddle
  18. import paddle.nn as nn
  19. from paddle import ParamAttr
  20. from paddle.nn import Conv2D, MaxPool2D, AdaptiveAvgPool2D, BatchNorm
  21. from paddle.nn.initializer import KaimingNormal
  22. from paddlex.ppdet.core.workspace import register, serializable
  23. from numbers import Integral
  24. from ..shape_spec import ShapeSpec
  25. from paddlex.ppdet.modeling.ops import channel_shuffle
  26. __all__ = ['ShuffleNetV2']
  27. class ConvBNLayer(nn.Layer):
  28. def __init__(self,
  29. in_channels,
  30. out_channels,
  31. kernel_size,
  32. stride,
  33. padding,
  34. groups=1,
  35. act=None):
  36. super(ConvBNLayer, self).__init__()
  37. self._conv = Conv2D(
  38. in_channels=in_channels,
  39. out_channels=out_channels,
  40. kernel_size=kernel_size,
  41. stride=stride,
  42. padding=padding,
  43. groups=groups,
  44. weight_attr=ParamAttr(initializer=KaimingNormal()),
  45. bias_attr=False)
  46. self._batch_norm = BatchNorm(out_channels, act=act)
  47. def forward(self, inputs):
  48. y = self._conv(inputs)
  49. y = self._batch_norm(y)
  50. return y
  51. class InvertedResidual(nn.Layer):
  52. def __init__(self, in_channels, out_channels, stride, act="relu"):
  53. super(InvertedResidual, self).__init__()
  54. self._conv_pw = ConvBNLayer(
  55. in_channels=in_channels // 2,
  56. out_channels=out_channels // 2,
  57. kernel_size=1,
  58. stride=1,
  59. padding=0,
  60. groups=1,
  61. act=act)
  62. self._conv_dw = ConvBNLayer(
  63. in_channels=out_channels // 2,
  64. out_channels=out_channels // 2,
  65. kernel_size=3,
  66. stride=stride,
  67. padding=1,
  68. groups=out_channels // 2,
  69. act=None)
  70. self._conv_linear = ConvBNLayer(
  71. in_channels=out_channels // 2,
  72. out_channels=out_channels // 2,
  73. kernel_size=1,
  74. stride=1,
  75. padding=0,
  76. groups=1,
  77. act=act)
  78. def forward(self, inputs):
  79. x1, x2 = paddle.split(
  80. inputs,
  81. num_or_sections=[inputs.shape[1] // 2, inputs.shape[1] // 2],
  82. axis=1)
  83. x2 = self._conv_pw(x2)
  84. x2 = self._conv_dw(x2)
  85. x2 = self._conv_linear(x2)
  86. out = paddle.concat([x1, x2], axis=1)
  87. return channel_shuffle(out, 2)
  88. class InvertedResidualDS(nn.Layer):
  89. def __init__(self, in_channels, out_channels, stride, act="relu"):
  90. super(InvertedResidualDS, self).__init__()
  91. # branch1
  92. self._conv_dw_1 = ConvBNLayer(
  93. in_channels=in_channels,
  94. out_channels=in_channels,
  95. kernel_size=3,
  96. stride=stride,
  97. padding=1,
  98. groups=in_channels,
  99. act=None)
  100. self._conv_linear_1 = ConvBNLayer(
  101. in_channels=in_channels,
  102. out_channels=out_channels // 2,
  103. kernel_size=1,
  104. stride=1,
  105. padding=0,
  106. groups=1,
  107. act=act)
  108. # branch2
  109. self._conv_pw_2 = ConvBNLayer(
  110. in_channels=in_channels,
  111. out_channels=out_channels // 2,
  112. kernel_size=1,
  113. stride=1,
  114. padding=0,
  115. groups=1,
  116. act=act)
  117. self._conv_dw_2 = ConvBNLayer(
  118. in_channels=out_channels // 2,
  119. out_channels=out_channels // 2,
  120. kernel_size=3,
  121. stride=stride,
  122. padding=1,
  123. groups=out_channels // 2,
  124. act=None)
  125. self._conv_linear_2 = ConvBNLayer(
  126. in_channels=out_channels // 2,
  127. out_channels=out_channels // 2,
  128. kernel_size=1,
  129. stride=1,
  130. padding=0,
  131. groups=1,
  132. act=act)
  133. def forward(self, inputs):
  134. x1 = self._conv_dw_1(inputs)
  135. x1 = self._conv_linear_1(x1)
  136. x2 = self._conv_pw_2(inputs)
  137. x2 = self._conv_dw_2(x2)
  138. x2 = self._conv_linear_2(x2)
  139. out = paddle.concat([x1, x2], axis=1)
  140. return channel_shuffle(out, 2)
  141. @register
  142. @serializable
  143. class ShuffleNetV2(nn.Layer):
  144. def __init__(self,
  145. scale=1.0,
  146. act="relu",
  147. feature_maps=[5, 13, 17],
  148. with_last_conv=False):
  149. super(ShuffleNetV2, self).__init__()
  150. self.scale = scale
  151. self.with_last_conv = with_last_conv
  152. if isinstance(feature_maps, Integral):
  153. feature_maps = [feature_maps]
  154. self.feature_maps = feature_maps
  155. stage_repeats = [4, 8, 4]
  156. if scale == 0.25:
  157. stage_out_channels = [-1, 24, 24, 48, 96, 512]
  158. elif scale == 0.33:
  159. stage_out_channels = [-1, 24, 32, 64, 128, 512]
  160. elif scale == 0.5:
  161. stage_out_channels = [-1, 24, 48, 96, 192, 1024]
  162. elif scale == 1.0:
  163. stage_out_channels = [-1, 24, 116, 232, 464, 1024]
  164. elif scale == 1.5:
  165. stage_out_channels = [-1, 24, 176, 352, 704, 1024]
  166. elif scale == 2.0:
  167. stage_out_channels = [-1, 24, 224, 488, 976, 2048]
  168. else:
  169. raise NotImplementedError("This scale size:[" + str(scale) +
  170. "] is not implemented!")
  171. self._out_channels = []
  172. self._feature_idx = 0
  173. # 1. conv1
  174. self._conv1 = ConvBNLayer(
  175. in_channels=3,
  176. out_channels=stage_out_channels[1],
  177. kernel_size=3,
  178. stride=2,
  179. padding=1,
  180. act=act)
  181. self._max_pool = MaxPool2D(kernel_size=3, stride=2, padding=1)
  182. self._feature_idx += 1
  183. # 2. bottleneck sequences
  184. self._block_list = []
  185. for stage_id, num_repeat in enumerate(stage_repeats):
  186. for i in range(num_repeat):
  187. if i == 0:
  188. block = self.add_sublayer(
  189. name=str(stage_id + 2) + '_' + str(i + 1),
  190. sublayer=InvertedResidualDS(
  191. in_channels=stage_out_channels[stage_id + 1],
  192. out_channels=stage_out_channels[stage_id + 2],
  193. stride=2,
  194. act=act))
  195. else:
  196. block = self.add_sublayer(
  197. name=str(stage_id + 2) + '_' + str(i + 1),
  198. sublayer=InvertedResidual(
  199. in_channels=stage_out_channels[stage_id + 2],
  200. out_channels=stage_out_channels[stage_id + 2],
  201. stride=1,
  202. act=act))
  203. self._block_list.append(block)
  204. self._feature_idx += 1
  205. self._update_out_channels(stage_out_channels[stage_id + 2],
  206. self._feature_idx, self.feature_maps)
  207. if self.with_last_conv:
  208. # last_conv
  209. self._last_conv = ConvBNLayer(
  210. in_channels=stage_out_channels[-2],
  211. out_channels=stage_out_channels[-1],
  212. kernel_size=1,
  213. stride=1,
  214. padding=0,
  215. act=act)
  216. self._feature_idx += 1
  217. self._update_out_channels(stage_out_channels[-1],
  218. self._feature_idx, self.feature_maps)
  219. def _update_out_channels(self, channel, feature_idx, feature_maps):
  220. if feature_idx in feature_maps:
  221. self._out_channels.append(channel)
  222. def forward(self, inputs):
  223. y = self._conv1(inputs['image'])
  224. y = self._max_pool(y)
  225. outs = []
  226. for i, inv in enumerate(self._block_list):
  227. y = inv(y)
  228. if i + 2 in self.feature_maps:
  229. outs.append(y)
  230. if self.with_last_conv:
  231. y = self._last_conv(y)
  232. outs.append(y)
  233. return outs
  234. @property
  235. def out_shape(self):
  236. return [ShapeSpec(channels=c) for c in self._out_channels]