loading.py 3.72 KB
Newer Older
zhangwenwei's avatar
zhangwenwei committed
1
2
3
import mmcv
import numpy as np

4
from mmdet.datasets.builder import PIPELINES
zhangwenwei's avatar
zhangwenwei committed
5
6


7
@PIPELINES.register_module()
zhangwenwei's avatar
zhangwenwei committed
8
9
class LoadMultiViewImageFromFiles(object):
    """ Load multi channel images from a list of separate channel files.
zhangwenwei's avatar
zhangwenwei committed
10
11

    Expects results['img_filename'] to be a list of filenames
zhangwenwei's avatar
zhangwenwei committed
12
    """
zhangwenwei's avatar
zhangwenwei committed
13

zhangwenwei's avatar
zhangwenwei committed
14
15
16
    def __init__(self, to_float32=False, color_type='unchanged'):
        self.to_float32 = to_float32
        self.color_type = color_type
zhangwenwei's avatar
zhangwenwei committed
17
18

    def __call__(self, results):
zhangwenwei's avatar
zhangwenwei committed
19
        filename = results['img_filename']
zhangwenwei's avatar
zhangwenwei committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        img = np.stack(
            [mmcv.imread(name, self.color_type) for name in filename], axis=-1)
        if self.to_float32:
            img = img.astype(np.float32)
        results['filename'] = filename
        results['img'] = img
        results['img_shape'] = img.shape
        results['ori_shape'] = img.shape
        # Set initial values for default meta_keys
        results['pad_shape'] = img.shape
        results['scale_factor'] = 1.0
        num_channels = 1 if len(img.shape) < 3 else img.shape[2]
        results['img_norm_cfg'] = dict(
            mean=np.zeros(num_channels, dtype=np.float32),
            std=np.ones(num_channels, dtype=np.float32),
            to_rgb=False)
zhangwenwei's avatar
zhangwenwei committed
36
37
38
        return results

    def __repr__(self):
zhangwenwei's avatar
zhangwenwei committed
39
40
        return "{} (to_float32={}, color_type='{}')".format(
            self.__class__.__name__, self.to_float32, self.color_type)
zhangwenwei's avatar
zhangwenwei committed
41
42
43
44


@PIPELINES.register_module()
class LoadPointsFromMultiSweeps(object):
zhangwenwei's avatar
zhangwenwei committed
45
    """Load points from multiple sweeps
zhangwenwei's avatar
zhangwenwei committed
46

zhangwenwei's avatar
zhangwenwei committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    This is usually used for nuScenes dataset to utilize previous sweeps.

    Args:
        sweeps_num (int): number of sweeps
        load_dim (int): dimension number of the loaded points
        file_client_args (dict): Config dict of file clients, refer to
            https://github.com/open-mmlab/mmcv/blob/master/mmcv/fileio/file_client.py
            for more details.
    """

    def __init__(self,
                 sweeps_num=10,
                 load_dim=5,
                 file_client_args=dict(backend='disk')):
        self.load_dim = load_dim
zhangwenwei's avatar
zhangwenwei committed
62
        self.sweeps_num = sweeps_num
zhangwenwei's avatar
zhangwenwei committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        self.file_client_args = file_client_args.copy()
        self.file_client = None

    def _load_points(self, pts_filename):
        if self.file_client is None:
            self.file_client = mmcv.FileClient(**self.file_client_args)
        try:
            pts_bytes = self.file_client.get(pts_filename)
            points = np.frombuffer(pts_bytes, dtype=np.float32)
        except ConnectionError:
            mmcv.check_file_exist(pts_filename)
            if pts_filename.endswith('.npy'):
                points = np.load(pts_filename)
            else:
                points = np.fromfile(pts_filename, dtype=np.float32)
        return points
zhangwenwei's avatar
zhangwenwei committed
79
80
81
82
83
84
85
86
87
88
89

    def __call__(self, results):
        points = results['points']
        points[:, 3] /= 255
        points[:, 4] = 0
        sweep_points_list = [points]
        ts = results['timestamp']

        for idx, sweep in enumerate(results['sweeps']):
            if idx >= self.sweeps_num:
                break
zhangwenwei's avatar
zhangwenwei committed
90
91
            points_sweep = self._load_points(sweep['data_path'])
            points_sweep = np.copy(points_sweep).reshape(-1, self.load_dim)
zhangwenwei's avatar
zhangwenwei committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
            sweep_ts = sweep['timestamp'] / 1e6
            points_sweep[:, 3] /= 255
            points_sweep[:, :3] = points_sweep[:, :3] @ sweep[
                'sensor2lidar_rotation'].T
            points_sweep[:, :3] += sweep['sensor2lidar_translation']
            points_sweep[:, 4] = ts - sweep_ts
            sweep_points_list.append(points_sweep)

        points = np.concatenate(sweep_points_list, axis=0)[:, [0, 1, 2, 4]]
        results['points'] = points
        return results

    def __repr__(self):
        return f'{self.__class__.__name__}(sweeps_num={self.sweeps_num})'