test_ops.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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 print_function
  15. import os, sys
  16. # add python path of PadleDetection to sys.path
  17. parent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 4)))
  18. if parent_path not in sys.path:
  19. sys.path.append(parent_path)
  20. import unittest
  21. import numpy as np
  22. import paddle
  23. import paddle.fluid as fluid
  24. from paddle.fluid.framework import Program, program_guard
  25. from paddle.fluid.dygraph import base
  26. import paddlex.ppdet.modeling.ops as ops
  27. from paddlex.ppdet.modeling.tests.test_base import LayerTest
  28. def make_rois(h, w, rois_num, output_size):
  29. rois = np.zeros((0, 4)).astype('float32')
  30. for roi_num in rois_num:
  31. roi = np.zeros((roi_num, 4)).astype('float32')
  32. roi[:, 0] = np.random.randint(0, h - output_size[0], size=roi_num)
  33. roi[:, 1] = np.random.randint(0, w - output_size[1], size=roi_num)
  34. roi[:, 2] = np.random.randint(roi[:, 0] + output_size[0], h)
  35. roi[:, 3] = np.random.randint(roi[:, 1] + output_size[1], w)
  36. rois = np.vstack((rois, roi))
  37. return rois
  38. def softmax(x):
  39. # clip to shiftx, otherwise, when calc loss with
  40. # log(exp(shiftx)), may get log(0)=INF
  41. shiftx = (x - np.max(x)).clip(-64.)
  42. exps = np.exp(shiftx)
  43. return exps / np.sum(exps)
  44. class TestCollectFpnProposals(LayerTest):
  45. def test_collect_fpn_proposals(self):
  46. multi_bboxes_np = []
  47. multi_scores_np = []
  48. rois_num_per_level_np = []
  49. for i in range(4):
  50. bboxes_np = np.random.rand(5, 4).astype('float32')
  51. scores_np = np.random.rand(5, 1).astype('float32')
  52. rois_num = np.array([2, 3]).astype('int32')
  53. multi_bboxes_np.append(bboxes_np)
  54. multi_scores_np.append(scores_np)
  55. rois_num_per_level_np.append(rois_num)
  56. with self.static_graph():
  57. multi_bboxes = []
  58. multi_scores = []
  59. rois_num_per_level = []
  60. for i in range(4):
  61. bboxes = paddle.static.data(
  62. name='rois' + str(i),
  63. shape=[5, 4],
  64. dtype='float32',
  65. lod_level=1)
  66. scores = paddle.static.data(
  67. name='scores' + str(i),
  68. shape=[5, 1],
  69. dtype='float32',
  70. lod_level=1)
  71. rois_num = paddle.static.data(
  72. name='rois_num' + str(i), shape=[None], dtype='int32')
  73. multi_bboxes.append(bboxes)
  74. multi_scores.append(scores)
  75. rois_num_per_level.append(rois_num)
  76. fpn_rois, rois_num = ops.collect_fpn_proposals(
  77. multi_bboxes,
  78. multi_scores,
  79. 2,
  80. 5,
  81. 10,
  82. rois_num_per_level=rois_num_per_level)
  83. feed = {}
  84. for i in range(4):
  85. feed['rois' + str(i)] = multi_bboxes_np[i]
  86. feed['scores' + str(i)] = multi_scores_np[i]
  87. feed['rois_num' + str(i)] = rois_num_per_level_np[i]
  88. fpn_rois_stat, rois_num_stat = self.get_static_graph_result(
  89. feed=feed, fetch_list=[fpn_rois, rois_num], with_lod=True)
  90. fpn_rois_stat = np.array(fpn_rois_stat)
  91. rois_num_stat = np.array(rois_num_stat)
  92. with self.dynamic_graph():
  93. multi_bboxes_dy = []
  94. multi_scores_dy = []
  95. rois_num_per_level_dy = []
  96. for i in range(4):
  97. bboxes_dy = base.to_variable(multi_bboxes_np[i])
  98. scores_dy = base.to_variable(multi_scores_np[i])
  99. rois_num_dy = base.to_variable(rois_num_per_level_np[i])
  100. multi_bboxes_dy.append(bboxes_dy)
  101. multi_scores_dy.append(scores_dy)
  102. rois_num_per_level_dy.append(rois_num_dy)
  103. fpn_rois_dy, rois_num_dy = ops.collect_fpn_proposals(
  104. multi_bboxes_dy,
  105. multi_scores_dy,
  106. 2,
  107. 5,
  108. 10,
  109. rois_num_per_level=rois_num_per_level_dy)
  110. fpn_rois_dy = fpn_rois_dy.numpy()
  111. rois_num_dy = rois_num_dy.numpy()
  112. self.assertTrue(np.array_equal(fpn_rois_stat, fpn_rois_dy))
  113. self.assertTrue(np.array_equal(rois_num_stat, rois_num_dy))
  114. def test_collect_fpn_proposals_error(self):
  115. def generate_input(bbox_type, score_type, name):
  116. multi_bboxes = []
  117. multi_scores = []
  118. for i in range(4):
  119. bboxes = paddle.static.data(
  120. name='rois' + name + str(i),
  121. shape=[10, 4],
  122. dtype=bbox_type,
  123. lod_level=1)
  124. scores = paddle.static.data(
  125. name='scores' + name + str(i),
  126. shape=[10, 1],
  127. dtype=score_type,
  128. lod_level=1)
  129. multi_bboxes.append(bboxes)
  130. multi_scores.append(scores)
  131. return multi_bboxes, multi_scores
  132. with self.static_graph():
  133. bbox1 = paddle.static.data(
  134. name='rois', shape=[5, 10, 4], dtype='float32', lod_level=1)
  135. score1 = paddle.static.data(
  136. name='scores', shape=[5, 10, 1], dtype='float32', lod_level=1)
  137. bbox2, score2 = generate_input('int32', 'float32', '2')
  138. self.assertRaises(
  139. TypeError,
  140. ops.collect_fpn_proposals,
  141. multi_rois=bbox1,
  142. multi_scores=score1,
  143. min_level=2,
  144. max_level=5,
  145. post_nms_top_n=2000)
  146. self.assertRaises(
  147. TypeError,
  148. ops.collect_fpn_proposals,
  149. multi_rois=bbox2,
  150. multi_scores=score2,
  151. min_level=2,
  152. max_level=5,
  153. post_nms_top_n=2000)
  154. paddle.disable_static()
  155. class TestDistributeFpnProposals(LayerTest):
  156. def test_distribute_fpn_proposals(self):
  157. rois_np = np.random.rand(10, 4).astype('float32')
  158. rois_num_np = np.array([4, 6]).astype('int32')
  159. with self.static_graph():
  160. rois = paddle.static.data(
  161. name='rois', shape=[10, 4], dtype='float32')
  162. rois_num = paddle.static.data(
  163. name='rois_num', shape=[None], dtype='int32')
  164. multi_rois, restore_ind, rois_num_per_level = ops.distribute_fpn_proposals(
  165. fpn_rois=rois,
  166. min_level=2,
  167. max_level=5,
  168. refer_level=4,
  169. refer_scale=224,
  170. rois_num=rois_num)
  171. fetch_list = multi_rois + [restore_ind] + rois_num_per_level
  172. output_stat = self.get_static_graph_result(
  173. feed={'rois': rois_np,
  174. 'rois_num': rois_num_np},
  175. fetch_list=fetch_list,
  176. with_lod=True)
  177. output_stat_np = []
  178. for output in output_stat:
  179. output_np = np.array(output)
  180. if len(output_np) > 0:
  181. output_stat_np.append(output_np)
  182. with self.dynamic_graph():
  183. rois_dy = base.to_variable(rois_np)
  184. rois_num_dy = base.to_variable(rois_num_np)
  185. multi_rois_dy, restore_ind_dy, rois_num_per_level_dy = ops.distribute_fpn_proposals(
  186. fpn_rois=rois_dy,
  187. min_level=2,
  188. max_level=5,
  189. refer_level=4,
  190. refer_scale=224,
  191. rois_num=rois_num_dy)
  192. output_dy = multi_rois_dy + [restore_ind_dy
  193. ] + rois_num_per_level_dy
  194. output_dy_np = []
  195. for output in output_dy:
  196. output_np = output.numpy()
  197. if len(output_np) > 0:
  198. output_dy_np.append(output_np)
  199. for res_stat, res_dy in zip(output_stat_np, output_dy_np):
  200. self.assertTrue(np.array_equal(res_stat, res_dy))
  201. def test_distribute_fpn_proposals_error(self):
  202. with self.static_graph():
  203. fpn_rois = paddle.static.data(
  204. name='data_error', shape=[10, 4], dtype='int32', lod_level=1)
  205. self.assertRaises(
  206. TypeError,
  207. ops.distribute_fpn_proposals,
  208. fpn_rois=fpn_rois,
  209. min_level=2,
  210. max_level=5,
  211. refer_level=4,
  212. refer_scale=224)
  213. paddle.disable_static()
  214. class TestROIAlign(LayerTest):
  215. def test_roi_align(self):
  216. b, c, h, w = 2, 12, 20, 20
  217. inputs_np = np.random.rand(b, c, h, w).astype('float32')
  218. rois_num = [4, 6]
  219. output_size = (7, 7)
  220. rois_np = make_rois(h, w, rois_num, output_size)
  221. rois_num_np = np.array(rois_num).astype('int32')
  222. with self.static_graph():
  223. inputs = paddle.static.data(
  224. name='inputs', shape=[b, c, h, w], dtype='float32')
  225. rois = paddle.static.data(
  226. name='rois', shape=[10, 4], dtype='float32')
  227. rois_num = paddle.static.data(
  228. name='rois_num', shape=[None], dtype='int32')
  229. output = ops.roi_align(
  230. input=inputs,
  231. rois=rois,
  232. output_size=output_size,
  233. rois_num=rois_num)
  234. output_np, = self.get_static_graph_result(
  235. feed={
  236. 'inputs': inputs_np,
  237. 'rois': rois_np,
  238. 'rois_num': rois_num_np
  239. },
  240. fetch_list=output,
  241. with_lod=False)
  242. with self.dynamic_graph():
  243. inputs_dy = base.to_variable(inputs_np)
  244. rois_dy = base.to_variable(rois_np)
  245. rois_num_dy = base.to_variable(rois_num_np)
  246. output_dy = ops.roi_align(
  247. input=inputs_dy,
  248. rois=rois_dy,
  249. output_size=output_size,
  250. rois_num=rois_num_dy)
  251. output_dy_np = output_dy.numpy()
  252. self.assertTrue(np.array_equal(output_np, output_dy_np))
  253. def test_roi_align_error(self):
  254. with self.static_graph():
  255. inputs = paddle.static.data(
  256. name='inputs', shape=[2, 12, 20, 20], dtype='float32')
  257. rois = paddle.static.data(
  258. name='data_error', shape=[10, 4], dtype='int32', lod_level=1)
  259. self.assertRaises(
  260. TypeError,
  261. ops.roi_align,
  262. input=inputs,
  263. rois=rois,
  264. output_size=(7, 7))
  265. paddle.disable_static()
  266. class TestROIPool(LayerTest):
  267. def test_roi_pool(self):
  268. b, c, h, w = 2, 12, 20, 20
  269. inputs_np = np.random.rand(b, c, h, w).astype('float32')
  270. rois_num = [4, 6]
  271. output_size = (7, 7)
  272. rois_np = make_rois(h, w, rois_num, output_size)
  273. rois_num_np = np.array(rois_num).astype('int32')
  274. with self.static_graph():
  275. inputs = paddle.static.data(
  276. name='inputs', shape=[b, c, h, w], dtype='float32')
  277. rois = paddle.static.data(
  278. name='rois', shape=[10, 4], dtype='float32')
  279. rois_num = paddle.static.data(
  280. name='rois_num', shape=[None], dtype='int32')
  281. output, _ = ops.roi_pool(
  282. input=inputs,
  283. rois=rois,
  284. output_size=output_size,
  285. rois_num=rois_num)
  286. output_np, = self.get_static_graph_result(
  287. feed={
  288. 'inputs': inputs_np,
  289. 'rois': rois_np,
  290. 'rois_num': rois_num_np
  291. },
  292. fetch_list=[output],
  293. with_lod=False)
  294. with self.dynamic_graph():
  295. inputs_dy = base.to_variable(inputs_np)
  296. rois_dy = base.to_variable(rois_np)
  297. rois_num_dy = base.to_variable(rois_num_np)
  298. output_dy, _ = ops.roi_pool(
  299. input=inputs_dy,
  300. rois=rois_dy,
  301. output_size=output_size,
  302. rois_num=rois_num_dy)
  303. output_dy_np = output_dy.numpy()
  304. self.assertTrue(np.array_equal(output_np, output_dy_np))
  305. def test_roi_pool_error(self):
  306. with self.static_graph():
  307. inputs = paddle.static.data(
  308. name='inputs', shape=[2, 12, 20, 20], dtype='float32')
  309. rois = paddle.static.data(
  310. name='data_error', shape=[10, 4], dtype='int32', lod_level=1)
  311. self.assertRaises(
  312. TypeError,
  313. ops.roi_pool,
  314. input=inputs,
  315. rois=rois,
  316. output_size=(7, 7))
  317. paddle.disable_static()
  318. class TestIoUSimilarity(LayerTest):
  319. def test_iou_similarity(self):
  320. b, c, h, w = 2, 12, 20, 20
  321. inputs_np = np.random.rand(b, c, h, w).astype('float32')
  322. output_size = (7, 7)
  323. x_np = make_rois(h, w, [20], output_size)
  324. y_np = make_rois(h, w, [10], output_size)
  325. with self.static_graph():
  326. x = paddle.static.data(name='x', shape=[20, 4], dtype='float32')
  327. y = paddle.static.data(name='y', shape=[10, 4], dtype='float32')
  328. iou = ops.iou_similarity(x=x, y=y)
  329. iou_np, = self.get_static_graph_result(
  330. feed={
  331. 'x': x_np,
  332. 'y': y_np,
  333. },
  334. fetch_list=[iou],
  335. with_lod=False)
  336. with self.dynamic_graph():
  337. x_dy = base.to_variable(x_np)
  338. y_dy = base.to_variable(y_np)
  339. iou_dy = ops.iou_similarity(x=x_dy, y=y_dy)
  340. iou_dy_np = iou_dy.numpy()
  341. self.assertTrue(np.array_equal(iou_np, iou_dy_np))
  342. class TestBipartiteMatch(LayerTest):
  343. def test_bipartite_match(self):
  344. distance = np.random.random((20, 10)).astype('float32')
  345. with self.static_graph():
  346. x = paddle.static.data(name='x', shape=[20, 10], dtype='float32')
  347. match_indices, match_dist = ops.bipartite_match(
  348. x, match_type='per_prediction', dist_threshold=0.5)
  349. match_indices_np, match_dist_np = self.get_static_graph_result(
  350. feed={'x': distance, },
  351. fetch_list=[match_indices, match_dist],
  352. with_lod=False)
  353. with self.dynamic_graph():
  354. x_dy = base.to_variable(distance)
  355. match_indices_dy, match_dist_dy = ops.bipartite_match(
  356. x_dy, match_type='per_prediction', dist_threshold=0.5)
  357. match_indices_dy_np = match_indices_dy.numpy()
  358. match_dist_dy_np = match_dist_dy.numpy()
  359. self.assertTrue(np.array_equal(match_indices_np, match_indices_dy_np))
  360. self.assertTrue(np.array_equal(match_dist_np, match_dist_dy_np))
  361. class TestYoloBox(LayerTest):
  362. def test_yolo_box(self):
  363. # x shape [N C H W], C=K * (5 + class_num), class_num=10, K=2
  364. np_x = np.random.random([1, 30, 7, 7]).astype('float32')
  365. np_origin_shape = np.array([[608, 608]], dtype='int32')
  366. class_num = 10
  367. conf_thresh = 0.01
  368. downsample_ratio = 32
  369. scale_x_y = 1.2
  370. # static
  371. with self.static_graph():
  372. # x shape [N C H W], C=K * (5 + class_num), class_num=10, K=2
  373. x = paddle.static.data(
  374. name='x', shape=[1, 30, 7, 7], dtype='float32')
  375. origin_shape = paddle.static.data(
  376. name='origin_shape', shape=[1, 2], dtype='int32')
  377. boxes, scores = ops.yolo_box(
  378. x,
  379. origin_shape, [10, 13, 30, 13],
  380. class_num,
  381. conf_thresh,
  382. downsample_ratio,
  383. scale_x_y=scale_x_y)
  384. boxes_np, scores_np = self.get_static_graph_result(
  385. feed={
  386. 'x': np_x,
  387. 'origin_shape': np_origin_shape,
  388. },
  389. fetch_list=[boxes, scores],
  390. with_lod=False)
  391. # dygraph
  392. with self.dynamic_graph():
  393. x_dy = fluid.layers.assign(np_x)
  394. origin_shape_dy = fluid.layers.assign(np_origin_shape)
  395. boxes_dy, scores_dy = ops.yolo_box(
  396. x_dy,
  397. origin_shape_dy, [10, 13, 30, 13],
  398. 10,
  399. 0.01,
  400. 32,
  401. scale_x_y=scale_x_y)
  402. boxes_dy_np = boxes_dy.numpy()
  403. scores_dy_np = scores_dy.numpy()
  404. self.assertTrue(np.array_equal(boxes_np, boxes_dy_np))
  405. self.assertTrue(np.array_equal(scores_np, scores_dy_np))
  406. def test_yolo_box_error(self):
  407. with self.static_graph():
  408. # x shape [N C H W], C=K * (5 + class_num), class_num=10, K=2
  409. x = paddle.static.data(
  410. name='x', shape=[1, 30, 7, 7], dtype='float32')
  411. origin_shape = paddle.static.data(
  412. name='origin_shape', shape=[1, 2], dtype='int32')
  413. self.assertRaises(
  414. TypeError,
  415. ops.yolo_box,
  416. x,
  417. origin_shape, [10, 13, 30, 13],
  418. 10.123,
  419. 0.01,
  420. 32,
  421. scale_x_y=1.2)
  422. paddle.disable_static()
  423. class TestPriorBox(LayerTest):
  424. def test_prior_box(self):
  425. input_np = np.random.rand(2, 10, 32, 32).astype('float32')
  426. image_np = np.random.rand(2, 10, 40, 40).astype('float32')
  427. min_sizes = [2, 4]
  428. with self.static_graph():
  429. input = paddle.static.data(
  430. name='input', shape=[2, 10, 32, 32], dtype='float32')
  431. image = paddle.static.data(
  432. name='image', shape=[2, 10, 40, 40], dtype='float32')
  433. box, var = ops.prior_box(
  434. input=input,
  435. image=image,
  436. min_sizes=min_sizes,
  437. clip=True,
  438. flip=True)
  439. box_np, var_np = self.get_static_graph_result(
  440. feed={
  441. 'input': input_np,
  442. 'image': image_np,
  443. },
  444. fetch_list=[box, var],
  445. with_lod=False)
  446. with self.dynamic_graph():
  447. inputs_dy = base.to_variable(input_np)
  448. image_dy = base.to_variable(image_np)
  449. box_dy, var_dy = ops.prior_box(
  450. input=inputs_dy,
  451. image=image_dy,
  452. min_sizes=min_sizes,
  453. clip=True,
  454. flip=True)
  455. box_dy_np = box_dy.numpy()
  456. var_dy_np = var_dy.numpy()
  457. self.assertTrue(np.array_equal(box_np, box_dy_np))
  458. self.assertTrue(np.array_equal(var_np, var_dy_np))
  459. def test_prior_box_error(self):
  460. with self.static_graph():
  461. input = paddle.static.data(
  462. name='input', shape=[2, 10, 32, 32], dtype='int32')
  463. image = paddle.static.data(
  464. name='image', shape=[2, 10, 40, 40], dtype='int32')
  465. self.assertRaises(
  466. TypeError,
  467. ops.prior_box,
  468. input=input,
  469. image=image,
  470. min_sizes=[2, 4],
  471. clip=True,
  472. flip=True)
  473. paddle.disable_static()
  474. class TestMulticlassNms(LayerTest):
  475. def test_multiclass_nms(self):
  476. boxes_np = np.random.rand(10, 81, 4).astype('float32')
  477. scores_np = np.random.rand(10, 81).astype('float32')
  478. rois_num_np = np.array([2, 8]).astype('int32')
  479. with self.static_graph():
  480. boxes = paddle.static.data(
  481. name='bboxes',
  482. shape=[None, 81, 4],
  483. dtype='float32',
  484. lod_level=1)
  485. scores = paddle.static.data(
  486. name='scores', shape=[None, 81], dtype='float32', lod_level=1)
  487. rois_num = paddle.static.data(
  488. name='rois_num', shape=[None], dtype='int32')
  489. output = ops.multiclass_nms(
  490. bboxes=boxes,
  491. scores=scores,
  492. background_label=0,
  493. score_threshold=0.5,
  494. nms_top_k=400,
  495. nms_threshold=0.3,
  496. keep_top_k=200,
  497. normalized=False,
  498. return_index=True,
  499. rois_num=rois_num)
  500. out_np, index_np, nms_rois_num_np = self.get_static_graph_result(
  501. feed={
  502. 'bboxes': boxes_np,
  503. 'scores': scores_np,
  504. 'rois_num': rois_num_np
  505. },
  506. fetch_list=output,
  507. with_lod=True)
  508. out_np = np.array(out_np)
  509. index_np = np.array(index_np)
  510. nms_rois_num_np = np.array(nms_rois_num_np)
  511. with self.dynamic_graph():
  512. boxes_dy = base.to_variable(boxes_np)
  513. scores_dy = base.to_variable(scores_np)
  514. rois_num_dy = base.to_variable(rois_num_np)
  515. out_dy, index_dy, nms_rois_num_dy = ops.multiclass_nms(
  516. bboxes=boxes_dy,
  517. scores=scores_dy,
  518. background_label=0,
  519. score_threshold=0.5,
  520. nms_top_k=400,
  521. nms_threshold=0.3,
  522. keep_top_k=200,
  523. normalized=False,
  524. return_index=True,
  525. rois_num=rois_num_dy)
  526. out_dy_np = out_dy.numpy()
  527. index_dy_np = index_dy.numpy()
  528. nms_rois_num_dy_np = nms_rois_num_dy.numpy()
  529. self.assertTrue(np.array_equal(out_np, out_dy_np))
  530. self.assertTrue(np.array_equal(index_np, index_dy_np))
  531. self.assertTrue(np.array_equal(nms_rois_num_np, nms_rois_num_dy_np))
  532. def test_multiclass_nms_error(self):
  533. with self.static_graph():
  534. boxes = paddle.static.data(
  535. name='bboxes', shape=[81, 4], dtype='float32', lod_level=1)
  536. scores = paddle.static.data(
  537. name='scores', shape=[81], dtype='float32', lod_level=1)
  538. rois_num = paddle.static.data(
  539. name='rois_num', shape=[40, 41], dtype='int32')
  540. self.assertRaises(
  541. TypeError,
  542. ops.multiclass_nms,
  543. boxes=boxes,
  544. scores=scores,
  545. background_label=0,
  546. score_threshold=0.5,
  547. nms_top_k=400,
  548. nms_threshold=0.3,
  549. keep_top_k=200,
  550. normalized=False,
  551. return_index=True,
  552. rois_num=rois_num)
  553. class TestMatrixNMS(LayerTest):
  554. def test_matrix_nms(self):
  555. N, M, C = 7, 1200, 21
  556. BOX_SIZE = 4
  557. nms_top_k = 400
  558. keep_top_k = 200
  559. score_threshold = 0.01
  560. post_threshold = 0.
  561. scores_np = np.random.random((N * M, C)).astype('float32')
  562. scores_np = np.apply_along_axis(softmax, 1, scores_np)
  563. scores_np = np.reshape(scores_np, (N, M, C))
  564. scores_np = np.transpose(scores_np, (0, 2, 1))
  565. boxes_np = np.random.random((N, M, BOX_SIZE)).astype('float32')
  566. boxes_np[:, :, 0:2] = boxes_np[:, :, 0:2] * 0.5
  567. boxes_np[:, :, 2:4] = boxes_np[:, :, 2:4] * 0.5 + 0.5
  568. with self.static_graph():
  569. boxes = paddle.static.data(
  570. name='boxes', shape=[N, M, BOX_SIZE], dtype='float32')
  571. scores = paddle.static.data(
  572. name='scores', shape=[N, C, M], dtype='float32')
  573. out, index, _ = ops.matrix_nms(
  574. bboxes=boxes,
  575. scores=scores,
  576. score_threshold=score_threshold,
  577. post_threshold=post_threshold,
  578. nms_top_k=nms_top_k,
  579. keep_top_k=keep_top_k,
  580. return_index=True)
  581. out_np, index_np = self.get_static_graph_result(
  582. feed={'boxes': boxes_np,
  583. 'scores': scores_np},
  584. fetch_list=[out, index],
  585. with_lod=True)
  586. with self.dynamic_graph():
  587. boxes_dy = base.to_variable(boxes_np)
  588. scores_dy = base.to_variable(scores_np)
  589. out_dy, index_dy, _ = ops.matrix_nms(
  590. bboxes=boxes_dy,
  591. scores=scores_dy,
  592. score_threshold=score_threshold,
  593. post_threshold=post_threshold,
  594. nms_top_k=nms_top_k,
  595. keep_top_k=keep_top_k,
  596. return_index=True)
  597. out_dy_np = out_dy.numpy()
  598. index_dy_np = index_dy.numpy()
  599. self.assertTrue(np.array_equal(out_np, out_dy_np))
  600. self.assertTrue(np.array_equal(index_np, index_dy_np))
  601. def test_matrix_nms_error(self):
  602. with self.static_graph():
  603. bboxes = paddle.static.data(
  604. name='bboxes', shape=[7, 1200, 4], dtype='float32')
  605. scores = paddle.static.data(
  606. name='data_error', shape=[7, 21, 1200], dtype='int32')
  607. self.assertRaises(
  608. TypeError,
  609. ops.matrix_nms,
  610. bboxes=bboxes,
  611. scores=scores,
  612. score_threshold=0.01,
  613. post_threshold=0.,
  614. nms_top_k=400,
  615. keep_top_k=200,
  616. return_index=True)
  617. paddle.disable_static()
  618. class TestBoxCoder(LayerTest):
  619. def test_box_coder(self):
  620. prior_box_np = np.random.random((81, 4)).astype('float32')
  621. prior_box_var_np = np.random.random((81, 4)).astype('float32')
  622. target_box_np = np.random.random((20, 81, 4)).astype('float32')
  623. # static
  624. with self.static_graph():
  625. prior_box = paddle.static.data(
  626. name='prior_box', shape=[81, 4], dtype='float32')
  627. prior_box_var = paddle.static.data(
  628. name='prior_box_var', shape=[81, 4], dtype='float32')
  629. target_box = paddle.static.data(
  630. name='target_box', shape=[20, 81, 4], dtype='float32')
  631. boxes = ops.box_coder(
  632. prior_box=prior_box,
  633. prior_box_var=prior_box_var,
  634. target_box=target_box,
  635. code_type="decode_center_size",
  636. box_normalized=False)
  637. boxes_np, = self.get_static_graph_result(
  638. feed={
  639. 'prior_box': prior_box_np,
  640. 'prior_box_var': prior_box_var_np,
  641. 'target_box': target_box_np,
  642. },
  643. fetch_list=[boxes],
  644. with_lod=False)
  645. # dygraph
  646. with self.dynamic_graph():
  647. prior_box_dy = base.to_variable(prior_box_np)
  648. prior_box_var_dy = base.to_variable(prior_box_var_np)
  649. target_box_dy = base.to_variable(target_box_np)
  650. boxes_dy = ops.box_coder(
  651. prior_box=prior_box_dy,
  652. prior_box_var=prior_box_var_dy,
  653. target_box=target_box_dy,
  654. code_type="decode_center_size",
  655. box_normalized=False)
  656. boxes_dy_np = boxes_dy.numpy()
  657. self.assertTrue(np.array_equal(boxes_np, boxes_dy_np))
  658. def test_box_coder_error(self):
  659. with self.static_graph():
  660. prior_box = paddle.static.data(
  661. name='prior_box', shape=[81, 4], dtype='int32')
  662. prior_box_var = paddle.static.data(
  663. name='prior_box_var', shape=[81, 4], dtype='float32')
  664. target_box = paddle.static.data(
  665. name='target_box', shape=[20, 81, 4], dtype='float32')
  666. self.assertRaises(TypeError, ops.box_coder, prior_box,
  667. prior_box_var, target_box)
  668. paddle.disable_static()
  669. class TestGenerateProposals(LayerTest):
  670. def test_generate_proposals(self):
  671. scores_np = np.random.rand(2, 3, 4, 4).astype('float32')
  672. bbox_deltas_np = np.random.rand(2, 12, 4, 4).astype('float32')
  673. im_shape_np = np.array([[8, 8], [6, 6]]).astype('float32')
  674. anchors_np = np.reshape(np.arange(4 * 4 * 3 * 4),
  675. [4, 4, 3, 4]).astype('float32')
  676. variances_np = np.ones((4, 4, 3, 4)).astype('float32')
  677. with self.static_graph():
  678. scores = paddle.static.data(
  679. name='scores', shape=[2, 3, 4, 4], dtype='float32')
  680. bbox_deltas = paddle.static.data(
  681. name='bbox_deltas', shape=[2, 12, 4, 4], dtype='float32')
  682. im_shape = paddle.static.data(
  683. name='im_shape', shape=[2, 2], dtype='float32')
  684. anchors = paddle.static.data(
  685. name='anchors', shape=[4, 4, 3, 4], dtype='float32')
  686. variances = paddle.static.data(
  687. name='var', shape=[4, 4, 3, 4], dtype='float32')
  688. rois, roi_probs, rois_num = ops.generate_proposals(
  689. scores,
  690. bbox_deltas,
  691. im_shape,
  692. anchors,
  693. variances,
  694. pre_nms_top_n=10,
  695. post_nms_top_n=5,
  696. return_rois_num=True)
  697. rois_stat, roi_probs_stat, rois_num_stat = self.get_static_graph_result(
  698. feed={
  699. 'scores': scores_np,
  700. 'bbox_deltas': bbox_deltas_np,
  701. 'im_shape': im_shape_np,
  702. 'anchors': anchors_np,
  703. 'var': variances_np
  704. },
  705. fetch_list=[rois, roi_probs, rois_num],
  706. with_lod=True)
  707. with self.dynamic_graph():
  708. scores_dy = base.to_variable(scores_np)
  709. bbox_deltas_dy = base.to_variable(bbox_deltas_np)
  710. im_shape_dy = base.to_variable(im_shape_np)
  711. anchors_dy = base.to_variable(anchors_np)
  712. variances_dy = base.to_variable(variances_np)
  713. rois, roi_probs, rois_num = ops.generate_proposals(
  714. scores_dy,
  715. bbox_deltas_dy,
  716. im_shape_dy,
  717. anchors_dy,
  718. variances_dy,
  719. pre_nms_top_n=10,
  720. post_nms_top_n=5,
  721. return_rois_num=True)
  722. rois_dy = rois.numpy()
  723. roi_probs_dy = roi_probs.numpy()
  724. rois_num_dy = rois_num.numpy()
  725. self.assertTrue(np.array_equal(np.array(rois_stat), rois_dy))
  726. self.assertTrue(np.array_equal(np.array(roi_probs_stat), roi_probs_dy))
  727. self.assertTrue(np.array_equal(np.array(rois_num_stat), rois_num_dy))
  728. if __name__ == '__main__':
  729. unittest.main()