prediction_to_waymo.py 16.1 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
Wenwei Zhang's avatar
Wenwei Zhang committed
2
3
4
5
r"""Adapted from `Waymo to KITTI converter
    <https://github.com/caizhongang/waymo_kitti_converter>`_.
"""

6
7
try:
    from waymo_open_dataset import dataset_pb2 as open_dataset
8
9
10
    from waymo_open_dataset import label_pb2
    from waymo_open_dataset.protos import metrics_pb2
    from waymo_open_dataset.protos.metrics_pb2 import Objects
11
except ImportError:
12
    Objects = None
13
14
15
16
    raise ImportError(
        'Please run "pip install waymo-open-dataset-tf-2-1-0==1.2.0" '
        'to install the official devkit first.')

17
18
from glob import glob
from os.path import join
19
from typing import List, Optional
20

21
import mmengine
Wenwei Zhang's avatar
Wenwei Zhang committed
22
23
24
25
import numpy as np
import tensorflow as tf


26
27
28
class Prediction2Waymo(object):
    """Predictions to Waymo converter. The format of prediction results could
    be original format or kitti-format.
Wenwei Zhang's avatar
Wenwei Zhang committed
29
30
31
32
33

    This class serves as the converter to change predictions from KITTI to
    Waymo format.

    Args:
34
        results (list[dict]): Prediction results.
Wenwei Zhang's avatar
Wenwei Zhang committed
35
36
37
38
39
40
41
        waymo_tfrecords_dir (str): Directory to load waymo raw data.
        waymo_results_save_dir (str): Directory to save converted predictions
            in waymo format (.bin files).
        waymo_results_final_path (str): Path to save combined
            predictions in waymo format (.bin file), like 'a/b/c.bin'.
        prefix (str): Prefix of filename. In general, 0 for training, 1 for
            validation and 2 for testing.
42
43
44
45
46
47
48
49
50
        classes (dict): A list of class name.
        workers (str): Number of parallel processes. Defaults to 2.
        file_client_args (str): File client for reading gt in waymo format.
            Defaults to ``dict(backend='disk')``.
        from_kitti_format (bool, optional): Whether the reuslts are kitti
            format. Defaults to False.
        idx2metainfo (Optional[dict], optional): The mapping from sample_idx to
            metainfo. The metainfo must contain the keys: 'idx2contextname' and
            'idx2timestamp'. Defaults to None.
Wenwei Zhang's avatar
Wenwei Zhang committed
51
52
53
    """

    def __init__(self,
54
55
56
57
58
59
60
61
62
63
64
65
                 results: List[dict],
                 waymo_tfrecords_dir: str,
                 waymo_results_save_dir: str,
                 waymo_results_final_path: str,
                 prefix: str,
                 classes: dict,
                 workers: int = 2,
                 file_client_args: dict = dict(backend='disk'),
                 from_kitti_format: bool = False,
                 idx2metainfo: Optional[dict] = None):

        self.results = results
Wenwei Zhang's avatar
Wenwei Zhang committed
66
67
68
69
        self.waymo_tfrecords_dir = waymo_tfrecords_dir
        self.waymo_results_save_dir = waymo_results_save_dir
        self.waymo_results_final_path = waymo_results_final_path
        self.prefix = prefix
70
        self.classes = classes
Wenwei Zhang's avatar
Wenwei Zhang committed
71
        self.workers = int(workers)
72
        self.file_client_args = file_client_args
73
74
75
76
77
78
79
80
        self.from_kitti_format = from_kitti_format
        if idx2metainfo is not None:
            self.idx2metainfo = idx2metainfo
            # If ``fast_eval``, the metainfo does not need to be read from
            # original data online. It's preprocessed offline.
            self.fast_eval = True
        else:
            self.fast_eval = False
Wenwei Zhang's avatar
Wenwei Zhang committed
81

82
        self.name2idx = {}
