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):
def test_call(self, model):
# single img
img_path = 'demo/data/kitti/000008.png'
calib_path = 'demo/data/kitti/000008.txt'
infos_path = 'demo/data/kitti/000008.pkl'
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)
# ndarray
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)
self.assert_predictions_equal(res_path['predictions'],
res_ndarray['predictions'])
......@@ -59,16 +59,18 @@ class TestMonoDet3DInferencer(TestCase):
inputs = [
dict(
img='demo/data/kitti/000008.png',
calib='demo/data/kitti/000008.txt'),
infos='demo/data/kitti/000008.pkl'),
dict(
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)
# list of ndarray
imgs = [mmcv.imread(p['img']) for p in inputs]
inputs[0]['img'] = imgs[0]
inputs[1]['img'] = imgs[1]
inputs = [
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)
self.assert_predictions_equal(res_path['predictions'],
res_ndarray['predictions'])
......@@ -77,36 +79,30 @@ class TestMonoDet3DInferencer(TestCase):
@parameterized.expand(['pgd_kitti'])
def test_visualize(self, model):
inputs = [
dict(
img='demo/data/kitti/000008.png',
calib='demo/data/kitti/000008.txt'),
dict(
img='demo/data/kitti/000008.png',
calib='demo/data/kitti/000008.txt')
]
inputs = dict(
img='demo/data/kitti/000008.png',
infos='demo/data/kitti/000008.pkl')
inferencer = MonoDet3DInferencer(model)
# img_out_dir
with tempfile.TemporaryDirectory() as tmp_dir:
inferencer(inputs, img_out_dir=tmp_dir)
for img_dir in ['000008.png', '000008.png']:
self.assertTrue(osp.exists(osp.join(tmp_dir, img_dir)))
inferencer(inputs, out_dir=tmp_dir)
self.assertTrue(
osp.exists(osp.join(tmp_dir, 'vis_camera/CAM2/000008.png')))
@parameterized.expand(['pgd_kitti'])
def test_postprocess(self, model):
# return_datasample
img_path = 'demo/data/kitti/000008.png'
calib_path = 'demo/data/kitti/000008.txt'
inputs = dict(img=img_path, calib=calib_path)
infos_path = 'demo/data/kitti/000008.pkl'
inputs = dict(img=img_path, infos=infos_path)
inferencer = MonoDet3DInferencer(model)
res = inferencer(inputs, return_datasamples=True)
self.assertTrue(is_list_of(res['predictions'], Det3DDataSample))
# pred_out_file
# pred_out_dir
with tempfile.TemporaryDirectory() as tmp_dir:
pred_out_file = osp.join(tmp_dir, 'tmp.json')
res = inferencer(
inputs, print_result=True, pred_out_file=pred_out_file)
dumped_res = mmengine.load(pred_out_file)
self.assert_predictions_equal(res['predictions'],
dumped_res['predictions'])
inputs = dict(img=img_path, infos=infos_path)
res = inferencer(inputs, print_result=True, out_dir=tmp_dir)
dumped_res = mmengine.load(
osp.join(tmp_dir, 'preds', '000008.json'))
self.assertEqual(res['predictions'][0], dumped_res)
......@@ -44,11 +44,11 @@ class TestMultiModalityDet3DInferencer(TestCase):
def test_call(self):
if not torch.cuda.is_available():
return
calib_path = 'tests/data/kitti/training/calib/000000.pkl'
points_path = 'tests/data/kitti/training/velodyne/000000.bin'
img_path = 'tests/data/kitti/training/image_2/000000.png'
infos_path = 'demo/data/kitti/000008.pkl'
points_path = 'demo/data/kitti/000008.bin'
img_path = 'demo/data/kitti/000008.png'
# 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)
# ndarray
......@@ -57,7 +57,7 @@ class TestMultiModalityDet3DInferencer(TestCase):
points = points.reshape(-1, 4)
points = points[:, :4]
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)
self.assert_predictions_equal(res_path['predictions'],
res_ndarray['predictions'])
......@@ -66,8 +66,8 @@ class TestMultiModalityDet3DInferencer(TestCase):
# multiple imgs & point clouds
inputs = [
dict(points=points_path, img=img_path, calib=calib_path),
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, infos=infos_path)
]
res_path = self.inferencer(inputs, return_vis=True)
# list of ndarray
......@@ -77,7 +77,7 @@ class TestMultiModalityDet3DInferencer(TestCase):
points = np.frombuffer(pts_bytes, dtype=np.float32)
points = points.reshape(-1, 4)
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)
self.assert_predictions_equal(res_path['predictions'],
......@@ -89,12 +89,12 @@ class TestMultiModalityDet3DInferencer(TestCase):
if not torch.cuda.is_available():
return
inputs = dict(
points='tests/data/kitti/training/velodyne/000000.bin',
img='tests/data/kitti/training/image_2/000000.png',
calib='tests/data/kitti/training/calib/000000.pkl'),
points='demo/data/kitti/000008.bin',
img='demo/data/kitti/000008.png',
infos='demo/data/kitti/000008.pkl'),
# img_out_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
# exists when show=True.
# self.assertTrue(osp.exists(osp.join(tmp_dir, '000000.png')))
......@@ -103,18 +103,18 @@ class TestMultiModalityDet3DInferencer(TestCase):
if not torch.cuda.is_available():
return
# return_datasample
inputs = dict(
points='tests/data/kitti/training/velodyne/000000.bin',
img='tests/data/kitti/training/image_2/000000.png',
calib='tests/data/kitti/training/calib/000000.pkl')
infos_path = 'demo/data/kitti/000008.pkl'
points_path = 'demo/data/kitti/000008.bin'
img_path = 'demo/data/kitti/000008.png'
# single img & point cloud
inputs = dict(points=points_path, img=img_path, infos=infos_path)
res = self.inferencer(inputs, return_datasamples=True)
self.assertTrue(is_list_of(res['predictions'], Det3DDataSample))
# pred_out_file
# pred_out_dir
with tempfile.TemporaryDirectory() as tmp_dir:
pred_out_file = osp.join(tmp_dir, 'tmp.json')
res = self.inferencer(
inputs, print_result=True, pred_out_file=pred_out_file)
dumped_res = mmengine.load(pred_out_file)
self.assert_predictions_equal(res['predictions'],
dumped_res['predictions'])
inputs = dict(points=points_path, img=img_path, infos=infos_path)
res = self.inferencer(inputs, print_result=True, out_dir=tmp_dir)
dumped_res = mmengine.load(
osp.join(tmp_dir, 'preds', '000008.json'))
self.assertEqual(res['predictions'][0], dumped_res)
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