Ultra-Fast-Lane-Detection方法测试自己的视频基于culane模型

Ultra-Fast-Lane-Detection方法测试自己的视频基于culane模型

1.修改demo.py

#-*-coding:GBK -*- 
import torch, os, cv2
from model.model import parsingNet
from utils.common import merge_config
from utils.dist_utils import dist_print
import torch
import scipy.special, tqdm
import numpy as np
import torchvision.transforms as transforms
from data.dataset import LaneTestDataset

if __name__ == "__main__":
    torch.backends.cudnn.benchmark = True
    args, cfg = merge_config()
    dist_print('start testing...')
    assert cfg.backbone in ['18','34','50','101','152','50next','101next','50wide','101wide']

    if cfg.dataset == 'CULane':
        cls_num_per_lane = 18
    elif cfg.dataset == 'Tusimple':
        cls_num_per_lane = 56
    else:
        raise NotImplementedError

    net = parsingNet(pretrained = False, backbone=cfg.backbone,cls_dim = (cfg.griding_num+1,cls_num_per_lane,4),
                    use_aux=False).cuda() # we dont need auxiliary segmentation in testing

    state_dict = torch.load(cfg.test_model, map_location='cpu')['model']
    compatible_state_dict = {}
    for k, v in state_dict.items():
        if 'module.' in k:
            compatible_state_dict[k[7:]] = v
        else:
            compatible_state_dict[k] = v

    net.load_state_dict(compatible_state_dict, strict=False)
    net.eval()

    img_transforms = transforms.Compose([
        transforms.Resize((288, 800)),
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
    ])


    cap = cv2.VideoCapture("/home/yxhuang/Ultra-Fast-Lane-Detection-master/data/test_video.mp4")
    fourcc = cv2.VideoWriter_fourcc(*'MJPG')
    vout = cv2.VideoWriter(str(111)+'.avi', fourcc , 30.0, (int(cap.get(3)),int(cap.get(4))))

    print("w = {},h = {}".format(cap.get(3),cap.get(4)))

    from PIL import Image
    
    print("cuda:{}",torch.cuda.is_available())
    while 1:
        rval,frame = cap.read()
        if rval == False:
            break
        # cv2.imwrite("ssss.jpg",frame)
        # img_ = Image.open("ssss.jpg")
       #frame = cv2.resize(frame, (288, 800))
        img  = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
        img_ = Image.fromarray(img)#实现array到image的转换
        imgs = img_transforms(img_)
        imgs = imgs.unsqueeze(0)#起到升维的作用
        imgs = imgs.cuda()
        with torch.no_grad():
            out = net(imgs)

        col_sample = np.linspace(0, 800 - 1, cfg.griding_num)
        col_sample_w = col_sample[1] - col_sample[0]


        out_j = out[0].data.cpu().numpy()
        out_j = out_j[:, ::-1, :]
        prob = scipy.special.softmax(out_j[:-1, :, :], axis=0)
        idx = np.arange(cfg.griding_num) + 1
        idx = idx.reshape(-1, 1, 1)
        loc = np.sum(prob * idx, axis=0)
        out_j = np.argmax(out_j, axis=0)
        loc[out_j == cfg.griding_num] = 0
        out_j = loc

        # import pdb; pdb.set_trace()
        # vis = cv2.imread(os.path.join(cfg.data_root,names[0]))
        for i in range(out_j.shape[1]):
            if np.sum(out_j[:, i] != 0) > 2:
                for k in range(out_j.shape[0]):
                    if out_j[k, i] > 0:
                        ppp = (int(out_j[k, i] * col_sample_w * cap.get(3) / 800) - 1, int(cap.get(4) - k * 20) - 1)
                        cv2.circle(frame,ppp,5,(0,255,0),-1)
        vout.write(frame)

    vout.release()

注意自己视频的路径。

2.culane.py路径的修改

在这里插入图片描述

3.测试

python demo.py configs/culane.py --test_model model/cualne_18.pth

在这里插入图片描述
这里culane_18.pth的路径一定要对

4.结果

在这里插入图片描述
选取视频的一个截图,效果一般

  • 6
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 33
    评论
要复现"ultra fast structure-aware deep lane detection"代码,首先需要了解该算法的原理和网络结构。该算法是一种深度学习方法,用于车道线检测。其核心思想是结合结构感知机制和快速推理策略,以实现高效、准确的车道线检测。 为了复现该算法,需要完成以下步骤: 1. 数据集准备:收集车道线数据集并进行相应的标注。可以使用公开数据集,如CULane或TuSimple等,或者自己采集数据集。数据集应包含车道线图像以及对应的标注信息。 2. 网络结构构建:根据论文中提到的网络结构,构建模型。根据论文中的说明,可以选择使用FCN、UNet等结构。确保灵活地调整网络的深度和宽度,以适应不同的数据集和性能要求。 3. 损失函数定义:根据论文中的介绍,选择适当的损失函数,如二分类交叉熵损失函数等,以最小化预测标注和真实标注之间的差异。 4. 数据预处理:对输入图像进行预处理,如图像归一化、resize等,以适应网络的输入要求。 5. 模型训练:使用准备好的数据集和网络结构,进行模型的训练。设置合适的超参数,如学习率、批大小等。通过迭代优化网络参数,使模型逐渐学习到车道线的特征。 6. 模型评估:使用测试集对模型进行评估,计算准确率、召回率、F1得分等指标,以评估模型的性能。 7. 代码测试:使用测试集对复现的代码进行测试,观察模型的预测结果。可进行可视化展示,比较模型的预测结果与真实标注的差距。 8. 优化和改进:根据测试结果和需要,对网络结构、超参数等进行调整和优化,进一步提升模型性能。 通过以上步骤,就可以较为全面地复现"ultra fast structure-aware deep lane detection"代码,从而实现高效、准确的车道线检测算法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 33
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值