-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generators.py
349 lines (257 loc) · 11 KB
/
Generators.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
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
import numpy as np
import PIL
from random import shuffle
import random
import keras
import cv2
import math
from keras.preprocessing.image import load_img
def getFrames(videoPath, startFrame, numFrames, COLOR):
cap = cv2.VideoCapture(videoPath)
frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
cap.set(1,startFrame)
frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
buf = []
fc = startFrame
ret = True
counter = numFrames-1
while (fc < startFrame + numFrames):
ret, frame = cap.read()
if COLOR:
buf.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
else:
buf.append(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY))
fc += 1
counter -= 1
cap.release()
return buf
class DepthEstGenerator(keras.utils.Sequence):
def __init__(self, data, batch_size=6, dim=(768, 1024), n_channels=3, shuffle=True):
"""
Initialization
"""
self.data = data
self.indices = self.data.index.tolist()
self.dim = dim
self.n_channels = n_channels
self.batch_size = batch_size
self.shuffle = shuffle
self.min_depth = 0.1
self.on_epoch_end()
def __len__(self):
return int(np.ceil(len(self.data) / self.batch_size))
def __getitem__(self, index):
if (index + 1) * self.batch_size > len(self.indices):
self.batch_size = len(self.indices) - index * self.batch_size
# Generate one batch of data
# Generate indices of the batch
index = self.indices[index * self.batch_size : (index + 1) * self.batch_size]
# Find list of IDs
batch = [self.indices[k] for k in index]
x, y = self.data_generation(batch)
return x, y
def on_epoch_end(self):
"""
Updates indexes after each epoch
"""
self.index = np.arange(len(self.indices))
if self.shuffle == True:
np.random.shuffle(self.index)
def load(self, image_path, depth_map, mask):
"""Load input and target image."""
image_ = cv2.imread(image_path)
image_ = cv2.cvtColor(image_, cv2.COLOR_BGR2RGB)
image_ = cv2.resize(image_, self.dim)
image_ = tf.image.convert_image_dtype(image_, tf.float32)
depth_map = np.load(depth_map).squeeze()
mask = np.load(mask)
mask = mask > 0
max_depth = min(300, np.percentile(depth_map, 99))
depth_map = np.clip(depth_map, self.min_depth, max_depth)
depth_map = np.log(depth_map, where=mask)
depth_map = np.ma.masked_where(~mask, depth_map)
depth_map = np.clip(depth_map, 0.1, np.log(max_depth))
depth_map = cv2.resize(depth_map, self.dim)
depth_map = np.expand_dims(depth_map, axis=2)
depth_map = tf.image.convert_image_dtype(depth_map, tf.float32)
return image_, depth_map
def data_generation(self, batch):
x = np.empty((self.batch_size, *self.dim, self.n_channels))
y = np.empty((self.batch_size, *self.dim, 1))
for i, batch_id in enumerate(batch):
x[i,], y[i,] = self.load(
self.data["image"][batch_id],
self.data["depth"][batch_id],
self.data["mask"][batch_id],
)
return x, y
class SegmentationGenerator(keras.utils.Sequence):
"""Helper to iterate over the data (as Numpy arrays)."""
def __init__(self, batch_size, img_size, input_img_paths, target_img_paths):
self.batch_size = batch_size
self.img_size = img_size
self.input_img_paths = input_img_paths
self.target_img_paths = target_img_paths
def __len__(self):
return len(self.target_img_paths) // self.batch_size
def __getitem__(self, idx):
"""Returns tuple (input, target) correspond to batch #idx."""
i = idx * self.batch_size
batch_input_img_paths = self.input_img_paths[i : i + self.batch_size]
batch_target_img_paths = self.target_img_paths[i : i + self.batch_size]
x = np.zeros((self.batch_size,) + self.img_size + (3,), dtype="float32")
for j, path in enumerate(batch_input_img_paths):
img = load_img(path, target_size=self.img_size)
x[j] = img
y = np.zeros((self.batch_size,) + self.img_size + (1,), dtype="uint8")
for j, path in enumerate(batch_target_img_paths):
img = load_img(path, target_size=self.img_size, color_mode="RGB")
y[j] = np.expand_dims(img, 2)
# Ground truth labels are 1, 2, 3. Subtract one to make them 0, 1, 2:
y[j] -= 1
return x, y
class Generator2Images(keras.utils.Sequence):
def __init__(self, images, labels,
batch_size,
inputShape=(640, 360, 3),
shuffle=True,
COLOR=True):
self.images = images
self.labels = labels
self.batch_size = batch_size
self.inputShape = inputShape
self.shuffle = shuffle
self.imageSize = inputShape[0], inputShape[1]
self.COLOR = COLOR
self.n = len(self.labels)
def __get_input(self, image):
image.thumbnail(self.imageSize, PIL.Image.ANTIALIAS)
image = np.array(image)
image = image.astype('float32')
return image/255.
def on_epoch_end(self):
if self.shuffle:
temp = list(zip(self.images, self.labels))
random.shuffle(temp)
res1, res2 = zip(*temp)
# res1 and res2 come out as tuples, and so must be converted to lists.
self.images, self.labels = list(res1), list(res2)
def __getitem__(self, index):
imageBatch = self.images[index * self.batch_size:(index + 1) * self.batch_size]
labelBatch = self.labels[index * self.batch_size:(index + 1) * self.batch_size]
X, y = self.__get_data(imageBatch, labelBatch)
return X, y
def test_getitem(self, index):
return self.__getitem__(index)
def __len__(self):
return self.n // self.batch_size
def __get_output(self, startImages, endImages, labelBatch):
X1 = np.array(startImages, np.float32)
X2 = np.array(endImages, np.float32)
Y = np.array(list(map(int,labelBatch)), np.int64)
return X1, X2, Y
def __get_data(self, imageBatch, labelBatch):
# Generates data containing batch_size samples
startImages = []
endImages = []
for path, startFrame in imageBatch:
frames = getFrames(path, startFrame, 2, self.COLOR)
startImages.append(self.__get_input(PIL.Image.fromarray(frames[0])))
endImages.append(self.__get_input(PIL.Image.fromarray(frames[1])))
X1, X2, Y = self.__get_output(startImages, endImages, labelBatch)
return tuple([[X1, X2], Y])
class GeneratorStartImage(keras.utils.Sequence):
def __init__(self, images, labels,
batch_size,
inputShape=(640, 360, 3),
shuffle=True,
COLOR=True):
self.images = images
self.labels = labels
self.batch_size = batch_size
self.inputShape = inputShape
self.shuffle = shuffle
self.imageSize = inputShape[0], inputShape[1]
self.COLOR = COLOR
self.n = len(self.labels)
def __get_input(self, image):
image.thumbnail(self.imageSize, PIL.Image.ANTIALIAS)
image = np.array(image)
image = image.astype('float32')
return image/255.
def on_epoch_end(self):
if self.shuffle:
temp = list(zip(self.images, self.labels))
random.shuffle(temp)
res1, res2 = zip(*temp)
# res1 and res2 come out as tuples, and so must be converted to lists.
self.images, self.labels = list(res1), list(res2)
def __getitem__(self, index):
imageBatch = self.images[index * self.batch_size:(index + 1) * self.batch_size]
labelBatch = self.labels[index * self.batch_size:(index + 1) * self.batch_size]
X, y = self.__get_data(imageBatch, labelBatch)
return X, y
def test_getitem(self, index):
return self.__getitem__(index)
def __len__(self):
return self.n // self.batch_size
def __get_output(self, startImages, labelBatch):
X1 = np.array(startImages, np.float32)
Y = np.array(list(map(int,labelBatch)), np.int64)
return X1, Y
def __get_data(self, imageBatch, labelBatch):
# Generates data containing batch_size samples
startImages = []
for path, startFrame in imageBatch:
frames = getFrames(path, startFrame, 1, self.COLOR)
startImages.append(self.__get_input(PIL.Image.fromarray(frames[0])))
X1, Y = self.__get_output(startImages, labelBatch)
return tuple([X1, Y])
class GeneratorEndImage(keras.utils.Sequence):
def __init__(self, images, labels,
batch_size,
inputShape=(640, 360, 3),
shuffle=True,
COLOR=True):
self.images = images
self.labels = labels
self.batch_size = batch_size
self.inputShape = inputShape
self.shuffle = shuffle
self.imageSize = inputShape[0], inputShape[1]
self.COLOR = COLOR
self.n = len(self.labels)
def __get_input(self, image):
image.thumbnail(self.imageSize, PIL.Image.ANTIALIAS)
image = np.array(image)
image = image.astype('float32')
return image/255.
def on_epoch_end(self):
if self.shuffle:
temp = list(zip(self.images, self.labels))
random.shuffle(temp)
res1, res2 = zip(*temp)
# res1 and res2 come out as tuples, and so must be converted to lists.
self.images, self.labels = list(res1), list(res2)
def __getitem__(self, index):
imageBatch = self.images[index * self.batch_size:(index + 1) * self.batch_size]
labelBatch = self.labels[index * self.batch_size:(index + 1) * self.batch_size]
X, y = self.__get_data(imageBatch, labelBatch)
return X, y
def test_getitem(self, index):
return self.__getitem__(index)
def __len__(self):
return self.n // self.batch_size
def __get_output(self, startImages, labelBatch):
X1 = np.array(startImages, np.float32)
Y = np.array(list(map(int,labelBatch)), np.int64)
return X1, Y
def __get_data(self, imageBatch, labelBatch):
# Generates data containing batch_size samples
startImages = []
for path, startFrame in imageBatch:
frames = getFrames(path, startFrame, 1, self.COLOR)
startImages.append(self.__get_input(PIL.Image.fromarray(frames[0])))
X1, Y = self.__get_output(startImages, labelBatch)
return tuple([X1, Y])