waymo_converter.py 24.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
8
try:
    from waymo_open_dataset import dataset_pb2
except ImportError:
9
10
    raise ImportError('Please run "pip install waymo-open-dataset-tf-2-5-0" '
                      '>1.4.5 to install the official devkit first.')
11

12
13
14
from glob import glob
from os.path import join

15
import mmengine
Wenwei Zhang's avatar
Wenwei Zhang committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import numpy as np
import tensorflow as tf
from waymo_open_dataset.utils import range_image_utils, transform_utils
from waymo_open_dataset.utils.frame_utils import \
    parse_range_image_and_camera_projection


class Waymo2KITTI(object):
    """Waymo to KITTI converter.

    This class serves as the converter to change the waymo raw data to KITTI
    format.

    Args:
        load_dir (str): Directory to load waymo raw data.
        save_dir (str): Directory to save data in KITTI format.
        prefix (str): Prefix of filename. In general, 0 for training, 1 for
            validation and 2 for testing.
34
        workers (int, optional): Number of workers for the parallel process.
35
36
37
38
            Defaults to 64.
        test_mode (bool, optional): Whether in the test_mode.
            Defaults to False.
        save_cam_sync_labels (bool, optional): Whether to save cam sync labels.
39
            Defaults to True.
Wenwei Zhang's avatar
Wenwei Zhang committed
40
41
42
43
44
45
46
    """

    def __init__(self,
                 load_dir,
                 save_dir,
                 prefix,
                 workers=64,
47
48
                 test_mode=False,
                 save_cam_sync_labels=True):
Wenwei Zhang's avatar
Wenwei Zhang committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        self.filter_empty_3dboxes = True
        self.filter_no_label_zone_points = True

        self.selected_waymo_classes = ['VEHICLE', 'PEDESTRIAN', 'CYCLIST']

        # Only data collected in specific locations will be converted
        # If set None, this filter is disabled
        # Available options: location_sf (main dataset)
        self.selected_waymo_locations = None
        self.save_track_id = False

        # turn on eager execution for older tensorflow versions
        if int(tf.__version__.split('.')[0]) < 2:
            tf.enable_eager_execution()

64
65
66
67
68
69
70
71
        # keep the order defined by the official protocol
        self.cam_list = [
            '_FRONT',
            '_FRONT_LEFT',
            '_FRONT_RIGHT',
            '_SIDE_LEFT',
            '_SIDE_RIGHT',
        ]
72
        self.lidar_list = ['TOP', 'FRONT', 'SIDE_LEFT', 'SIDE_RIGHT', 'REAR']
Wenwei Zhang's avatar
Wenwei Zhang committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        self.type_list = [
            'UNKNOWN', 'VEHICLE', 'PEDESTRIAN', 'SIGN', 'CYCLIST'
        ]
        self.waymo_to_kitti_class_map = {
            'UNKNOWN': 'DontCare',
            'PEDESTRIAN': 'Pedestrian',
            'VEHICLE': 'Car',
            'CYCLIST': 'Cyclist',
            'SIGN': 'Sign'  # not in kitti
        }

        self.load_dir = load_dir
        self.save_dir = save_dir
        self.prefix = prefix
        self.workers = int(workers)
        self.test_mode = test_mode
89
        self.save_cam_sync_labels = save_cam_sync_labels
Wenwei Zhang's avatar
Wenwei Zhang committed
90
91
92
93
94
95
96
97
98
99

        self.tfrecord_pathnames = sorted(
            glob(join(self.load_dir, '*.tfrecord')))

        self.label_save_dir = f'{self.save_dir}/label_'
        self.label_all_save_dir = f'{self.save_dir}/label_all'
        self.image_save_dir = f'{self.save_dir}/image_'
        self.calib_save_dir = f'{self.save_dir}/calib'
        self.point_cloud_save_dir = f'{self.save_dir}/velodyne'
        self.pose_save_dir = f'{self.save_dir}/pose'
