se_resnext.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. "SE_ResNeXt50_32x4d":
  29. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNeXt50_32x4d_pretrained.pdparams",
  30. "SE_ResNeXt101_32x4d":
  31. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNeXt101_32x4d_pretrained.pdparams",
  32. "SE_ResNeXt152_64x4d":
  33. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SE_ResNeXt152_64x4d_pretrained.pdparams",
  34. }
  35. __all__ = list(MODEL_URLS.keys())
  36. class ConvBNLayer(nn.Layer):
  37. def __init__(self,
  38. num_channels,
  39. num_filters,
  40. filter_size,
  41. stride=1,
  42. groups=1,
  43. act=None,
  44. name=None,
  45. data_format='NCHW'):
  46. super(ConvBNLayer, self).__init__()
  47. self._conv = Conv2D(
  48. in_channels=num_channels,
  49. out_channels=num_filters,
  50. kernel_size=filter_size,
  51. stride=stride,
  52. padding=(filter_size - 1) // 2,
  53. groups=groups,
  54. weight_attr=ParamAttr(name=name + "_weights"),
  55. bias_attr=False,
  56. data_format=data_format)
  57. bn_name = name + '_bn'
  58. self._batch_norm = BatchNorm(
  59. num_filters,
  60. act=act,
  61. param_attr=ParamAttr(name=bn_name + '_scale'),
  62. bias_attr=ParamAttr(bn_name + '_offset'),
  63. moving_mean_name=bn_name + '_mean',
  64. moving_variance_name=bn_name + '_variance',
  65. data_layout=data_format)
  66. def forward(self, inputs):
  67. y = self._conv(inputs)
  68. y = self._batch_norm(y)
  69. return y
  70. class BottleneckBlock(nn.Layer):
  71. def __init__(self,
  72. num_channels,
  73. num_filters,
  74. stride,
  75. cardinality,
  76. reduction_ratio,
  77. shortcut=True,
  78. if_first=False,
  79. name=None,
  80. data_format="NCHW"):
  81. super(BottleneckBlock, self).__init__()
  82. self.conv0 = ConvBNLayer(
  83. num_channels=num_channels,
  84. num_filters=num_filters,
  85. filter_size=1,
  86. act='relu',
  87. name='conv' + name + '_x1',
  88. data_format=data_format)
  89. self.conv1 = ConvBNLayer(
  90. num_channels=num_filters,
  91. num_filters=num_filters,
  92. filter_size=3,
  93. groups=cardinality,
  94. stride=stride,
  95. act='relu',
  96. name='conv' + name + '_x2',
  97. data_format=data_format)
  98. self.conv2 = ConvBNLayer(
  99. num_channels=num_filters,
  100. num_filters=num_filters * 2 if cardinality == 32 else num_filters,
  101. filter_size=1,
  102. act=None,
  103. name='conv' + name + '_x3',
  104. data_format=data_format)
  105. self.scale = SELayer(
  106. num_channels=num_filters * 2 if cardinality == 32 else num_filters,
  107. num_filters=num_filters * 2 if cardinality == 32 else num_filters,
  108. reduction_ratio=reduction_ratio,
  109. name='fc' + name,
  110. data_format=data_format)
  111. if not shortcut:
  112. self.short = ConvBNLayer(
  113. num_channels=num_channels,
  114. num_filters=num_filters * 2
  115. if cardinality == 32 else num_filters,
  116. filter_size=1,
  117. stride=stride,
  118. name='conv' + name + '_prj',
  119. data_format=data_format)
  120. self.shortcut = shortcut
  121. def forward(self, inputs):
  122. y = self.conv0(inputs)
  123. conv1 = self.conv1(y)
  124. conv2 = self.conv2(conv1)
  125. scale = self.scale(conv2)
  126. if self.shortcut:
  127. short = inputs
  128. else:
  129. short = self.short(inputs)
  130. y = paddle.add(x=short, y=scale)
  131. y = F.relu(y)
  132. return y
  133. class SELayer(nn.Layer):
  134. def __init__(self,
  135. num_channels,
  136. num_filters,
  137. reduction_ratio,
  138. name=None,
  139. data_format="NCHW"):
  140. super(SELayer, self).__init__()
  141. self.data_format = data_format
  142. self.pool2d_gap = AdaptiveAvgPool2D(1, data_format=self.data_format)
  143. self._num_channels = num_channels
  144. med_ch = int(num_channels / reduction_ratio)
  145. stdv = 1.0 / math.sqrt(num_channels * 1.0)
  146. self.squeeze = Linear(
  147. num_channels,
  148. med_ch,
  149. weight_attr=ParamAttr(
  150. initializer=Uniform(-stdv, stdv), name=name + "_sqz_weights"),
  151. bias_attr=ParamAttr(name=name + '_sqz_offset'))
  152. self.relu = nn.ReLU()
  153. stdv = 1.0 / math.sqrt(med_ch * 1.0)
  154. self.excitation = Linear(
  155. med_ch,
  156. num_filters,
  157. weight_attr=ParamAttr(
  158. initializer=Uniform(-stdv, stdv), name=name + "_exc_weights"),
  159. bias_attr=ParamAttr(name=name + '_exc_offset'))
  160. self.sigmoid = nn.Sigmoid()
  161. def forward(self, input):
  162. pool = self.pool2d_gap(input)
  163. if self.data_format == "NHWC":
  164. pool = paddle.squeeze(pool, axis=[1, 2])
  165. else:
  166. pool = paddle.squeeze(pool, axis=[2, 3])
  167. squeeze = self.squeeze(pool)
  168. squeeze = self.relu(squeeze)
  169. excitation = self.excitation(squeeze)
  170. excitation = self.sigmoid(excitation)
  171. if self.data_format == "NHWC":
  172. excitation = paddle.unsqueeze(excitation, axis=[1, 2])
  173. else:
  174. excitation = paddle.unsqueeze(excitation, axis=[2, 3])
  175. out = input * excitation
  176. return out
  177. class ResNeXt(nn.Layer):
  178. def __init__(self,
  179. layers=50,
  180. class_num=1000,
  181. cardinality=32,
  182. input_image_channel=3,
  183. data_format="NCHW"):
  184. super(ResNeXt, self).__init__()
  185. self.layers = layers
  186. self.cardinality = cardinality
  187. self.reduction_ratio = 16
  188. self.data_format = data_format
  189. self.input_image_channel = input_image_channel
  190. supported_layers = [50, 101, 152]
  191. assert layers in supported_layers, \
  192. "supported layers are {} but input layer is {}".format(
  193. supported_layers, layers)
  194. supported_cardinality = [32, 64]
  195. assert cardinality in supported_cardinality, \
  196. "supported cardinality is {} but input cardinality is {}" \
  197. .format(supported_cardinality, cardinality)
  198. if layers == 50:
  199. depth = [3, 4, 6, 3]
  200. elif layers == 101:
  201. depth = [3, 4, 23, 3]
  202. elif layers == 152:
  203. depth = [3, 8, 36, 3]
  204. num_channels = [64, 256, 512, 1024]
  205. num_filters = [128, 256, 512,
  206. 1024] if cardinality == 32 else [256, 512, 1024, 2048]
  207. if layers < 152:
  208. self.conv = ConvBNLayer(
  209. num_channels=self.input_image_channel,
  210. num_filters=64,
  211. filter_size=7,
  212. stride=2,
  213. act='relu',
  214. name="conv1",
  215. data_format=self.data_format)
  216. else:
  217. self.conv1_1 = ConvBNLayer(
  218. num_channels=self.input_image_channel,
  219. num_filters=64,
  220. filter_size=3,
  221. stride=2,
  222. act='relu',
  223. name="conv1",
  224. data_format=self.data_format)
  225. self.conv1_2 = ConvBNLayer(
  226. num_channels=64,
  227. num_filters=64,
  228. filter_size=3,
  229. stride=1,
  230. act='relu',
  231. name="conv2",
  232. data_format=self.data_format)
  233. self.conv1_3 = ConvBNLayer(
  234. num_channels=64,
  235. num_filters=128,
  236. filter_size=3,
  237. stride=1,
  238. act='relu',
  239. name="conv3",
  240. data_format=self.data_format)
  241. self.pool2d_max = MaxPool2D(
  242. kernel_size=3, stride=2, padding=1, data_format=self.data_format)
  243. self.block_list = []
  244. n = 1 if layers == 50 or layers == 101 else 3
  245. for block in range(len(depth)):
  246. n += 1
  247. shortcut = False
  248. for i in range(depth[block]):
  249. bottleneck_block = self.add_sublayer(
  250. 'bb_%d_%d' % (block, i),
  251. BottleneckBlock(
  252. num_channels=num_channels[block] if i == 0 else
  253. num_filters[block] * int(64 // self.cardinality),
  254. num_filters=num_filters[block],
  255. stride=2 if i == 0 and block != 0 else 1,
  256. cardinality=self.cardinality,
  257. reduction_ratio=self.reduction_ratio,
  258. shortcut=shortcut,
  259. if_first=block == 0,
  260. name=str(n) + '_' + str(i + 1),
  261. data_format=self.data_format))
  262. self.block_list.append(bottleneck_block)
  263. shortcut = True
  264. self.pool2d_avg = AdaptiveAvgPool2D(1, data_format=self.data_format)
  265. self.pool2d_avg_channels = num_channels[-1] * 2
  266. stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0)
  267. self.out = Linear(
  268. self.pool2d_avg_channels,
  269. class_num,
  270. weight_attr=ParamAttr(
  271. initializer=Uniform(-stdv, stdv), name="fc6_weights"),
  272. bias_attr=ParamAttr(name="fc6_offset"))
  273. def forward(self, inputs):
  274. with paddle.static.amp.fp16_guard():
  275. if self.data_format == "NHWC":
  276. inputs = paddle.tensor.transpose(inputs, [0, 2, 3, 1])
  277. inputs.stop_gradient = True
  278. if self.layers < 152:
  279. y = self.conv(inputs)
  280. else:
  281. y = self.conv1_1(inputs)
  282. y = self.conv1_2(y)
  283. y = self.conv1_3(y)
  284. y = self.pool2d_max(y)
  285. for i, block in enumerate(self.block_list):
  286. y = block(y)
  287. y = self.pool2d_avg(y)
  288. y = paddle.reshape(y, shape=[-1, self.pool2d_avg_channels])
  289. y = self.out(y)
  290. return y
  291. def _load_pretrained(pretrained, model, model_url, use_ssld=False):
  292. if pretrained is False:
  293. pass
  294. elif pretrained is True:
  295. load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld)
  296. elif isinstance(pretrained, str):
  297. load_dygraph_pretrain(model, pretrained)
  298. else:
  299. raise RuntimeError(
  300. "pretrained type is not available. Please use `string` or `boolean` type."
  301. )
  302. def SE_ResNeXt50_32x4d(pretrained=False, use_ssld=False, **kwargs):
  303. model = ResNeXt(layers=50, cardinality=32, **kwargs)
  304. _load_pretrained(
  305. pretrained, model, MODEL_URLS["SE_ResNeXt50_32x4d"], use_ssld=use_ssld)
  306. return model
  307. def SE_ResNeXt101_32x4d(pretrained=False, use_ssld=False, **kwargs):
  308. model = ResNeXt(layers=101, cardinality=32, **kwargs)
  309. _load_pretrained(
  310. pretrained,
  311. model,
  312. MODEL_URLS["SE_ResNeXt101_32x4d"],
  313. use_ssld=use_ssld)
  314. return model
  315. def SE_ResNeXt152_64x4d(pretrained=False, use_ssld=False, **kwargs):
  316. model = ResNeXt(layers=152, cardinality=64, **kwargs)
  317. _load_pretrained(
  318. pretrained,
  319. model,
  320. MODEL_URLS["SE_ResNeXt152_64x4d"],
  321. use_ssld=use_ssld)
  322. return model