test_ops.py 25 KB

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