det_resnet_vd_sast.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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_SAST"]
  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=2, stride=2, 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=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=1,
  97. is_vd_mode=False if if_first else True,
  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=1,
  140. is_vd_mode=False if if_first else True,
  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_SAST(nn.Layer):
  154. def __init__(self, in_channels=3, layers=50, **kwargs):
  155. super(ResNet_SAST, 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. depth = [3, 4, 6, 3, 3]
  166. elif layers == 101:
  167. depth = [3, 4, 23, 3]
  168. elif layers == 152:
  169. depth = [3, 8, 36, 3]
  170. elif layers == 200:
  171. depth = [3, 12, 48, 3]
  172. # num_channels = [64, 256, 512,
  173. # 1024] if layers >= 50 else [64, 64, 128, 256]
  174. # num_filters = [64, 128, 256, 512]
  175. num_channels = [64, 256, 512,
  176. 1024, 2048] if layers >= 50 else [64, 64, 128, 256]
  177. num_filters = [64, 128, 256, 512, 512]
  178. self.conv1_1 = ConvBNLayer(
  179. in_channels=in_channels,
  180. out_channels=32,
  181. kernel_size=3,
  182. stride=2,
  183. act='relu',
  184. name="conv1_1")
  185. self.conv1_2 = ConvBNLayer(
  186. in_channels=32,
  187. out_channels=32,
  188. kernel_size=3,
  189. stride=1,
  190. act='relu',
  191. name="conv1_2")
  192. self.conv1_3 = ConvBNLayer(
  193. in_channels=32,
  194. out_channels=64,
  195. kernel_size=3,
  196. stride=1,
  197. act='relu',
  198. name="conv1_3")
  199. self.pool2d_max = nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
  200. self.stages = []
  201. self.out_channels = [3, 64]
  202. if layers >= 50:
  203. for block in range(len(depth)):
  204. block_list = []
  205. shortcut = False
  206. for i in range(depth[block]):
  207. if layers in [101, 152] and block == 2:
  208. if i == 0:
  209. conv_name = "res" + str(block + 2) + "a"
  210. else:
  211. conv_name = "res" + str(block + 2) + "b" + str(i)
  212. else:
  213. conv_name = "res" + str(block + 2) + chr(97 + i)
  214. bottleneck_block = self.add_sublayer(
  215. 'bb_%d_%d' % (block, i),
  216. BottleneckBlock(
  217. in_channels=num_channels[block]
  218. if i == 0 else num_filters[block] * 4,
  219. out_channels=num_filters[block],
  220. stride=2 if i == 0 and block != 0 else 1,
  221. shortcut=shortcut,
  222. if_first=block == i == 0,
  223. name=conv_name))
  224. shortcut = True
  225. block_list.append(bottleneck_block)
  226. self.out_channels.append(num_filters[block] * 4)
  227. self.stages.append(nn.Sequential(*block_list))
  228. else:
  229. for block in range(len(depth)):
  230. block_list = []
  231. shortcut = False
  232. for i in range(depth[block]):
  233. conv_name = "res" + str(block + 2) + chr(97 + i)
  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=2 if i == 0 and block != 0 else 1,
  241. shortcut=shortcut,
  242. if_first=block == i == 0,
  243. name=conv_name))
  244. shortcut = True
  245. block_list.append(basic_block)
  246. self.out_channels.append(num_filters[block])
  247. self.stages.append(nn.Sequential(*block_list))
  248. def forward(self, inputs):
  249. out = [inputs]
  250. y = self.conv1_1(inputs)
  251. y = self.conv1_2(y)
  252. y = self.conv1_3(y)
  253. out.append(y)
  254. y = self.pool2d_max(y)
  255. for block in self.stages:
  256. y = block(y)
  257. out.append(y)
  258. return out