forked from stesha2016/lanenet-enet-hnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_lanenet.py
executable file
·251 lines (196 loc) · 9.58 KB
/
test_lanenet.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 18-5-23 上午11:33
# @Author : Luo Yao
# @Site : https://github.com/MaybeShewill-CV/lanenet-lane-detection
# @File : test_lanenet.py
# @IDE: PyCharm Community Edition
"""
测试LaneNet模型
"""
import os
import os.path as ops
import argparse
import time
import math
import tensorflow as tf
import glob
import glog as log
import numpy as np
import matplotlib.pyplot as plt
import cv2
from lanenet_model import lanenet_merge_model
from lanenet_model import lanenet_cluster
from lanenet_model import lanenet_postprocess
from config import global_config
CFG = global_config.cfg
VGG_MEAN = [103.939, 116.779, 123.68]
def init_args():
"""
:return:
"""
parser = argparse.ArgumentParser()
parser.add_argument('--image_path', type=str, help='The image path or the src image save dir')
parser.add_argument('--weights_path', type=str, help='The model weights path')
parser.add_argument('--is_batch', type=str, help='If test a batch of images', default='false')
parser.add_argument('--batch_size', type=int, help='The batch size of the test images', default=8)
parser.add_argument('--save_dir', type=str, help='Test result image save dir', default=None)
parser.add_argument('--use_gpu', type=int, help='If use gpu set 1 or 0 instead', default=1)
return parser.parse_args()
def minmax_scale(input_arr):
"""
:param input_arr:
:return:
"""
min_val = np.min(input_arr)
max_val = np.max(input_arr)
output_arr = (input_arr - min_val) * 255.0 / (max_val - min_val)
return output_arr
def test_lanenet(image_path, weights_path, use_gpu):
"""
:param image_path:
:param weights_path:
:param use_gpu:
:return:
"""
assert ops.exists(image_path), '{:s} not exist'.format(image_path)
log.info('开始读取图像数据并进行预处理')
t_start = time.time()
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
image_vis = image
image = cv2.resize(image, (512, 256), interpolation=cv2.INTER_LINEAR)
image = image - VGG_MEAN
log.info('图像读取完毕, 耗时: {:.5f}s'.format(time.time() - t_start))
input_tensor = tf.placeholder(dtype=tf.float32, shape=[1, 256, 512, 3], name='input_tensor')
phase_tensor = tf.constant(False, tf.bool)
net = lanenet_merge_model.LaneNet(phase=phase_tensor, net_flag='enet')
binary_seg_ret, instance_seg_ret = net.inference(input_tensor=input_tensor, name='lanenet_model')
cluster = lanenet_cluster.LaneNetCluster()
postprocessor = lanenet_postprocess.LaneNetPoseProcessor()
saver = tf.train.Saver()
# Set sess configuration
if use_gpu:
sess_config = tf.ConfigProto(device_count={'GPU': 1})
else:
sess_config = tf.ConfigProto(device_count={'CPU': 0})
sess_config.gpu_options.per_process_gpu_memory_fraction = CFG.TEST.GPU_MEMORY_FRACTION
sess_config.gpu_options.allow_growth = CFG.TRAIN.TF_ALLOW_GROWTH
sess_config.gpu_options.allocator_type = 'BFC'
sess = tf.Session(config=sess_config)
with sess.as_default():
saver.restore(sess=sess, save_path=weights_path)
for i in range(1):
t_start = time.time()
binary_seg_image, instance_seg_image = sess.run([binary_seg_ret, instance_seg_ret],
feed_dict={input_tensor: [image]})
t_cost = time.time() - t_start
log.info('单张图像车道线预测耗时: {:.5f}s'.format(t_cost))
# 删除一些比较小的联通区域
# binary_seg_image[0] = postprocessor.postprocess(binary_seg_image[0])
t_start = time.time()
mask_image, _, _, _ = cluster.get_lane_mask(binary_seg_ret=binary_seg_image[0],
instance_seg_ret=instance_seg_image[0])
t_cost = time.time() - t_start
log.info('单张图像车道线聚类耗时: {:.5f}s'.format(t_cost))
print(instance_seg_image.shape)
for i in range(4):
instance_seg_image[0][:, :, i] = minmax_scale(instance_seg_image[0][:, :, i])
embedding_image = np.array(instance_seg_image[0], np.uint8)
cv2.imwrite('./out/out_bin_img.png', binary_seg_image[0] * 255)
cv2.imwrite('./out/out_mask_img.png', mask_image)
cv2.imwrite('./out/out_ori_img.png', image_vis)
cv2.imwrite('./out/out_ins_img.png', embedding_image)
sess.close()
return
def test_lanenet_batch(image_dir, weights_path, batch_size, use_gpu, save_dir=None):
"""
:param image_dir:
:param weights_path:
:param batch_size:
:param use_gpu:
:param save_dir:
:return:
"""
assert ops.exists(image_dir), '{:s} not exist'.format(image_dir)
log.info('开始获取图像文件路径...')
image_path_list = glob.glob('{:s}/**/*.jpg'.format(image_dir), recursive=True) + \
glob.glob('{:s}/**/*.png'.format(image_dir), recursive=True) + \
glob.glob('{:s}/**/*.jpeg'.format(image_dir), recursive=True)
input_tensor = tf.placeholder(dtype=tf.float32, shape=[None, 256, 512, 3], name='input_tensor')
phase_tensor = tf.constant('test', tf.string)
net = lanenet_merge_model.LaneNet(phase=phase_tensor, net_flag='vgg')
binary_seg_ret, instance_seg_ret = net.inference(input_tensor=input_tensor, name='lanenet_model')
cluster = lanenet_cluster.LaneNetCluster()
postprocessor = lanenet_postprocess.LaneNetPoseProcessor()
saver = tf.train.Saver()
# Set sess configuration
if use_gpu:
sess_config = tf.ConfigProto(device_count={'GPU': 1})
else:
sess_config = tf.ConfigProto(device_count={'GPU': 0})
sess_config.gpu_options.per_process_gpu_memory_fraction = CFG.TEST.GPU_MEMORY_FRACTION
sess_config.gpu_options.allow_growth = CFG.TRAIN.TF_ALLOW_GROWTH
sess_config.gpu_options.allocator_type = 'BFC'
sess = tf.Session(config=sess_config)
with sess.as_default():
saver.restore(sess=sess, save_path=weights_path)
epoch_nums = int(math.ceil(len(image_path_list) / batch_size))
for epoch in range(epoch_nums):
log.info('[Epoch:{:d}] 开始图像读取和预处理...'.format(epoch))
t_start = time.time()
image_path_epoch = image_path_list[epoch * batch_size:(epoch + 1) * batch_size]
image_list_epoch = [cv2.imread(tmp, cv2.IMREAD_COLOR) for tmp in image_path_epoch]
image_vis_list = image_list_epoch
image_list_epoch = [cv2.resize(tmp, (512, 256), interpolation=cv2.INTER_LINEAR)
for tmp in image_list_epoch]
image_list_epoch = [tmp - VGG_MEAN for tmp in image_list_epoch]
t_cost = time.time() - t_start
log.info('[Epoch:{:d}] 预处理{:d}张图像, 共耗时: {:.5f}s, 平均每张耗时: {:.5f}'.format(
epoch, len(image_path_epoch), t_cost, t_cost / len(image_path_epoch)))
t_start = time.time()
binary_seg_images, instance_seg_images = sess.run(
[binary_seg_ret, instance_seg_ret], feed_dict={input_tensor: image_list_epoch})
t_cost = time.time() - t_start
log.info('[Epoch:{:d}] 预测{:d}张图像车道线, 共耗时: {:.5f}s, 平均每张耗时: {:.5f}s'.format(
epoch, len(image_path_epoch), t_cost, t_cost / len(image_path_epoch)))
cluster_time = []
for index, binary_seg_image in enumerate(binary_seg_images):
t_start = time.time()
binary_seg_image = postprocessor.postprocess(binary_seg_image)
mask_image = cluster.get_lane_mask(binary_seg_ret=binary_seg_image,
instance_seg_ret=instance_seg_images[index])
cluster_time.append(time.time() - t_start)
mask_image = cv2.resize(mask_image, (image_vis_list[index].shape[1],
image_vis_list[index].shape[0]),
interpolation=cv2.INTER_LINEAR)
if save_dir is None:
plt.ion()
plt.figure('mask_image')
plt.imshow(mask_image[:, :, (2, 1, 0)])
plt.figure('src_image')
plt.imshow(image_vis_list[index][:, :, (2, 1, 0)])
plt.pause(3.0)
plt.show()
plt.ioff()
if save_dir is not None:
mask_image = cv2.addWeighted(image_vis_list[index], 1.0, mask_image, 1.0, 0)
image_name = ops.split(image_path_epoch[index])[1]
image_save_path = ops.join(save_dir, image_name)
cv2.imwrite(image_save_path, mask_image)
log.info('[Epoch:{:d}] 进行{:d}张图像车道线聚类, 共耗时: {:.5f}s, 平均每张耗时: {:.5f}'.format(
epoch, len(image_path_epoch), np.sum(cluster_time), np.mean(cluster_time)))
sess.close()
return
if __name__ == '__main__':
# init args
args = init_args()
if args.save_dir is not None and not ops.exists(args.save_dir):
log.error('{:s} not exist and has been made'.format(args.save_dir))
os.makedirs(args.save_dir)
if args.is_batch.lower() == 'false':
# test hnet model on single image
test_lanenet(args.image_path, args.weights_path, args.use_gpu)
else:
# test hnet model on a batch of image
test_lanenet_batch(image_dir=args.image_path, weights_path=args.weights_path,
save_dir=args.save_dir, use_gpu=args.use_gpu, batch_size=args.batch_size)