open-mmlab / mmdetection3d

OpenMMLab's next-generation platform for general 3D object detection.
https://mmdetection3d.readthedocs.io/en/latest/
Apache License 2.0
5.35k stars 1.55k forks source link

[Bug] Loading dataset takes really long #2764

Open TheCodez opened 1 year ago

TheCodez commented 1 year ago

Prerequisite

Task

I'm using the official example scripts/configs for the officially supported tasks/models/datasets.

Branch

main branch https://github.com/open-mmlab/mmdetection3d

Environment

sys.platform: linux Python: 3.8.17 (default, Jul 5 2023, 21:04:15) [GCC 11.2.0] CUDA available: True numpy_random_seed: 2147483648 GPU 0,1: NVIDIA TITAN RTX CUDA_HOME: /usr/local/cuda NVCC: Cuda compilation tools, release 12.1, V12.1.105 GCC: gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 PyTorch: 2.0.1+cu118 PyTorch compiling details: PyTorch built with:

OpenCV: 4.8.0 MMEngine: 0.8.4 MMDetection: 3.1.0 MMDetection3D: 1.2.0+9f40d4f spconv2.0: False

Reproduces the problem - code sample

import timeit, functools, copy, pickle

if __name__ == '__main__':
    class MyDataset:
        def __init__(self):
            self._metainfo = dict(
                classes=['car', 'truck', 'trailer', 'bus', 'construction_vehicle', 'bicycle',
                                           'motorcycle', 'pedestrian', 'traffic_cone', 'barrier'],
                version='v1.0-trainval',
                palette=[
                    (255, 158, 0),  # Orange
                    (255, 99, 71),  # Tomato
                    (255, 140, 0),  # Darkorange
                    (255, 127, 80),  # Coral
                    (233, 150, 70),  # Darksalmon
                    (220, 20, 60),  # Crimson
                    (255, 61, 99),  # Red
                    (0, 0, 230),  # Blue
                    (47, 79, 79),  # Darkslategrey
                    (112, 128, 144),  # Slategrey
                ])

        @property
        def metainfo(self):
            return copy.deepcopy(self._metainfo)

    def deepcopy(dataset):
        classes = dataset.metainfo['classes']

    def directly(dataset):
        classes = dataset._metainfo['classes']

    dataset = MyDataset()
    t = timeit.Timer(functools.partial(deepcopy, dataset))
    print(f'metainfo deepcopy: {t.timeit()}')
    t = timeit.Timer(functools.partial(directly, dataset))
    print(f'metainfo directly: {t.timeit()}')

    def deepcopy(dataset):
        dc = copy.deepcopy(dataset._metainfo)

    def _pickle(dataset):
        dc = pickle.loads(pickle.dumps(dataset._metainfo))

    dataset = MyDataset()
    t = timeit.Timer(functools.partial(deepcopy, dataset))
    print(f'Data deepcopy: {t.timeit()}')
    t = timeit.Timer(functools.partial(_pickle, dataset))
    print(f'Data pickle: {t.timeit()}')

Reproduces the problem - command or script

Reproduces the problem - error message

Additional information

Loading the dataset either during training/testing or running browse_dataset takes a really long time for large datasets, such as nuScenes.

I found out that it is spending a lot of time here: https://github.com/open-mmlab/mmdetection3d/blob/0f9dfa97a35ef87e16b700742d3c358d0ad15452/mmdet3d/datasets/det3d_dataset.py#L259 This is called for all instances in each frame and since accessing metadata always does a deepcopy of _metainfo it takes a lot of time. To see how slow deepcopy is I did some experimentation and found out, that it is significantly slower than directly accessing the elements:

metainfo deepcopy: 46.957518891999825
metainfo directly: 0.11945905200082052

I think directly accessing self._metainfo['classes'] could help improving loading times.

Another bottleneck is here: https://github.com/open-mmlab/mmdetection3d/blob/0f9dfa97a35ef87e16b700742d3c358d0ad15452/mmdet3d/datasets/det3d_dataset.py#L374 also caused by a deepcopy. Using input_dict = pickle.loads(pickle.dumps(ori_input_dict)) should also help reducing the runtime:

Data deepcopy: 49.796394890000556
Data pickle: 6.8219003000003795

[UPDATE] The deepcopy isn't actually required anymore, since the deepcopy is already performed by the base dataset https://github.com/open-mmlab/mmengine/pull/471/files

sunjiahao1999 commented 1 year ago

We also find this bug. It will be fixed. Thanks for your report.