| """ |
| Semantic KITTI dataset |
| |
| Author: Xiaoyang Wu (xiaoyang.wu.cs@gmail.com) |
| Please cite our work if the code is helpful to you. |
| """ |
|
|
| import os |
| import numpy as np |
|
|
| from .builder import DATASETS |
| from .defaults import DefaultDataset |
|
|
|
|
| @DATASETS.register_module() |
| class SemanticKITTIDataset(DefaultDataset): |
| def __init__( |
| self, |
| split="train", |
| data_root="/media/changbryan/BC_T7/itriDataset/", |
| transform=None, |
| test_mode=False, |
| test_cfg=None, |
| loop=1, |
| ignore_index=-1, |
| ): |
| self.ignore_index = ignore_index |
| self.learning_map = self.get_learning_map(ignore_index) |
| self.learning_map_inv = self.get_learning_map_inv(ignore_index) |
| super().__init__( |
| split=split, |
| data_root=data_root, |
| transform=transform, |
| test_mode=test_mode, |
| test_cfg=test_cfg, |
| loop=loop, |
| ) |
|
|
| def get_data_list(self): |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| split2seq = dict( |
| |
| |
| |
| train=[00], |
| val=[00], |
| test=[00], |
| ) |
| |
| if isinstance(self.split, str): |
| seq_list = split2seq[self.split] |
| elif isinstance(self.split, list): |
| seq_list = [] |
| for split in self.split: |
| seq_list += split2seq[split] |
| else: |
| raise NotImplementedError |
|
|
| data_list = [] |
| for seq in seq_list: |
| seq = str(seq).zfill(2) |
| seq_folder = os.path.join(self.data_root, "dataset", "sequences", seq) |
| seq_files = sorted(os.listdir(os.path.join(seq_folder, "velodyne"))) |
| |
| |
| num_files = len(seq_files) |
| num_train = int(num_files * 0.7) |
| num_val = int(num_files * 0.15) |
| num_test = num_files - num_train - num_val |
|
|
| |
| frame_sequences = np.array(range(num_files)) |
| np.random.seed(42) |
| np.random.shuffle(frame_sequences) |
| train_files = sorted(np.take(seq_files, frame_sequences[:num_train])) |
| val_files = sorted(np.take(seq_files, frame_sequences[num_train:num_train + num_val])) |
| test_files = sorted(np.take(seq_files, frame_sequences[num_train + num_val:])) |
|
|
| np.save('frameSequence.npy', frame_sequences) |
|
|
| |
| if self.split == "train": |
| data_list += [os.path.join(seq_folder, "velodyne", file) for file in train_files] |
| elif self.split == "val": |
| data_list += [os.path.join(seq_folder, "velodyne", file) for file in val_files] |
| elif self.split == "test": |
| data_list += [os.path.join(seq_folder, "velodyne", file) for file in test_files] |
|
|
| return data_list |
|
|
| def get_data(self, idx): |
| data_path = self.data_list[idx % len(self.data_list)] |
| with open(data_path, "rb") as b: |
| scan = np.fromfile(b, dtype=np.float32).reshape(-1, 4) |
| coord = scan[:, :3] |
| strength = scan[:, -1].reshape([-1, 1]) |
| |
| |
|
|
| label_file = data_path.replace("velodyne", "labels").replace(".bin", ".label") |
| if os.path.exists(label_file): |
| with open(label_file, "rb") as a: |
| segment = np.fromfile(a, dtype=np.int32).reshape(-1) |
| segment = np.vectorize(self.learning_map.__getitem__)( |
| segment & 0xFFFF |
| ).astype(np.int32) |
| else: |
| segment = np.zeros(scan.shape[0]).astype(np.int32) |
| data_dict = dict(coord=coord, strength=strength, segment=segment) |
| return data_dict |
|
|
| def get_data_name(self, idx): |
| file_path = self.data_list[idx % len(self.data_list)] |
| dir_path, file_name = os.path.split(file_path) |
| sequence_name = os.path.basename(os.path.dirname(dir_path)) |
| frame_name = os.path.splitext(file_name)[0] |
| data_name = f"{sequence_name}_{frame_name}" |
| return data_name |
|
|
| @staticmethod |
| def get_learning_map(ignore_index): |
| learning_map = { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| 0: 0, |
| |
| |
| 1: 1, |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| return learning_map |
|
|
| @staticmethod |
| def get_learning_map_inv(ignore_index): |
| learning_map_inv = { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| ignore_index: ignore_index, |
| 0: 0, |
| 1: 1, |
| |
| |
| } |
| return learning_map_inv |
|
|