Wenwei Zhang's avatar
Wenwei Zhang committed
83
84
85
86
87
88
89
90

        self.k2w_cls_map = {
            'Car': label_pb2.Label.TYPE_VEHICLE,
            'Pedestrian': label_pb2.Label.TYPE_PEDESTRIAN,
            'Sign': label_pb2.Label.TYPE_SIGN,
            'Cyclist': label_pb2.Label.TYPE_CYCLIST,
        }

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        if self.from_kitti_format:
            self.T_ref_to_front_cam = np.array([[0.0, 0.0, 1.0, 0.0],
                                                [-1.0, 0.0, 0.0, 0.0],
                                                [0.0, -1.0, 0.0, 0.0],
                                                [0.0, 0.0, 0.0, 1.0]])
            # ``sample_idx`` of the sample in kitti-format is an array
            for idx, result in enumerate(results):
                if len(result['sample_idx']) > 0:
                    self.name2idx[str(result['sample_idx'][0])] = idx
        else:
            # ``sample_idx`` of the sample in the original prediction
            # is an int value.
            for idx, result in enumerate(results):
                self.name2idx[str(result['sample_idx'])] = idx

        if not self.fast_eval:
            # need to read original '.tfrecord' file
            self.get_file_names()
            # turn on eager execution for older tensorflow versions
            if int(tf.__version__.split('.')[0]) < 2:
                tf.enable_eager_execution()
Wenwei Zhang's avatar
Wenwei Zhang committed
112
113
114
115
116

        self.create_folder()

    def get_file_names(self):
        """Get file names of waymo raw data."""
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
        if 'path_mapping' in self.file_client_args:
            for path in self.file_client_args['path_mapping'].keys():
                if path in self.waymo_tfrecords_dir:
                    self.waymo_tfrecords_dir = \
                        self.waymo_tfrecords_dir.replace(
                            path, self.file_client_args['path_mapping'][path])
            from petrel_client.client import Client
            client = Client()
            contents = client.list(self.waymo_tfrecords_dir)
            self.waymo_tfrecord_pathnames = list()
            for content in sorted(list(contents)):
                if content.endswith('tfrecord'):
                    self.waymo_tfrecord_pathnames.append(
                        join(self.waymo_tfrecords_dir, content))
        else:
            self.waymo_tfrecord_pathnames = sorted(
                glob(join(self.waymo_tfrecords_dir, '*.tfrecord')))
Wenwei Zhang's avatar
Wenwei Zhang committed
134
135
136
137
        print(len(self.waymo_tfrecord_pathnames), 'tfrecords found.')

    def create_folder(self):
        """Create folder for data conversion."""
138
        mmengine.mkdir_or_exist(self.waymo_results_save_dir)
Wenwei Zhang's avatar
Wenwei Zhang committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

    def parse_objects(self, kitti_result, T_k2w, context_name,
                      frame_timestamp_micros):
        """Parse one prediction with several instances in kitti format and
        convert them to `Object` proto.

        Args:
            kitti_result (dict): Predictions in kitti format.

                - name (np.ndarray): Class labels of predictions.
                - dimensions (np.ndarray): Height, width, length of boxes.
                - location (np.ndarray): Bottom center of boxes (x, y, z).
                - rotation_y (np.ndarray): Orientation of boxes.
                - score (np.ndarray): Scores of predictions.
            T_k2w (np.ndarray): Transformation matrix from kitti to waymo.
            context_name (str): Context name of the frame.
            frame_timestamp_micros (int): Frame timestamp.

        Returns:
            :obj:`Object`: Predictions in waymo dataset Object proto.
        """

        def parse_one_object(instance_idx):
