pandaset_dataset.py 18.6 KB
Newer Older
lea-v's avatar
lea-v committed
1
2
3
4
5
6
"""
    Dataset from Pandaset (Hesai)
"""

import pickle
import os
7
8
9
10
11
try:
    import pandas as pd
    import pandaset as ps
except:
    pass 
lea-v's avatar
lea-v 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import numpy as np

from ..dataset import DatasetTemplate
from ...ops.roiaware_pool3d import roiaware_pool3d_utils

import torch


def pose_dict_to_numpy(pose):
    """
        Conert pandaset pose dict to a numpy vector in order to pass it through the network
    """
    pose_np = [pose["position"]["x"],
               pose["position"]["y"],
               pose["position"]["z"],
               pose["heading"]["w"],
               pose["heading"]["x"],
               pose["heading"]["y"],
               pose["heading"]["z"]]

    return pose_np


def pose_numpy_to_dict(pose):
    """
        Conert pandaset pose dict to a numpy vector in order to pass it through the network
    """
    pose_dict = {'position':
                    {'x': pose[0],
                     'y': pose[1],
                     'z': pose[2]},
                 'heading':
                    {'w': pose[3],
                     'x': pose[4],
                     'y': pose[5],
                     'z': pose[6]}}

    return pose_dict


class PandasetDataset(DatasetTemplate):
    def __init__(self, dataset_cfg, class_names, training=True, root_path=None, logger=None):
        """
        Args:
            root_path:
            dataset_cfg:
            class_names:
            training:
            logger:
        """
        super().__init__(
            dataset_cfg=dataset_cfg, class_names=class_names, training=training, root_path=root_path, logger=logger
        )
        if root_path is None:
            root_path = self.dataset_cfg.DATA_PATH
        self.dataset = ps.DataSet(os.path.join(root_path, 'dataset'))
        self.split = self.dataset_cfg.DATA_SPLIT[self.mode]
        self.pandaset_infos = []
        self.include_pandaset_infos(self.mode)


    def include_pandaset_infos(self, mode):
        if self.logger is not None:
            self.logger.info('Loading PandaSet dataset')
        pandaset_infos = []

        for info_path in self.dataset_cfg.INFO_PATH[mode]:
            info_path = os.path.join(self.root_path, info_path)
            if not os.path.exists(info_path):
                continue
            with open(info_path, 'rb') as f:
                infos = pickle.load(f)
                pandaset_infos.extend(infos)

        self.pandaset_infos.extend(pandaset_infos)

        if self.logger is not None:
            self.logger.info('Total samples for PandaSet dataset ({}): {}'.format(self.mode, len(pandaset_infos)))


    def set_split(self, split):
        self.sequences = self.dataset_cfg.SEQUENCES[split]
        self.split = split


    def __len__(self):
        return len(self.pandaset_infos)


    def __getitem__(self, index):
        """
        To support a custom dataset, implement this function to load the raw data (and labels), then transform them to
        the unified normative coordinate (x pointing forward, z pointing upwards) and call the function self.prepare_data() to process the data and send them
        to the model.

        Args:
            index:

        Returns:

        """
        info = self.pandaset_infos[index]
        seq_idx = info['sequence']

        pose = self._get_pose(info)
        points = self._get_lidar_points(info, pose)
        boxes, labels, zrot_world_to_ego = self._get_annotations(info, pose)
        pose_np = pose_dict_to_numpy(pose)

        input_dict = {'points': points,
                      'gt_boxes': boxes,
                      'gt_names': labels,
                      'sequence': int(seq_idx),
                      'frame_idx': info['frame_idx'],
                      'zrot_world_to_ego': zrot_world_to_ego,
                      'pose': pose_dict_to_numpy(pose)
                     }
        # seq_idx is converted to int because strings can't be passed to
        # the gpu in pytorch
        # zrot_world_to_ego is propagated in order to be able to transform the
        # predicted yaws back to world coordinates

        data_dict = self.prepare_data(data_dict=input_dict)

        return data_dict


    def _get_pose(self, info):
        seq_idx = info['sequence']
        # get pose for world to ego frame transformation
        if self.dataset[seq_idx].lidar.poses is None:
            self.dataset[seq_idx].lidar._load_poses()

        pose = self.dataset[seq_idx].lidar.poses[info['frame_idx']]

        return pose


    def _get_lidar_points(self, info, pose):
        """
        Get lidar in the unified normative coordinate system for a given frame
        The intensity is normalized to fit [0-1] range (pandaset intensity is in [0-255] range)
        """
        # get lidar points
        lidar_frame = pd.read_pickle(info['lidar_path'])
        # get points for the required lidar(s) only
        device = self.dataset_cfg.get('LIDAR_DEVICE', 0)
        if device != -1:
            lidar_frame = lidar_frame[lidar_frame.d == device]
        world_points = lidar_frame.to_numpy()
        # There seems to be issues with the automatic deletion of pandas datasets sometimes
        del lidar_frame

        points_loc = world_points[:, :3]
        points_int = world_points[:, 3]

        # nromalize intensity
        points_int = points_int / 255

        ego_points = ps.geometry.lidar_points_to_ego(points_loc, pose)
        # Pandaset ego coordinates are:
        # - x pointing to the right
        # - y pointing to the front
        # - z pointing up
        # Normative coordinates are:
        # - x pointing foreward
        # - y pointings to the left
        # - z pointing to the top
        # So a transformation is required to the match the normative coordinates
        ego_points = ego_points[:, [1, 0, 2]] # switch x and y
        ego_points[:, 1] = - ego_points[:, 1] # revert y axis

        return np.append(ego_points, np.expand_dims(points_int, axis=1), axis=1).astype(np.float32)


    def _get_annotations(self,info, pose):
        """
        Get box informations in the unified normative coordinate system for a given frame
        """

        # get boxes
        cuboids = pd.read_pickle(info["cuboids_path"])
        device = self.dataset_cfg.get('LIDAR_DEVICE', 0)
        if device != -1:
            # keep cuboids that are seen by a given device
            cuboids = cuboids[cuboids["cuboids.sensor_id"] != 1 - device]

        xs = cuboids['position.x'].to_numpy()
        ys = cuboids['position.y'].to_numpy()
        zs = cuboids['position.z'].to_numpy()
        dxs = cuboids['dimensions.x'].to_numpy()
        dys = cuboids['dimensions.y'].to_numpy()
        dzs = cuboids['dimensions.z'].to_numpy()
        yaws = cuboids['yaw'].to_numpy()
        labels = cuboids['label'].to_numpy()

        del cuboids  # There seem to be issues with the automatic deletion of pandas datasets sometimes

        labels = np.array([self.dataset_cfg.TRAINING_CATEGORIES.get(lab, lab)
                           for lab in labels] )

        # Compute the center points coordinates in ego coordinates
        centers = np.vstack([xs, ys, zs]).T
        ego_centers = ps.geometry.lidar_points_to_ego(centers, pose)

        # Compute the yaw in ego coordinates
        # The following implementation supposes that the pitch of the car is
        # negligible compared to its yaw, in order to be able to express the
        # bbox coordinates in the ego coordinate system with an {axis aligned
        # box + yaw} only representation
        yaxis_points_from_pose = ps.geometry.lidar_points_to_ego(np.array([[0, 0, 0], [0, 1., 0]]), pose)
        yaxis_from_pose = yaxis_points_from_pose[1, :] - yaxis_points_from_pose[0, :]

        if yaxis_from_pose[-1] >= 10**-1:
            if self.logger is not None:
                self.logger.warning("The car's pitch is supposed to be negligible " +
                                    "sin(pitch) is >= 10**-1 ({})".format(yaxis_from_pose[-1]))

        # rotation angle in rads of the y axis around thz z axis
        zrot_world_to_ego = np.arctan2(-yaxis_from_pose[0], yaxis_from_pose[1])
        ego_yaws = yaws + zrot_world_to_ego

        # Pandaset ego coordinates are:
        # - x pointing to the right
        # - y pointing to the front
        # - z pointing up
        # Normative coordinates are:
        # - x pointing foreward
        # - y pointings to the left
        # - z pointing to the top
        # So a transformation is required to the match the normative coordinates
        ego_xs = ego_centers[:, 1]
        ego_ys = -ego_centers[:, 0]
        ego_zs = ego_centers[:, 2]
        ego_dxs = dys
        ego_dys = dxs  # stays >= 0
        ego_dzs = dzs

        ego_boxes = np.vstack([ego_xs, ego_ys, ego_zs, ego_dxs, ego_dys, ego_dzs, ego_yaws]).T

        return ego_boxes.astype(np.float32), labels, zrot_world_to_ego


    @staticmethod
    def generate_prediction_dicts(batch_dict, pred_dicts, class_names, output_path=None):
        """
        To support a custom dataset, implement this function to receive the predicted results from the model, and then
        transform the unified normative coordinate to your required coordinate, and optionally save them to disk.

        Args:
            batch_dict: dict of original data from the dataloader
            pred_dicts: dict of predicted results from the model
                pred_boxes: (N, 7), Tensor
                pred_scores: (N), Tensor
                pred_labels: (N), Tensor
            class_names:
            output_path: if it is not None, save the results to this path
        Returns:

        """

        def generate_single_sample_dataframe(batch_index, box_dict, zrot_world_to_ego, pose):
            pred_boxes = box_dict["pred_boxes"].cpu().numpy()
            pred_scores = box_dict["pred_scores"].cpu().numpy()
            pred_labels = box_dict["pred_labels"].cpu().numpy()
            zrot = zrot_world_to_ego.cpu().numpy()
            pose_dict = pose_numpy_to_dict(pose.cpu().numpy())

            xs = pred_boxes[:, 0]
            ys = pred_boxes[:, 1]
            zs = pred_boxes[:, 2]
            dxs = pred_boxes[:, 3]
            dys = pred_boxes[:, 4]
            dzs = pred_boxes[:, 5]
            yaws = pred_boxes[:, 6]
            names = np.array(class_names)[pred_labels - 1]  # Predicted labels start on 1

            # convert from normative coordinates to pandaset ego coordinates
            ego_xs = - ys
            ego_ys = xs
            ego_zs = zs
            ego_dxs = dys
            ego_dys = dxs
            ego_dzs = dzs
            ego_yaws = yaws

            # convert from pandaset ego coordinates to world coordinates
            # for the moment, an simplified estimation of the ego yaw is computed in __getitem__
            # which sets ego_yaw = world_yaw + zrot_world_to_ego
            world_yaws = ego_yaws - zrot

            ego_centers = np.vstack([ego_xs, ego_ys, ego_zs]).T
            world_centers = ps.geometry.ego_to_lidar_points(ego_centers, pose_dict)
            world_xs = world_centers[:, 0]
            world_ys = world_centers[:, 1]
            world_zs = world_centers[:, 2]
            # dx, dy, dz remain unchanged as the bbox orientation is handled by
            # the yaw information

            data_dict = {'position.x': world_xs,
                         'position.y': world_ys,
                         'position.z': world_zs,
                         'dimensions.x': ego_dxs,
                         'dimensions.y': ego_dys,
                         'dimensions.z': ego_dzs,
                         'yaw': world_yaws % (2 * np.pi),
                         'label': names,
                         'score': pred_scores
            }

            return pd.DataFrame(data_dict)


        annos = []
        for index, box_dict in enumerate(pred_dicts):
            frame_idx = batch_dict['frame_idx'][index]
            seq_idx = batch_dict['sequence'][index]
            zrot = batch_dict['zrot_world_to_ego'][index]
            pose = batch_dict['pose'][index]

            single_pred_df = generate_single_sample_dataframe(index, box_dict, zrot, pose)


            single_pred_dict = {'preds' : single_pred_df,
                                # 'name 'ensures testing the number of detections in a compatible format as kitti
                                'name' : single_pred_df['label'].tolist(),
                                'frame_idx': frame_idx,
                                'sequence': str(seq_idx).zfill(3)}
            # seq_idx was converted to int in self.__getitem__` because strings
            # can't be passed to the gpu in pytorch.
            # To convert it back to a string, we assume that the sequence is
            # provided in pandaset format with 3 digits

            if output_path is not None:
                frame_id = str(int(frame_idx)).zfill(2)
                seq_id = str(int(seq_idx)).zfill(3)
                cur_det_file = os.path.join(output_path, seq_id, 'predictions',
                                            'cuboids', ("{}.pkl.gz".format(frame_id)))
                os.makedirs(os.path.dirname(cur_det_file), exist_ok=True)
                single_pred_df.to_pickle(cur_det_file)

            annos.append(single_pred_dict)

        return annos


    def get_infos(self):
        """
        Generate the dataset infos dict for each sample of the dataset.
        For each sample, this dict contains:
            - the sequence index
            - the frame index
            - the path to the lidar data
            - the path to the bounding box annotations
        """
        infos = []
        for seq in self.sequences:
            s = self.dataset[seq]
            s.load_lidar()
            if len(s.lidar.data) > 100:
                raise ValueError("The implementation for this dataset assumes that each sequence is " +
                                 "no longer than 100 frames. The current sequence has {}".format(len(s.lidar.data)))
            info = [{'sequence': seq,
                     'frame_idx': ii,
                     'lidar_path': os.path.join(self.root_path, 'dataset', seq, 'lidar', ("{:02d}.pkl.gz".format(ii))),
                     'cuboids_path': os.path.join(self.root_path, 'dataset', seq,
                                                  'annotations', 'cuboids', ("{:02d}.pkl.gz".format(ii)))
                    } for ii in range(len(s.lidar.data))]
            infos.extend(info)
            del self.dataset._sequences[seq]

        return infos


    def create_groundtruth_database(self, info_path=None, used_classes=None, split='train'):
        database_save_path = os.path.join(self.root_path,
                'gt_database' if split == 'train' else 'gt_database_{}'.format(split))
        db_info_save_path = os.path.join(self.root_path,
                'pandaset_dbinfos_{}.pkl'.format(split))

        os.makedirs(database_save_path, exist_ok=True)
        all_db_infos = {}

        with open(info_path, 'rb') as f:
            infos = pickle.load(f)

        for k in range(len(infos)):
            print('gt_database sample: %d/%d' % (k + 1, len(infos)))
            info = infos[k]
            sample_idx = info['frame_idx']
            pose = self._get_pose(info)
            points = self._get_lidar_points(info, pose)
            gt_boxes, names, _ = self._get_annotations(info, pose)

            num_obj = gt_boxes.shape[0]

            point_indices = roiaware_pool3d_utils.points_in_boxes_cpu(
                torch.from_numpy(points[:, 0:3]), torch.from_numpy(gt_boxes)
            ).numpy()  # (nboxes, npoints)

            for i in range(num_obj):
                tmp_name = names[i].replace("/", "").replace(" ", "")
                filename = '%s_%s_%d.bin' % (sample_idx, tmp_name, i)
                filepath = os.path.join(database_save_path, filename)
                gt_points = points[point_indices[i] > 0]
                gt_points[:, :3] -= gt_boxes[i, :3]
                with open(filepath, 'wb') as f:
                    gt_points.tofile(f)

                if (used_classes is None) or names[i] in used_classes:
                    db_path = os.path.relpath(filepath, self.root_path)  # gt_database/xxxxx.bin
                    db_info = {'name': names[i], 'path': db_path, 'gt_idx': i,
                               'box3d_lidar': gt_boxes[i], 'num_points_in_gt': gt_points.shape[0],
                               'difficulty': -1}
                    if names[i] in all_db_infos:
                        all_db_infos[names[i]].append(db_info)
                    else:
                        all_db_infos[names[i]] = [db_info]
        for k, v in all_db_infos.items():
            print('Database %s: %d' % (k, len(v)))

        with open(db_info_save_path, 'wb') as f:
            pickle.dump(all_db_infos, f)


    def evaluation(self, det_annos, class_names, **kwargs):
        self.logger.warning('Evaluation is not implemented for Pandaset as there is no official one. ' +
                            'Returning an empty evaluation result.')
        ap_result_str = ''
        ap_dict = {}

        return ap_result_str, ap_dict


