rec_resnet_vd.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 paddle
  18. from paddle import ParamAttr
  19. import paddle.nn as nn
  20. import paddle.nn.functional as F
  21. __all__ = ["ResNet"]
  22. class ConvBNLayer(nn.Layer):
  23. def __init__(
  24. self,
  25. in_channels,
  26. out_channels,
  27. kernel_size,
  28. stride=1,
  29. groups=1,
  30. is_vd_mode=False,
  31. act=None,
  32. name=None, ):
  33. super(ConvBNLayer, self).__init__()
  34. self.is_vd_mode = is_vd_mode
  35. self._pool2d_avg = nn.AvgPool2D(
  36. kernel_size=stride, stride=stride, padding=0, ceil_mode=True)
  37. self._conv = nn.Conv2D(
  38. in_channels=in_channels,
  39. out_channels=out_channels,
  40. kernel_size=kernel_size,
  41. stride=1 if is_vd_mode else stride,
  42. padding=(kernel_size - 1) // 2,
  43. groups=groups,
  44. weight_attr=ParamAttr(name=name + "_weights"),
  45. bias_attr=False)
  46. if name == "conv1":
  47. bn_name = "bn_" + name
  48. else:
  49. bn_name = "bn" + name[3:]
  50. self._batch_norm = nn.BatchNorm(
  51. out_channels,
  52. act=act,
  53. param_attr=ParamAttr(name=bn_name + '_scale'),
  54. bias_attr=ParamAttr(bn_name + '_offset'),
  55. moving_mean_name=bn_name + '_mean',
  56. moving_variance_name=bn_name + '_variance')
  57. def forward(self, inputs):
  58. if self.is_vd_mode:
  59. inputs = self._pool2d_avg(inputs)
  60. y = self._conv(inputs)
  61. y = self._batch_norm(y)
  62. return y
  63. class BottleneckBlock(nn.Layer):
  64. def __init__(self,
  65. in_channels,
  66. out_channels,
  67. stride,
  68. shortcut=True,
  69. if_first=False,
  70. name=None):
  71. super(BottleneckBlock, self).__init__()
  72. self.conv0 = ConvBNLayer(
  73. in_channels=in_channels,
  74. out_channels=out_channels,
  75. kernel_size=1,
  76. act='relu',
  77. name=name + "_branch2a")
  78. self.conv1 = ConvBNLayer(
  79. in_channels=out_channels,
  80. out_channels=out_channels,
  81. kernel_size=3,
  82. stride=stride,
  83. act='relu',
  84. name=name + "_branch2b")
  85. self.conv2 = ConvBNLayer(
  86. in_channels=out_channels,
  87. out_channels=out_channels * 4,
  88. kernel_size=1,
  89. act=None,
  90. name=name + "_branch2c")
  91. if not shortcut:
  92. self.short = ConvBNLayer(
  93. in_channels=in_channels,
  94. out_channels=out_channels * 4,
  95. kernel_size=1,
  96. stride=stride,
  97. is_vd_mode=not if_first and stride[0] != 1,
  98. name=name + "_branch1")
  99. self.shortcut = shortcut
  100. def forward(self, inputs):
  101. y = self.conv0(inputs)
  102. conv1 = self.conv1(y)
  103. conv2 = self.conv2(conv1)
  104. if self.shortcut:
  105. short = inputs
  106. else:
  107. short = self.short(inputs)
  108. y = paddle.add(x=short, y=conv2)
  109. y = F.relu(y)
  110. return y
  111. class BasicBlock(nn.Layer):
  112. def __init__(self,
  113. in_channels,
  114. out_channels,
  115. stride,
  116. shortcut=True,
  117. if_first=False,
  118. name=None):
  119. super(BasicBlock, self).__init__()
  120. self.stride = stride
  121. self.conv0 = ConvBNLayer(
  122. in_channels=in_channels,
  123. out_channels=out_channels,
  124. kernel_size=3,
  125. stride=stride,
  126. act='relu',
  127. name=name + "_branch2a")
  128. self.conv1 = ConvBNLayer(
  129. in_channels=out_channels,
  130. out_channels=out_channels,
  131. kernel_size=3,
  132. act=None,
  133. name=name + "_branch2b")
  134. if not shortcut:
  135. self.short = ConvBNLayer(
  136. in_channels=in_channels,
  137. out_channels=out_channels,
  138. kernel_size=1,
  139. stride=stride,
  140. is_vd_mode=not if_first and stride[0] != 1,
  141. name=name + "_branch1")
  142. self.shortcut = shortcut
  143. def forward(self, inputs):
  144. y = self.conv0(inputs)
  145. conv1 = self.conv1(y)
  146. if self.shortcut:
  147. short = inputs
  148. else:
  149. short = self.short(inputs)
  150. y = paddle.add(x=short, y=conv1)
  151. y = F.relu(y)
  152. return y
  153. class ResNet(nn.Layer):
  154. def __init__(self, in_channels=3, layers=50, **kwargs):
  155. super(ResNet, self).__init__()
  156. self.layers = layers
  157. supported_layers = [18, 34, 50, 101, 152, 200]
  158. assert layers in supported_layers, \
  159. "supported layers are {} but input layer is {}".format(
  160. supported_layers, layers)
  161. if layers == 18:
  162. depth = [2, 2, 2, 2]
  163. elif layers == 34 or layers == 50:
  164. depth = [3, 4, 6, 3]
  165. elif layers == 101:
  166. depth = [3, 4, 23, 3]
  167. elif layers == 152:
  168. depth = [3, 8, 36, 3]
  169. elif layers == 200:
  170. depth = [3, 12, 48, 3]
  171. num_channels = [64, 256, 512,
  172. 1024] if layers >= 50 else [64, 64, 128, 256]
  173. num_filters = [64, 128, 256, 512]
  174. self.conv1_1 = ConvBNLayer(
  175. in_channels=in_channels,
  176. out_channels=32,
  177. kernel_size=3,
  178. stride=1,
  179. act='relu',
  180. name="conv1_1")
  181. self.conv1_2 = ConvBNLayer(
  182. in_channels=32,
  183. out_channels=32,
  184. kernel_size=3,
  185. stride=1,
  186. act='relu',
  187. name="conv1_2")
  188. self.conv1_3 = ConvBNLayer(
  189. in_channels=32,
  190. out_channels=64,
  191. kernel_size=3,
  192. stride=1,
  193. act='relu',
  194. name="conv1_3")
  195. self.pool2d_max = nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
  196. self.block_list = []
  197. if layers >= 50:
  198. for block in range(len(depth)):
  199. shortcut = False
  200. for i in range(depth[block]):
  201. if layers in [101, 152, 200] and block == 2:
  202. if i == 0:
  203. conv_name = "res" + str(block + 2) + "a"
  204. else:
  205. conv_name = "res" + str(block + 2) + "b" + str(i)
  206. else:
  207. conv_name = "res" + str(block + 2) + chr(97 + i)
  208. if i == 0 and block != 0:
  209. stride = (2, 1)
  210. else:
  211. stride = (1, 1)
  212. bottleneck_block = self.add_sublayer(
  213. 'bb_%d_%d' % (block, i),
  214. BottleneckBlock(
  215. in_channels=num_channels[block]
  216. if i == 0 else num_filters[block] * 4,
  217. out_channels=num_filters[block],
  218. stride=stride,
  219. shortcut=shortcut,
  220. if_first=block == i == 0,
  221. name=conv_name))
  222. shortcut = True
  223. self.block_list.append(bottleneck_block)
  224. self.out_channels = num_filters[block] * 4
  225. else:
  226. for block in range(len(depth)):
  227. shortcut = False
  228. for i in range(depth[block]):
  229. conv_name = "res" + str(block + 2) + chr(97 + i)
  230. if i == 0 and block != 0:
  231. stride = (2, 1)
  232. else:
  233. stride = (1, 1)
  234. basic_block = self.add_sublayer(
  235. 'bb_%d_%d' % (block, i),
  236. BasicBlock(
  237. in_channels=num_channels[block]
  238. if i == 0 else num_filters[block],
  239. out_channels=num_filters[block],
  240. stride=stride,
  241. shortcut=shortcut,
  242. if_first=block == i == 0,
  243. name=conv_name))
  244. shortcut = True
  245. self.block_list.append(basic_block)
  246. self.out_channels = num_filters[block]
  247. self.out_pool = nn.MaxPool2D(kernel_size=2, stride=2, padding=0)
  248. def forward(self, inputs):
  249. y = self.conv1_1(inputs)
  250. y = self.conv1_2(y)
  251. y = self.conv1_3(y)
  252. y = self.pool2d_max(y)
  253. for block in self.block_list:
  254. y = block(y)
  255. y = self.out_pool(y)
  256. return y