-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmentation_single_lane_lidar_fcn.py
432 lines (328 loc) · 14.4 KB
/
segmentation_single_lane_lidar_fcn.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
420
421
422
423
424
425
426
427
428
429
430
431
432
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from IPython.display import clear_output
import matplotlib
import matplotlib.pyplot as plt
import os
import keras
from keras.models import Sequential, Model
from keras.layers import Activation, Dropout, UpSampling2D, Input, Dense, concatenate
from keras.layers import Conv2DTranspose, Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras import regularizers
from keras.optimizers import Adam
from dataloader import DataLoader
import numpy as np
import os
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
import cv2
def import_data():
IMAGE_DIR_PATH = '/home/jessica/Downloads/data_road/training/image_2'
MASK_DIR_PATH = '/home/jessica/Downloads/data_road/training/gt_image_2'
LIDAR_DIR_PATH = '/home/jessica/Downloads/data_road/training/lidar_2d'
# create list of PATHS
image_paths = [os.path.join(IMAGE_DIR_PATH, x) for x in os.listdir(IMAGE_DIR_PATH) if x.endswith('.png')]
mask_paths = [os.path.join(MASK_DIR_PATH, x) for x in os.listdir(MASK_DIR_PATH) if x.endswith('.png')]
lidar_paths = [os.path.join(LIDAR_DIR_PATH, x) for x in os.listdir(LIDAR_DIR_PATH) if x.endswith('.png')]
dataset = DataLoader(image_paths=image_paths,
mask_paths=mask_paths,
label_paths=lidar_paths,
image_size=[128, 128],
crop_percent=None,
channels=[3, 3],
seed=47,
lidar=True)
dataset = dataset.data_batch(batch_size=100,
augment = True,
shuffle = True)
train_images = []
train_mask = []
train_labels = []
lidar_images = []
for image, mask, lidar in dataset.take(1):
train_images.append(image)
train_mask.append(mask)
lidar_images.append(lidar)
# print(image.shape)
#train_data = np.stack((train_images, lidar_images), axis = 1)
#print(train_data.shape)
# print((train_images, train_mask))
return train_images, train_mask, lidar_images
def import_test_data():
IMAGE_DIR_PATH = '/home/jessica/Downloads/data_road/testing/image'
MASK_DIR_PATH = '/home/jessica/Downloads/data_road/testing/masks'
LIDAR_DIR_PATH = '/home/jessica/Downloads/data_road/testing/lidar_2d'
# create list of PATHS
image_paths = [os.path.join(IMAGE_DIR_PATH, x) for x in os.listdir(IMAGE_DIR_PATH) if x.endswith('.png')]
mask_paths = [os.path.join(MASK_DIR_PATH, x) for x in os.listdir(MASK_DIR_PATH) if x.endswith('.png')]
lidar_paths = [os.path.join(LIDAR_DIR_PATH, x) for x in os.listdir(LIDAR_DIR_PATH) if x.endswith('.png')]
dataset = DataLoader(image_paths=image_paths,
mask_paths=mask_paths,
label_paths=lidar_paths,
image_size=[128, 128],
crop_percent=None,
channels=[3, 3],
seed=47,
lidar = True)
dataset = dataset.data_batch(batch_size=100,
augment=False,
shuffle=False)
test_images = []
test_mask = []
test_labels = []
lidar_images = []
for image, mask, lidar in dataset.take(1):
test_images.append(image)
test_mask.append(mask)
lidar_images.append(lidar)
#test_data = np.stack((test_images, lidar_images), axis=1)
print('test', len(test_images), len(test_images[0]))
#exit()
return test_images, test_mask, lidar_images
def normalize(input_image, input_mask):
input_image = tf.cast(input_image, tf.float32) / 255.0
input_mask -= 1
return input_image, input_mask
def get_train():
TRAIN_LENGTH = 100
BATCH_SIZE = 100
BUFFER_SIZE = 1000
STEPS_PER_EPOCH = TRAIN_LENGTH // BATCH_SIZE
# train = dataset['train'].map(train_dataset, num_parallel_calls=tf.data.experimental.AUTOTUNE)
# test = dataset['test'].map(test_dataset)
train_dataset = import_data()
test_dataset = import_test_data()
# train_dataset = train.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat()
# train_dataset = train_dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
# test_dataset = test.batch(BATCH_SIZE)
print("length", len(train_dataset[1][0]))
return train_dataset, test_dataset, STEPS_PER_EPOCH
def get_train2():
TRAIN_LENGTH = 100
BATCH_SIZE = 100
BUFFER_SIZE = 1000
STEPS_PER_EPOCH = TRAIN_LENGTH // BATCH_SIZE
# train = dataset['train'].map(load_image_train, num_parallel_calls=tf.data.experimental.AUTOTUNE)
# test = dataset['test'].map(load_image_test)
train, mask = import_data()
test = import_test_data()
# train_dataset = train.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat()
# train_dataset = train_dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
# test_dataset = test.batch(BATCH_SIZE)
return train, mask
def display(display_list):
plt.figure(figsize=(15, 15))
title = ['Input Image', 'True Mask', 'Predicted Mask']
for i in range(2):
plt.subplot(1, len(display_list), i+1)
plt.title(title[i])
plt.imshow(display_list[i][0])
plt.axis('off')
if len(display_list) > 2:
print(display_list[2])
plt.subplot(1, len(display_list), 3)
plt.title(title[2])
plt.imshow(tf.keras.preprocessing.image.array_to_img(display_list[2]))
plt.axis('off')
plt.show()
# print(display_list[0])
#
# cv2.imshow("Display window", display_list[0])
# cv2.imshow("Display window", display_list[1])
#
# cv2.waitkey(0)
def show_example():
train, test, lidar = get_train()
print(np.shape(test[1][0]))
print('lol', len(test[1][0]))
sample_image, sample_mask, sample_lidar = test[0][0], test[1][0], test[2][0][1]
#sample_image = train[0]
#sample_mask = mask[0]
print("lol")
display([sample_image, sample_mask, sample_lidar])
print("displayed")
#print(sample_image)
return sample_image, sample_mask, sample_lidar
def unet_model(output_channels):
batch_size = 200
epochs = 10
pool_size = (2, 2)
input_shape = [2, 128, 128, 3]
inputA = Input(shape=(128,128,3))
inputB = Input(shape=(128,128,3))
# the first branch operates on the first input
x = Dense(8, activation="relu")(inputA)
x = Dense(4, activation="relu")(x)
x = Model(inputs=inputA, outputs=x)
# the second branch opreates on the second input
y = Dense(64, activation="relu")(inputB)
y = Dense(32, activation="relu")(y)
y = Dense(4, activation="relu")(y)
y = Model(inputs=inputB, outputs=y)
# combine the output of the two branches
combined = concatenate([x.output, y.output])
# apply a FC layer and then a regression prediction on the
# combined outputs
z = Dense(3, activation="relu")(combined)
#z = Dense(1, activation="linear")(z)
# our model will accept the inputs of the two branches and
# then output a single value
# model = Model(inputs=[x.input, y.input], outputs=z)
### Here is the actual neural network ###
# model = Sequential()
# inputA =
# # Normalizes incoming inputs. First layer needs the input shape to work
# model.add(BatchNormalization(input_shape=input_shape))
# Below layers were re-named for easier reading of model summary; this not necessary
# Conv Layer 1
z = Conv2D(8, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Conv1') (z)
# Conv Layer 2
z = Conv2D(16, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Conv2') (z)
# Pooling 1
z = MaxPooling2D(pool_size=pool_size)(z)
# Conv Layer 3
z = Conv2D(16, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Conv3')(z)
z = Dropout(0.2)(z)
# Conv Layer 4
z = Conv2D(32, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Conv4')(z)
z = Dropout(0.2)(z)
# Conv Layer 5
z = Conv2D(32, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Conv5')(z)
z = Dropout(0.2)(z)
# Pooling 2
z = MaxPooling2D(pool_size=pool_size)(z)
# Conv Layer 6
z = Conv2D(64, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Conv6') (z)
z = Dropout(0.2)(z)
# Conv Layer 7
z = Conv2D(64, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Conv7')(z)
z = Dropout(0.2)(z)
# Pooling 3
z = MaxPooling2D(pool_size=pool_size)(z)
# Upsample 1
z = UpSampling2D(size=pool_size)(z)
# Deconv 1
z = Conv2DTranspose(64, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Deconv1')(z)
z = Dropout(0.2)(z)
# Deconv 2
z = Conv2DTranspose(64, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Deconv2')(z)
z = Dropout(0.2)(z)
# Upsample 2
z = UpSampling2D(size=pool_size)(z)
# Deconv 3
z = Conv2DTranspose(32, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Deconv3')(z)
z = Dropout(0.2)(z)
# Deconv 4
z = Conv2DTranspose(32, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Deconv4')(z)
z = Dropout(0.2)(z)
# Deconv 5
z = Conv2DTranspose(16, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Deconv5')(z)
z = Dropout(0.2)(z)
# Upsample 3
z = UpSampling2D(size=pool_size)(z)
# Deconv 6
z = Conv2DTranspose(16, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Deconv6')(z)
# Final layer - only including one channel so 1 filter
z = Conv2DTranspose(3, (3, 3), padding='valid', strides=(1, 1), activation='relu', name='Final')(z)
model = Model(inputs=[x.input, y.input], outputs=z)
return model
def create_model():
OUTPUT_CHANNELS = 3
model = unet_model(OUTPUT_CHANNELS)
model.compile(optimizer='adam', metrics=['accuracy'], loss='mean_squared_error')
tf.keras.utils.plot_model(model, show_shapes=True)
#model.summary()
return model
def load_model():
checkpoint_path = "ncc/training_single_lane_lidar_fcn_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
#latest = tf.train.latest_checkpoint(checkpoint_dir)
# Create a new model instance
model = create_model()
# Load the previously saved weights
model.load_weights('ncc/training_single_lane_lidar_fcn_1/weights.h5')
test_images, test_labels, lidar = import_test_data()
test_images = test_images[0].numpy()
test_labels = test_labels[0].numpy()
lidar = lidar[0].numpy()
print(np.shape(test_images))
print(np.shape(test_labels))
print(np.shape(lidar))
# # Re-evaluate the model
loss, acc = model.evaluate([test_images, lidar], test_labels, verbose=1)
print("Restored model, accuracy: {:5.2f}%".format(100 * acc))
return model
def create_mask(pred_mask):
#print("mask", pred_mask)
# pred_mask = tf.argmax(pred_mask, axis=-1)
# pred_mask = pred_mask[..., tf.newaxis]
#print(pred_mask)
#print(pred_mask)
return pred_mask[0]
def show_predictions( model, dataset=None, num=1):
sample_image, sample_mask, sample_lidar = show_example()
#print("reshaped", sample_image[0,:,:,:])
sample_image = sample_image[0,:,:,:]
#print(tf.reshape(sample_image, (1,128,128,3)))
sample_image = tf.reshape(sample_image, (1,128,128,3))
if dataset:
for image, mask in dataset.take(num):
pred_mask = model.predict(image)
display([image[0], mask[0], create_mask(pred_mask)])
else:
print(sample_image.shape)
sample_lidar = [sample_lidar]
display([sample_image, sample_mask,
create_mask(model.predict([np.array(sample_image), np.array(sample_lidar)], steps=1))])
class DisplayCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
clear_output(wait=True)
show_predictions()
print ('\nSample Prediction after epoch {}\n'.format(epoch+1))
def train(model):
train_dataset, test_dataset, STEPS_PER_EPOCH = get_train()
test_image, test_mask, test_lidar = show_example()
# loss, acc = model.evaluate(test_dataset)
# print("Restored model, accuracy: {:5.2f}%".format(100 * acc))
checkpoint_path = "training_single_lanes_lidar_fcn_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
# Create a callback that saves the model's weights
# cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
# save_weights_only=True,
# verbose=1)
#return
#model = create_model()
EPOCHS = 5
VAL_SUBSPLITS = 5
BATCH_SIZE = 200
VALIDATION_STEPS = 100 // BATCH_SIZE // VAL_SUBSPLITS
print(len(train_dataset[2][0]), len(train_dataset[1][0]), len(train_dataset[0][0]))
print(len(train_dataset[2][0][0]), len(train_dataset[1][0][0]))
print(len(train_dataset[2][0][0][0]), len(train_dataset[1][0][0][0]))
print(len(train_dataset[2][0][0][0][0]), len(train_dataset[1][0][0][0][0]))
print(len(test_dataset[1][0]), len(test_dataset[0][0]), len(test_dataset[2][0]))
print(np.shape(test_dataset[1][0]), np.shape(test_dataset[0][0]), np.shape(test_dataset[2][0]))
model_history = model.fit([train_dataset[0][0], train_dataset[2][0]], train_dataset[1][0], epochs=EPOCHS,
#batch_size=BATCH_SIZE,
steps_per_epoch=100,
validation_steps=100,
validation_data=([test_dataset[0][0], test_dataset[2][0]], test_dataset[1][0]))
#callbacks=[cp_callback])
# model.fit_generator((train_dataset[0][0], train_dataset[1][0]),
# #steps_per_epoch=STEPS_PER_EPOCH,
# epochs=EPOCHS, verbose=0, validation_data=(test_dataset[0][0], test_dataset[1][0]))
model.save_weights('training_single_lane_lidar_fcn_1/weights.h5')
model.save('training_single_lane_lidar_fcn_1/fcn.h5')
model.summary()
show_predictions(model)
def main():
# model = create_model()
# model.summary()
model = load_model()
model.summary()
#model = keras.models.load_model('training_lanes_fcn_1/fcn.h5')
#train(model)
show_predictions(model)
if __name__ == "__main__":
main()