162
163
            """Parse one instance in kitti format and convert them to `Object`
            proto.
Wenwei Zhang's avatar
Wenwei Zhang committed
164
165
166
167
168

            Args:
                instance_idx (int): Index of the instance to be converted.

            Returns:
169
                :obj:`Object`: Predicted instance in waymo dataset
Wenwei Zhang's avatar
Wenwei Zhang committed
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
                    Object proto.
            """
            cls = kitti_result['name'][instance_idx]
            length = round(kitti_result['dimensions'][instance_idx, 0], 4)
            height = round(kitti_result['dimensions'][instance_idx, 1], 4)
            width = round(kitti_result['dimensions'][instance_idx, 2], 4)
            x = round(kitti_result['location'][instance_idx, 0], 4)
            y = round(kitti_result['location'][instance_idx, 1], 4)
            z = round(kitti_result['location'][instance_idx, 2], 4)
            rotation_y = round(kitti_result['rotation_y'][instance_idx], 4)
            score = round(kitti_result['score'][instance_idx], 4)

            # y: downwards; move box origin from bottom center (kitti) to
            # true center (waymo)
            y -= height / 2
            # frame transformation: kitti -> waymo
            x, y, z = self.transform(T_k2w, x, y, z)

            # different conventions
            heading = -(rotation_y + np.pi / 2)
            while heading < -np.pi:
                heading += 2 * np.pi
            while heading > np.pi:
                heading -= 2 * np.pi

            box = label_pb2.Label.Box()
            box.center_x = x
            box.center_y = y
            box.center_z = z
            box.length = length
            box.width = width
            box.height = height
            box.heading = heading

            o = metrics_pb2.Object()
            o.object.box.CopyFrom(box)
            o.object.type = self.k2w_cls_map[cls]
            o.score = score

            o.context_name = context_name
            o.frame_timestamp_micros = frame_timestamp_micros

            return o

        objects = metrics_pb2.Objects()

        for instance_idx in range(len(kitti_result['name'])):
            o = parse_one_object(instance_idx)
            objects.objects.append(o)

        return objects

    def convert_one(self, file_idx):
        """Convert action for single file.

        Args:
            file_idx (int): Index of the file to be converted.
        """
        file_pathname = self.waymo_tfrecord_pathnames[file_idx]
229
230
231
232
233
234
235
        if 's3://' in file_pathname and tf.__version__ >= '2.6.0':
            try:
                import tensorflow_io as tfio  # noqa: F401
            except ImportError:
                raise ImportError(
                    "Please run 'pip install tensorflow-io' to install tensorflow_io first."  # noqa: E501
                )
Wenwei Zhang's avatar
Wenwei Zhang committed
236
237
238
239
240
241
242
243
244
245
246
247
        file_data = tf.data.TFRecordDataset(file_pathname, compression_type='')

        for frame_num, frame_data in enumerate(file_data):
            frame = open_dataset.Frame()
            frame.ParseFromString(bytearray(frame_data.numpy()))

            filename = f'{self.prefix}{file_idx:03d}{frame_num:03d}'

            context_name = frame.context.name
            frame_timestamp_micros = frame.timestamp_micros

            if filename in self.name2idx:
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
                if self.from_kitti_format:
                    for camera in frame.context.camera_calibrations:
                        # FRONT = 1, see dataset.proto for details
                        if camera.name == 1:
                            T_front_cam_to_vehicle = np.array(
                                camera.extrinsic.transform).reshape(4, 4)

                    T_k2w = T_front_cam_to_vehicle @ self.T_ref_to_front_cam

                    kitti_result = \
                        self.results[self.name2idx[filename]]
                    objects = self.parse_objects(kitti_result, T_k2w,
                                                 context_name,
                                                 frame_timestamp_micros)
                else:
                    index = self.name2idx[filename]
                    objects = self.parse_objects_from_origin(
                        self.results[index], context_name,
                        frame_timestamp_micros)

