Unverified Commit db39fd4a authored by Sun Jiahao's avatar Sun Jiahao Committed by GitHub
Browse files

[Enhance] Use Inferencer to implement Demo (#2763)

parent f4c032e4
...@@ -42,13 +42,13 @@ class TestMonoDet3DInferencer(TestCase): ...@@ -42,13 +42,13 @@ class TestMonoDet3DInferencer(TestCase):
def test_call(self, model): def test_call(self, model):
# single img # single img
img_path = 'demo/data/kitti/000008.png' img_path = 'demo/data/kitti/000008.png'
calib_path = 'demo/data/kitti/000008.txt' infos_path = 'demo/data/kitti/000008.pkl'
inferencer = MonoDet3DInferencer(model) inferencer = MonoDet3DInferencer(model)
inputs = dict(img=img_path, calib=calib_path) inputs = dict(img=img_path, infos=infos_path)
res_path = inferencer(inputs, return_vis=True) res_path = inferencer(inputs, return_vis=True)
# ndarray # ndarray
img = mmcv.imread(img_path) img = mmcv.imread(img_path)
inputs = dict(img=img, calib=calib_path) inputs = dict(img=img, infos=infos_path)
res_ndarray = inferencer(inputs, return_vis=True) res_ndarray = inferencer(inputs, return_vis=True)
self.assert_predictions_equal(res_path['predictions'], self.assert_predictions_equal(res_path['predictions'],
res_ndarray['predictions']) res_ndarray['predictions'])
...@@ -59,16 +59,18 @@ class TestMonoDet3DInferencer(TestCase): ...@@ -59,16 +59,18 @@ class TestMonoDet3DInferencer(TestCase):
inputs = [ inputs = [
dict( dict(
img='demo/data/kitti/000008.png', img='demo/data/kitti/000008.png',
calib='demo/data/kitti/000008.txt'), infos='demo/data/kitti/000008.pkl'),
dict( dict(
img='demo/data/kitti/000008.png', img='demo/data/kitti/000008.png',
calib='demo/data/kitti/000008.txt') infos='demo/data/kitti/000008.pkl')
] ]
res_path = inferencer(inputs, return_vis=True) res_path = inferencer(inputs, return_vis=True)
# list of ndarray # list of ndarray
imgs = [mmcv.imread(p['img']) for p in inputs] imgs = [mmcv.imread(p['img']) for p in inputs]
inputs[0]['img'] = imgs[0] inputs = [
inputs[1]['img'] = imgs[1] dict(img=imgs[0], infos='demo/data/kitti/000008.pkl'),
dict(img=imgs[1], infos='demo/data/kitti/000008.pkl')
]
res_ndarray = inferencer(inputs, return_vis=True) res_ndarray = inferencer(inputs, return_vis=True)
self.assert_predictions_equal(res_path['predictions'], self.assert_predictions_equal(res_path['predictions'],
res_ndarray['predictions']) res_ndarray['predictions'])
...@@ -77,36 +79,30 @@ class TestMonoDet3DInferencer(TestCase): ...@@ -77,36 +79,30 @@ class TestMonoDet3DInferencer(TestCase):
@parameterized.expand(['pgd_kitti']) @parameterized.expand(['pgd_kitti'])
def test_visualize(self, model): def test_visualize(self, model):
inputs = [ inputs = dict(
dict( img='demo/data/kitti/000008.png',
img='demo/data/kitti/000008.png', infos='demo/data/kitti/000008.pkl')
calib='demo/data/kitti/000008.txt'),
dict(
img='demo/data/kitti/000008.png',
calib='demo/data/kitti/000008.txt')
]
inferencer = MonoDet3DInferencer(model) inferencer = MonoDet3DInferencer(model)
# img_out_dir # img_out_dir
with tempfile.TemporaryDirectory() as tmp_dir: with tempfile.TemporaryDirectory() as tmp_dir:
inferencer(inputs, img_out_dir=tmp_dir) inferencer(inputs, out_dir=tmp_dir)
for img_dir in ['000008.png', '000008.png']: self.assertTrue(
self.assertTrue(osp.exists(osp.join(tmp_dir, img_dir))) osp.exists(osp.join(tmp_dir, 'vis_camera/CAM2/000008.png')))
@parameterized.expand(['pgd_kitti']) @parameterized.expand(['pgd_kitti'])
def test_postprocess(self, model): def test_postprocess(self, model):
# return_datasample # return_datasample
img_path = 'demo/data/kitti/000008.png' img_path = 'demo/data/kitti/000008.png'
calib_path = 'demo/data/kitti/000008.txt' infos_path = 'demo/data/kitti/000008.pkl'
inputs = dict(img=img_path, calib=calib_path) inputs = dict(img=img_path, infos=infos_path)
inferencer = MonoDet3DInferencer(model) inferencer = MonoDet3DInferencer(model)
res = inferencer(inputs, return_datasamples=True) res = inferencer(inputs, return_datasamples=True)
self.assertTrue(is_list_of(res['predictions'], Det3DDataSample)) self.assertTrue(is_list_of(res['predictions'], Det3DDataSample))
# pred_out_file # pred_out_dir
with tempfile.TemporaryDirectory() as tmp_dir: with tempfile.TemporaryDirectory() as tmp_dir:
pred_out_file = osp.join(tmp_dir, 'tmp.json') inputs = dict(img=img_path, infos=infos_path)
res = inferencer( res = inferencer(inputs, print_result=True, out_dir=tmp_dir)
inputs, print_result=True, pred_out_file=pred_out_file) dumped_res = mmengine.load(
dumped_res = mmengine.load(pred_out_file) osp.join(tmp_dir, 'preds', '000008.json'))
self.assert_predictions_equal(res['predictions'], self.assertEqual(res['predictions'][0], dumped_res)
dumped_res['predictions'])
...@@ -44,11 +44,11 @@ class TestMultiModalityDet3DInferencer(TestCase): ...@@ -44,11 +44,11 @@ class TestMultiModalityDet3DInferencer(TestCase):
def test_call(self): def test_call(self):
if not torch.cuda.is_available(): if not torch.cuda.is_available():
return return
calib_path = 'tests/data/kitti/training/calib/000000.pkl' infos_path = 'demo/data/kitti/000008.pkl'
points_path = 'tests/data/kitti/training/velodyne/000000.bin' points_path = 'demo/data/kitti/000008.bin'
img_path = 'tests/data/kitti/training/image_2/000000.png' img_path = 'demo/data/kitti/000008.png'
# single img & point cloud # single img & point cloud
inputs = dict(points=points_path, img=img_path, calib=calib_path) inputs = dict(points=points_path, img=img_path, infos=infos_path)
res_path = self.inferencer(inputs, return_vis=True) res_path = self.inferencer(inputs, return_vis=True)
# ndarray # ndarray
...@@ -57,7 +57,7 @@ class TestMultiModalityDet3DInferencer(TestCase): ...@@ -57,7 +57,7 @@ class TestMultiModalityDet3DInferencer(TestCase):
points = points.reshape(-1, 4) points = points.reshape(-1, 4)
points = points[:, :4] points = points[:, :4]
img = mmcv.imread(inputs['img']) img = mmcv.imread(inputs['img'])
inputs = dict(points=points, img=img, calib=calib_path) inputs = dict(points=points, img=img, infos=infos_path)
res_ndarray = self.inferencer(inputs, return_vis=True) res_ndarray = self.inferencer(inputs, return_vis=True)
self.assert_predictions_equal(res_path['predictions'], self.assert_predictions_equal(res_path['predictions'],
res_ndarray['predictions']) res_ndarray['predictions'])
...@@ -66,8 +66,8 @@ class TestMultiModalityDet3DInferencer(TestCase): ...@@ -66,8 +66,8 @@ class TestMultiModalityDet3DInferencer(TestCase):
# multiple imgs & point clouds # multiple imgs & point clouds
inputs = [ inputs = [
dict(points=points_path, img=img_path, calib=calib_path), dict(points=points_path, img=img_path, infos=infos_path),
dict(points=points_path, img=img_path, calib=calib_path) dict(points=points_path, img=img_path, infos=infos_path)
] ]
res_path = self.inferencer(inputs, return_vis=True) res_path = self.inferencer(inputs, return_vis=True)
# list of ndarray # list of ndarray
...@@ -77,7 +77,7 @@ class TestMultiModalityDet3DInferencer(TestCase): ...@@ -77,7 +77,7 @@ class TestMultiModalityDet3DInferencer(TestCase):
points = np.frombuffer(pts_bytes, dtype=np.float32) points = np.frombuffer(pts_bytes, dtype=np.float32)
points = points.reshape(-1, 4) points = points.reshape(-1, 4)
img = mmcv.imread(p['img']) img = mmcv.imread(p['img'])
all_inputs.append(dict(points=points, img=img, calib=p['calib'])) all_inputs.append(dict(points=points, img=img, infos=infos_path))
res_ndarray = self.inferencer(all_inputs, return_vis=True) res_ndarray = self.inferencer(all_inputs, return_vis=True)
self.assert_predictions_equal(res_path['predictions'], self.assert_predictions_equal(res_path['predictions'],
...@@ -89,12 +89,12 @@ class TestMultiModalityDet3DInferencer(TestCase): ...@@ -89,12 +89,12 @@ class TestMultiModalityDet3DInferencer(TestCase):
if not torch.cuda.is_available(): if not torch.cuda.is_available():
return return
inputs = dict( inputs = dict(
points='tests/data/kitti/training/velodyne/000000.bin', points='demo/data/kitti/000008.bin',
img='tests/data/kitti/training/image_2/000000.png', img='demo/data/kitti/000008.png',
calib='tests/data/kitti/training/calib/000000.pkl'), infos='demo/data/kitti/000008.pkl'),
# img_out_dir # img_out_dir
with tempfile.TemporaryDirectory() as tmp_dir: with tempfile.TemporaryDirectory() as tmp_dir:
self.inferencer(inputs, img_out_dir=tmp_dir) self.inferencer(inputs, out_dir=tmp_dir)
# TODO: For results of LiDAR-based detection, the saved image only # TODO: For results of LiDAR-based detection, the saved image only
# exists when show=True. # exists when show=True.
# self.assertTrue(osp.exists(osp.join(tmp_dir, '000000.png'))) # self.assertTrue(osp.exists(osp.join(tmp_dir, '000000.png')))
...@@ -103,18 +103,18 @@ class TestMultiModalityDet3DInferencer(TestCase): ...@@ -103,18 +103,18 @@ class TestMultiModalityDet3DInferencer(TestCase):
if not torch.cuda.is_available(): if not torch.cuda.is_available():
return return
# return_datasample # return_datasample
inputs = dict( infos_path = 'demo/data/kitti/000008.pkl'
points='tests/data/kitti/training/velodyne/000000.bin', points_path = 'demo/data/kitti/000008.bin'
img='tests/data/kitti/training/image_2/000000.png', img_path = 'demo/data/kitti/000008.png'
calib='tests/data/kitti/training/calib/000000.pkl') # single img & point cloud
inputs = dict(points=points_path, img=img_path, infos=infos_path)
res = self.inferencer(inputs, return_datasamples=True) res = self.inferencer(inputs, return_datasamples=True)
self.assertTrue(is_list_of(res['predictions'], Det3DDataSample)) self.assertTrue(is_list_of(res['predictions'], Det3DDataSample))
# pred_out_file # pred_out_dir
with tempfile.TemporaryDirectory() as tmp_dir: with tempfile.TemporaryDirectory() as tmp_dir:
pred_out_file = osp.join(tmp_dir, 'tmp.json') inputs = dict(points=points_path, img=img_path, infos=infos_path)
res = self.inferencer( res = self.inferencer(inputs, print_result=True, out_dir=tmp_dir)
inputs, print_result=True, pred_out_file=pred_out_file) dumped_res = mmengine.load(
dumped_res = mmengine.load(pred_out_file) osp.join(tmp_dir, 'preds', '000008.json'))
self.assert_predictions_equal(res['predictions'], self.assertEqual(res['predictions'][0], dumped_res)
dumped_res['predictions'])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment