test_imvoxelnet.py 3.3 KB
Newer Older
Tai-Wang's avatar
Tai-Wang committed
1
2
3
4
5
6
import unittest

import torch
from mmengine import DefaultScope

from mmdet3d.registry import MODELS
7
8
from mmdet3d.testing import (create_detector_inputs, get_detector_cfg,
                             setup_seed)
Tai-Wang's avatar
Tai-Wang committed
9
10
11
12


class TestImVoxelNet(unittest.TestCase):

13
    def test_imvoxelnet_kitti(self):
Tai-Wang's avatar
Tai-Wang committed
14
15
16
        import mmdet3d.models

        assert hasattr(mmdet3d.models, 'ImVoxelNet')
17
18
        DefaultScope.get_instance(
            'test_imvoxelnet_kitti', scope_name='mmdet3d')
19
20
        setup_seed(0)
        imvoxel_net_cfg = get_detector_cfg(
21
            'imvoxelnet/imvoxelnet_8xb4_kitti-3d-car.py')
Tai-Wang's avatar
Tai-Wang committed
22
23
        model = MODELS.build(imvoxel_net_cfg)
        num_gt_instance = 1
24
        packed_inputs = create_detector_inputs(
25
26
27
28
29
30
            with_points=False,
            with_img=True,
            img_size=(128, 128),
            num_gt_instance=num_gt_instance,
            with_pts_semantic_mask=False,
            with_pts_instance_mask=False)
Tai-Wang's avatar
Tai-Wang committed
31
32
33
34
35

        if torch.cuda.is_available():
            model = model.cuda()
            # test simple_test
            with torch.no_grad():
36
                data = model.data_preprocessor(packed_inputs, True)
zhangshilong's avatar
zhangshilong committed
37
                torch.cuda.empty_cache()
38
39
                results = model.forward(**data, mode='predict')
            self.assertEqual(len(results), 1)
Tai-Wang's avatar
Tai-Wang committed
40
41
42
43
44
45
            self.assertIn('bboxes_3d', results[0].pred_instances_3d)
            self.assertIn('scores_3d', results[0].pred_instances_3d)
            self.assertIn('labels_3d', results[0].pred_instances_3d)

            # save the memory
            with torch.no_grad():
46
                losses = model.forward(**data, mode='loss')
Tai-Wang's avatar
Tai-Wang committed
47

48
49
50
            self.assertGreaterEqual(losses['loss_cls'][0], 0)
            self.assertGreaterEqual(losses['loss_bbox'][0], 0)
            self.assertGreaterEqual(losses['loss_dir'][0], 0)
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

    def test_imvoxelnet_sunrgbd(self):
        import mmdet3d.models

        assert hasattr(mmdet3d.models, 'ImVoxelNet')
        DefaultScope.get_instance(
            'test_imvoxelnet_sunrgbd', scope_name='mmdet3d')
        setup_seed(0)
        imvoxel_net_cfg = get_detector_cfg(
            'imvoxelnet/imvoxelnet_2xb4_sunrgbd-3d-10class.py')
        model = MODELS.build(imvoxel_net_cfg)
        num_gt_instance = 1
        packed_inputs = create_detector_inputs(
            with_points=False,
            with_img=True,
            img_size=(128, 128),
            num_gt_instance=num_gt_instance,
            with_pts_semantic_mask=False,
            with_pts_instance_mask=False)

        if torch.cuda.is_available():
            model = model.cuda()
            # test simple_test
            with torch.no_grad():
                data = model.data_preprocessor(packed_inputs, True)
                torch.cuda.empty_cache()
                results = model.forward(**data, mode='predict')
            self.assertEqual(len(results), 1)
            self.assertIn('bboxes_3d', results[0].pred_instances_3d)
            self.assertIn('scores_3d', results[0].pred_instances_3d)
            self.assertIn('labels_3d', results[0].pred_instances_3d)

            # save the memory
            with torch.no_grad():
                losses = model.forward(**data, mode='loss')

            self.assertGreaterEqual(losses['center_loss'], 0)
            self.assertGreaterEqual(losses['bbox_loss'], 0)
            self.assertGreaterEqual(losses['cls_loss'], 0)