-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkitti360_dataset.py
237 lines (184 loc) · 15.3 KB
/
kitti360_dataset.py
1
2
3
4
5
6
7
8
9
10
11
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
# THIS IS WHERE YOU CREATE THE DATA FETCHING FOR KITTI360 IMG, SEGM AND DEPTH!
import os
import numpy as np
from glob import glob
from PIL import Image
from skimage.transform import resize
from collections import namedtuple
from torch.utils.data import Dataset
class Kitti360Dataset(Dataset):
"""Loads KITTI-360 Dataset"""
# Source from https://github.com/autonomousvision/kitti360Scripts/blob/master/kitti360scripts/helpers/labels.py
# a label and all meta information
Label = namedtuple( 'Label' , [
'name' , # The identifier of this label, e.g. 'car', 'person', ... .
# We use them to uniquely name a class
'id' , # An integer ID that is associated with this label.
# The IDs are used to represent the label in ground truth images
# An ID of -1 means that this label does not have an ID and thus
# is ignored when creating ground truth images (e.g. license plate).
# Do not modify these IDs, since exactly these IDs are expected by the
# evaluation server.
'kittiId' , # An integer ID that is associated with this label for KITTI-360
# NOT FOR RELEASING
'trainId' , # Feel free to modify these IDs as suitable for your method. Then create
# ground truth images with train IDs, using the tools provided in the
# 'preparation' folder. However, make sure to validate or submit results
# to our evaluation server using the regular IDs above!
# For trainIds, multiple labels might have the same ID. Then, these labels
# are mapped to the same class in the ground truth images. For the inverse
# mapping, we use the label that is defined first in the list below.
# For example, mapping all void-type classes to the same ID in training,
# might make sense for some approaches.
# Max value is 255!
'category' , # The name of the category that this label belongs to
'categoryId' , # The ID of this category. Used to create ground truth images
# on category level.
'hasInstances', # Whether this label distinguishes between single instances or not
'ignoreInEval', # Whether pixels having this class as ground truth label are ignored
# during evaluations or not
'ignoreInInst', # Whether pixels having this class as ground truth label are ignored
# during evaluations of instance segmentation or not
'color' , # The color of this label
] )
#--------------------------------------------------------------------------------
# A list of all labels
#--------------------------------------------------------------------------------
# Please adapt the train IDs as appropriate for your approach.
# Note that you might want to ignore labels with ID 255 during training.
# Further note that the current train IDs are only a suggestion. You can use whatever you like.
# Make sure to provide your results using the original IDs and not the training IDs.
# Note that many IDs are ignored in evaluation and thus you never need to predict these!
labels = [
# name id kittiId, trainId category catId hasInstances ignoreInEval ignoreInInst color
Label( 'unlabeled' , 0 , -1 , 255 , 'void' , 0 , False , True , True , ( 0, 0, 0) ),
Label( 'ego vehicle' , 1 , -1 , 255 , 'void' , 0 , False , True , True , ( 0, 0, 0) ),
Label( 'rectification border' , 2 , -1 , 255 , 'void' , 0 , False , True , True , ( 0, 0, 0) ),
Label( 'out of roi' , 3 , -1 , 255 , 'void' , 0 , False , True , True , ( 0, 0, 0) ),
Label( 'static' , 4 , -1 , 255 , 'void' , 0 , False , True , True , ( 0, 0, 0) ),
Label( 'dynamic' , 5 , -1 , 255 , 'void' , 0 , False , True , True , (111, 74, 0) ),
Label( 'ground' , 6 , -1 , 255 , 'void' , 0 , False , True , True , ( 81, 0, 81) ),
Label( 'road' , 7 , 1 , 0 , 'flat' , 1 , False , False , False , (128, 64,128) ),
Label( 'sidewalk' , 8 , 3 , 1 , 'flat' , 1 , False , False , False , (244, 35,232) ),
Label( 'parking' , 9 , 2 , 255 , 'flat' , 1 , False , True , True , (250,170,160) ),
Label( 'rail track' , 10 , 10, 255 , 'flat' , 1 , False , True , True , (230,150,140) ),
Label( 'building' , 11 , 11, 2 , 'construction' , 2 , True , False , False , ( 70, 70, 70) ),
Label( 'wall' , 12 , 7 , 3 , 'construction' , 2 , False , False , False , (102,102,156) ),
Label( 'fence' , 13 , 8 , 4 , 'construction' , 2 , False , False , False , (190,153,153) ),
Label( 'guard rail' , 14 , 30, 255 , 'construction' , 2 , False , True , True , (180,165,180) ),
Label( 'bridge' , 15 , 31, 255 , 'construction' , 2 , False , True , True , (150,100,100) ),
Label( 'tunnel' , 16 , 32, 255 , 'construction' , 2 , False , True , True , (150,120, 90) ),
Label( 'pole' , 17 , 21, 5 , 'object' , 3 , True , False , True , (153,153,153) ),
Label( 'polegroup' , 18 , -1 , 255 , 'object' , 3 , False , True , True , (153,153,153) ),
Label( 'traffic light' , 19 , 23, 6 , 'object' , 3 , True , False , True , (250,170, 30) ),
Label( 'traffic sign' , 20 , 24, 7 , 'object' , 3 , True , False , True , (220,220, 0) ),
Label( 'vegetation' , 21 , 5 , 8 , 'nature' , 4 , False , False , False , (107,142, 35) ),
Label( 'terrain' , 22 , 4 , 9 , 'nature' , 4 , False , False , False , (152,251,152) ),
Label( 'sky' , 23 , 9 , 10 , 'sky' , 5 , False , False , False , ( 70,130,180) ),
Label( 'person' , 24 , 19, 11 , 'human' , 6 , True , False , False , (220, 20, 60) ),
Label( 'rider' , 25 , 20, 12 , 'human' , 6 , True , False , False , (255, 0, 0) ),
Label( 'car' , 26 , 13, 13 , 'vehicle' , 7 , True , False , False , ( 0, 0,142) ),
Label( 'truck' , 27 , 14, 14 , 'vehicle' , 7 , True , False , False , ( 0, 0, 70) ),
Label( 'bus' , 28 , 34, 15 , 'vehicle' , 7 , True , False , False , ( 0, 60,100) ),
Label( 'caravan' , 29 , 16, 255 , 'vehicle' , 7 , True , True , True , ( 0, 0, 90) ),
Label( 'trailer' , 30 , 15, 255 , 'vehicle' , 7 , True , True , True , ( 0, 0,110) ),
Label( 'train' , 31 , 33, 16 , 'vehicle' , 7 , True , False , False , ( 0, 80,100) ),
Label( 'motorcycle' , 32 , 17, 17 , 'vehicle' , 7 , True , False , False , ( 0, 0,230) ),
Label( 'bicycle' , 33 , 18, 18 , 'vehicle' , 7 , True , False , False , (119, 11, 32) ),
Label( 'garage' , 34 , 12, 2 , 'construction' , 2 , True , True , True , ( 64,128,128) ),
Label( 'gate' , 35 , 6 , 4 , 'construction' , 2 , False , True , True , (190,153,153) ),
Label( 'stop' , 36 , 29, 255 , 'construction' , 2 , True , True , True , (150,120, 90) ),
Label( 'smallpole' , 37 , 22, 5 , 'object' , 3 , True , True , True , (153,153,153) ),
Label( 'lamp' , 38 , 25, 255 , 'object' , 3 , True , True , True , (0, 64, 64) ),
Label( 'trash bin' , 39 , 26, 255 , 'object' , 3 , True , True , True , (0, 128,192) ),
Label( 'vending machine' , 40 , 27, 255 , 'object' , 3 , True , True , True , (128, 64, 0) ),
Label( 'box' , 41 , 28, 255 , 'object' , 3 , True , True , True , (64, 64,128) ),
Label( 'unknown construction' , 42 , 35, 255 , 'void' , 0 , False , True , True , (102, 0, 0) ),
Label( 'unknown vehicle' , 43 , 36, 255 , 'void' , 0 , False , True , True , ( 51, 0, 51) ),
Label( 'unknown object' , 44 , 37, 255 , 'void' , 0 , False , True , True , ( 32, 32, 32) ),
Label( 'license plate' , -1 , -1, -1 , 'vehicle' , 7 , False , True , True , ( 0, 0,142) ),
]
def __init__(self, root, form_dir, train: bool = True, transform=None, target_transform=None, segm_only_transform=None, dep_only_transform=None):
super(Kitti360Dataset, self).__init__()
self.root = root
self.form_dir = form_dir
self.train = train
self.transform = transform
self.target_transform = target_transform
self.segm_only_transform = segm_only_transform
self.dep_only_transform = dep_only_transform
if not os.path.isdir(root) and not os.path.isdir(form_dir):
raise RuntimeError("Path to main Dataset directories not found or incomplete.")
path_to_txt = os.path.join(root, "data_2d_semantics/train/2013_05_28_drive_train_frames.txt") if self.train else os.path.join(root, "data_2d_semantics/train/2013_05_28_drive_val_frames.txt")
if not os.path.isfile(path_to_txt):
raise RuntimeError("Files for main Dataset not found i.e. "
"2013_05_28_drive_train_frames.txt and 2013_05_28_drive_val_frames.txt")
formatted_dirs = glob(os.path.join(form_dir, "*_sync_00"))
self.images = []
self.targets = []
with open(path_to_txt, "r") as file:
for line in file:
rgb, seg = line.split()
for formatted_dir in sorted(formatted_dirs):
fd_sync_dir = os.path.basename(formatted_dir)
rgb_sync_dir = rgb.split(os.sep)[1]
# Retrieves the *.png files
rgb_base = os.path.basename(rgb)
if rgb_sync_dir in fd_sync_dir:
# Handle missing dataset in formatted_kitti_360
if (rgb_sync_dir in "2013_05_28_drive_0005_sync_00"
and rgb_base.replace(".png", ".jpg") == "0000000771.jpg"):
skip_path = os.path.join(formatted_dir, rgb_base.replace(".png", ".jpg"))
print(f"Skipped: {skip_path}")
break
# image = Image.open(os.path.join(formatted_dir, rgb_base.replace(".png", ".jpg")))
image = os.path.join(formatted_dir, rgb_base.replace(".png", ".jpg"))
self.images.append(image)
# target_segm = Image.open(os.path.join(root, seg))
# target_segm = target_segm.resize((416, 128), Image.Resampling.NEAREST)
target_segm = os.path.join(root, seg)
# depth = np.load(os.path.join(formatted_dir, rgb_base.replace(".png", ".npy")))
# target_depth = Image.fromarray(depth)
target_depth = os.path.join(formatted_dir, rgb_base.replace(".png", ".npy"))
self.targets.append((target_segm, target_depth))
def __getitem__(self, idx):
# image = self.images[idx]
# target = self.targets[idx]
image = self.images[idx]
target_segm, target_depth = self.targets[idx]
image = Image.open(image)
target_segm = Image.open(target_segm)
target_segm = target_segm.resize((416, 128), Image.Resampling.NEAREST)
target_depth = np.load(target_depth)
target_depth = Image.fromarray(target_depth)
target = (target_segm, target_depth)
if self.transform:
image = self.transform(image)
if self.target_transform:
target = self.target_transform(target)
if self.dep_only_transform:
target_segm, target_depth = target
target_depth = self.dep_only_transform(target_depth)
target = (target_segm, target_depth)
if self.segm_only_transform:
target_segm, target_depth = target
target_segm = self.segm_only_transform(target_segm)
target = (target_segm, target_depth)
return image, target
def __len__(self):
return len(self.images)
# x = Kitti360Dataset(
# root='../../KITTI-360/',
# form_dir='../SfmLearner-Pytorch/formatted_kitti360/',
# train=False
# )
# print(len(x))
# import random
# from dataset_viewer import view_both_targets
# for i in range(10):
# idx = random.randint(0, len(x) - 1)
# img, (seg, dep) = x[idx]
# print(f"img: {np.asarray(img).dtype}")
# print(f"seg: {np.asarray(seg).dtype}")
# print(f"dep: {np.asarray(dep).dtype}")
# view_both_targets(img, seg, np.asarray(dep))