inception_v4.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. import paddle
  15. from paddle import ParamAttr
  16. import paddle.nn as nn
  17. import paddle.nn.functional as F
  18. from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
  19. from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
  20. from paddle.nn.initializer import Uniform
  21. import math
  22. __all__ = ["InceptionV4"]
  23. class ConvBNLayer(nn.Layer):
  24. def __init__(self,
  25. num_channels,
  26. num_filters,
  27. filter_size,
  28. stride=1,
  29. padding=0,
  30. groups=1,
  31. act='relu',
  32. name=None):
  33. super(ConvBNLayer, self).__init__()
  34. self._conv = Conv2D(
  35. in_channels=num_channels,
  36. out_channels=num_filters,
  37. kernel_size=filter_size,
  38. stride=stride,
  39. padding=padding,
  40. groups=groups,
  41. weight_attr=ParamAttr(name=name + "_weights"),
  42. bias_attr=False)
  43. bn_name = name + "_bn"
  44. self._batch_norm = BatchNorm(
  45. num_filters,
  46. act=act,
  47. param_attr=ParamAttr(name=bn_name + "_scale"),
  48. bias_attr=ParamAttr(name=bn_name + "_offset"),
  49. moving_mean_name=bn_name + '_mean',
  50. moving_variance_name=bn_name + '_variance')
  51. def forward(self, inputs):
  52. y = self._conv(inputs)
  53. y = self._batch_norm(y)
  54. return y
  55. class InceptionStem(nn.Layer):
  56. def __init__(self):
  57. super(InceptionStem, self).__init__()
  58. self._conv_1 = ConvBNLayer(
  59. 3, 32, 3, stride=2, act="relu", name="conv1_3x3_s2")
  60. self._conv_2 = ConvBNLayer(32, 32, 3, act="relu", name="conv2_3x3_s1")
  61. self._conv_3 = ConvBNLayer(
  62. 32, 64, 3, padding=1, act="relu", name="conv3_3x3_s1")
  63. self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
  64. self._conv2 = ConvBNLayer(
  65. 64, 96, 3, stride=2, act="relu", name="inception_stem1_3x3_s2")
  66. self._conv1_1 = ConvBNLayer(
  67. 160, 64, 1, act="relu", name="inception_stem2_3x3_reduce")
  68. self._conv1_2 = ConvBNLayer(
  69. 64, 96, 3, act="relu", name="inception_stem2_3x3")
  70. self._conv2_1 = ConvBNLayer(
  71. 160, 64, 1, act="relu", name="inception_stem2_1x7_reduce")
  72. self._conv2_2 = ConvBNLayer(
  73. 64,
  74. 64, (7, 1),
  75. padding=(3, 0),
  76. act="relu",
  77. name="inception_stem2_1x7")
  78. self._conv2_3 = ConvBNLayer(
  79. 64,
  80. 64, (1, 7),
  81. padding=(0, 3),
  82. act="relu",
  83. name="inception_stem2_7x1")
  84. self._conv2_4 = ConvBNLayer(
  85. 64, 96, 3, act="relu", name="inception_stem2_3x3_2")
  86. self._conv3 = ConvBNLayer(
  87. 192, 192, 3, stride=2, act="relu", name="inception_stem3_3x3_s2")
  88. def forward(self, inputs):
  89. conv = self._conv_1(inputs)
  90. conv = self._conv_2(conv)
  91. conv = self._conv_3(conv)
  92. pool1 = self._pool(conv)
  93. conv2 = self._conv2(conv)
  94. concat = paddle.concat([pool1, conv2], axis=1)
  95. conv1 = self._conv1_1(concat)
  96. conv1 = self._conv1_2(conv1)
  97. conv2 = self._conv2_1(concat)
  98. conv2 = self._conv2_2(conv2)
  99. conv2 = self._conv2_3(conv2)
  100. conv2 = self._conv2_4(conv2)
  101. concat = paddle.concat([conv1, conv2], axis=1)
  102. conv1 = self._conv3(concat)
  103. pool1 = self._pool(concat)
  104. concat = paddle.concat([conv1, pool1], axis=1)
  105. return concat
  106. class InceptionA(nn.Layer):
  107. def __init__(self, name):
  108. super(InceptionA, self).__init__()
  109. self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1)
  110. self._conv1 = ConvBNLayer(
  111. 384, 96, 1, act="relu", name="inception_a" + name + "_1x1")
  112. self._conv2 = ConvBNLayer(
  113. 384, 96, 1, act="relu", name="inception_a" + name + "_1x1_2")
  114. self._conv3_1 = ConvBNLayer(
  115. 384, 64, 1, act="relu", name="inception_a" + name + "_3x3_reduce")
  116. self._conv3_2 = ConvBNLayer(
  117. 64,
  118. 96,
  119. 3,
  120. padding=1,
  121. act="relu",
  122. name="inception_a" + name + "_3x3")
  123. self._conv4_1 = ConvBNLayer(
  124. 384,
  125. 64,
  126. 1,
  127. act="relu",
  128. name="inception_a" + name + "_3x3_2_reduce")
  129. self._conv4_2 = ConvBNLayer(
  130. 64,
  131. 96,
  132. 3,
  133. padding=1,
  134. act="relu",
  135. name="inception_a" + name + "_3x3_2")
  136. self._conv4_3 = ConvBNLayer(
  137. 96,
  138. 96,
  139. 3,
  140. padding=1,
  141. act="relu",
  142. name="inception_a" + name + "_3x3_3")
  143. def forward(self, inputs):
  144. pool1 = self._pool(inputs)
  145. conv1 = self._conv1(pool1)
  146. conv2 = self._conv2(inputs)
  147. conv3 = self._conv3_1(inputs)
  148. conv3 = self._conv3_2(conv3)
  149. conv4 = self._conv4_1(inputs)
  150. conv4 = self._conv4_2(conv4)
  151. conv4 = self._conv4_3(conv4)
  152. concat = paddle.concat([conv1, conv2, conv3, conv4], axis=1)
  153. return concat
  154. class ReductionA(nn.Layer):
  155. def __init__(self):
  156. super(ReductionA, self).__init__()
  157. self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
  158. self._conv2 = ConvBNLayer(
  159. 384, 384, 3, stride=2, act="relu", name="reduction_a_3x3")
  160. self._conv3_1 = ConvBNLayer(
  161. 384, 192, 1, act="relu", name="reduction_a_3x3_2_reduce")
  162. self._conv3_2 = ConvBNLayer(
  163. 192, 224, 3, padding=1, act="relu", name="reduction_a_3x3_2")
  164. self._conv3_3 = ConvBNLayer(
  165. 224, 256, 3, stride=2, act="relu", name="reduction_a_3x3_3")
  166. def forward(self, inputs):
  167. pool1 = self._pool(inputs)
  168. conv2 = self._conv2(inputs)
  169. conv3 = self._conv3_1(inputs)
  170. conv3 = self._conv3_2(conv3)
  171. conv3 = self._conv3_3(conv3)
  172. concat = paddle.concat([pool1, conv2, conv3], axis=1)
  173. return concat
  174. class InceptionB(nn.Layer):
  175. def __init__(self, name=None):
  176. super(InceptionB, self).__init__()
  177. self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1)
  178. self._conv1 = ConvBNLayer(
  179. 1024, 128, 1, act="relu", name="inception_b" + name + "_1x1")
  180. self._conv2 = ConvBNLayer(
  181. 1024, 384, 1, act="relu", name="inception_b" + name + "_1x1_2")
  182. self._conv3_1 = ConvBNLayer(
  183. 1024,
  184. 192,
  185. 1,
  186. act="relu",
  187. name="inception_b" + name + "_1x7_reduce")
  188. self._conv3_2 = ConvBNLayer(
  189. 192,
  190. 224, (1, 7),
  191. padding=(0, 3),
  192. act="relu",
  193. name="inception_b" + name + "_1x7")
  194. self._conv3_3 = ConvBNLayer(
  195. 224,
  196. 256, (7, 1),
  197. padding=(3, 0),
  198. act="relu",
  199. name="inception_b" + name + "_7x1")
  200. self._conv4_1 = ConvBNLayer(
  201. 1024,
  202. 192,
  203. 1,
  204. act="relu",
  205. name="inception_b" + name + "_7x1_2_reduce")
  206. self._conv4_2 = ConvBNLayer(
  207. 192,
  208. 192, (1, 7),
  209. padding=(0, 3),
  210. act="relu",
  211. name="inception_b" + name + "_1x7_2")
  212. self._conv4_3 = ConvBNLayer(
  213. 192,
  214. 224, (7, 1),
  215. padding=(3, 0),
  216. act="relu",
  217. name="inception_b" + name + "_7x1_2")
  218. self._conv4_4 = ConvBNLayer(
  219. 224,
  220. 224, (1, 7),
  221. padding=(0, 3),
  222. act="relu",
  223. name="inception_b" + name + "_1x7_3")
  224. self._conv4_5 = ConvBNLayer(
  225. 224,
  226. 256, (7, 1),
  227. padding=(3, 0),
  228. act="relu",
  229. name="inception_b" + name + "_7x1_3")
  230. def forward(self, inputs):
  231. pool1 = self._pool(inputs)
  232. conv1 = self._conv1(pool1)
  233. conv2 = self._conv2(inputs)
  234. conv3 = self._conv3_1(inputs)
  235. conv3 = self._conv3_2(conv3)
  236. conv3 = self._conv3_3(conv3)
  237. conv4 = self._conv4_1(inputs)
  238. conv4 = self._conv4_2(conv4)
  239. conv4 = self._conv4_3(conv4)
  240. conv4 = self._conv4_4(conv4)
  241. conv4 = self._conv4_5(conv4)
  242. concat = paddle.concat([conv1, conv2, conv3, conv4], axis=1)
  243. return concat
  244. class ReductionB(nn.Layer):
  245. def __init__(self):
  246. super(ReductionB, self).__init__()
  247. self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
  248. self._conv2_1 = ConvBNLayer(
  249. 1024, 192, 1, act="relu", name="reduction_b_3x3_reduce")
  250. self._conv2_2 = ConvBNLayer(
  251. 192, 192, 3, stride=2, act="relu", name="reduction_b_3x3")
  252. self._conv3_1 = ConvBNLayer(
  253. 1024, 256, 1, act="relu", name="reduction_b_1x7_reduce")
  254. self._conv3_2 = ConvBNLayer(
  255. 256,
  256. 256, (1, 7),
  257. padding=(0, 3),
  258. act="relu",
  259. name="reduction_b_1x7")
  260. self._conv3_3 = ConvBNLayer(
  261. 256,
  262. 320, (7, 1),
  263. padding=(3, 0),
  264. act="relu",
  265. name="reduction_b_7x1")
  266. self._conv3_4 = ConvBNLayer(
  267. 320, 320, 3, stride=2, act="relu", name="reduction_b_3x3_2")
  268. def forward(self, inputs):
  269. pool1 = self._pool(inputs)
  270. conv2 = self._conv2_1(inputs)
  271. conv2 = self._conv2_2(conv2)
  272. conv3 = self._conv3_1(inputs)
  273. conv3 = self._conv3_2(conv3)
  274. conv3 = self._conv3_3(conv3)
  275. conv3 = self._conv3_4(conv3)
  276. concat = paddle.concat([pool1, conv2, conv3], axis=1)
  277. return concat
  278. class InceptionC(nn.Layer):
  279. def __init__(self, name=None):
  280. super(InceptionC, self).__init__()
  281. self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1)
  282. self._conv1 = ConvBNLayer(
  283. 1536, 256, 1, act="relu", name="inception_c" + name + "_1x1")
  284. self._conv2 = ConvBNLayer(
  285. 1536, 256, 1, act="relu", name="inception_c" + name + "_1x1_2")
  286. self._conv3_0 = ConvBNLayer(
  287. 1536, 384, 1, act="relu", name="inception_c" + name + "_1x1_3")
  288. self._conv3_1 = ConvBNLayer(
  289. 384,
  290. 256, (1, 3),
  291. padding=(0, 1),
  292. act="relu",
  293. name="inception_c" + name + "_1x3")
  294. self._conv3_2 = ConvBNLayer(
  295. 384,
  296. 256, (3, 1),
  297. padding=(1, 0),
  298. act="relu",
  299. name="inception_c" + name + "_3x1")
  300. self._conv4_0 = ConvBNLayer(
  301. 1536, 384, 1, act="relu", name="inception_c" + name + "_1x1_4")
  302. self._conv4_00 = ConvBNLayer(
  303. 384,
  304. 448, (1, 3),
  305. padding=(0, 1),
  306. act="relu",
  307. name="inception_c" + name + "_1x3_2")
  308. self._conv4_000 = ConvBNLayer(
  309. 448,
  310. 512, (3, 1),
  311. padding=(1, 0),
  312. act="relu",
  313. name="inception_c" + name + "_3x1_2")
  314. self._conv4_1 = ConvBNLayer(
  315. 512,
  316. 256, (1, 3),
  317. padding=(0, 1),
  318. act="relu",
  319. name="inception_c" + name + "_1x3_3")
  320. self._conv4_2 = ConvBNLayer(
  321. 512,
  322. 256, (3, 1),
  323. padding=(1, 0),
  324. act="relu",
  325. name="inception_c" + name + "_3x1_3")
  326. def forward(self, inputs):
  327. pool1 = self._pool(inputs)
  328. conv1 = self._conv1(pool1)
  329. conv2 = self._conv2(inputs)
  330. conv3 = self._conv3_0(inputs)
  331. conv3_1 = self._conv3_1(conv3)
  332. conv3_2 = self._conv3_2(conv3)
  333. conv4 = self._conv4_0(inputs)
  334. conv4 = self._conv4_00(conv4)
  335. conv4 = self._conv4_000(conv4)
  336. conv4_1 = self._conv4_1(conv4)
  337. conv4_2 = self._conv4_2(conv4)
  338. concat = paddle.concat(
  339. [conv1, conv2, conv3_1, conv3_2, conv4_1, conv4_2], axis=1)
  340. return concat
  341. class InceptionV4DY(nn.Layer):
  342. def __init__(self, class_dim=1000):
  343. super(InceptionV4DY, self).__init__()
  344. self._inception_stem = InceptionStem()
  345. self._inceptionA_1 = InceptionA(name="1")
  346. self._inceptionA_2 = InceptionA(name="2")
  347. self._inceptionA_3 = InceptionA(name="3")
  348. self._inceptionA_4 = InceptionA(name="4")
  349. self._reductionA = ReductionA()
  350. self._inceptionB_1 = InceptionB(name="1")
  351. self._inceptionB_2 = InceptionB(name="2")
  352. self._inceptionB_3 = InceptionB(name="3")
  353. self._inceptionB_4 = InceptionB(name="4")
  354. self._inceptionB_5 = InceptionB(name="5")
  355. self._inceptionB_6 = InceptionB(name="6")
  356. self._inceptionB_7 = InceptionB(name="7")
  357. self._reductionB = ReductionB()
  358. self._inceptionC_1 = InceptionC(name="1")
  359. self._inceptionC_2 = InceptionC(name="2")
  360. self._inceptionC_3 = InceptionC(name="3")
  361. self.avg_pool = AdaptiveAvgPool2D(1)
  362. self._drop = Dropout(p=0.2, mode="downscale_in_infer")
  363. stdv = 1.0 / math.sqrt(1536 * 1.0)
  364. self.out = Linear(
  365. 1536,
  366. class_dim,
  367. weight_attr=ParamAttr(
  368. initializer=Uniform(-stdv, stdv), name="final_fc_weights"),
  369. bias_attr=ParamAttr(name="final_fc_offset"))
  370. def forward(self, inputs):
  371. x = self._inception_stem(inputs)
  372. x = self._inceptionA_1(x)
  373. x = self._inceptionA_2(x)
  374. x = self._inceptionA_3(x)
  375. x = self._inceptionA_4(x)
  376. x = self._reductionA(x)
  377. x = self._inceptionB_1(x)
  378. x = self._inceptionB_2(x)
  379. x = self._inceptionB_3(x)
  380. x = self._inceptionB_4(x)
  381. x = self._inceptionB_5(x)
  382. x = self._inceptionB_6(x)
  383. x = self._inceptionB_7(x)
  384. x = self._reductionB(x)
  385. x = self._inceptionC_1(x)
  386. x = self._inceptionC_2(x)
  387. x = self._inceptionC_3(x)
  388. x = self.avg_pool(x)
  389. x = paddle.squeeze(x, axis=[2, 3])
  390. x = self._drop(x)
  391. x = self.out(x)
  392. return x
  393. def InceptionV4(**args):
  394. model = InceptionV4DY(**args)
  395. return model