res2net_vd.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 numpy as np
  18. import paddle
  19. from paddle import ParamAttr
  20. import paddle.nn as nn
  21. import paddle.nn.functional as F
  22. from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
  23. from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
  24. from paddle.nn.initializer import Uniform
  25. import math
  26. from paddlex.ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url
  27. MODEL_URLS = {
  28. "Res2Net50_vd_26w_4s":
  29. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Res2Net50_vd_26w_4s_pretrained.pdparams",
  30. "Res2Net101_vd_26w_4s":
  31. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Res2Net101_vd_26w_4s_pretrained.pdparams",
  32. "Res2Net200_vd_26w_4s":
  33. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Res2Net200_vd_26w_4s_pretrained.pdparams",
  34. }
  35. __all__ = list(MODEL_URLS.keys())
  36. class ConvBNLayer(nn.Layer):
  37. def __init__(
  38. self,
  39. num_channels,
  40. num_filters,
  41. filter_size,
  42. stride=1,
  43. groups=1,
  44. is_vd_mode=False,
  45. act=None,
  46. name=None, ):
  47. super(ConvBNLayer, self).__init__()
  48. self.is_vd_mode = is_vd_mode
  49. self._pool2d_avg = AvgPool2D(
  50. kernel_size=2, stride=2, padding=0, ceil_mode=True)
  51. self._conv = Conv2D(
  52. in_channels=num_channels,
  53. out_channels=num_filters,
  54. kernel_size=filter_size,
  55. stride=stride,
  56. padding=(filter_size - 1) // 2,
  57. groups=groups,
  58. weight_attr=ParamAttr(name=name + "_weights"),
  59. bias_attr=False)
  60. if name == "conv1":
  61. bn_name = "bn_" + name
  62. else:
  63. bn_name = "bn" + name[3:]
  64. self._batch_norm = BatchNorm(
  65. num_filters,
  66. act=act,
  67. param_attr=ParamAttr(name=bn_name + '_scale'),
  68. bias_attr=ParamAttr(bn_name + '_offset'),
  69. moving_mean_name=bn_name + '_mean',
  70. moving_variance_name=bn_name + '_variance')
  71. def forward(self, inputs):
  72. if self.is_vd_mode:
  73. inputs = self._pool2d_avg(inputs)
  74. y = self._conv(inputs)
  75. y = self._batch_norm(y)
  76. return y
  77. class BottleneckBlock(nn.Layer):
  78. def __init__(self,
  79. num_channels1,
  80. num_channels2,
  81. num_filters,
  82. stride,
  83. scales,
  84. shortcut=True,
  85. if_first=False,
  86. name=None):
  87. super(BottleneckBlock, self).__init__()
  88. self.stride = stride
  89. self.scales = scales
  90. self.conv0 = ConvBNLayer(
  91. num_channels=num_channels1,
  92. num_filters=num_filters,
  93. filter_size=1,
  94. act='relu',
  95. name=name + "_branch2a")
  96. self.conv1_list = []
  97. for s in range(scales - 1):
  98. conv1 = self.add_sublayer(
  99. name + '_branch2b_' + str(s + 1),
  100. ConvBNLayer(
  101. num_channels=num_filters // scales,
  102. num_filters=num_filters // scales,
  103. filter_size=3,
  104. stride=stride,
  105. act='relu',
  106. name=name + '_branch2b_' + str(s + 1)))
  107. self.conv1_list.append(conv1)
  108. self.pool2d_avg = AvgPool2D(kernel_size=3, stride=stride, padding=1)
  109. self.conv2 = ConvBNLayer(
  110. num_channels=num_filters,
  111. num_filters=num_channels2,
  112. filter_size=1,
  113. act=None,
  114. name=name + "_branch2c")
  115. if not shortcut:
  116. self.short = ConvBNLayer(
  117. num_channels=num_channels1,
  118. num_filters=num_channels2,
  119. filter_size=1,
  120. stride=1,
  121. is_vd_mode=False if if_first else True,
  122. name=name + "_branch1")
  123. self.shortcut = shortcut
  124. def forward(self, inputs):
  125. y = self.conv0(inputs)
  126. xs = paddle.split(y, self.scales, 1)
  127. ys = []
  128. for s, conv1 in enumerate(self.conv1_list):
  129. if s == 0 or self.stride == 2:
  130. ys.append(conv1(xs[s]))
  131. else:
  132. ys.append(conv1(xs[s] + ys[-1]))
  133. if self.stride == 1:
  134. ys.append(xs[-1])
  135. else:
  136. ys.append(self.pool2d_avg(xs[-1]))
  137. conv1 = paddle.concat(ys, axis=1)
  138. conv2 = self.conv2(conv1)
  139. if self.shortcut:
  140. short = inputs
  141. else:
  142. short = self.short(inputs)
  143. y = paddle.add(x=short, y=conv2)
  144. y = F.relu(y)
  145. return y
  146. class Res2Net_vd(nn.Layer):
  147. def __init__(self, layers=50, scales=4, width=26, class_num=1000):
  148. super(Res2Net_vd, self).__init__()
  149. self.layers = layers
  150. self.scales = scales
  151. self.width = width
  152. basic_width = self.width * self.scales
  153. supported_layers = [50, 101, 152, 200]
  154. assert layers in supported_layers, \
  155. "supported layers are {} but input layer is {}".format(
  156. supported_layers, layers)
  157. if layers == 50:
  158. depth = [3, 4, 6, 3]
  159. elif layers == 101:
  160. depth = [3, 4, 23, 3]
  161. elif layers == 152:
  162. depth = [3, 8, 36, 3]
  163. elif layers == 200:
  164. depth = [3, 12, 48, 3]
  165. num_channels = [64, 256, 512, 1024]
  166. num_channels2 = [256, 512, 1024, 2048]
  167. num_filters = [basic_width * t for t in [1, 2, 4, 8]]
  168. self.conv1_1 = ConvBNLayer(
  169. num_channels=3,
  170. num_filters=32,
  171. filter_size=3,
  172. stride=2,
  173. act='relu',
  174. name="conv1_1")
  175. self.conv1_2 = ConvBNLayer(
  176. num_channels=32,
  177. num_filters=32,
  178. filter_size=3,
  179. stride=1,
  180. act='relu',
  181. name="conv1_2")
  182. self.conv1_3 = ConvBNLayer(
  183. num_channels=32,
  184. num_filters=64,
  185. filter_size=3,
  186. stride=1,
  187. act='relu',
  188. name="conv1_3")
  189. self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1)
  190. self.block_list = []
  191. for block in range(len(depth)):
  192. shortcut = False
  193. for i in range(depth[block]):
  194. if layers in [101, 152, 200] and block == 2:
  195. if i == 0:
  196. conv_name = "res" + str(block + 2) + "a"
  197. else:
  198. conv_name = "res" + str(block + 2) + "b" + str(i)
  199. else:
  200. conv_name = "res" + str(block + 2) + chr(97 + i)
  201. bottleneck_block = self.add_sublayer(
  202. 'bb_%d_%d' % (block, i),
  203. BottleneckBlock(
  204. num_channels1=num_channels[block]
  205. if i == 0 else num_channels2[block],
  206. num_channels2=num_channels2[block],
  207. num_filters=num_filters[block],
  208. stride=2 if i == 0 and block != 0 else 1,
  209. scales=scales,
  210. shortcut=shortcut,
  211. if_first=block == i == 0,
  212. name=conv_name))
  213. self.block_list.append(bottleneck_block)
  214. shortcut = True
  215. self.pool2d_avg = AdaptiveAvgPool2D(1)
  216. self.pool2d_avg_channels = num_channels[-1] * 2
  217. stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0)
  218. self.out = Linear(
  219. self.pool2d_avg_channels,
  220. class_num,
  221. weight_attr=ParamAttr(
  222. initializer=Uniform(-stdv, stdv), name="fc_weights"),
  223. bias_attr=ParamAttr(name="fc_offset"))
  224. def forward(self, inputs):
  225. y = self.conv1_1(inputs)
  226. y = self.conv1_2(y)
  227. y = self.conv1_3(y)
  228. y = self.pool2d_max(y)
  229. for block in self.block_list:
  230. y = block(y)
  231. y = self.pool2d_avg(y)
  232. y = paddle.reshape(y, shape=[-1, self.pool2d_avg_channels])
  233. y = self.out(y)
  234. return y
  235. def _load_pretrained(pretrained, model, model_url, use_ssld=False):
  236. if pretrained is False:
  237. pass
  238. elif pretrained is True:
  239. load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld)
  240. elif isinstance(pretrained, str):
  241. load_dygraph_pretrain(model, pretrained)
  242. else:
  243. raise RuntimeError(
  244. "pretrained type is not available. Please use `string` or `boolean` type."
  245. )
  246. def Res2Net50_vd_26w_4s(pretrained=False, use_ssld=False, **kwargs):
  247. model = Res2Net_vd(layers=50, scales=4, width=26, **kwargs)
  248. _load_pretrained(
  249. pretrained,
  250. model,
  251. MODEL_URLS["Res2Net50_vd_26w_4s"],
  252. use_ssld=use_ssld)
  253. return model
  254. def Res2Net101_vd_26w_4s(pretrained=False, use_ssld=False, **kwargs):
  255. model = Res2Net_vd(layers=101, scales=4, width=26, **kwargs)
  256. _load_pretrained(
  257. pretrained,
  258. model,
  259. MODEL_URLS["Res2Net101_vd_26w_4s"],
  260. use_ssld=use_ssld)
  261. return model
  262. def Res2Net200_vd_26w_4s(pretrained=False, use_ssld=False, **kwargs):
  263. model = Res2Net_vd(layers=200, scales=4, width=26, **kwargs)
  264. _load_pretrained(
  265. pretrained,
  266. model,
  267. MODEL_URLS["Res2Net200_vd_26w_4s"],
  268. use_ssld=use_ssld)
  269. return model