resnet_vd.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. import paddle
  15. import paddle.nn as nn
  16. import paddle.nn.functional as F
  17. from paddlex.paddleseg.cvlibs import manager
  18. from paddlex.paddleseg.models import layers
  19. from paddlex.paddleseg.utils import utils
  20. __all__ = [
  21. "ResNet18_vd", "ResNet34_vd", "ResNet50_vd", "ResNet101_vd", "ResNet152_vd"
  22. ]
  23. class ConvBNLayer(nn.Layer):
  24. def __init__(
  25. self,
  26. in_channels,
  27. out_channels,
  28. kernel_size,
  29. stride=1,
  30. dilation=1,
  31. groups=1,
  32. is_vd_mode=False,
  33. act=None,
  34. ):
  35. super(ConvBNLayer, self).__init__()
  36. self.is_vd_mode = is_vd_mode
  37. self._pool2d_avg = nn.AvgPool2D(
  38. kernel_size=2, stride=2, padding=0, ceil_mode=True)
  39. self._conv = nn.Conv2D(
  40. in_channels=in_channels,
  41. out_channels=out_channels,
  42. kernel_size=kernel_size,
  43. stride=stride,
  44. padding=(kernel_size - 1) // 2 if dilation == 1 else 0,
  45. dilation=dilation,
  46. groups=groups,
  47. bias_attr=False)
  48. self._batch_norm = layers.SyncBatchNorm(out_channels)
  49. self._act_op = layers.Activation(act=act)
  50. def forward(self, inputs):
  51. if self.is_vd_mode:
  52. inputs = self._pool2d_avg(inputs)
  53. y = self._conv(inputs)
  54. y = self._batch_norm(y)
  55. y = self._act_op(y)
  56. return y
  57. class BottleneckBlock(nn.Layer):
  58. def __init__(self,
  59. in_channels,
  60. out_channels,
  61. stride,
  62. shortcut=True,
  63. if_first=False,
  64. dilation=1):
  65. super(BottleneckBlock, self).__init__()
  66. self.conv0 = ConvBNLayer(
  67. in_channels=in_channels,
  68. out_channels=out_channels,
  69. kernel_size=1,
  70. act='relu')
  71. self.dilation = dilation
  72. self.conv1 = ConvBNLayer(
  73. in_channels=out_channels,
  74. out_channels=out_channels,
  75. kernel_size=3,
  76. stride=stride,
  77. act='relu',
  78. dilation=dilation)
  79. self.conv2 = ConvBNLayer(
  80. in_channels=out_channels,
  81. out_channels=out_channels * 4,
  82. kernel_size=1,
  83. act=None)
  84. if not shortcut:
  85. self.short = ConvBNLayer(
  86. in_channels=in_channels,
  87. out_channels=out_channels * 4,
  88. kernel_size=1,
  89. stride=1,
  90. is_vd_mode=False if if_first or stride == 1 else True)
  91. self.shortcut = shortcut
  92. def forward(self, inputs):
  93. y = self.conv0(inputs)
  94. ####################################################################
  95. # If given dilation rate > 1, using corresponding padding.
  96. # The performance drops down without the follow padding.
  97. if self.dilation > 1:
  98. padding = self.dilation
  99. y = F.pad(y, [padding, padding, padding, padding])
  100. #####################################################################
  101. conv1 = self.conv1(y)
  102. conv2 = self.conv2(conv1)
  103. if self.shortcut:
  104. short = inputs
  105. else:
  106. short = self.short(inputs)
  107. y = paddle.add(x=short, y=conv2)
  108. y = F.relu(y)
  109. return y
  110. class BasicBlock(nn.Layer):
  111. def __init__(self,
  112. in_channels,
  113. out_channels,
  114. stride,
  115. shortcut=True,
  116. if_first=False):
  117. super(BasicBlock, self).__init__()
  118. self.stride = stride
  119. self.conv0 = ConvBNLayer(
  120. in_channels=in_channels,
  121. out_channels=out_channels,
  122. kernel_size=3,
  123. stride=stride,
  124. act='relu')
  125. self.conv1 = ConvBNLayer(
  126. in_channels=out_channels,
  127. out_channels=out_channels,
  128. kernel_size=3,
  129. act=None)
  130. if not shortcut:
  131. self.short = ConvBNLayer(
  132. in_channels=in_channels,
  133. out_channels=out_channels,
  134. kernel_size=1,
  135. stride=1,
  136. is_vd_mode=False if if_first else True)
  137. self.shortcut = shortcut
  138. def forward(self, inputs):
  139. y = self.conv0(inputs)
  140. conv1 = self.conv1(y)
  141. if self.shortcut:
  142. short = inputs
  143. else:
  144. short = self.short(inputs)
  145. y = paddle.add(x=short, y=conv1)
  146. y = F.relu(y)
  147. return y
  148. class ResNet_vd(nn.Layer):
  149. """
  150. The ResNet_vd implementation based on PaddlePaddle.
  151. The original article refers to Jingdong
  152. Tong He, et, al. "Bag of Tricks for Image Classification with Convolutional Neural Networks"
  153. (https://arxiv.org/pdf/1812.01187.pdf).
  154. Args:
  155. layers (int, optional): The layers of ResNet_vd. The supported layers are (18, 34, 50, 101, 152, 200). Default: 50.
  156. output_stride (int, optional): The stride of output features compared to input images. It is 8 or 16. Default: 8.
  157. multi_grid (tuple|list, optional): The grid of stage4. Defult: (1, 1, 1).
  158. pretrained (str, optional): The path of pretrained model.
  159. """
  160. def __init__(self,
  161. layers=50,
  162. output_stride=8,
  163. multi_grid=(1, 1, 1),
  164. pretrained=None):
  165. super(ResNet_vd, self).__init__()
  166. self.conv1_logit = None # for gscnn shape stream
  167. self.layers = layers
  168. supported_layers = [18, 34, 50, 101, 152, 200]
  169. assert layers in supported_layers, \
  170. "supported layers are {} but input layer is {}".format(
  171. supported_layers, layers)
  172. if layers == 18:
  173. depth = [2, 2, 2, 2]
  174. elif layers == 34 or layers == 50:
  175. depth = [3, 4, 6, 3]
  176. elif layers == 101:
  177. depth = [3, 4, 23, 3]
  178. elif layers == 152:
  179. depth = [3, 8, 36, 3]
  180. elif layers == 200:
  181. depth = [3, 12, 48, 3]
  182. num_channels = [64, 256, 512, 1024
  183. ] if layers >= 50 else [64, 64, 128, 256]
  184. num_filters = [64, 128, 256, 512]
  185. # for channels of four returned stages
  186. self.feat_channels = [c * 4 for c in num_filters
  187. ] if layers >= 50 else num_filters
  188. dilation_dict = None
  189. if output_stride == 8:
  190. dilation_dict = {2: 2, 3: 4}
  191. elif output_stride == 16:
  192. dilation_dict = {3: 2}
  193. self.conv1_1 = ConvBNLayer(
  194. in_channels=3, out_channels=32, kernel_size=3, stride=2, act='relu')
  195. self.conv1_2 = ConvBNLayer(
  196. in_channels=32,
  197. out_channels=32,
  198. kernel_size=3,
  199. stride=1,
  200. act='relu')
  201. self.conv1_3 = ConvBNLayer(
  202. in_channels=32,
  203. out_channels=64,
  204. kernel_size=3,
  205. stride=1,
  206. act='relu')
  207. self.pool2d_max = nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
  208. # self.block_list = []
  209. self.stage_list = []
  210. if layers >= 50:
  211. for block in range(len(depth)):
  212. shortcut = False
  213. block_list = []
  214. for i in range(depth[block]):
  215. if layers in [101, 152] and block == 2:
  216. if i == 0:
  217. conv_name = "res" + str(block + 2) + "a"
  218. else:
  219. conv_name = "res" + str(block + 2) + "b" + str(i)
  220. else:
  221. conv_name = "res" + str(block + 2) + chr(97 + i)
  222. ###############################################################################
  223. # Add dilation rate for some segmentation tasks, if dilation_dict is not None.
  224. dilation_rate = dilation_dict[
  225. block] if dilation_dict and block in dilation_dict else 1
  226. # Actually block here is 'stage', and i is 'block' in 'stage'
  227. # At the stage 4, expand the the dilation_rate if given multi_grid
  228. if block == 3:
  229. dilation_rate = dilation_rate * multi_grid[i]
  230. ###############################################################################
  231. bottleneck_block = self.add_sublayer(
  232. 'bb_%d_%d' % (block, i),
  233. BottleneckBlock(
  234. in_channels=num_channels[block]
  235. if i == 0 else num_filters[block] * 4,
  236. out_channels=num_filters[block],
  237. stride=2 if i == 0 and block != 0
  238. and dilation_rate == 1 else 1,
  239. shortcut=shortcut,
  240. if_first=block == i == 0,
  241. dilation=dilation_rate))
  242. block_list.append(bottleneck_block)
  243. shortcut = True
  244. self.stage_list.append(block_list)
  245. else:
  246. for block in range(len(depth)):
  247. shortcut = False
  248. block_list = []
  249. for i in range(depth[block]):
  250. conv_name = "res" + str(block + 2) + chr(97 + i)
  251. basic_block = self.add_sublayer(
  252. 'bb_%d_%d' % (block, i),
  253. BasicBlock(
  254. in_channels=num_channels[block]
  255. if i == 0 else num_filters[block],
  256. out_channels=num_filters[block],
  257. stride=2 if i == 0 and block != 0 else 1,
  258. shortcut=shortcut,
  259. if_first=block == i == 0))
  260. block_list.append(basic_block)
  261. shortcut = True
  262. self.stage_list.append(block_list)
  263. self.pretrained = pretrained
  264. self.init_weight()
  265. def forward(self, inputs):
  266. y = self.conv1_1(inputs)
  267. y = self.conv1_2(y)
  268. y = self.conv1_3(y)
  269. self.conv1_logit = y.clone()
  270. y = self.pool2d_max(y)
  271. # A feature list saves the output feature map of each stage.
  272. feat_list = []
  273. for stage in self.stage_list:
  274. for block in stage:
  275. y = block(y)
  276. feat_list.append(y)
  277. return feat_list
  278. def init_weight(self):
  279. utils.load_pretrained_model(self, self.pretrained)
  280. @manager.BACKBONES.add_component
  281. def ResNet18_vd(**args):
  282. model = ResNet_vd(layers=18, **args)
  283. return model
  284. def ResNet34_vd(**args):
  285. model = ResNet_vd(layers=34, **args)
  286. return model
  287. @manager.BACKBONES.add_component
  288. def ResNet50_vd(**args):
  289. model = ResNet_vd(layers=50, **args)
  290. return model
  291. @manager.BACKBONES.add_component
  292. def ResNet101_vd(**args):
  293. model = ResNet_vd(layers=101, **args)
  294. return model
  295. def ResNet152_vd(**args):
  296. model = ResNet_vd(layers=152, **args)
  297. return model
  298. def ResNet200_vd(**args):
  299. model = ResNet_vd(layers=200, **args)
  300. return model