se_resnet_vd.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. from __future__ import absolute_import
  14. from __future__ import division
  15. from __future__ import print_function
  16. import numpy as np
  17. import paddle
  18. from paddle import ParamAttr
  19. import paddle.nn as nn
  20. import paddle.nn.functional as F
  21. from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
  22. from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
  23. from paddle.nn.initializer import Uniform
  24. import math
  25. from paddlex.ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url
  26. MODEL_URLS = {
  27. "SE_ResNet18_vd":
  28. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNet18_vd_pretrained.pdparams",
  29. "SE_ResNet34_vd":
  30. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNet34_vd_pretrained.pdparams",
  31. "SE_ResNet50_vd":
  32. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNet50_vd_pretrained.pdparams",
  33. }
  34. __all__ = list(MODEL_URLS.keys())
  35. class ConvBNLayer(nn.Layer):
  36. def __init__(
  37. self,
  38. num_channels,
  39. num_filters,
  40. filter_size,
  41. stride=1,
  42. groups=1,
  43. is_vd_mode=False,
  44. act=None,
  45. name=None, ):
  46. super(ConvBNLayer, self).__init__()
  47. self.is_vd_mode = is_vd_mode
  48. self._pool2d_avg = AvgPool2D(
  49. kernel_size=2, stride=2, padding=0, ceil_mode=True)
  50. self._conv = Conv2D(
  51. in_channels=num_channels,
  52. out_channels=num_filters,
  53. kernel_size=filter_size,
  54. stride=stride,
  55. padding=(filter_size - 1) // 2,
  56. groups=groups,
  57. weight_attr=ParamAttr(name=name + "_weights"),
  58. bias_attr=False)
  59. if name == "conv1":
  60. bn_name = "bn_" + name
  61. else:
  62. bn_name = "bn" + name[3:]
  63. self._batch_norm = BatchNorm(
  64. num_filters,
  65. act=act,
  66. param_attr=ParamAttr(name=bn_name + '_scale'),
  67. bias_attr=ParamAttr(bn_name + '_offset'),
  68. moving_mean_name=bn_name + '_mean',
  69. moving_variance_name=bn_name + '_variance')
  70. def forward(self, inputs):
  71. if self.is_vd_mode:
  72. inputs = self._pool2d_avg(inputs)
  73. y = self._conv(inputs)
  74. y = self._batch_norm(y)
  75. return y
  76. class BottleneckBlock(nn.Layer):
  77. def __init__(self,
  78. num_channels,
  79. num_filters,
  80. stride,
  81. shortcut=True,
  82. if_first=False,
  83. reduction_ratio=16,
  84. name=None):
  85. super(BottleneckBlock, self).__init__()
  86. self.conv0 = ConvBNLayer(
  87. num_channels=num_channels,
  88. num_filters=num_filters,
  89. filter_size=1,
  90. act='relu',
  91. name=name + "_branch2a")
  92. self.conv1 = ConvBNLayer(
  93. num_channels=num_filters,
  94. num_filters=num_filters,
  95. filter_size=3,
  96. stride=stride,
  97. act='relu',
  98. name=name + "_branch2b")
  99. self.conv2 = ConvBNLayer(
  100. num_channels=num_filters,
  101. num_filters=num_filters * 4,
  102. filter_size=1,
  103. act=None,
  104. name=name + "_branch2c")
  105. self.scale = SELayer(
  106. num_channels=num_filters * 4,
  107. num_filters=num_filters * 4,
  108. reduction_ratio=reduction_ratio,
  109. name='fc_' + name)
  110. if not shortcut:
  111. self.short = ConvBNLayer(
  112. num_channels=num_channels,
  113. num_filters=num_filters * 4,
  114. filter_size=1,
  115. stride=1,
  116. is_vd_mode=False if if_first else True,
  117. name=name + "_branch1")
  118. self.shortcut = shortcut
  119. def forward(self, inputs):
  120. y = self.conv0(inputs)
  121. conv1 = self.conv1(y)
  122. conv2 = self.conv2(conv1)
  123. scale = self.scale(conv2)
  124. if self.shortcut:
  125. short = inputs
  126. else:
  127. short = self.short(inputs)
  128. y = paddle.add(x=short, y=scale)
  129. y = F.relu(y)
  130. return y
  131. class BasicBlock(nn.Layer):
  132. def __init__(self,
  133. num_channels,
  134. num_filters,
  135. stride,
  136. shortcut=True,
  137. if_first=False,
  138. reduction_ratio=16,
  139. name=None):
  140. super(BasicBlock, self).__init__()
  141. self.stride = stride
  142. self.conv0 = ConvBNLayer(
  143. num_channels=num_channels,
  144. num_filters=num_filters,
  145. filter_size=3,
  146. stride=stride,
  147. act='relu',
  148. name=name + "_branch2a")
  149. self.conv1 = ConvBNLayer(
  150. num_channels=num_filters,
  151. num_filters=num_filters,
  152. filter_size=3,
  153. act=None,
  154. name=name + "_branch2b")
  155. self.scale = SELayer(
  156. num_channels=num_filters,
  157. num_filters=num_filters,
  158. reduction_ratio=reduction_ratio,
  159. name='fc_' + name)
  160. if not shortcut:
  161. self.short = ConvBNLayer(
  162. num_channels=num_channels,
  163. num_filters=num_filters,
  164. filter_size=1,
  165. stride=1,
  166. is_vd_mode=False if if_first else True,
  167. name=name + "_branch1")
  168. self.shortcut = shortcut
  169. def forward(self, inputs):
  170. y = self.conv0(inputs)
  171. conv1 = self.conv1(y)
  172. scale = self.scale(conv1)
  173. if self.shortcut:
  174. short = inputs
  175. else:
  176. short = self.short(inputs)
  177. y = paddle.add(x=short, y=scale)
  178. y = F.relu(y)
  179. return y
  180. class SELayer(nn.Layer):
  181. def __init__(self, num_channels, num_filters, reduction_ratio, name=None):
  182. super(SELayer, self).__init__()
  183. self.pool2d_gap = AdaptiveAvgPool2D(1)
  184. self._num_channels = num_channels
  185. med_ch = int(num_channels / reduction_ratio)
  186. stdv = 1.0 / math.sqrt(num_channels * 1.0)
  187. self.squeeze = Linear(
  188. num_channels,
  189. med_ch,
  190. weight_attr=ParamAttr(
  191. initializer=Uniform(-stdv, stdv), name=name + "_sqz_weights"),
  192. bias_attr=ParamAttr(name=name + '_sqz_offset'))
  193. stdv = 1.0 / math.sqrt(med_ch * 1.0)
  194. self.excitation = Linear(
  195. med_ch,
  196. num_filters,
  197. weight_attr=ParamAttr(
  198. initializer=Uniform(-stdv, stdv), name=name + "_exc_weights"),
  199. bias_attr=ParamAttr(name=name + '_exc_offset'))
  200. def forward(self, input):
  201. pool = self.pool2d_gap(input)
  202. pool = paddle.squeeze(pool, axis=[2, 3])
  203. squeeze = self.squeeze(pool)
  204. squeeze = F.relu(squeeze)
  205. excitation = self.excitation(squeeze)
  206. excitation = F.sigmoid(excitation)
  207. excitation = paddle.unsqueeze(excitation, axis=[2, 3])
  208. out = input * excitation
  209. return out
  210. class SE_ResNet_vd(nn.Layer):
  211. def __init__(self, layers=50, class_num=1000):
  212. super(SE_ResNet_vd, self).__init__()
  213. self.layers = layers
  214. supported_layers = [18, 34, 50, 101, 152, 200]
  215. assert layers in supported_layers, \
  216. "supported layers are {} but input layer is {}".format(
  217. supported_layers, layers)
  218. if layers == 18:
  219. depth = [2, 2, 2, 2]
  220. elif layers == 34 or layers == 50:
  221. depth = [3, 4, 6, 3]
  222. elif layers == 101:
  223. depth = [3, 4, 23, 3]
  224. elif layers == 152:
  225. depth = [3, 8, 36, 3]
  226. elif layers == 200:
  227. depth = [3, 12, 48, 3]
  228. num_channels = [64, 256, 512,
  229. 1024] if layers >= 50 else [64, 64, 128, 256]
  230. num_filters = [64, 128, 256, 512]
  231. self.conv1_1 = ConvBNLayer(
  232. num_channels=3,
  233. num_filters=32,
  234. filter_size=3,
  235. stride=2,
  236. act='relu',
  237. name="conv1_1")
  238. self.conv1_2 = ConvBNLayer(
  239. num_channels=32,
  240. num_filters=32,
  241. filter_size=3,
  242. stride=1,
  243. act='relu',
  244. name="conv1_2")
  245. self.conv1_3 = ConvBNLayer(
  246. num_channels=32,
  247. num_filters=64,
  248. filter_size=3,
  249. stride=1,
  250. act='relu',
  251. name="conv1_3")
  252. self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1)
  253. self.block_list = []
  254. if layers >= 50:
  255. for block in range(len(depth)):
  256. shortcut = False
  257. for i in range(depth[block]):
  258. if layers in [101, 152] and block == 2:
  259. if i == 0:
  260. conv_name = "res" + str(block + 2) + "a"
  261. else:
  262. conv_name = "res" + str(block + 2) + "b" + str(i)
  263. else:
  264. conv_name = "res" + str(block + 2) + chr(97 + i)
  265. bottleneck_block = self.add_sublayer(
  266. 'bb_%d_%d' % (block, i),
  267. BottleneckBlock(
  268. num_channels=num_channels[block]
  269. if i == 0 else num_filters[block] * 4,
  270. num_filters=num_filters[block],
  271. stride=2 if i == 0 and block != 0 else 1,
  272. shortcut=shortcut,
  273. if_first=block == i == 0,
  274. name=conv_name))
  275. self.block_list.append(bottleneck_block)
  276. shortcut = True
  277. else:
  278. for block in range(len(depth)):
  279. shortcut = False
  280. for i in range(depth[block]):
  281. conv_name = "res" + str(block + 2) + chr(97 + i)
  282. basic_block = self.add_sublayer(
  283. 'bb_%d_%d' % (block, i),
  284. BasicBlock(
  285. num_channels=num_channels[block]
  286. if i == 0 else num_filters[block],
  287. num_filters=num_filters[block],
  288. stride=2 if i == 0 and block != 0 else 1,
  289. shortcut=shortcut,
  290. if_first=block == i == 0,
  291. name=conv_name))
  292. self.block_list.append(basic_block)
  293. shortcut = True
  294. self.pool2d_avg = AdaptiveAvgPool2D(1)
  295. self.pool2d_avg_channels = num_channels[-1] * 2
  296. stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0)
  297. self.out = Linear(
  298. self.pool2d_avg_channels,
  299. class_num,
  300. weight_attr=ParamAttr(
  301. initializer=Uniform(-stdv, stdv), name="fc6_weights"),
  302. bias_attr=ParamAttr(name="fc6_offset"))
  303. def forward(self, inputs):
  304. y = self.conv1_1(inputs)
  305. y = self.conv1_2(y)
  306. y = self.conv1_3(y)
  307. y = self.pool2d_max(y)
  308. for block in self.block_list:
  309. y = block(y)
  310. y = self.pool2d_avg(y)
  311. y = paddle.reshape(y, shape=[-1, self.pool2d_avg_channels])
  312. y = self.out(y)
  313. return y
  314. def _load_pretrained(pretrained, model, model_url, use_ssld=False):
  315. if pretrained is False:
  316. pass
  317. elif pretrained is True:
  318. load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld)
  319. elif isinstance(pretrained, str):
  320. load_dygraph_pretrain(model, pretrained)
  321. else:
  322. raise RuntimeError(
  323. "pretrained type is not available. Please use `string` or `boolean` type."
  324. )
  325. def SE_ResNet18_vd(pretrained=False, use_ssld=False, **kwargs):
  326. model = SE_ResNet_vd(layers=18, **kwargs)
  327. _load_pretrained(
  328. pretrained, model, MODEL_URLS["SE_ResNet18_vd"], use_ssld=use_ssld)
  329. return model
  330. def SE_ResNet34_vd(pretrained=False, use_ssld=False, **kwargs):
  331. model = SE_ResNet_vd(layers=34, **kwargs)
  332. _load_pretrained(
  333. pretrained, model, MODEL_URLS["SE_ResNet34_vd"], use_ssld=use_ssld)
  334. return model
  335. def SE_ResNet50_vd(pretrained=False, use_ssld=False, **kwargs):
  336. model = SE_ResNet_vd(layers=50, **kwargs)
  337. _load_pretrained(
  338. pretrained, model, MODEL_URLS["SE_ResNet50_vd"], use_ssld=use_ssld)
  339. return model