100
        self.timestamp_save_dir = f'{self.save_dir}/timestamp'
101
102
103
104
        if self.save_cam_sync_labels:
            self.cam_sync_label_save_dir = f'{self.save_dir}/cam_sync_label_'
            self.cam_sync_label_all_save_dir = \
                f'{self.save_dir}/cam_sync_label_all'
Wenwei Zhang's avatar
Wenwei Zhang committed
105
106
107
108
109
110

        self.create_folder()

    def convert(self):
        """Convert action."""
        print('Start converting ...')
111
112
        mmengine.track_parallel_progress(self.convert_one, range(len(self)),
                                         self.workers)
Wenwei Zhang's avatar
Wenwei Zhang committed
113
114
115
116
117
118
119
120
121
122
123
124
125
        print('\nFinished ...')

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

        Args:
            file_idx (int): Index of the file to be converted.
        """
        pathname = self.tfrecord_pathnames[file_idx]
        dataset = tf.data.TFRecordDataset(pathname, compression_type='')

        for frame_idx, data in enumerate(dataset):

126
            frame = dataset_pb2.Frame()
Wenwei Zhang's avatar
Wenwei Zhang committed
127
128
129
130
131
132
133
134
            frame.ParseFromString(bytearray(data.numpy()))
            if (self.selected_waymo_locations is not None
                    and frame.context.stats.location
                    not in self.selected_waymo_locations):
                continue

            self.save_image(frame, file_idx, frame_idx)
            self.save_calib(frame, file_idx, frame_idx)
135
            if 'testing_3d_camera_only_detection' not in self.load_dir:
136
                # the camera only split doesn't contain lidar points.
137
                self.save_lidar(frame, file_idx, frame_idx)
Wenwei Zhang's avatar
Wenwei Zhang committed
138
            self.save_pose(frame, file_idx, frame_idx)
139
            self.save_timestamp(frame, file_idx, frame_idx)
Wenwei Zhang's avatar
Wenwei Zhang committed
140
141

            if not self.test_mode:
142
                # TODO save the depth image for waymo challenge solution.
Wenwei Zhang's avatar
Wenwei Zhang committed
143
                self.save_label(frame, file_idx, frame_idx)
144
145
                if self.save_cam_sync_labels:
                    self.save_label(frame, file_idx, frame_idx, cam_sync=True)
Wenwei Zhang's avatar
Wenwei Zhang committed
146
147
148
149
150
151

    def __len__(self):
        """Length of the filename list."""
        return len(self.tfrecord_pathnames)

    def save_image(self, frame, file_idx, frame_idx):
152
        """Parse and save the images in jpg format.
