test_imvotenet.py 2.88 KB
Newer Older
zhangshilong's avatar
zhangshilong 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)
zhangshilong's avatar
zhangshilong committed
9
10
11
12
13
14
15
16
17


class TestImvoteNet(unittest.TestCase):

    def test_imvotenet_only_img(self):
        import mmdet3d.models

        assert hasattr(mmdet3d.models, 'ImVoteNet')
        DefaultScope.get_instance('test_imvotenet_img', scope_name='mmdet3d')
18
19
        setup_seed(0)
        votenet_net_cfg = get_detector_cfg(
20
            'imvotenet/imvotenet_faster-rcnn-r50_fpn_4xb2_sunrgbd-3d.py')
zhangshilong's avatar
zhangshilong committed
21
22
        model = MODELS.build(votenet_net_cfg)

23
        packed_inputs = create_detector_inputs(
24
            with_points=False, with_img=True, img_size=128)
zhangshilong's avatar
zhangshilong committed
25
26
27
28
29

        if torch.cuda.is_available():
            model = model.cuda()
            # test simple_test
            with torch.no_grad():
30
31
32
                data = model.data_preprocessor(packed_inputs, True)
                results = model.forward(**data, mode='predict')
            self.assertEqual(len(results), 1)
zhangshilong's avatar
zhangshilong committed
33
34
35
36
37
38
            self.assertIn('bboxes', results[0].pred_instances)
            self.assertIn('scores', results[0].pred_instances)
            self.assertIn('labels', results[0].pred_instances)

            # save the memory
            with torch.no_grad():
zhangshilong's avatar
zhangshilong committed
39
                torch.cuda.empty_cache()
40
                losses = model.forward(**data, mode='loss')
zhangshilong's avatar
zhangshilong committed
41
42
43
44
45
46
47
48
49
50
51

            self.assertGreater(sum(losses['loss_rpn_cls']), 0)

            self.assertGreater(losses['loss_cls'], 0)
            self.assertGreater(losses['loss_bbox'], 0)

    def test_imvotenet(self):
        import mmdet3d.models

        assert hasattr(mmdet3d.models, 'ImVoteNet')
        DefaultScope.get_instance('test_imvotenet', scope_name='mmdet3d')
52
53
        setup_seed(0)
        votenet_net_cfg = get_detector_cfg(
54
            'imvotenet/imvotenet_stage2_8xb16_sunrgbd-3d.py')
zhangshilong's avatar
zhangshilong committed
55
56
        model = MODELS.build(votenet_net_cfg)

57
        packed_inputs = create_detector_inputs(
58
59
60
61
            with_points=True,
            with_img=True,
            img_size=128,
            bboxes_3d_type='depth')
zhangshilong's avatar
zhangshilong committed
62
63
64
65
66

        if torch.cuda.is_available():
            model = model.cuda()
            # test simple_test
            with torch.no_grad():
67
68
69
                data = model.data_preprocessor(packed_inputs, True)
                results = model.forward(**data, mode='predict')
            self.assertEqual(len(results), 1)
zhangshilong's avatar
zhangshilong committed
70
71
72
73
74
75
            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():
76
                losses = model.forward(**data, mode='loss')
zhangshilong's avatar
zhangshilong committed
77
78
79
80

            self.assertGreater(losses['vote_loss'], 0)
            self.assertGreater(losses['objectness_loss'], 0)
            self.assertGreater(losses['semantic_loss'], 0)