model_utils.py 3.89 KB
Newer Older
jshilong's avatar
jshilong committed
1
2
3
4
5
6
7
8
9
# Copyright (c) OpenMMLab. All rights reserved.
import copy
import random
from os.path import dirname, exists, join

import numpy as np
import torch
from mmengine import InstanceData

jshilong's avatar
jshilong committed
10
11
from mmdet3d.core import (CameraInstance3DBoxes, DepthInstance3DBoxes,
                          Det3DDataSample, LiDARInstance3DBoxes, PointData)
jshilong's avatar
jshilong committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74


def _setup_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True


def _get_config_directory():
    """Find the predefined detector config directory."""
    try:
        # Assume we are running in the source mmdetection3d repo
        repo_dpath = dirname(dirname(dirname(__file__)))
    except NameError:
        # For IPython development when this __file__ is not defined
        import mmdet3d
        repo_dpath = dirname(dirname(mmdet3d.__file__))
    config_dpath = join(repo_dpath, 'configs')
    if not exists(config_dpath):
        raise Exception('Cannot find config path')
    return config_dpath


def _get_config_module(fname):
    """Load a configuration as a python module."""
    from mmcv import Config
    config_dpath = _get_config_directory()
    config_fpath = join(config_dpath, fname)
    config_mod = Config.fromfile(config_fpath)
    return config_mod


def _get_model_cfg(fname):
    """Grab configs necessary to create a model.

    These are deep copied to allow for safe modification of parameters without
    influencing other tests.
    """
    config = _get_config_module(fname)
    model = copy.deepcopy(config.model)

    return model


def _get_detector_cfg(fname):
    """Grab configs necessary to create a detector.

    These are deep copied to allow for safe modification of parameters without
    influencing other tests.
    """
    import mmcv
    config = _get_config_module(fname)
    model = copy.deepcopy(config.model)
    train_cfg = mmcv.Config(copy.deepcopy(config.model.train_cfg))
    test_cfg = mmcv.Config(copy.deepcopy(config.model.test_cfg))

    model.update(train_cfg=train_cfg)
    model.update(test_cfg=test_cfg)
    return model


jshilong's avatar
jshilong committed
75
76
77
78
79
80
81
82
83
84
85
def _create_detector_inputs(seed=0,
                            with_points=True,
                            with_img=False,
                            num_gt_instance=20,
                            num_points=10,
                            points_feat_dim=4,
                            num_classes=3,
                            gt_bboxes_dim=7,
                            with_pts_semantic_mask=False,
                            with_pts_instance_mask=False,
                            bboxes_3d_type='lidar'):
jshilong's avatar
jshilong committed
86
    _setup_seed(seed)
jshilong's avatar
jshilong committed
87
88
89
90
91
92
    assert bboxes_3d_type in ('lidar', 'depth', 'cam')
    bbox_3d_class = {
        'lidar': LiDARInstance3DBoxes,
        'depth': DepthInstance3DBoxes,
        'cam': CameraInstance3DBoxes
    }
jshilong's avatar
jshilong committed
93
    if with_points:
jshilong's avatar
jshilong committed
94
95
96
        points = torch.rand([num_points, points_feat_dim])
    else:
        points = None
jshilong's avatar
jshilong committed
97
98
    if with_img:
        img = torch.rand(3, 10, 10)
jshilong's avatar
jshilong committed
99
100
101
    else:
        img = None
    inputs_dict = dict(img=img, points=points)
jshilong's avatar
jshilong committed
102

jshilong's avatar
jshilong committed
103
    gt_instance_3d = InstanceData()
jshilong's avatar
jshilong committed
104
    gt_instance_3d.bboxes_3d = bbox_3d_class[bboxes_3d_type](
105
        torch.rand([num_gt_instance, gt_bboxes_dim]), box_dim=gt_bboxes_dim)
jshilong's avatar
jshilong committed
106
107
    gt_instance_3d.labels_3d = torch.randint(0, num_classes, [num_gt_instance])
    data_sample = Det3DDataSample(
jshilong's avatar
jshilong committed
108
        metainfo=dict(box_type_3d=bbox_3d_class[bboxes_3d_type]))
jshilong's avatar
jshilong committed
109
    data_sample.gt_instances_3d = gt_instance_3d
jshilong's avatar
jshilong committed
110
111
112
113
114
115
116
117
    data_sample.gt_pts_seg = PointData()
    if with_pts_instance_mask:
        pts_instance_mask = torch.randint(0, num_gt_instance, [num_points])
        data_sample.gt_pts_seg['pts_instance_mask'] = pts_instance_mask
    if with_pts_semantic_mask:
        pts_semantic_mask = torch.randint(0, num_classes, [num_points])
        data_sample.gt_pts_seg['pts_semantic_mask'] = pts_semantic_mask

jshilong's avatar
jshilong committed
118
    return dict(inputs=inputs_dict, data_sample=data_sample)