xception.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import paddle
  2. from paddle import ParamAttr
  3. import paddle.nn as nn
  4. import paddle.nn.functional as F
  5. from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
  6. from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
  7. from paddle.nn.initializer import Uniform
  8. import math
  9. import sys
  10. from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url
  11. MODEL_URLS = {
  12. "Xception41":
  13. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Xception41_pretrained.pdparams",
  14. "Xception65":
  15. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Xception65_pretrained.pdparams",
  16. "Xception71":
  17. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Xception71_pretrained.pdparams"
  18. }
  19. __all__ = list(MODEL_URLS.keys())
  20. class ConvBNLayer(nn.Layer):
  21. def __init__(self,
  22. num_channels,
  23. num_filters,
  24. filter_size,
  25. stride=1,
  26. groups=1,
  27. act=None,
  28. name=None):
  29. super(ConvBNLayer, self).__init__()
  30. self._conv = Conv2D(
  31. in_channels=num_channels,
  32. out_channels=num_filters,
  33. kernel_size=filter_size,
  34. stride=stride,
  35. padding=(filter_size - 1) // 2,
  36. groups=groups,
  37. weight_attr=ParamAttr(name=name + "_weights"),
  38. bias_attr=False)
  39. bn_name = "bn_" + name
  40. self._batch_norm = BatchNorm(
  41. num_filters,
  42. act=act,
  43. param_attr=ParamAttr(name=bn_name + "_scale"),
  44. bias_attr=ParamAttr(name=bn_name + "_offset"),
  45. moving_mean_name=bn_name + '_mean',
  46. moving_variance_name=bn_name + '_variance')
  47. def forward(self, inputs):
  48. y = self._conv(inputs)
  49. y = self._batch_norm(y)
  50. return y
  51. class SeparableConv(nn.Layer):
  52. def __init__(self, input_channels, output_channels, stride=1, name=None):
  53. super(SeparableConv, self).__init__()
  54. self._pointwise_conv = ConvBNLayer(
  55. input_channels, output_channels, 1, name=name + "_sep")
  56. self._depthwise_conv = ConvBNLayer(
  57. output_channels,
  58. output_channels,
  59. 3,
  60. stride=stride,
  61. groups=output_channels,
  62. name=name + "_dw")
  63. def forward(self, inputs):
  64. x = self._pointwise_conv(inputs)
  65. x = self._depthwise_conv(x)
  66. return x
  67. class EntryFlowBottleneckBlock(nn.Layer):
  68. def __init__(self,
  69. input_channels,
  70. output_channels,
  71. stride=2,
  72. name=None,
  73. relu_first=False):
  74. super(EntryFlowBottleneckBlock, self).__init__()
  75. self.relu_first = relu_first
  76. self._short = Conv2D(
  77. in_channels=input_channels,
  78. out_channels=output_channels,
  79. kernel_size=1,
  80. stride=stride,
  81. padding=0,
  82. weight_attr=ParamAttr(name + "_branch1_weights"),
  83. bias_attr=False)
  84. self._conv1 = SeparableConv(
  85. input_channels,
  86. output_channels,
  87. stride=1,
  88. name=name + "_branch2a_weights")
  89. self._conv2 = SeparableConv(
  90. output_channels,
  91. output_channels,
  92. stride=1,
  93. name=name + "_branch2b_weights")
  94. self._pool = MaxPool2D(kernel_size=3, stride=stride, padding=1)
  95. def forward(self, inputs):
  96. conv0 = inputs
  97. short = self._short(inputs)
  98. if self.relu_first:
  99. conv0 = F.relu(conv0)
  100. conv1 = self._conv1(conv0)
  101. conv2 = F.relu(conv1)
  102. conv2 = self._conv2(conv2)
  103. pool = self._pool(conv2)
  104. return paddle.add(x=short, y=pool)
  105. class EntryFlow(nn.Layer):
  106. def __init__(self, block_num=3):
  107. super(EntryFlow, self).__init__()
  108. name = "entry_flow"
  109. self.block_num = block_num
  110. self._conv1 = ConvBNLayer(
  111. 3, 32, 3, stride=2, act="relu", name=name + "_conv1")
  112. self._conv2 = ConvBNLayer(32, 64, 3, act="relu", name=name + "_conv2")
  113. if block_num == 3:
  114. self._conv_0 = EntryFlowBottleneckBlock(
  115. 64, 128, stride=2, name=name + "_0", relu_first=False)
  116. self._conv_1 = EntryFlowBottleneckBlock(
  117. 128, 256, stride=2, name=name + "_1", relu_first=True)
  118. self._conv_2 = EntryFlowBottleneckBlock(
  119. 256, 728, stride=2, name=name + "_2", relu_first=True)
  120. elif block_num == 5:
  121. self._conv_0 = EntryFlowBottleneckBlock(
  122. 64, 128, stride=2, name=name + "_0", relu_first=False)
  123. self._conv_1 = EntryFlowBottleneckBlock(
  124. 128, 256, stride=1, name=name + "_1", relu_first=True)
  125. self._conv_2 = EntryFlowBottleneckBlock(
  126. 256, 256, stride=2, name=name + "_2", relu_first=True)
  127. self._conv_3 = EntryFlowBottleneckBlock(
  128. 256, 728, stride=1, name=name + "_3", relu_first=True)
  129. self._conv_4 = EntryFlowBottleneckBlock(
  130. 728, 728, stride=2, name=name + "_4", relu_first=True)
  131. else:
  132. sys.exit(-1)
  133. def forward(self, inputs):
  134. x = self._conv1(inputs)
  135. x = self._conv2(x)
  136. if self.block_num == 3:
  137. x = self._conv_0(x)
  138. x = self._conv_1(x)
  139. x = self._conv_2(x)
  140. elif self.block_num == 5:
  141. x = self._conv_0(x)
  142. x = self._conv_1(x)
  143. x = self._conv_2(x)
  144. x = self._conv_3(x)
  145. x = self._conv_4(x)
  146. return x
  147. class MiddleFlowBottleneckBlock(nn.Layer):
  148. def __init__(self, input_channels, output_channels, name):
  149. super(MiddleFlowBottleneckBlock, self).__init__()
  150. self._conv_0 = SeparableConv(
  151. input_channels,
  152. output_channels,
  153. stride=1,
  154. name=name + "_branch2a_weights")
  155. self._conv_1 = SeparableConv(
  156. output_channels,
  157. output_channels,
  158. stride=1,
  159. name=name + "_branch2b_weights")
  160. self._conv_2 = SeparableConv(
  161. output_channels,
  162. output_channels,
  163. stride=1,
  164. name=name + "_branch2c_weights")
  165. def forward(self, inputs):
  166. conv0 = F.relu(inputs)
  167. conv0 = self._conv_0(conv0)
  168. conv1 = F.relu(conv0)
  169. conv1 = self._conv_1(conv1)
  170. conv2 = F.relu(conv1)
  171. conv2 = self._conv_2(conv2)
  172. return paddle.add(x=inputs, y=conv2)
  173. class MiddleFlow(nn.Layer):
  174. def __init__(self, block_num=8):
  175. super(MiddleFlow, self).__init__()
  176. self.block_num = block_num
  177. self._conv_0 = MiddleFlowBottleneckBlock(
  178. 728, 728, name="middle_flow_0")
  179. self._conv_1 = MiddleFlowBottleneckBlock(
  180. 728, 728, name="middle_flow_1")
  181. self._conv_2 = MiddleFlowBottleneckBlock(
  182. 728, 728, name="middle_flow_2")
  183. self._conv_3 = MiddleFlowBottleneckBlock(
  184. 728, 728, name="middle_flow_3")
  185. self._conv_4 = MiddleFlowBottleneckBlock(
  186. 728, 728, name="middle_flow_4")
  187. self._conv_5 = MiddleFlowBottleneckBlock(
  188. 728, 728, name="middle_flow_5")
  189. self._conv_6 = MiddleFlowBottleneckBlock(
  190. 728, 728, name="middle_flow_6")
  191. self._conv_7 = MiddleFlowBottleneckBlock(
  192. 728, 728, name="middle_flow_7")
  193. if block_num == 16:
  194. self._conv_8 = MiddleFlowBottleneckBlock(
  195. 728, 728, name="middle_flow_8")
  196. self._conv_9 = MiddleFlowBottleneckBlock(
  197. 728, 728, name="middle_flow_9")
  198. self._conv_10 = MiddleFlowBottleneckBlock(
  199. 728, 728, name="middle_flow_10")
  200. self._conv_11 = MiddleFlowBottleneckBlock(
  201. 728, 728, name="middle_flow_11")
  202. self._conv_12 = MiddleFlowBottleneckBlock(
  203. 728, 728, name="middle_flow_12")
  204. self._conv_13 = MiddleFlowBottleneckBlock(
  205. 728, 728, name="middle_flow_13")
  206. self._conv_14 = MiddleFlowBottleneckBlock(
  207. 728, 728, name="middle_flow_14")
  208. self._conv_15 = MiddleFlowBottleneckBlock(
  209. 728, 728, name="middle_flow_15")
  210. def forward(self, inputs):
  211. x = self._conv_0(inputs)
  212. x = self._conv_1(x)
  213. x = self._conv_2(x)
  214. x = self._conv_3(x)
  215. x = self._conv_4(x)
  216. x = self._conv_5(x)
  217. x = self._conv_6(x)
  218. x = self._conv_7(x)
  219. if self.block_num == 16:
  220. x = self._conv_8(x)
  221. x = self._conv_9(x)
  222. x = self._conv_10(x)
  223. x = self._conv_11(x)
  224. x = self._conv_12(x)
  225. x = self._conv_13(x)
  226. x = self._conv_14(x)
  227. x = self._conv_15(x)
  228. return x
  229. class ExitFlowBottleneckBlock(nn.Layer):
  230. def __init__(self, input_channels, output_channels1, output_channels2,
  231. name):
  232. super(ExitFlowBottleneckBlock, self).__init__()
  233. self._short = Conv2D(
  234. in_channels=input_channels,
  235. out_channels=output_channels2,
  236. kernel_size=1,
  237. stride=2,
  238. padding=0,
  239. weight_attr=ParamAttr(name + "_branch1_weights"),
  240. bias_attr=False)
  241. self._conv_1 = SeparableConv(
  242. input_channels,
  243. output_channels1,
  244. stride=1,
  245. name=name + "_branch2a_weights")
  246. self._conv_2 = SeparableConv(
  247. output_channels1,
  248. output_channels2,
  249. stride=1,
  250. name=name + "_branch2b_weights")
  251. self._pool = MaxPool2D(kernel_size=3, stride=2, padding=1)
  252. def forward(self, inputs):
  253. short = self._short(inputs)
  254. conv0 = F.relu(inputs)
  255. conv1 = self._conv_1(conv0)
  256. conv2 = F.relu(conv1)
  257. conv2 = self._conv_2(conv2)
  258. pool = self._pool(conv2)
  259. return paddle.add(x=short, y=pool)
  260. class ExitFlow(nn.Layer):
  261. def __init__(self, class_num):
  262. super(ExitFlow, self).__init__()
  263. name = "exit_flow"
  264. self._conv_0 = ExitFlowBottleneckBlock(
  265. 728, 728, 1024, name=name + "_1")
  266. self._conv_1 = SeparableConv(1024, 1536, stride=1, name=name + "_2")
  267. self._conv_2 = SeparableConv(1536, 2048, stride=1, name=name + "_3")
  268. self._pool = AdaptiveAvgPool2D(1)
  269. stdv = 1.0 / math.sqrt(2048 * 1.0)
  270. self._out = Linear(
  271. 2048,
  272. class_num,
  273. weight_attr=ParamAttr(
  274. name="fc_weights", initializer=Uniform(-stdv, stdv)),
  275. bias_attr=ParamAttr(name="fc_offset"))
  276. def forward(self, inputs):
  277. conv0 = self._conv_0(inputs)
  278. conv1 = self._conv_1(conv0)
  279. conv1 = F.relu(conv1)
  280. conv2 = self._conv_2(conv1)
  281. conv2 = F.relu(conv2)
  282. pool = self._pool(conv2)
  283. pool = paddle.flatten(pool, start_axis=1, stop_axis=-1)
  284. out = self._out(pool)
  285. return out
  286. class Xception(nn.Layer):
  287. def __init__(self,
  288. entry_flow_block_num=3,
  289. middle_flow_block_num=8,
  290. class_num=1000):
  291. super(Xception, self).__init__()
  292. self.entry_flow_block_num = entry_flow_block_num
  293. self.middle_flow_block_num = middle_flow_block_num
  294. self._entry_flow = EntryFlow(entry_flow_block_num)
  295. self._middle_flow = MiddleFlow(middle_flow_block_num)
  296. self._exit_flow = ExitFlow(class_num)
  297. def forward(self, inputs):
  298. x = self._entry_flow(inputs)
  299. x = self._middle_flow(x)
  300. x = self._exit_flow(x)
  301. return x
  302. def _load_pretrained(pretrained, model, model_url, use_ssld=False):
  303. if pretrained is False:
  304. pass
  305. elif pretrained is True:
  306. load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld)
  307. elif isinstance(pretrained, str):
  308. load_dygraph_pretrain(model, pretrained)
  309. else:
  310. raise RuntimeError(
  311. "pretrained type is not available. Please use `string` or `boolean` type."
  312. )
  313. def Xception41(pretrained=False, use_ssld=False, **kwargs):
  314. model = Xception(entry_flow_block_num=3, middle_flow_block_num=8, **kwargs)
  315. _load_pretrained(
  316. pretrained, model, MODEL_URLS["Xception41"], use_ssld=use_ssld)
  317. return model
  318. def Xception65(pretrained=False, use_ssld=False, **kwargs):
  319. model = Xception(
  320. entry_flow_block_num=3, middle_flow_block_num=16, **kwargs)
  321. _load_pretrained(
  322. pretrained, model, MODEL_URLS["Xception65"], use_ssld=use_ssld)
  323. return model
  324. def Xception71(pretrained=False, use_ssld=False, **kwargs):
  325. model = Xception(
  326. entry_flow_block_num=5, middle_flow_block_num=16, **kwargs)
  327. _load_pretrained(
  328. pretrained, model, MODEL_URLS["Xception71"], use_ssld=use_ssld)
  329. return model