resnext_vd.py 10 KB

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