inception_v4.py 15 KB

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