Wenwei Zhang's avatar
Wenwei Zhang committed
153
154
155
156
157
158
159
160
161

        Args:
            frame (:obj:`Frame`): Open dataset frame proto.
            file_idx (int): Current file index.
            frame_idx (int): Current frame index.
        """
        for img in frame.images:
            img_path = f'{self.image_save_dir}{str(img.name - 1)}/' + \
                f'{self.prefix}{str(file_idx).zfill(3)}' + \
162
                f'{str(frame_idx).zfill(3)}.jpg'
163
164
            with open(img_path, 'wb') as fp:
                fp.write(img.image)
Wenwei Zhang's avatar
Wenwei Zhang committed
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

    def save_calib(self, frame, file_idx, frame_idx):
        """Parse and save the calibration data.

        Args:
            frame (:obj:`Frame`): Open dataset frame proto.
            file_idx (int): Current file index.
            frame_idx (int): Current frame index.
        """
        # waymo front camera to kitti reference camera
        T_front_cam_to_ref = np.array([[0.0, -1.0, 0.0], [0.0, 0.0, -1.0],
                                       [1.0, 0.0, 0.0]])
        camera_calibs = []
        R0_rect = [f'{i:e}' for i in np.eye(3).flatten()]
        Tr_velo_to_cams = []
        calib_context = ''

        for camera in frame.context.camera_calibrations:
            # extrinsic parameters
            T_cam_to_vehicle = np.array(camera.extrinsic.transform).reshape(
                4, 4)
            T_vehicle_to_cam = np.linalg.inv(T_cam_to_vehicle)
            Tr_velo_to_cam = \
                self.cart_to_homo(T_front_cam_to_ref) @ T_vehicle_to_cam
            if camera.name == 1:  # FRONT = 1, see dataset.proto for details
                self.T_velo_to_front_cam = Tr_velo_to_cam.copy()
            Tr_velo_to_cam = Tr_velo_to_cam[:3, :].reshape((12, ))
            Tr_velo_to_cams.append([f'{i:e}' for i in Tr_velo_to_cam])

            # intrinsic parameters
            camera_calib = np.zeros((3, 4))
            camera_calib[0, 0] = camera.intrinsic[0]
            camera_calib[1, 1] = camera.intrinsic[1]
            camera_calib[0, 2] = camera.intrinsic[2]
            camera_calib[1, 2] = camera.intrinsic[3]
            camera_calib[2, 2] = 1
            camera_calib = list(camera_calib.reshape(12))
            camera_calib = [f'{i:e}' for i in camera_calib]
            camera_calibs.append(camera_calib)

        # all camera ids are saved as id-1 in the result because
        # camera 0 is unknown in the proto
        for i in range(5):
            calib_context += 'P' + str(i) + ': ' + \
                ' '.join(camera_calibs[i]) + '\n'
        calib_context += 'R0_rect' + ': ' + ' '.join(R0_rect) + '\n'
        for i in range(5):
            calib_context += 'Tr_velo_to_cam_' + str(i) + ': ' + \
                ' '.join(Tr_velo_to_cams[i]) + '\n'

        with open(
                f'{self.calib_save_dir}/{self.prefix}' +
                f'{str(file_idx).zfill(3)}{str(frame_idx).zfill(3)}.txt',
                'w+') as fp_calib:
            fp_calib.write(calib_context)
            fp_calib.close()

    def save_lidar(self, frame, file_idx, frame_idx):
        """Parse and save the lidar data in psd format.

        Args:
            frame (:obj:`Frame`): Open dataset frame proto.
            file_idx (int): Current file index.
            frame_idx (int): Current frame index.
        """
230
        range_images, camera_projections, seg_labels, range_image_top_pose = \
Wenwei Zhang's avatar
Wenwei Zhang committed
231
232
233
            parse_range_image_and_camera_projection(frame)

        # First return
234
        points_0, cp_points_0, intensity_0, elongation_0, mask_indices_0 = \
Wenwei Zhang's avatar
Wenwei Zhang committed
235
236
237
238
239
240
241
242
243
244
            self.convert_range_image_to_point_cloud(
                frame,
                range_images,
                camera_projections,
                range_image_top_pose,
                ri_index=0
            )
        points_0 = np.concatenate(points_0, axis=0)
        intensity_0 = np.concatenate(intensity_0, axis=0)
        elongation_0 = np.concatenate(elongation_0, axis=0)
245
        mask_indices_0 = np.concatenate(mask_indices_0, axis=0)
Wenwei Zhang's avatar
Wenwei Zhang committed
246
247

        # Second return
248
        points_1, cp_points_1, intensity_1, elongation_1, mask_indices_1 = \
Wenwei Zhang's avatar
Wenwei Zhang committed
249
250
251
252
253
254
255
256
257
258
            self.convert_range_image_to_point_cloud(
                frame,
                range_images,
                camera_projections,
                range_image_top_pose,
                ri_index=1
            )
        points_1 = np.concatenate(points_1, axis=0)
        intensity_1 = np.concatenate(intensity_1, axis=0)
        elongation_1 = np.concatenate(elongation_1, axis=0)
259
        mask_indices_1 = np.concatenate(mask_indices_1, axis=0)
Wenwei Zhang's avatar
Wenwei Zhang committed
260
261
262
263

        points = np.concatenate([points_0, points_1], axis=0)
        intensity = np.concatenate([intensity_0, intensity_1], axis=0)
        elongation = np.concatenate([elongation_0, elongation_1], axis=0)
264
265
266
        mask_indices = np.concatenate([mask_indices_0, mask_indices_1], axis=0)

        # timestamp = frame.timestamp_micros * np.ones_like(intensity)
Wenwei Zhang's avatar
Wenwei Zhang committed
267
268
269

        # concatenate x,y,z, intensity, elongation, timestamp (6-dim)
        point_cloud = np.column_stack(
270
            (points, intensity, elongation, mask_indices))
Wenwei Zhang's avatar
Wenwei Zhang committed
271
272
273
274
275

        pc_path = f'{self.point_cloud_save_dir}/{self.prefix}' + \
            f'{str(file_idx).zfill(3)}{str(frame_idx).zfill(3)}.bin'
        point_cloud.astype(np.float32).tofile(pc_path)

276
    def save_label(self, frame, file_idx, frame_idx, cam_sync=False):
Wenwei Zhang's avatar
Wenwei Zhang committed
277
278
279
280
281
282
283
284
285
286
287
        """Parse and save the label data in txt format.
        The relation between waymo and kitti coordinates is noteworthy:
        1. x, y, z correspond to l, w, h (waymo) -> l, h, w (kitti)
        2. x-y-z: front-left-up (waymo) -> right-down-front(kitti)
        3. bbox origin at volumetric center (waymo) -> bottom center (kitti)
        4. rotation: +x around y-axis (kitti) -> +x around z-axis (waymo)

        Args:
            frame (:obj:`Frame`): Open dataset frame proto.
            file_idx (int): Current file index.
            frame_idx (int): Current frame index.
