browse_dataset.py 5.08 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
2
3
4
import argparse
from os import path as osp

ChaimZhu's avatar
ChaimZhu committed
5
6
from mmengine.config import Config, DictAction
from mmengine.utils import ProgressBar, mkdir_or_exist
7

ChaimZhu's avatar
ChaimZhu committed
8
9
from mmdet3d.registry import DATASETS, VISUALIZERS
from mmdet3d.utils import register_all_modules, replace_ceph_backend
10
11
12
13
14
15
16
17
18
19


def parse_args():
    parser = argparse.ArgumentParser(description='Browse a dataset')
    parser.add_argument('config', help='train config file path')
    parser.add_argument(
        '--output-dir',
        default=None,
        type=str,
        help='If there is no display interface, you can save it')
ChaimZhu's avatar
ChaimZhu committed
20
21
22
23
24
25
    parser.add_argument('--not-show', default=False, action='store_true')
    parser.add_argument(
        '--show-interval',
        type=float,
        default=2,
        help='the interval of show (s)')
26
    parser.add_argument(
27
28
        '--task',
        type=str,
29
30
31
32
        choices=[
            'mono_det', 'multi-view_det', 'lidar_det', 'lidar_seg',
            'multi-modality_det'
        ],
33
        help='Determine the visualization method depending on the task.')
34
35
36
37
    parser.add_argument(
        '--aug',
        action='store_true',
        help='Whether to visualize augmented datasets or original dataset.')
38
    parser.add_argument(
ChaimZhu's avatar
ChaimZhu committed
39
        '--ceph', action='store_true', help='Use ceph as data storage backend')
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    parser.add_argument(
        '--cfg-options',
        nargs='+',
        action=DictAction,
        help='override some settings in the used config, the key-value pair '
        'in xxx=yyy format will be merged into config file. If the value to '
        'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
        'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
        'Note that the quotation marks are necessary and that no white space '
        'is allowed.')
    args = parser.parse_args()
    return args


ChaimZhu's avatar
ChaimZhu committed
54
def build_data_cfg(config_path, aug, cfg_options):
55
    """Build data config for loading visualization data."""
56

57
58
59
    cfg = Config.fromfile(config_path)
    if cfg_options is not None:
        cfg.merge_from_dict(cfg_options)
ChaimZhu's avatar
ChaimZhu committed
60
61
62
63
64
65

    # extract inner dataset of `RepeatDataset` as
    # `cfg.train_dataloader.dataset` so we don't
    # need to worry about it later
    if cfg.train_dataloader.dataset['type'] == 'RepeatDataset':
        cfg.train_dataloader.dataset = cfg.train_dataloader.dataset.dataset
66
    # use only first dataset for `ConcatDataset`
ChaimZhu's avatar
ChaimZhu committed
67
68
69
    if cfg.train_dataloader.dataset['type'] == 'ConcatDataset':
        cfg.train_dataloader.dataset = cfg.train_dataloader.dataset.datasets[0]
    train_data_cfg = cfg.train_dataloader.dataset
70
71
72
73

    if aug:
        show_pipeline = cfg.train_pipeline
    else:
74
        show_pipeline = cfg.test_pipeline
75
76
77
        for i in range(len(cfg.train_pipeline)):
            if cfg.train_pipeline[i]['type'] == 'LoadAnnotations3D':
                show_pipeline.insert(i, cfg.train_pipeline[i])
ChaimZhu's avatar
ChaimZhu committed
78
79
80
            # Collect data as well as labels
            if cfg.train_pipeline[i]['type'] == 'Pack3DDetInputs':
                if show_pipeline[-1]['type'] == 'Pack3DDetInputs':
81
82
83
                    show_pipeline[-1] = cfg.train_pipeline[i]
                else:
                    show_pipeline.append(cfg.train_pipeline[i])
84

ChaimZhu's avatar
ChaimZhu committed
85
    train_data_cfg['pipeline'] = show_pipeline
86
87
88
89
90
91
92
93
94
95

    return cfg


def main():
    args = parse_args()

    if args.output_dir is not None:
        mkdir_or_exist(args.output_dir)

ChaimZhu's avatar
ChaimZhu committed
96
97
98
99
100
    cfg = build_data_cfg(args.config, args.aug, args.cfg_options)

    # TODO: We will unify the ceph support approach with other OpenMMLab repos
    if args.ceph:
        cfg = replace_ceph_backend(cfg)
ZCMax's avatar
ZCMax committed
101
102
103
104

    # register all modules in mmdet3d into the registries
    register_all_modules()

105
    try:
ChaimZhu's avatar
ChaimZhu committed
106
        dataset = DATASETS.build(
ZCMax's avatar
ZCMax committed
107
108
            cfg.train_dataloader.dataset,
            default_args=dict(filter_empty_gt=False))
109
    except TypeError:  # seg dataset doesn't have `filter_empty_gt` key
ChaimZhu's avatar
ChaimZhu committed
110
        dataset = DATASETS.build(cfg.train_dataloader.dataset)
111
112

    # configure visualization mode
113
    vis_task = args.task
ZCMax's avatar
ZCMax committed
114
115
116
117

    visualizer = VISUALIZERS.build(cfg.visualizer)
    visualizer.dataset_meta = dataset.metainfo

ChaimZhu's avatar
ChaimZhu committed
118
    progress_bar = ProgressBar(len(dataset))
119

120
    for i, item in enumerate(dataset):
ZCMax's avatar
ZCMax committed
121
122
        # the 3D Boxes in input could be in any of three coordinates
        data_input = item['inputs']
123
        data_sample = item['data_samples'].numpy()
ZCMax's avatar
ZCMax committed
124
125

        out_file = osp.join(
126
127
128
129
130
131
132
133
            args.output_dir,
            f'{i}.jpg') if args.output_dir is not None else None

        # o3d_save_path is valid when args.not_show is False
        o3d_save_path = osp.join(args.output_dir, f'pc_{i}.png') if (
            args.output_dir is not None
            and vis_task in ['lidar_det', 'lidar_seg', 'multi-modality_det']
            and not args.not_show) else None
ZCMax's avatar
ZCMax committed
134
135
136
137

        visualizer.add_datasample(
            '3d visualzier',
            data_input,
ChaimZhu's avatar
ChaimZhu committed
138
            data_sample=data_sample,
ZCMax's avatar
ZCMax committed
139
140
141
            show=not args.not_show,
            wait_time=args.show_interval,
            out_file=out_file,
142
            o3d_save_path=o3d_save_path,
ZCMax's avatar
ZCMax committed
143
144
            vis_task=vis_task)

145
        progress_bar.update()
146
147
148
149


if __name__ == '__main__':
    main()