外卖网站建设的策划方案,网站开发运营维护方案建议文档,科普网站栏目建设方案,音乐网站如何做最近在研究视觉语义地图#xff0c;需要进行车道线检测#xff0c;发现这篇车道线检测论文效果蛮好的 #xff08;Ultra Fast Structure-aware Deep Lane Detection#xff09;。论文作者在知乎上已经介绍过了#xff1a;https://zhuanlan.zhihu.com/p/157530787#xff… 最近在研究视觉语义地图需要进行车道线检测发现这篇车道线检测论文效果蛮好的 Ultra Fast Structure-aware Deep Lane Detection。论文作者在知乎上已经介绍过了https://zhuanlan.zhihu.com/p/157530787本文中我简单介绍下论文原理重点介绍如何使用原作者开源的模型检测自己采集的数据。 1.Method介绍
目前大多数车道线检测算法中都是将车道线检测看作是像素分割问题。但是这会存在两个问题一是计算速度慢二是在一些有挑战的场景中遮挡、夜晚、极端光照条件由于没有视觉线索很难进行检测。因此在本文中作者提出了一个按行检测车道线的方法。
作者首先将原始图像按照一定像素间隔划分成 hhh 个anchors同时又将每一个anchor划分为 www 个网格。若图像全局特征用 XXX 表示车道线数量用 CCC 表示车道线分类器为 fijf^{ij}fij则车道线预测概率为 Pi,j,:fij(X),s.t. i∈[1,C],j∈[1,h]P_{i, j,:}f^{i j}(X), \text { s.t. } i \in[1, C], j \in[1, h] Pi,j,:fij(X), s.t. i∈[1,C],j∈[1,h]
概率 Pi,j,:P_{i, j,:}Pi,j,: 是 (w1)(w1)(w1) 维的向量。
为什么本文提出的方法可以快速检测车道线呢如下图所示基于像素分割的车道线检测要进行 H×W×(C1)H \times W \times (C1)H×W×(C1) 次分类而本文提出的方法只需要进行 C×h×(w1)C \times h \times (w1)C×h×(w1) 次分类由于 hHwWhHwWhHwW因此整体计算量大幅减小。 同时在本文中作者又提出了两个新的结构损失函数。
由于车道线一般是连续的所有同一车道线在相邻anchor的预测概率应该是接近的作者提出了车道线相似损失函数 Lsim∑i1C∑j1h−1∥Pi,j,:−Pi,j1,:∥1L_{s i m}\sum_{i1}^{C} \sum_{j1}^{h-1}\left\|P_{i, j,:}-P_{i, j1,:}\right\|_{1} Lsimi1∑Cj1∑h−1∥Pi,j,:−Pi,j1,:∥1另一个是车道线形状损失函数作者使用softmax函数得到车道线的概率Probi,j,:softmax(Pi,j,1:w)\operatorname{Prob}_{i, j,:}\operatorname{softmax}\left(P_{i, j, 1: w}\right)Probi,j,:softmax(Pi,j,1:w)同时使用期望来表示车道线所处位置Loci,j∑k1wk⋅Probi,j,kL o c_{i, j}\sum_{k1}^{w} k \cdot \operatorname{Prob}_{i, j, k}Loci,j∑k1wk⋅Probi,j,k。最后作者使用二阶微分方程来约束车道线位置 Lshp∑i1C∑j1h−2∥(Loci,j−Loci,j1)−(Loci,j1−Loci,j2)∥1\begin{aligned} L_{s h p}\sum_{i1}^{C} \sum_{j1}^{h-2} \| \left(L o c_{i, j}-L o c_{i, j1}\right) -\left(L o c_{i, j1}-L o c_{i, j2}\right) \|_{1} \end{aligned} Lshpi1∑Cj1∑h−2∥(Loci,j−Loci,j1)−(Loci,j1−Loci,j2)∥1 2.自采数据集车道线检测
下面使用作者训练好的模型来检测车道线代码链接为https://github.com/cfzd/Ultra-Fast-Lane-Detection。
下载完源码后创建一个lane_detection.py首先导入需要的库
import torch
import scipy.special
import os, cv2
from PIL import Image
from model.model import parsingNet
import numpy as np
import torchvision.transforms as transforms
from data.constant import tusimple_row_anchor然后加载预训练模型作者在两个车道线数据集上进行了训练我这里选择是的在TuSimple数据集训练的模型代码如下 net parsingNet(pretrained False, backbone18,cls_dim (101,56,4),use_auxFalse) # we dont need auxiliary segmentation in testingtest_model tusimple_18.pth
state_dict torch.load(test_model, map_locationcpu)[model]compatible_state_dict {}
for k, v in state_dict.items():if module. in k:compatible_state_dict[k[7:]] velse:compatible_state_dict[k] vnet.load_state_dict(compatible_state_dict, strictFalse)
net.eval()然后是一些参数设置如图片大小尺寸我这里图片大小为 1280×5601280\times5601280×560row anchor 尺寸这里使用的是作者设置的anchor尺寸图片预处理等将图片大小resize为 800×288800\times288800×288
img_w, img_h 1280, 560
size (img_w,img_h)
cls_num_per_lane 56
row_anchor tusimple_row_anchorimg_transforms transforms.Compose([transforms.Resize((288, 800)),transforms.ToTensor(),transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])下一步就是车道线检测与可视化 with torch.no_grad():out net(img)col_sample np.linspace(0, 800-1, 100)
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, :, :], axis0)
idx np.arange(100) 1
idx idx.reshape(-1, 1, 1)
loc np.sum(prob * idx, axis0)
out_j np.argmax(out_j, axis0)
loc[out_j 100] 0
out_j loc 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 * img_w / 800) - 1, int(img_h * (row_anchor[cls_num_per_lane-1-k]/288))-1)cv2.circle(vis,ppp,4,(0,0,255),-1)
Image.fromarray(vis).show()最后检测结果如下