288
289
            cam_sync (bool, optional): Whether to save the cam sync labels.
                Defaults to False.
Wenwei Zhang's avatar
Wenwei Zhang committed
290
        """
291
292
293
294
295
296
        label_all_path = f'{self.label_all_save_dir}/{self.prefix}' + \
            f'{str(file_idx).zfill(3)}{str(frame_idx).zfill(3)}.txt'
        if cam_sync:
            label_all_path = label_all_path.replace('label_',
                                                    'cam_sync_label_')
        fp_label_all = open(label_all_path, 'w+')
Wenwei Zhang's avatar
Wenwei Zhang committed
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
        id_to_bbox = dict()
        id_to_name = dict()
        for labels in frame.projected_lidar_labels:
            name = labels.name
            for label in labels.labels:
                # TODO: need a workaround as bbox may not belong to front cam
                bbox = [
                    label.box.center_x - label.box.length / 2,
                    label.box.center_y - label.box.width / 2,
                    label.box.center_x + label.box.length / 2,
                    label.box.center_y + label.box.width / 2
                ]
                id_to_bbox[label.id] = bbox
                id_to_name[label.id] = name - 1

        for obj in frame.laser_labels:
            bounding_box = None
            name = None
            id = obj.id
            for lidar in self.lidar_list:
                if id + lidar in id_to_bbox:
                    bounding_box = id_to_bbox.get(id + lidar)
                    name = str(id_to_name.get(id + lidar))
                    break

322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
            # NOTE: the 2D labels do not have strict correspondence with
            # the projected 2D lidar labels
            # e.g.: the projected 2D labels can be in camera 2
            # while the most_visible_camera can have id 4
            if cam_sync:
                if obj.most_visible_camera_name:
                    name = str(
                        self.cam_list.index(
                            f'_{obj.most_visible_camera_name}'))
                    box3d = obj.camera_synced_box
                else:
                    continue
            else:
                box3d = obj.box

Wenwei Zhang's avatar
Wenwei Zhang committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
            if bounding_box is None or name is None:
                name = '0'
                bounding_box = (0, 0, 0, 0)

            my_type = self.type_list[obj.type]

            if my_type not in self.selected_waymo_classes:
                continue

            if self.filter_empty_3dboxes and obj.num_lidar_points_in_box < 1:
                continue

            my_type = self.waymo_to_kitti_class_map[my_type]

351
352
353
            height = box3d.height
            width = box3d.width
            length = box3d.length
Wenwei Zhang's avatar
Wenwei Zhang committed
354

355
356
357
            x = box3d.center_x
            y = box3d.center_y
            z = box3d.center_z - height / 2
Wenwei Zhang's avatar
Wenwei Zhang committed
358
359
360
361
362
363

            # project bounding box to the virtual reference frame
            pt_ref = self.T_velo_to_front_cam @ \
                np.array([x, y, z, 1]).reshape((4, 1))
            x, y, z, _ = pt_ref.flatten().tolist()

364
            rotation_y = -box3d.heading - np.pi / 2
Wenwei Zhang's avatar
Wenwei Zhang committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
            track_id = obj.id

            # not available
            truncated = 0
            occluded = 0
            alpha = -10

            line = my_type + \
                ' {} {} {} {} {} {} {} {} {} {} {} {} {} {}\n'.format(
                    round(truncated, 2), occluded, round(alpha, 2),
                    round(bounding_box[0], 2), round(bounding_box[1], 2),
                    round(bounding_box[2], 2), round(bounding_box[3], 2),
                    round(height, 2), round(width, 2), round(length, 2),
                    round(x, 2), round(y, 2), round(z, 2),
                    round(rotation_y, 2))

            if self.save_track_id:
                line_all = line[:-1] + ' ' + name + ' ' + track_id + '\n'
            else:
                line_all = line[:-1] + ' ' + name + '\n'

386
387
388
389
390
            label_path = f'{self.label_save_dir}{name}/{self.prefix}' + \
                f'{str(file_idx).zfill(3)}{str(frame_idx).zfill(3)}.txt'
            if cam_sync:
                label_path = label_path.replace('label_', 'cam_sync_label_')
            fp_label = open(label_path, 'a')
Wenwei Zhang's avatar
Wenwei Zhang committed
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
            fp_label.write(line)
            fp_label.close()

            fp_label_all.write(line_all)

        fp_label_all.close()

    def save_pose(self, frame, file_idx, frame_idx):
        """Parse and save the pose data.

        Note that SDC's own pose is not included in the regular training
        of KITTI dataset. KITTI raw dataset contains ego motion files
        but are not often used. Pose is important for algorithms that
        take advantage of the temporal information.

        Args:
            frame (:obj:`Frame`): Open dataset frame proto.
            file_idx (int): Current file index.
            frame_idx (int): Current frame index.
        """
        pose = np.array(frame.pose.transform).reshape(4, 4)
        np.savetxt(
            join(f'{self.pose_save_dir}/{self.prefix}' +
                 f'{str(file_idx).zfill(3)}{str(frame_idx).zfill(3)}.txt'),
            pose)

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
    def save_timestamp(self, frame, file_idx, frame_idx):
        """Save the timestamp data in a separate file instead of the
        pointcloud.

        Note that SDC's own pose is not included in the regular training
        of KITTI dataset. KITTI raw dataset contains ego motion files
        but are not often used. Pose is important for algorithms that
        take advantage of the temporal information.

        Args:
            frame (:obj:`Frame`): Open dataset frame proto.
            file_idx (int): Current file index.
            frame_idx (int): Current frame index.
        """
        with open(
                join(f'{self.timestamp_save_dir}/{self.prefix}' +
                     f'{str(file_idx).zfill(3)}{str(frame_idx).zfill(3)}.txt'),
                'w') as f:
            f.write(str(frame.timestamp_micros))

Wenwei Zhang's avatar
Wenwei Zhang committed
437
438
439
440
    def create_folder(self):
        """Create folder for data preprocessing."""
        if not self.test_mode:
            dir_list1 = [
441
442
443
444
                self.label_all_save_dir,
                self.calib_save_dir,
                self.pose_save_dir,
                self.timestamp_save_dir,
Wenwei Zhang's avatar
Wenwei Zhang committed
445
446
            ]
            dir_list2 = [self.label_save_dir, self.image_save_dir]
447
448
449
            if self.save_cam_sync_labels:
                dir_list1.append(self.cam_sync_label_all_save_dir)
                dir_list2.append(self.cam_sync_label_save_dir)
Wenwei Zhang's avatar
Wenwei Zhang committed
450
451
        else:
            dir_list1 = [
452
453
                self.calib_save_dir, self.pose_save_dir,
                self.timestamp_save_dir
Wenwei Zhang's avatar
Wenwei Zhang committed
454
455
            ]
            dir_list2 = [self.image_save_dir]
456
457
        if 'testing_3d_camera_only_detection' not in self.load_dir:
            dir_list1.append(self.point_cloud_save_dir)
Wenwei Zhang's avatar
Wenwei Zhang committed
458
        for d in dir_list1:
459
            mmengine.mkdir_or_exist(d)
Wenwei Zhang's avatar
Wenwei Zhang committed
460
461
        for d in dir_list2:
            for i in range(5):
462
                mmengine.mkdir_or_exist(f'{d}{str(i)}')
Wenwei Zhang's avatar
Wenwei Zhang committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479

    def convert_range_image_to_point_cloud(self,
                                           frame,
                                           range_images,
                                           camera_projections,
                                           range_image_top_pose,
                                           ri_index=0):
        """Convert range images to point cloud.

        Args:
            frame (:obj:`Frame`): Open dataset frame.
            range_images (dict): Mapping from laser_name to list of two
                range images corresponding with two returns.
            camera_projections (dict): Mapping from laser_name to list of two
                camera projections corresponding with two returns.
            range_image_top_pose (:obj:`Transform`): Range image pixel pose for
                top lidar.