Wenwei Zhang's avatar
Wenwei Zhang committed
268
269
270
271
272
273
274
275
276
            else:
                print(filename, 'not found.')
                objects = metrics_pb2.Objects()

            with open(
                    join(self.waymo_results_save_dir, f'{filename}.bin'),
                    'wb') as f:
                f.write(objects.SerializeToString())

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
    def convert_one_fast(self, res_index: int):
        """Convert action for single file. It read the metainfo from the
        preprocessed file offline and will be faster.

        Args:
            res_index (int): The indices of the results.
        """
        sample_idx = self.results[res_index]['sample_idx']
        if len(self.results[res_index]['pred_instances_3d']) > 0:
            objects = self.parse_objects_from_origin(
                self.results[res_index],
                self.idx2metainfo[str(sample_idx)]['contextname'],
                self.idx2metainfo[str(sample_idx)]['timestamp'])
        else:
            print(sample_idx, 'not found.')
            objects = metrics_pb2.Objects()

        with open(
                join(self.waymo_results_save_dir, f'{sample_idx}.bin'),
                'wb') as f:
            f.write(objects.SerializeToString())

    def parse_objects_from_origin(self, result: dict, contextname: str,
                                  timestamp: str) -> Objects:
        """Parse obejcts from the original prediction results.

        Args:
            result (dict): The original prediction results.
            contextname (str): The ``contextname`` of sample in waymo.
            timestamp (str): The ``timestamp`` of sample in waymo.

        Returns:
            metrics_pb2.Objects: The parsed object.
        """
        lidar_boxes = result['pred_instances_3d']['bboxes_3d'].tensor
        scores = result['pred_instances_3d']['scores_3d']
        labels = result['pred_instances_3d']['labels_3d']

        def parse_one_object(index):
            class_name = self.classes[labels[index].item()]

            box = label_pb2.Label.Box()
            height = lidar_boxes[index][5].item()
            heading = lidar_boxes[index][6].item()

            while heading < -np.pi:
                heading += 2 * np.pi
            while heading > np.pi:
                heading -= 2 * np.pi

            box.center_x = lidar_boxes[index][0].item()
            box.center_y = lidar_boxes[index][1].item()
            box.center_z = lidar_boxes[index][2].item() + height / 2
            box.length = lidar_boxes[index][3].item()
            box.width = lidar_boxes[index][4].item()
            box.height = height
            box.heading = heading

            o = metrics_pb2.Object()
            o.object.box.CopyFrom(box)
            o.object.type = self.k2w_cls_map[class_name]
            o.score = scores[index].item()
            o.context_name = contextname
            o.frame_timestamp_micros = timestamp

            return o

        objects = metrics_pb2.Objects()
        for i in range(len(lidar_boxes)):
            objects.objects.append(parse_one_object(i))

        return objects

Wenwei Zhang's avatar
Wenwei Zhang committed
350
351
352
    def convert(self):
        """Convert action."""
        print('Start converting ...')
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
        convert_func = self.convert_one_fast if self.fast_eval else \
            self.convert_one

        # from torch.multiprocessing import set_sharing_strategy
        # # Force using "file_system" sharing strategy for stability
        # set_sharing_strategy("file_system")

        # mmengine.track_parallel_progress(convert_func, range(len(self)),
        #                                  self.workers)

        # TODO: Support multiprocessing. Now, multiprocessing evaluation will
        # cause shared memory error in torch-1.10 and torch-1.11. Details can
        # be seen in https://github.com/pytorch/pytorch/issues/67864.
        prog_bar = mmengine.ProgressBar(len(self))
        for i in range(len(self)):
            convert_func(i)
            prog_bar.update()

Wenwei Zhang's avatar
Wenwei Zhang committed
371
372
373
374
375
376
377
378
379
380
381
        print('\nFinished ...')

        # combine all files into one .bin
        pathnames = sorted(glob(join(self.waymo_results_save_dir, '*.bin')))
        combined = self.combine(pathnames)

        with open(self.waymo_results_final_path, 'wb') as f:
            f.write(combined.SerializeToString())

    def __len__(self):
        """Length of the filename list."""
382
383
        return len(self.results) if self.fast_eval else len(
            self.waymo_tfrecord_pathnames)
Wenwei Zhang's avatar
Wenwei Zhang committed
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

    def transform(self, T, x, y, z):
        """Transform the coordinates with matrix T.

        Args:
            T (np.ndarray): Transformation matrix.
            x(float): Coordinate in x axis.
            y(float): Coordinate in y axis.
            z(float): Coordinate in z axis.

        Returns:
            list: Coordinates after transformation.
        """
        pt_bef = np.array([x, y, z, 1.0]).reshape(4, 1)
        pt_aft = np.matmul(T, pt_bef)
        return pt_aft[:3].flatten().tolist()

    def combine(self, pathnames):
        """Combine predictions in waymo format for each sample together.

        Args:
            pathnames (str): Paths to save predictions.

        Returns:
            :obj:`Objects`: Combined predictions in Objects proto.
        """
        combined = metrics_pb2.Objects()

        for pathname in pathnames:
            objects = metrics_pb2.Objects()
            with open(pathname, 'rb') as f:
                objects.ParseFromString(f.read())
            for o in objects.objects:
                combined.objects.append(o)

        return combined