def init_lanenet(self): ''' initlize the tensorflow model ''' self.input_tensor = tf.placeholder(dtype=tf.float32, shape=[1, 256, 512, 3], name='input_tensor') phase_tensor = tf.constant('test', tf.string) net = lanenet_merge_model.LaneNet(phase=phase_tensor, net_flag='vgg') self.binary_seg_ret, self.instance_seg_ret = net.inference(input_tensor=self.input_tensor, name='lanenet_model') self.cluster = lanenet_cluster.LaneNetCluster() self.postprocessor = lanenet_postprocess.LaneNetPoseProcessor() saver = tf.train.Saver() # Set sess configuration if self.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' self.sess = tf.Session(config=sess_config) saver.restore(sess=self.sess, save_path=self.weight_path)
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('train', 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_loss') 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}] Detection image {:s} complete'.format(epoch, image_name)) 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
def test_lanenet(image_path, weights_path, use_gpu): """ :param image_path:一张待测试的图片路径 :param weights_path:训练好的权重路径 :param use_gpu:是否使用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('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={'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) 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]) mask_image = cluster.get_lane_mask(binary_seg_ret=binary_seg_image[0], instance_seg_ret=instance_seg_image[0]) for i in range(4): # 获取矩阵的最大值和最小值并归一化到[0,255] instance_seg_image[0][:, :, i] = minmax_scale(instance_seg_image[0][:, :, i]) embedding_image = np.array(instance_seg_image[0], np.uint8)# 转换成图片的显示格式uint8(1, 256, 512, 4) plt.figure('mask_image') plt.imshow(mask_image[:, :, (2, 1, 0)]) plt.figure('src_image') plt.imshow(image_vis[:, :, (2, 1, 0)]) plt.figure('instance_image') plt.imshow(embedding_image[:, :, (2, 1, 0)])# (256, 512, 3) plt.figure('binary_image') plt.imshow(binary_seg_image[0] * 255, cmap='gray') plt.show() sess.close() return
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 org_size = (image_vis.shape[1], image_vis.shape[0]) print("org_size: ", org_size) input_tensor = tf.placeholder(dtype=tf.float32, shape=[1, 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={'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) w, h = image.shape[:2] w_part = int(w / split_num) h_part = int(h / split_num) for ii in range(0, split_num): for jj in range(0, split_num): print("ii, jj: ", ii, " ", jj) part_img = image[ii * w_part:(ii + 1) * w_part - 1, jj * h_part:(jj + 1) * h_part - 1] part_img = cv2.resize(part_img, (512, 256), interpolation=cv2.INTER_LINEAR) part_img = part_img - VGG_MEAN log.info('图像读取完毕, 耗时: {:.5f}s'.format(time.time() - t_start)) with sess.as_default(): saver.restore(sess=sess, save_path=weights_path) t_start = time.time() binary_seg_image, instance_seg_image = sess.run( [binary_seg_ret, instance_seg_ret], feed_dict={input_tensor: [part_img]}) t_cost = time.time() - t_start log.info('单张图像车道线预测耗时: {:.5f}s'.format(t_cost)) binary_seg_image[0] = postprocessor.postprocess( binary_seg_image[0]) mask_image = cluster.get_lane_mask( binary_seg_ret=binary_seg_image[0], instance_seg_ret=instance_seg_image[0]) 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) # plt.figure('mask_image') # plt.imshow(mask_image[:, :, (2, 1, 0)]) # plt.figure('src_image') # plt.imshow(image_vis[:, :, (2, 1, 0)]) # plt.figure('instance_image') # plt.imshow(embedding_image[:, :, (2, 1, 0)], cmap='gray') # plt.figure('binary_image') # plt.imshow(binary_seg_image[0] * 255, cmap='gray') # plt.show() embedding_img_list.append(embedding_image.copy()) binary_img_list.append(binary_seg_image[0] * 255) # merge to a complete embedding_image embedding_image = embedding_img_list[0].copy() binary_image = binary_img_list[0].copy() embedding_image_row = None binary_image_row = None for i in range(0, split_num): embedding_image_row = embedding_img_list[i * split_num] binary_image_row = binary_img_list[i * split_num] for j in range(0, split_num): if j > 0: embedding_image_row = np.hstack( (embedding_image_row, embedding_img_list[i * split_num + j])) binary_image_row = np.hstack( (binary_image_row, binary_img_list[i * split_num + j])) if i == 0: embedding_image = embedding_image_row.copy() binary_image = binary_image_row.copy() else: embedding_image = np.vstack((embedding_image, embedding_image_row)) binary_image = np.vstack((binary_image, binary_image_row)) #binary_image_org_size = cv2.resize(binary_image, org_size, interpolation=cv2.INTER_LINEAR) cv2.imwrite("binary_image_org_size.jpg", binary_image) instance_image_org_size = cv2.resize(embedding_image[:, :, (2, 1, 0)], org_size, interpolation=cv2.INTER_LINEAR) cv2.imwrite("instance_image_org_size.jpg", instance_image_org_size) instance_image_org_size_red = cv2.resize(embedding_image[:, :, 0], org_size, interpolation=cv2.INTER_LINEAR) cv2.imwrite("instance_image_org_size_red.jpg", instance_image_org_size_red) instance_image_org_size_green = cv2.resize(embedding_image[:, :, 1], org_size, interpolation=cv2.INTER_LINEAR) cv2.imwrite("instance_image_org_size_green.jpg", instance_image_org_size_green) instance_image_org_size_blue = cv2.resize(embedding_image[:, :, 2], org_size, interpolation=cv2.INTER_LINEAR) cv2.imwrite("instance_image_org_size_blue.jpg", instance_image_org_size_blue) # Remember -> OpenCV stores things in BGR order lowerBound_blue = np.array((33), dtype=np.uint8, ndmin=1) upperBound_blue = np.array((213), dtype=np.uint8, ndmin=1) lowerBound_red = np.array((148), dtype=np.uint8, ndmin=1) upperBound_red = np.array((208), dtype=np.uint8, ndmin=1) # this gives you the mask for those in the ranges you specified, # but you want the inverse, so we'll add bitwise_not... cv_rgb_thresh_blue = cv2.inRange(instance_image_org_size_blue, lowerBound_blue, upperBound_blue) cv_rgb_thresh_blue = cv2.bitwise_not(cv_rgb_thresh_blue) cv2.imwrite("instance_image_blue_thresh.jpg", cv_rgb_thresh_blue) th3 = cv2.adaptiveThreshold(instance_image_org_size_blue, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 13, 0) cv2.imwrite("instance_image_blue_adaptive_thresh.jpg", th3) cv_rgb_thresh_red = cv2.inRange(instance_image_org_size_red, lowerBound_red, upperBound_red) cv_rgb_thresh_red = cv2.bitwise_not(cv_rgb_thresh_red) cv2.imwrite("instance_image_red_thresh.jpg", cv_rgb_thresh_red) cv_rgb_thresh_mix = cv2.bitwise_or(cv_rgb_thresh_blue, cv_rgb_thresh_red) cv2.imwrite("instance_image_mix_thresh.jpg", cv_rgb_thresh_mix) #instance_image_org_gray = cv2.cvtColor(instance_image_org_size, cv2.COLOR_BGR2GRAY) #cv2.imwrite("instance_image_org_gray.jpg", instance_image_org_gray) sess.close() return
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)) orig = image 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('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={'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) 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]) mask_image = cluster.get_lane_mask(binary_seg_ret=binary_seg_image[0], instance_seg_ret=instance_seg_image[0]) # 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) # plt.figure('mask_image') # plt.imshow(mask_image[:, :, (2, 1, 0)]) splits = image_path.split("/") fileName = splits[-1] fileName = fileName.split(".")[0] print fileName[0:-4] path = "" for i in range(len(image_path.split("/")) - 1): path += str(splits[i]) + "/" print path np.save(os.path.join(path, fileName + "_output.npy"), mask_image[:, :, (2, 1, 0)]) # plt.figure('src_image') # plt.imshow(image_vis[:, :, (2, 1, 0)]) # plt.figure('instance_image') # plt.imshow(embedding_image[:, :, (2, 1, 0)]) # plt.figure('binary_image') # plt.imshow(binary_seg_image[0] * 255, cmap='gray') #plt.show() orig = cv2.resize(image_vis[:, :, (2, 1, 0)], (512, 256)) binary = binary_seg_image[0] * 255 for x in range(orig.shape[0]): for y in range(orig.shape[1]): if binary[x][y] > 0: orig[x][y] = (0,0,255) # plt.figure('output') # plt.imshow(orig) scipy.misc.imsave(os.path.join(path, fileName + "_output.jpg"), orig) # plt.show() sess.close() return
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('train', 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_loss') 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) 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]) mask_image = cluster.get_lane_mask( binary_seg_ret=binary_seg_image[0], instance_seg_ret=instance_seg_image[0]) # mask_image = cluster.get_lane_mask_v2(instance_seg_ret=instance_seg_image[0]) # mask_image = cv2.resize(mask_image, (image_vis.shape[1], image_vis.shape[0]), # interpolation=cv2.INTER_LINEAR) ele_mex = np.max(instance_seg_image[0], axis=(0, 1)) for i in range(3): if ele_mex[i] == 0: scale = 1 else: scale = 255 / ele_mex[i] instance_seg_image[0][:, :, i] *= int(scale) embedding_image = np.array(instance_seg_image[0], np.uint8) # cv2.imwrite('embedding_mask.png', embedding_image) # mask_image = cluster.get_lane_mask_v2(instance_seg_ret=embedding_image) # mask_image = cv2.resize(mask_image, (image_vis.shape[1], image_vis.shape[0]), # interpolation=cv2.INTER_LINEAR) cv2.imwrite('binary_ret.png', binary_seg_image[0] * 255) cv2.imwrite('instance_ret.png', embedding_image) plt.figure('mask_image') plt.imshow(mask_image[:, :, (2, 1, 0)]) plt.figure('src_image') plt.imshow(image_vis[:, :, (2, 1, 0)]) plt.figure('instance_image') plt.imshow(embedding_image[:, :, (2, 1, 0)]) plt.figure('binary_image') plt.imshow(binary_seg_image[0] * 255, cmap='gray') plt.show() sess.close() return
def test_lanenet(video_path, weights_path, use_gpu, output_path=''): #def detect_video(yolo, output_path=""): import cv2 from timeit import default_timer as timer from PIL import Image, ImageFont, ImageDraw print(video_path) vid = cv2.VideoCapture(video_path) #vid = cv2.VideoCapture(0) if not vid.isOpened(): raise IOError("Couldn't open webcam or video") video_FourCC = int(vid.get(cv2.CAP_PROP_FOURCC)) video_fps = vid.get(cv2.CAP_PROP_FPS) video_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))) isOutput = True if output_path != "" else False if isOutput: print("!!! TYPE:", type(output_path), type(video_FourCC), type(video_fps), type(video_size)) out = cv2.VideoWriter(output_path, video_FourCC, video_fps, video_size) accum_time = 0 curr_fps = 0 fps = "FPS: ??" prev_time = timer() while True: return_value, frame = vid.read() tf.reset_default_graph() image = Image.fromarray(frame) image = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR) 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('test', tf.string) net = lanenet_merge_model.LaneNet(phase=phase_tensor, net_flag='vgg') #tf.reset_default_graph() # zj添加 参考网址:https://blog.csdn.net/mr_brooks/article/details/80393396 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) 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]) mask_image = cluster.get_lane_mask( binary_seg_ret=binary_seg_image[0], instance_seg_ret=instance_seg_image[0]) 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) ''' plt.figure('mask_image') plt.imshow(mask_image[:, :, (2, 1, 0)]) plt.figure('src_image') plt.imshow(image_vis[:, :, (2, 1, 0)]) plt.figure('instance_image') plt.imshow(embedding_image[:, :, (2, 1, 0)]) plt.figure('binary_image') plt.imshow(binary_seg_image[0] * 255, cmap='gray') plt.show() ''' result = np.asarray(embedding_image[:, :, (2, 1, 0)]) curr_time = timer() exec_time = curr_time - prev_time prev_time = curr_time accum_time = accum_time + exec_time curr_fps = curr_fps + 1 if accum_time > 1: accum_time = accum_time - 1 fps = "FPS: " + str(curr_fps) curr_fps = 0 cv2.putText(result, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.50, color=(255, 0, 0), thickness=2) cv2.namedWindow("result", cv2.WINDOW_NORMAL) cv2.imshow("result", result) if isOutput: out.write(result) if cv2.waitKey(1) & 0xFF == ord('q'): break sess.close() return
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(image_path, weights_path, use_gpu): assert ops.exists(image_path), '{:s} not exist'.format(image_path) # 将原图保存为image_vis,并resize成分辨率512x256 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 # Tensorflow的创建Graph过程 input_tensor = tf.placeholder(dtype=tf.float32, shape=[1, 256, 512, 3], name='input_tensor') phase_tensor = tf.constant('test', tf.string) # 实例化LaneNet网络 net = lanenet_merge_model.LaneNet(phase=phase_tensor) 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() # 设置会话Session的全局配置 if use_gpu: sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False, device_count={'GPU': 0}) else: sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False, 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.TEST.TF_ALLOW_GROWTH sess_config.gpu_options.allocator_type = 'BFC' sess = tf.Session(config=sess_config) # Tensorflow的打开Session过程 with sess.as_default(): saver.restore(sess=sess, save_path=weights_path) 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('Predict a single image: cost_time {:.5f}s'.format(t_cost)) # 对掩模结果进行聚类以及后处理 t_start = time.time() binary_seg_image[0] = postprocessor.postprocess(binary_seg_image[0]) mask_image = cluster.get_lane_mask( binary_seg_ret=binary_seg_image[0], instance_seg_ret=instance_seg_image[0]) t_cluster = time.time() - t_start log.info('Cluster a single image: cost_time {:.5f}s'.format(t_cluster)) # 显示原图src_image和预测掩模结果图make_image plt.figure('src_image') plt.imshow(image_vis[:, :, (2, 1, 0)]) plt.figure('mask_image') plt.imshow(mask_image[:, :, (2, 1, 0)]) plt.show() # 关闭会话Session sess.close() return
def test_lanenet_batch(image_dir, weights_path, batch_size, use_gpu, save_dir): assert ops.exists(image_dir), '{:s} not exist'.format(image_dir) # 读取image_dir目录下的所有图片 log.info('Reading images...') 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) 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() if use_gpu: sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False, device_count={'GPU': 0}) else: sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False, 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.TEST.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): 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_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}] Predict {:d} images: total_cost_time {:.5f}s mean_cost_time {:.5f}s' .format(epoch + 1, 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) # 批量保存预测结果图 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}] Cluster {:d} images: total_cost_time {:.5f}s mean_cost_time {:.5f}' .format(epoch + 1, len(image_path_epoch), np.sum(cluster_time), np.mean(cluster_time))) sess.close() return
def test_lanenet_batch(self, batch_size=2, use_gpu=1): """ :param image_dir: :param weights_path: :param batch_size: :param use_gpu: :param save_dir: :return: """ assert ops.exists(self.path), '{:s} not exist'.format(self.path) assert ops.exists(self.save_path), '{:s} not exist'.format( self.save_path) assert ops.exists( self.save_processed_video_path), '{:s} not exist'.format( self.save_processed_video_path) log.info('Start getting the image file path...') image_path_list = sorted( glob.glob('{:s}/**/*.jpg'.format(self.path), recursive=True) + glob.glob('{:s}/**/*.png'.format(self.path), recursive=True) + glob.glob('{:s}/**/*.jpeg'.format(self.path), recursive=True)) input_tensor = tf.placeholder( dtype=tf.float32, shape=[2, 352, 640, 3], name='input_tensor') # 2, 640, 352, 3 #None, 256, 512, 3 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}) log.info('GPU detected, processing on GPU now..') 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=self.weights_path) epoch_nums = int(math.ceil(len(image_path_list) / batch_size)) for epoch in range(epoch_nums): log.info( '[Epoch:{:d}] Start image reading and preprocessing...'. 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, (640, 352), 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}] Pretreatment{:d}Image, total time consuming: {:.5f}s, Average Time per Sheet: {:.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}] prediction{:d}Image lane line, total time consuming: {:.5f}s, Average Time per Sheet: {:.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 self.save_path 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 self.save_path 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(self.save_path, image_name) cv2.imwrite(image_save_path, mask_image) log.info( '[Epoch:{:d}] Get on {:d}Image lane line clustering, total time consuming: {:.5f}s, Average Time per Sheet: {:.5f}' .format(epoch, len(image_path_epoch), np.sum(cluster_time), np.mean(cluster_time))) sess.close() return
def test_lanenet(self, 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('Start reading image data and pre-processing') 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('Image is read, time taken {:.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('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}) log.info('GPU detected, processing on GPU now..') 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=self.weights_path) 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( 'Single image lane line prediction time consuming: {:.5f}s'. format(t_cost)) binary_seg_image[0] = postprocessor.postprocess( binary_seg_image[0]) mask_image = cluster.get_lane_mask( binary_seg_ret=binary_seg_image[0], instance_seg_ret=instance_seg_image[0]) for i in range(4): instance_seg_image[0][:, :, i] = self.minmax_scale( instance_seg_image[0][:, :, i]) embedding_image = np.array(instance_seg_image[0], np.uint8) plt.figure('mask_image') plt.imshow(mask_image[:, :, (2, 1, 0)]) plt.figure('src_image') plt.imshow(image_vis[:, :, (2, 1, 0)]) plt.figure('instance_image') plt.imshow(embedding_image[:, :, (2, 1, 0)]) plt.figure('binary_image') plt.imshow(binary_seg_image[0] * 255, cmap='gray') plt.show() sess.close() return
def test_lanenet(image_path, weights_path, use_gpu, save_dir): """ :param save_dir: :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 / 128.0 - 1.0 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('test', tf.string) net = lanenet_merge_model.LaneNet(phase=phase_tensor, net_flag='mobilenet') binary_seg_ret, instance_seg_ret, _ = net.inference( input_tensor=input_tensor, name='lanenet_model') binary_seg_ret_32 = tf.cast(binary_seg_ret, tf.int32, name="binary_seg") cluster = lanenet_cluster.LaneNetCluster() postprocessor = lanenet_postprocess.LaneNetPoseProcessor() # Set tf saver # if weights_path is not None: # var_map = restore_from_classification_checkpoint_fn("lanenet_model/inference") # available_var_map = (get_variables_available_in_checkpoint( # var_map, weights_path, include_global_step=False)) # # saver = tf.train.Saver(available_var_map) saver = tf.train.Saver() iter_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': 1}) 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) t_start = time.time() binary_seg_image, instance_seg_image = sess.run( [binary_seg_ret_32, 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]) mask_image = cluster.get_lane_mask( binary_seg_ret=binary_seg_image[0], instance_seg_ret=instance_seg_image[0]) 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) plt.figure('mask_image') plt.imshow(mask_image[:, :, (2, 1, 0)]) plt.figure('src_image') plt.imshow(image_vis[:, :, (2, 1, 0)]) plt.figure('instance_image') plt.imshow(embedding_image[:, :, (3, 1, 0)]) plt.figure('binary_image') plt.imshow(binary_seg_image[0] * 255, cmap='gray') plt.show() mask_image = mask_image[:, :, (2, 1, 0)] image_name = ops.split(image_path)[1] image_save_path = ops.join(save_dir, image_name) cv2.imwrite(image_save_path, mask_image) iter_saver.save(sess=sess, save_path=save_dir + "inference_models/model20.ckpt") tf.train.write_graph(sess.graph.as_graph_def(), save_dir + "inference_models/", "graph20.pb") # tf.train.write_graph(graph_or_graph_def=sess.graph, logdir='', name='{:s}/lanenet_model.pb'.format(save_dir)) sess.close() return
def test_lanenet_batch(image_dir, weights_path, batch_size, use_gpu, save_dir=None, encoder="vgg"): """ :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=encoder) binary_seg_ret, instance_seg_ret, prob_seg_ret = net.inference( input_tensor=input_tensor, name='lanenet_model') cluster = lanenet_cluster.LaneNetCluster() postprocessor = lanenet_postprocess.LaneNetPoseProcessor() # Set tf saver # if weights_path is not None: # var_map = restore_from_classification_checkpoint_fn("") # available_var_map = (get_variables_available_in_checkpoint( # var_map, weights_path, include_global_step=False)) # # saver = tf.train.Saver(available_var_map) saver = tf.train.Saver() # Set sess configuration if use_gpu: sess_config = tf.ConfigProto(device_count={'GPU': 1}) # sess_config = tf.ConfigProto(device_count={'CPU': 0}) else: sess_config = tf.ConfigProto(device_count={'CPU': 1}) # 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' ignore_labels = cv2.imread( '/media/remus/datasets/AVMSnapshots/AVM/ignore_labels.png') ignore_labels = cv2.cvtColor(ignore_labels, cv2.COLOR_BGR2GRAY) 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}] starts image reading and preprocessing...'. 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 ] if encoder == "mobilenet": image_list_epoch = [ tmp / 128.0 - 1.0 for tmp in image_list_epoch ] else: image_list_epoch = [tmp - VGG_MEAN for tmp in image_list_epoch] t_cost = time.time() - t_start log.info( '[Epoch:{:d}] preprocesses {:d} images, total time: {:.5f}s, average time per sheet: {:.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, prob_seg_images = sess.run( [binary_seg_ret, instance_seg_ret, prob_seg_ret], feed_dict={input_tensor: image_list_epoch}) t_cost = time.time() - t_start log.info( '[Epoch:{:d}] predicts {:d} image lane lines, total time: {:.5f}s, average time per sheet: {:.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[ignore_labels == 0] = 0 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_NEAREST) _instance_seg_images = np.copy(instance_seg_images) prob_seg_image = prob_seg_images[index, :, :, 1] for i in range(4): _instance_seg_images[index][:, :, i] = minmax_scale( instance_seg_images[index][:, :, i]) _embedding_image = np.array(_instance_seg_images[index], np.uint8) 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", image_name) mask_save_path = ops.join(save_dir + "/mask", image_name) prob_save_path = ops.join(save_dir + "/prob", image_name) embedding_save_path = ops.join(save_dir + "/embedding", image_name) cv2.imwrite(mask_save_path, binary_seg_image * 255) cv2.imwrite(prob_save_path, prob_seg_image * 255) cv2.imwrite(image_save_path, mask_image) cv2.imwrite(embedding_save_path, _embedding_image[:, :, (2, 1, 0)]) # cv2.imwrite(embedding_save_path + "_", _embedding_image[:, :, (3, 2, 1)]) log.info( '[Epoch:{:d}] performs {:d} image lane line clustering, which takes a total of time: {:.5f}s, average time per sheet: {:.5f}' .format(epoch, len(image_path_epoch), np.sum(cluster_time), np.mean(cluster_time))) sess.close() return
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('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={'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) 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]) #二值图转换为灰度图 gray=np.array(binary_seg_image[0]*255) """ for i in range(gray.shape[0]): for j in range(gray.shape[1]): pv=gray[i][j] if pv!=0: print(pv) """ plt.imshow(gray, cmap='gray') plt.show() cv2.imshow('gray',binary_seg_image[0]) cv2.waitKey(0) sess.close() return """ plt.figure('binary_image') plt.imshow(gray, cmap='gray') plt.show() #expend_line.expend_lines(gray) """ #binary_image=cv2.cvtColor(binary_image,cv2.COLOR_RGB2GRAY) #cv2.imshow('test',binary_image) #cv2.waitKey(0) """
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('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={'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) print("TEST") with sess.as_default(): tf.train.write_graph(sess.graph_def, '.', "graph_full.pb") saver.restore(sess=sess, save_path=weights_path) 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)) tf.train.write_graph(sess.graph_def, '.', 'graph.pb') #saver.save(sess=sess, save_path="lanenet_model") binary_seg_image[0] = postprocessor.postprocess(binary_seg_image[0]) mask_image = cluster.get_lane_mask( binary_seg_ret=binary_seg_image[0], instance_seg_ret=instance_seg_image[0]) print("TEST2") # pb extraction #output_node_names =[n.name for n in tf.get_default_graph().as_graph_def().node] #output_node_names = ['binary_seg_ret','instance_seg_ret'] #print(' '.join(output_node_names)) #frozen_graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, output_node_names) #with open('output_graph3.pb', 'wb') as f: # f.write(frozen_graph.SerializeToString()) # end pb extraction 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) plt.figure('mask_image') plt.imshow(mask_image[:, :, (2, 1, 0)]) plt.figure('src_image') plt.imshow(image_vis[:, :, (2, 1, 0)]) plt.figure('instance_image') plt.imshow(embedding_image[:, :, (2, 1, 0)]) plt.figure('binary_image') plt.imshow(binary_seg_image[0] * 255, cmap='gray') plt.show() sess.close() return