browse_dataset.py 4.71 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
29
30
        '--task',
        type=str,
        choices=['det', 'seg', 'multi_modality-det', 'mono-det'],
        help='Determine the visualization method depending on the task.')
31
32
33
34
    parser.add_argument(
        '--aug',
        action='store_true',
        help='Whether to visualize augmented datasets or original dataset.')
35
    parser.add_argument(
ChaimZhu's avatar
ChaimZhu committed
36
        '--ceph', action='store_true', help='Use ceph as data storage backend')
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    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
51
def build_data_cfg(config_path, aug, cfg_options):
52
    """Build data config for loading visualization data."""
53

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

    # 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
63
    # use only first dataset for `ConcatDataset`
ChaimZhu's avatar
ChaimZhu committed
64
65
66
    if cfg.train_dataloader.dataset['type'] == 'ConcatDataset':
        cfg.train_dataloader.dataset = cfg.train_dataloader.dataset.datasets[0]
    train_data_cfg = cfg.train_dataloader.dataset
67
68
69
70
71
72
73
74

    if aug:
        show_pipeline = cfg.train_pipeline
    else:
        show_pipeline = cfg.eval_pipeline
        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
75
76
77
            # Collect data as well as labels
            if cfg.train_pipeline[i]['type'] == 'Pack3DDetInputs':
                if show_pipeline[-1]['type'] == 'Pack3DDetInputs':
78
79
80
                    show_pipeline[-1] = cfg.train_pipeline[i]
                else:
                    show_pipeline.append(cfg.train_pipeline[i])
81

ChaimZhu's avatar
ChaimZhu committed
82
    train_data_cfg['pipeline'] = show_pipeline
83
84
85
86
87
88
89
90
91
92

    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
93
94
95
96
97
    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
98
99
100
101

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

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

    # configure visualization mode
110
    vis_task = args.task  # 'det', 'seg', 'multi_modality-det', 'mono-det'
ZCMax's avatar
ZCMax committed
111
112
113
114

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

ChaimZhu's avatar
ChaimZhu committed
115
    progress_bar = ProgressBar(len(dataset))
116

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

        out_file = osp.join(
            args.output_dir) if args.output_dir is not None else None

        visualizer.add_datasample(
            '3d visualzier',
            data_input,
ChaimZhu's avatar
ChaimZhu committed
128
            data_sample=data_sample,
ZCMax's avatar
ZCMax committed
129
130
131
132
133
            show=not args.not_show,
            wait_time=args.show_interval,
            out_file=out_file,
            vis_task=vis_task)

134
        progress_bar.update()
135
136
137
138


if __name__ == '__main__':
    main()