forked from MaybeShewill-CV/lanenet-lane-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lanenet_data_feed_pipline.py
375 lines (291 loc) · 15 KB
/
lanenet_data_feed_pipline.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 19-4-23 下午3:54
# @Author : MaybeShewill-CV
# @Site : https://github.com/MaybeShewill-CV/lanenet-lane-detection
# @File : lanenet_data_feed_pipline.py
# @IDE: PyCharm
"""
Lanenet data feed pip line
"""
import argparse
import glob
import os
import os.path as ops
import random
import glog as log
import tensorflow as tf
from config import global_config
from data_provider import tf_io_pipline_tools
CFG = global_config.cfg
def init_args():
"""
:return:
"""
parser = argparse.ArgumentParser()
parser.add_argument('--dataset_dir', type=str, help='The source nsfw data dir path')
parser.add_argument('--tfrecords_dir', type=str, help='The dir path to save converted tfrecords')
return parser.parse_args()
class LaneNetDataProducer(object):
"""
Convert raw image file into tfrecords
"""
def __init__(self, dataset_dir):
"""
:param dataset_dir:
"""
self._dataset_dir = dataset_dir
self._gt_image_dir = ops.join(dataset_dir, 'gt_image')
self._gt_binary_image_dir = ops.join(dataset_dir, 'gt_binary_image')
self._gt_instance_image_dir = ops.join(dataset_dir, 'gt_instance_image')
self._train_example_index_file_path = ops.join(self._dataset_dir, 'train.txt')
self._test_example_index_file_path = ops.join(self._dataset_dir, 'test.txt')
self._val_example_index_file_path = ops.join(self._dataset_dir, 'val.txt')
if not self._is_source_data_complete():
raise ValueError('Source image data is not complete, '
'please check if one of the image folder is not exist')
if not self._is_training_sample_index_file_complete():
self._generate_training_example_index_file()
def generate_tfrecords(self, save_dir, step_size=10000):
"""
Generate tensorflow records file
:param save_dir:
:param step_size: generate a tfrecord every step_size examples
:return:
"""
def _read_training_example_index_file(_index_file_path):
assert ops.exists(_index_file_path)
_example_gt_path_info = []
_example_gt_binary_path_info = []
_example_gt_instance_path_info = []
with open(_index_file_path, 'r') as _file:
for _line in _file:
_example_info = _line.rstrip('\r').rstrip('\n').split(' ')
_example_gt_path_info.append(_example_info[0])
_example_gt_binary_path_info.append(_example_info[1])
_example_gt_instance_path_info.append(_example_info[2])
ret = {
'gt_path_info': _example_gt_path_info,
'gt_binary_path_info': _example_gt_binary_path_info,
'gt_instance_path_info': _example_gt_instance_path_info
}
return ret
def _split_writing_tfrecords_task(
_example_gt_paths, _example_gt_binary_paths, _example_gt_instance_paths, _flags='train'):
_split_example_gt_paths = []
_split_example_gt_binary_paths = []
_split_example_gt_instance_paths = []
_split_tfrecords_save_paths = []
for i in range(0, len(_example_gt_paths), step_size):
_split_example_gt_paths.append(_example_gt_paths[i:i + step_size])
_split_example_gt_binary_paths.append(_example_gt_binary_paths[i:i + step_size])
_split_example_gt_instance_paths.append(_example_gt_instance_paths[i:i + step_size])
if i + step_size > len(_example_gt_paths):
_split_tfrecords_save_paths.append(
ops.join(save_dir, '{:s}_{:d}_{:d}.tfrecords'.format(_flags, i, len(_example_gt_paths))))
else:
_split_tfrecords_save_paths.append(
ops.join(save_dir, '{:s}_{:d}_{:d}.tfrecords'.format(_flags, i, i + step_size)))
ret = {
'gt_paths': _split_example_gt_paths,
'gt_binary_paths': _split_example_gt_binary_paths,
'gt_instance_paths': _split_example_gt_instance_paths,
'tfrecords_paths': _split_tfrecords_save_paths
}
return ret
# make save dirs
os.makedirs(save_dir, exist_ok=True)
# start generating training example tfrecords
log.info('Start generating training example tfrecords')
# collecting train images paths info
train_image_paths_info = _read_training_example_index_file(self._train_example_index_file_path)
train_gt_images_paths = train_image_paths_info['gt_path_info']
train_gt_binary_images_paths = train_image_paths_info['gt_binary_path_info']
train_gt_instance_images_paths = train_image_paths_info['gt_instance_path_info']
# split training images according step size
train_split_result = _split_writing_tfrecords_task(
train_gt_images_paths, train_gt_binary_images_paths, train_gt_instance_images_paths, _flags='train')
train_example_gt_paths = train_split_result['gt_paths']
train_example_gt_binary_paths = train_split_result['gt_binary_paths']
train_example_gt_instance_paths = train_split_result['gt_instance_paths']
train_example_tfrecords_paths = train_split_result['tfrecords_paths']
for index, example_gt_paths in enumerate(train_example_gt_paths):
tf_io_pipline_tools.write_example_tfrecords(
example_gt_paths,
train_example_gt_binary_paths[index],
train_example_gt_instance_paths[index],
train_example_tfrecords_paths[index]
)
log.info('Generating training example tfrecords complete')
# start generating validation example tfrecords
log.info('Start generating validation example tfrecords')
# collecting validation images paths info
val_image_paths_info = _read_training_example_index_file(self._val_example_index_file_path)
val_gt_images_paths = val_image_paths_info['gt_path_info']
val_gt_binary_images_paths = val_image_paths_info['gt_binary_path_info']
val_gt_instance_images_paths = val_image_paths_info['gt_instance_path_info']
# split validation images according step size
val_split_result = _split_writing_tfrecords_task(
val_gt_images_paths, val_gt_binary_images_paths, val_gt_instance_images_paths, _flags='val')
val_example_gt_paths = val_split_result['gt_paths']
val_example_gt_binary_paths = val_split_result['gt_binary_paths']
val_example_gt_instance_paths = val_split_result['gt_instance_paths']
val_example_tfrecords_paths = val_split_result['tfrecords_paths']
for index, example_gt_paths in enumerate(val_example_gt_paths):
tf_io_pipline_tools.write_example_tfrecords(
example_gt_paths,
val_example_gt_binary_paths[index],
val_example_gt_instance_paths[index],
val_example_tfrecords_paths[index]
)
log.info('Generating validation example tfrecords complete')
# generate test example tfrecords
log.info('Start generating testing example tfrecords')
# collecting test images paths info
test_image_paths_info = _read_training_example_index_file(self._test_example_index_file_path)
test_gt_images_paths = test_image_paths_info['gt_path_info']
test_gt_binary_images_paths = test_image_paths_info['gt_binary_path_info']
test_gt_instance_images_paths = test_image_paths_info['gt_instance_path_info']
# split validating images according step size
test_split_result = _split_writing_tfrecords_task(
test_gt_images_paths, test_gt_binary_images_paths, test_gt_instance_images_paths, _flags='test')
test_example_gt_paths = test_split_result['gt_paths']
test_example_gt_binary_paths = test_split_result['gt_binary_paths']
test_example_gt_instance_paths = test_split_result['gt_instance_paths']
test_example_tfrecords_paths = test_split_result['tfrecords_paths']
for index, example_gt_paths in enumerate(test_example_gt_paths):
tf_io_pipline_tools.write_example_tfrecords(
example_gt_paths,
test_example_gt_binary_paths[index],
test_example_gt_instance_paths[index],
test_example_tfrecords_paths[index]
)
log.info('Generating testing example tfrecords complete')
return
def _is_source_data_complete(self):
"""
Check if source data complete
:return:
"""
return \
ops.exists(self._gt_binary_image_dir) and \
ops.exists(self._gt_instance_image_dir) and \
ops.exists(self._gt_image_dir)
def _is_training_sample_index_file_complete(self):
"""
Check if the training sample index file is complete
:return:
"""
return \
ops.exists(self._train_example_index_file_path) and \
ops.exists(self._test_example_index_file_path) and \
ops.exists(self._val_example_index_file_path)
def _generate_training_example_index_file(self):
"""
Generate training example index file, split source file into 0.85, 0.1, 0.05 for training,
testing and validation. Each image folder are processed separately
:return:
"""
def _gather_example_info():
"""
:return:
"""
_info = []
for _gt_image_path in glob.glob('{:s}/*.png'.format(self._gt_image_dir)):
_gt_binary_image_name = ops.split(_gt_image_path)[1]
_gt_binary_image_path = ops.join(self._gt_binary_image_dir, _gt_binary_image_name)
_gt_instance_image_name = ops.split(_gt_image_path)[1]
_gt_instance_image_path = ops.join(self._gt_instance_image_dir, _gt_instance_image_name)
assert ops.exists(_gt_binary_image_path), '{:s} not exist'.format(_gt_binary_image_path)
assert ops.exists(_gt_instance_image_path), '{:s} not exist'.format(_gt_instance_image_path)
_info.append('{:s} {:s} {:s}\n'.format(
_gt_image_path,
_gt_binary_image_path,
_gt_instance_image_path)
)
return _info
def _split_training_examples(_example_info):
random.shuffle(_example_info)
_example_nums = len(_example_info)
_train_example_info = _example_info[:int(_example_nums * 0.85)]
_val_example_info = _example_info[int(_example_nums * 0.85):int(_example_nums * 0.9)]
_test_example_info = _example_info[int(_example_nums * 0.9):]
return _train_example_info, _test_example_info, _val_example_info
train_example_info, test_example_info, val_example_info = _split_training_examples(_gather_example_info())
random.shuffle(train_example_info)
random.shuffle(test_example_info)
random.shuffle(val_example_info)
with open(ops.join(self._dataset_dir, 'train.txt'), 'w') as file:
file.write(''.join(train_example_info))
with open(ops.join(self._dataset_dir, 'test.txt'), 'w') as file:
file.write(''.join(test_example_info))
with open(ops.join(self._dataset_dir, 'val.txt'), 'w') as file:
file.write(''.join(val_example_info))
log.info('Generating training example index file complete')
return
class LaneNetDataFeeder(object):
"""
Read training examples from tfrecords for nsfw model
"""
def __init__(self, dataset_dir, flags='train'):
"""
:param dataset_dir:
:param flags:
"""
self._dataset_dir = dataset_dir
self._tfrecords_dir = ops.join(dataset_dir, 'tfrecords')
if not ops.exists(self._tfrecords_dir):
raise ValueError('{:s} not exist, please check again'.format(self._tfrecords_dir))
self._dataset_flags = flags.lower()
if self._dataset_flags not in ['train', 'test', 'val']:
raise ValueError('flags of the data feeder should be \'train\', \'test\', \'val\'')
def inputs(self, batch_size, num_epochs):
"""
dataset feed pipline input
:param batch_size:
:param num_epochs:
:return: A tuple (images, labels), where:
* images is a float tensor with shape [batch_size, H, W, C]
in the range [-0.5, 0.5].
* labels is an int32 tensor with shape [batch_size] with the true label,
a number in the range [0, CLASS_NUMS).
"""
if not num_epochs:
num_epochs = None
tfrecords_file_paths = glob.glob('{:s}/{:s}*.tfrecords'.format(
self._tfrecords_dir, self._dataset_flags)
)
random.shuffle(tfrecords_file_paths)
with tf.name_scope('input_tensor'):
# TFRecordDataset opens a binary file and reads one record at a time.
# `tfrecords_file_paths` could also be a list of filenames, which will be read in order.
dataset = tf.data.TFRecordDataset(tfrecords_file_paths)
# The map transformation takes a function and applies it to every element
# of the dataset.
dataset = dataset.map(map_func=tf_io_pipline_tools.decode,
num_parallel_calls=CFG.TRAIN.CPU_MULTI_PROCESS_NUMS)
if self._dataset_flags != 'test':
dataset = dataset.map(map_func=tf_io_pipline_tools.augment_for_train,
num_parallel_calls=CFG.TRAIN.CPU_MULTI_PROCESS_NUMS)
else:
dataset = dataset.map(map_func=tf_io_pipline_tools.augment_for_test,
num_parallel_calls=CFG.TRAIN.CPU_MULTI_PROCESS_NUMS)
dataset = dataset.map(map_func=tf_io_pipline_tools.normalize,
num_parallel_calls=CFG.TRAIN.CPU_MULTI_PROCESS_NUMS)
# The shuffle transformation uses a finite-sized buffer to shuffle elements
# in memory. The parameter is the number of elements in the buffer. For
# completely uniform shuffling, set the parameter to be the same as the
# number of elements in the dataset.
if self._dataset_flags != 'test':
dataset = dataset.shuffle(buffer_size=1000)
# repeat num epochs
dataset = dataset.repeat()
dataset = dataset.batch(batch_size, drop_remainder=True)
iterator = dataset.make_one_shot_iterator()
return iterator.get_next(name='{:s}_IteratorGetNext'.format(self._dataset_flags))
if __name__ == '__main__':
# init args
args = init_args()
assert ops.exists(args.dataset_dir), '{:s} not exist'.format(args.dataset_dir)
producer = LaneNetDataProducer(dataset_dir=args.dataset_dir)
producer.generate_tfrecords(save_dir=args.tfrecords_dir, step_size=1000)