def create_pandaset_infos(dataset_cfg, class_names, data_path, save_path):
    """
    Create dataset_infos files in order not to have it in a preprocessed pickle
    file with the info for each sample
    See PandasetDataset.get_infos for further details.
    """
    dataset = PandasetDataset(dataset_cfg=dataset_cfg, class_names=class_names, root_path=data_path, training=False)
    for split in ["train", "val", "test"]:
        print("---------------- Start to generate {} data infos ---------------".format(split))
        dataset.set_split(split)
        infos = dataset.get_infos()
        file_path = os.path.join(save_path, 'pandaset_infos_{}.pkl'.format(split))
        with open(file_path, 'wb') as f:
            pickle.dump(infos, f)
        print("Pandaset info {} file is saved to {}".format(split, file_path))

    print('------------Start create groundtruth database for data augmentation-----------')
    dataset = PandasetDataset(dataset_cfg=dataset_cfg, class_names=class_names, root_path=data_path, training=False)
    dataset.set_split("train")
    dataset.create_groundtruth_database(
        os.path.join(save_path, 'pandaset_infos_train.pkl'),
        split="train"
    )
    print('---------------Data preparation Done---------------')


if __name__ == '__main__':
    import sys
    if sys.argv.__len__() > 1 and sys.argv[1] == 'create_pandaset_infos':
        import yaml
        from pathlib import Path
        from easydict import EasyDict
        dataset_cfg = EasyDict(yaml.safe_load(open(sys.argv[2])))
        ROOT_DIR = (Path(__file__).resolve().parent / '../../../').resolve()
        create_pandaset_infos(
            dataset_cfg=dataset_cfg,
            class_names=['Car', 'Pedestrian', 'Cyclist'],
            data_path=ROOT_DIR / 'data' / 'pandaset',
            save_path=ROOT_DIR / 'data' / 'pandaset'
        )