480
481
            ri_index (int, optional): 0 for the first return,
                1 for the second return. Default: 0.
Wenwei Zhang's avatar
Wenwei Zhang committed
482
483
484
485

        Returns:
            tuple[list[np.ndarray]]: (List of points with shape [N, 3],
                camera projections of points with shape [N, 6], intensity
486
487
488
                with shape [N, 1], elongation with shape [N, 1], points'
                position in the depth map (element offset if points come from
                the main lidar otherwise -1) with shape[N, 1]). All the
Wenwei Zhang's avatar
Wenwei Zhang committed
489
490
491
492
493
494
495
496
                lists have the length of lidar numbers (5).
        """
        calibrations = sorted(
            frame.context.laser_calibrations, key=lambda c: c.name)
        points = []
        cp_points = []
        intensity = []
        elongation = []
497
        mask_indices = []
Wenwei Zhang's avatar
Wenwei Zhang committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552

        frame_pose = tf.convert_to_tensor(
            value=np.reshape(np.array(frame.pose.transform), [4, 4]))
        # [H, W, 6]
        range_image_top_pose_tensor = tf.reshape(
            tf.convert_to_tensor(value=range_image_top_pose.data),
            range_image_top_pose.shape.dims)
        # [H, W, 3, 3]
        range_image_top_pose_tensor_rotation = \
            transform_utils.get_rotation_matrix(
                range_image_top_pose_tensor[..., 0],
                range_image_top_pose_tensor[..., 1],
                range_image_top_pose_tensor[..., 2])
        range_image_top_pose_tensor_translation = \
            range_image_top_pose_tensor[..., 3:]
        range_image_top_pose_tensor = transform_utils.get_transform(
            range_image_top_pose_tensor_rotation,
            range_image_top_pose_tensor_translation)
        for c in calibrations:
            range_image = range_images[c.name][ri_index]
            if len(c.beam_inclinations) == 0:
                beam_inclinations = range_image_utils.compute_inclination(
                    tf.constant(
                        [c.beam_inclination_min, c.beam_inclination_max]),
                    height=range_image.shape.dims[0])
            else:
                beam_inclinations = tf.constant(c.beam_inclinations)

            beam_inclinations = tf.reverse(beam_inclinations, axis=[-1])
            extrinsic = np.reshape(np.array(c.extrinsic.transform), [4, 4])

            range_image_tensor = tf.reshape(
                tf.convert_to_tensor(value=range_image.data),
                range_image.shape.dims)
            pixel_pose_local = None
            frame_pose_local = None
            if c.name == dataset_pb2.LaserName.TOP:
                pixel_pose_local = range_image_top_pose_tensor
                pixel_pose_local = tf.expand_dims(pixel_pose_local, axis=0)
                frame_pose_local = tf.expand_dims(frame_pose, axis=0)
            range_image_mask = range_image_tensor[..., 0] > 0

            if self.filter_no_label_zone_points:
                nlz_mask = range_image_tensor[..., 3] != 1.0  # 1.0: in NLZ
                range_image_mask = range_image_mask & nlz_mask

            range_image_cartesian = \
                range_image_utils.extract_point_cloud_from_range_image(
                    tf.expand_dims(range_image_tensor[..., 0], axis=0),
                    tf.expand_dims(extrinsic, axis=0),
                    tf.expand_dims(tf.convert_to_tensor(
                        value=beam_inclinations), axis=0),
                    pixel_pose=pixel_pose_local,
                    frame_pose=frame_pose_local)

553
554
            mask_index = tf.where(range_image_mask)

Wenwei Zhang's avatar
Wenwei Zhang committed
555
            range_image_cartesian = tf.squeeze(range_image_cartesian, axis=0)
556
            points_tensor = tf.gather_nd(range_image_cartesian, mask_index)
Wenwei Zhang's avatar
Wenwei Zhang committed
557
558
559
560

            cp = camera_projections[c.name][ri_index]
            cp_tensor = tf.reshape(
                tf.convert_to_tensor(value=cp.data), cp.shape.dims)
561
            cp_points_tensor = tf.gather_nd(cp_tensor, mask_index)
Wenwei Zhang's avatar
Wenwei Zhang committed
562
563
564
565
            points.append(points_tensor.numpy())
            cp_points.append(cp_points_tensor.numpy())

            intensity_tensor = tf.gather_nd(range_image_tensor[..., 1],
566
                                            mask_index)
Wenwei Zhang's avatar
Wenwei Zhang committed
567
568
569
            intensity.append(intensity_tensor.numpy())

            elongation_tensor = tf.gather_nd(range_image_tensor[..., 2],
570
                                             mask_index)
Wenwei Zhang's avatar
Wenwei Zhang committed
571
            elongation.append(elongation_tensor.numpy())
572
573
574
575
576
577
578
579
580
            if c.name == 1:
                mask_index = (ri_index * range_image_mask.shape[0] +
                              mask_index[:, 0]
                              ) * range_image_mask.shape[1] + mask_index[:, 1]
                mask_index = mask_index.numpy().astype(elongation[-1].dtype)
            else:
                mask_index = np.full_like(elongation[-1], -1)

            mask_indices.append(mask_index)
Wenwei Zhang's avatar
Wenwei Zhang committed
581

582
        return points, cp_points, intensity, elongation, mask_indices
Wenwei Zhang's avatar
Wenwei Zhang committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603

    def cart_to_homo(self, mat):
        """Convert transformation matrix in Cartesian coordinates to
        homogeneous format.

        Args:
            mat (np.ndarray): Transformation matrix in Cartesian.
                The input matrix shape is 3x3 or 3x4.

        Returns:
            np.ndarray: Transformation matrix in homogeneous format.
                The matrix shape is 4x4.
        """
        ret = np.eye(4)
        if mat.shape == (3, 3):
            ret[:3, :3] = mat
        elif mat.shape == (3, 4):
            ret[:3, :] = mat
        else:
            raise ValueError(mat.shape)
        return ret