USTC 科研 7.13-7.17 LaneNet模型训练和测试

2020.7.13

        周一的早晨跟老师汇报上周进度,老师针对我车轮不能抱死的问题给了两个建议:1.试着降低速度。2.可以尝试stop后加短时间的车轮反转来进行刹车。

        本周任务是图像处理,进行LaneNet训练能将轨道斜率跑出来。对我这样一个初学者来说好像是个比较困难的任务。

2020.7.15

        1.环境:python3.6, cuda-9.0, cudnn-9.0,tensorflow_gpu==1.12.0,requirements.txt中opencv修改成opencv-python

        2.labelme标注自己的数据集,下载过程和之前在Anaconda里安装包类似。

 

        3.数据集处理:

代码贴上:

F:\LaneNet\Data Set\LanenetDataGenarate.py

import json
import os
import os.path as ops
import shutil
import random
import cv2
import numpy as np


def json_to_train_data(imagepath,jsonpath,dstpath):
    assert ops.exists(imagepath), '{:s} not exist'.format(imagepath)
    assert ops.exists(jsonpath), '{:s} not exist'.format(jsonpath)


    ori_dst_dir = ops.join(dstpath,'image')
    binary_dst_dir = ops.join(dstpath,'gt_image_binary')
    instance_dst_dir = ops.join(dstpath,'gt_image_instance')
    

    if not ops.exists(dstpath):
        os.mkdir(dstpath)
    if not ops.exists(ori_dst_dir):
        os.mkdir(ori_dst_dir)
    if not ops.exists(binary_dst_dir):
        os.mkdir(binary_dst_dir)
    if not ops.exists(instance_dst_dir):
        os.mkdir(instance_dst_dir)


    jsondir = os.listdir(jsonpath)
    for index,jsonname in enumerate(jsondir):
        with open(ops.join(jsonpath,jsonname), 'r') as file:
            context = json.load(file)
            image_name = ops.join(imagepath,jsonname.replace('.json','.jpg'))
            print(image_name)
            assert ops.exists(image_name), '{:s} not exist'.format(image_name)
            src_image = cv2.imread(image_name, cv2.IMREAD_COLOR)
            dst_binary_image = np.zeros([src_image.shape[0], src_image.shape[1]], np.uint8)
            dst_instance_image = np.zeros([src_image.shape[0], src_image.shape[1]], np.uint8)
            shapes = context['shapes']
            for shape_index, shape in enumerate(shapes):
                p = np.array(shape['points'],dtype=np.int64)
                p = [p]
            #    cv2.fillPoly(src_image,p,color=255)
                cv2.fillPoly(dst_binary_image,p,color=255)
                cv2.fillPoly(dst_instance_image,p,color=shape_index * 50 + 20)

            image_name_new = '{:s}.png'.format('{:d}'.format(index).zfill(4))
            dst_binary_image_path = ops.join(binary_dst_dir, image_name_new)
            dst_instance_image_path = ops.join(instance_dst_dir, image_name_new)
            dst_rgb_image_path = ops.join(ori_dst_dir, image_name_new)

            cv2.imwrite(dst_binary_image_path, dst_binary_image)
            cv2.imwrite(dst_instance_image_path, dst_instance_image)
            cv2.imwrite(dst_rgb_image_path, src_image)
            # cv2.imshow("src",cv2.resize(src_image,(640,480)))
            # cv2.imshow("dst_binary_image",cv2.resize(dst_binary_image,(640,480)))
            # cv2.imshow("dst_instance_image",cv2.resize(dst_instance_image,(640,480)))
            # cv2.waitKey(0)


def datatotxt(srcpath):
    path1 = srcpath + '/image/'
    path2 = srcpath + '/gt_image_binary/'
    path3 = srcpath + '/gt_image_instance/'
    imglist = os.listdir(path1)
    random.shuffle(imglist)
    train_ratio = 0.8
    

    with open(ops.join(srcpath,'train.txt'), 'w') as f:
        for i in range(int(len(imglist)*train_ratio)):
            content = path1 + imglist[i] + ' ' + path2 + imglist[i] + ' ' + path3 + imglist[i] + '\n' 
            f.write(content)
        
    with open(ops.join(srcpath,'val.txt'), 'w') as f:
        for i in range(int(len(imglist)*train_ratio), len(imglist)):
            content = path1 + imglist[i] + ' ' + path2 + imglist[i] + ' ' + path3 + imglist[i] + '\n'
            f.write(content)


imagepath = './image'
jsonpath = './label'
dstpath = './dataset'
json_to_train_data(imagepath,jsonpath,dstpath)
datatotxt(dstpath)

目标是生成以下这些文件

gt_image_binary:

gt_image_instance:

image就是原图像。

        4.训练自己的数据集

Anaconda里命令:F:\LaneNet\cmd

python train_lanenet.py --net vgg --dataset_dir ./dataset/ --weights_path encoder_decoder_model/tusimple_lanenet/guandao/tusimple_lanenet_vgg_2019-12-03-11-04-56.ckpt-20000

代码借鉴:https://github.com/MaybeShewill-CV/lanenet-lane-detection


2020.7.17

        5.利用训练出来的LaneNet跑之前在管道内拍摄的视频

代码:F:\lanenet_test\lanenet_test.py

import cv2
import time
from lanenet.test_lanenet import *



# path = 'lanenet/image/'
# files = os.listdir(path)
# sess, binary_seg_ret, instance_seg_ret, cluster, postprocessor, input_tensor = sess_init()
# frame_num = 0
# last_theta = 0
# for file in files:
#     if file.split('.')[1] == 'png':
#         imgpath = os.path.join(path, file)
#         img = cv2.imread(imgpath)
#         begin = time.time()
#         rho, last_theta = lane_detect(sess, binary_seg_ret, instance_seg_ret, cluster, postprocessor, input_tensor, img, frame_num,
#                     last_theta)
#         print('time', time.time()-begin)
#         cv2.waitKey(10)

#
print('开始')
begin_road_line = [0, 2.44346]

sess, binary_seg_ret, instance_seg_ret, cluster, postprocessor, input_tensor = sess_init()
num = 0
last_road_line = [0, 2.44346]
frame_num = 0

#frame = cv2.imread("F:/DPL/lanenet-lane-detection/image/1.png", 1)
cap = cv2.VideoCapture('入口1-灯10-2.mp4')
fourcc = cv2.VideoWriter_fourcc(*'XVID')  # 保存视频的编码
out1 = cv2.VideoWriter('result.avi',fourcc, 20.0, (512, 256))
out2 = cv2.VideoWriter('mask.avi',fourcc, 20.0, (512, 256))
ret,frame = cap.read()
while ret:
	start = time.time()
	ret,frame = cap.read()
	frame = cv2.resize(frame, (640, 384), interpolation=cv2.INTER_LINEAR)
	image,mask_image = lane_detect(sess, binary_seg_ret, instance_seg_ret, cluster, postprocessor, input_tensor, frame,
                            frame_num, last_road_line[1])
	print("fit time:",time.time()-start)
#	last_road_line = road_line
	frame_num += 1
	cv2.imshow("image", image)
	mask_image = np.dstack((mask_image,mask_image,mask_image))
	cv2.imshow("mask", mask_image)
	out1.write(image)
	out2.write(mask_image)
	if cv2.waitKey(10) > 0 : break
#cv2.waitKey(0)

cap.release()
out1.release()
out2.release()

(base) C:\Users\12151>activate tf-gpu

(tf-gpu) C:\Users\12151>F:

(tf-gpu) F:\>CD lanenet_test

(tf-gpu) F:\lanenet_test>Python lanenet_test.py

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值