se_resnext.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. __all__ = ["SE_ResNeXt50_32x4d", "SE_ResNeXt101_32x4d", "SE_ResNeXt152_64x4d"]
  27. class ConvBNLayer(nn.Layer):
  28. def __init__(self,
  29. num_channels,
  30. num_filters,
  31. filter_size,
  32. stride=1,
  33. groups=1,
  34. act=None,
  35. name=None):
  36. super(ConvBNLayer, self).__init__()
  37. self._conv = Conv2D(
  38. in_channels=num_channels,
  39. out_channels=num_filters,
  40. kernel_size=filter_size,
  41. stride=stride,
  42. padding=(filter_size - 1) // 2,
  43. groups=groups,
  44. weight_attr=ParamAttr(name=name + "_weights"),
  45. bias_attr=False)
  46. bn_name = name + '_bn'
  47. self._batch_norm = BatchNorm(
  48. num_filters,
  49. act=act,
  50. param_attr=ParamAttr(name=bn_name + '_scale'),
  51. bias_attr=ParamAttr(bn_name + '_offset'),
  52. moving_mean_name=bn_name + '_mean',
  53. moving_variance_name=bn_name + '_variance')
  54. def forward(self, inputs):
  55. y = self._conv(inputs)
  56. y = self._batch_norm(y)
  57. return y
  58. class BottleneckBlock(nn.Layer):
  59. def __init__(self,
  60. num_channels,
  61. num_filters,
  62. stride,
  63. cardinality,
  64. reduction_ratio,
  65. shortcut=True,
  66. if_first=False,
  67. name=None):
  68. super(BottleneckBlock, self).__init__()
  69. self.conv0 = ConvBNLayer(
  70. num_channels=num_channels,
  71. num_filters=num_filters,
  72. filter_size=1,
  73. act='relu',
  74. name='conv' + name + '_x1')
  75. self.conv1 = ConvBNLayer(
  76. num_channels=num_filters,
  77. num_filters=num_filters,
  78. filter_size=3,
  79. groups=cardinality,
  80. stride=stride,
  81. act='relu',
  82. name='conv' + name + '_x2')
  83. self.conv2 = ConvBNLayer(
  84. num_channels=num_filters,
  85. num_filters=num_filters * 2 if cardinality == 32 else num_filters,
  86. filter_size=1,
  87. act=None,
  88. name='conv' + name + '_x3')
  89. self.scale = SELayer(
  90. num_channels=num_filters * 2 if cardinality == 32 else num_filters,
  91. num_filters=num_filters * 2 if cardinality == 32 else num_filters,
  92. reduction_ratio=reduction_ratio,
  93. name='fc' + name)
  94. if not shortcut:
  95. self.short = ConvBNLayer(
  96. num_channels=num_channels,
  97. num_filters=num_filters * 2
  98. if cardinality == 32 else num_filters,
  99. filter_size=1,
  100. stride=stride,
  101. name='conv' + name + '_prj')
  102. self.shortcut = shortcut
  103. def forward(self, inputs):
  104. y = self.conv0(inputs)
  105. conv1 = self.conv1(y)
  106. conv2 = self.conv2(conv1)
  107. scale = self.scale(conv2)
  108. if self.shortcut:
  109. short = inputs
  110. else:
  111. short = self.short(inputs)
  112. y = paddle.add(x=short, y=scale)
  113. y = F.relu(y)
  114. return y
  115. class SELayer(nn.Layer):
  116. def __init__(self, num_channels, num_filters, reduction_ratio, name=None):
  117. super(SELayer, self).__init__()
  118. self.pool2d_gap = AdaptiveAvgPool2D(1)
  119. self._num_channels = num_channels
  120. med_ch = int(num_channels / reduction_ratio)
  121. stdv = 1.0 / math.sqrt(num_channels * 1.0)
  122. self.squeeze = Linear(
  123. num_channels,
  124. med_ch,
  125. weight_attr=ParamAttr(
  126. initializer=Uniform(-stdv, stdv), name=name + "_sqz_weights"),
  127. bias_attr=ParamAttr(name=name + '_sqz_offset'))
  128. self.relu = nn.ReLU()
  129. stdv = 1.0 / math.sqrt(med_ch * 1.0)
  130. self.excitation = Linear(
  131. med_ch,
  132. num_filters,
  133. weight_attr=ParamAttr(
  134. initializer=Uniform(-stdv, stdv), name=name + "_exc_weights"),
  135. bias_attr=ParamAttr(name=name + '_exc_offset'))
  136. self.sigmoid = nn.Sigmoid()
  137. def forward(self, input):
  138. pool = self.pool2d_gap(input)
  139. pool = paddle.squeeze(pool, axis=[2, 3])
  140. squeeze = self.squeeze(pool)
  141. squeeze = self.relu(squeeze)
  142. excitation = self.excitation(squeeze)
  143. excitation = self.sigmoid(excitation)
  144. excitation = paddle.unsqueeze(excitation, axis=[2, 3])
  145. out = input * excitation
  146. return out
  147. class ResNeXt(nn.Layer):
  148. def __init__(self, layers=50, class_dim=1000, cardinality=32):
  149. super(ResNeXt, self).__init__()
  150. self.layers = layers
  151. self.cardinality = cardinality
  152. self.reduction_ratio = 16
  153. supported_layers = [50, 101, 152]
  154. assert layers in supported_layers, \
  155. "supported layers are {} but input layer is {}".format(
  156. supported_layers, layers)
  157. supported_cardinality = [32, 64]
  158. assert cardinality in supported_cardinality, \
  159. "supported cardinality is {} but input cardinality is {}" \
  160. .format(supported_cardinality, cardinality)
  161. if layers == 50:
  162. depth = [3, 4, 6, 3]
  163. elif layers == 101:
  164. depth = [3, 4, 23, 3]
  165. elif layers == 152:
  166. depth = [3, 8, 36, 3]
  167. num_channels = [64, 256, 512, 1024]
  168. num_filters = [128, 256, 512,
  169. 1024] if cardinality == 32 else [256, 512, 1024, 2048]
  170. if layers < 152:
  171. self.conv = ConvBNLayer(
  172. num_channels=3,
  173. num_filters=64,
  174. filter_size=7,
  175. stride=2,
  176. act='relu',
  177. name="conv1")
  178. else:
  179. self.conv1_1 = ConvBNLayer(
  180. num_channels=3,
  181. num_filters=64,
  182. filter_size=3,
  183. stride=2,
  184. act='relu',
  185. name="conv1")
  186. self.conv1_2 = ConvBNLayer(
  187. num_channels=64,
  188. num_filters=64,
  189. filter_size=3,
  190. stride=1,
  191. act='relu',
  192. name="conv2")
  193. self.conv1_3 = ConvBNLayer(
  194. num_channels=64,
  195. num_filters=128,
  196. filter_size=3,
  197. stride=1,
  198. act='relu',
  199. name="conv3")
  200. self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1)
  201. self.block_list = []
  202. n = 1 if layers == 50 or layers == 101 else 3
  203. for block in range(len(depth)):
  204. n += 1
  205. shortcut = False
  206. for i in range(depth[block]):
  207. bottleneck_block = self.add_sublayer(
  208. 'bb_%d_%d' % (block, i),
  209. BottleneckBlock(
  210. num_channels=num_channels[block] if i == 0 else
  211. num_filters[block] * int(64 // self.cardinality),
  212. num_filters=num_filters[block],
  213. stride=2 if i == 0 and block != 0 else 1,
  214. cardinality=self.cardinality,
  215. reduction_ratio=self.reduction_ratio,
  216. shortcut=shortcut,
  217. if_first=block == 0,
  218. name=str(n) + '_' + str(i + 1)))
  219. self.block_list.append(bottleneck_block)
  220. shortcut = True
  221. self.pool2d_avg = AdaptiveAvgPool2D(1)
  222. self.pool2d_avg_channels = num_channels[-1] * 2
  223. stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0)
  224. self.out = Linear(
  225. self.pool2d_avg_channels,
  226. class_dim,
  227. weight_attr=ParamAttr(
  228. initializer=Uniform(-stdv, stdv), name="fc6_weights"),
  229. bias_attr=ParamAttr(name="fc6_offset"))
  230. def forward(self, inputs):
  231. if self.layers < 152:
  232. y = self.conv(inputs)
  233. else:
  234. y = self.conv1_1(inputs)
  235. y = self.conv1_2(y)
  236. y = self.conv1_3(y)
  237. y = self.pool2d_max(y)
  238. for block in self.block_list:
  239. y = block(y)
  240. y = self.pool2d_avg(y)
  241. y = paddle.reshape(y, shape=[-1, self.pool2d_avg_channels])
  242. y = self.out(y)
  243. return y
  244. def SE_ResNeXt50_32x4d(**args):
  245. model = ResNeXt(layers=50, cardinality=32, **args)
  246. return model
  247. def SE_ResNeXt101_32x4d(**args):
  248. model = ResNeXt(layers=101, cardinality=32, **args)
  249. return model
  250. def SE_ResNeXt152_64x4d(**args):
  251. model = ResNeXt(layers=152, cardinality=64, **args)
  252. return model