ultra fast lane detection读后感及使用情况(附训练好的模型及libtorch端代码)
ultra fast lane detection提供了很好的源码,根据演示视频来看,效果似乎不赖,很有必要试一试该算法。
一、基本情况
作者知乎:超快的车道线检测 - 知乎 (zhihu.com)
简单来说,作者认为卷积层形式的输出,导致局部感受野小,很明显车道线识别需要结合全局特征来分析。而全连接层形式的输出,运用了全局特征,也就没有感受野的问题。
另外,作者把这个问题转化为了分类问题。也就是如下图所示。
C是车道线数目,h和w则是图像长宽相关的cell,类似于锚点。w方向上的通道是w+1个,最后一位输出的是有无该通道。
大概意思,就是用h和w预测第c个车道的大概位置,然后按照下面的公式,算具体位置:
损失函数还考量到相邻的位置附近,因此有了,后两个损失函数就是邻近位置的二阶差分,邻近概率的差分。
二、使用情况
作者在github上已经给出了开源库。要求先设置configs下的py,我选了culane数据集(比较大,大概50多个G),因此修改了culane.py。
需要注意的是,culane数据集有黑夜的数据集,但貌似没有特殊天气的数据。
如果要使用tusimple数据集,要使用scripts下的convert_tusimple.py生成train_gt.txt。(运行时记得加 设置--root E:\tusimple数据集 )
之后就在pycharm进行了训练。
以下是笔者使用的libtorch C++端的代码,另外需要提醒一点(如果对数据的存储与运算不熟悉的话)
CPU类型下大多数函数都可以使用,但是推理速度比较慢。CUDA类型下并行速度快,但是有些函数不能用,遍历计算速度很慢,需要转为CPU类型。
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 | #include <torch/script.h> #include <torch/torch.h> #include<ATen/ATen.h> #include <opencv2/opencv.hpp> #include <iostream> #include <windows.h> using namespace torch::indexing; using namespace std; int tusimple_row_anchor[56] = {64, 68, 72, 76, 80, 84, 88, 92, 96, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152, 156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204, 208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 248, 252, 256, 260, 264, 268, 272, 276, 280, 284}; int culane_row_anchor[18] = {121, 131, 141, 150, 160, 170, 180, 189, 199, 209, 219, 228, 238, 248, 258, 267, 277, 287}; void tusimple_lane(at::Tensor& input, cv::Mat& img) { int lane_num = 4; int length_cell = 56; int width = 100; int topk_n = 3; double w_scale = img.cols / ( double )width; double h_scale = img.rows / ( double )(length_cell*2); input = input.to(torch::kCPU); //CPU for Ergodic computation,CUDA for Parallel computation for ( int i = 0; i < length_cell; i++) for ( int j = 0; j < lane_num; j++) { double lane_loc = 0; double sum_w = 0; double sum = 0; auto cut = input.index({ Slice(torch::indexing::None),Slice(torch::indexing::None), Slice(i,i + 1), Slice(j,j + 1) }); cut = torch::softmax(cut, 1).view(-1); if (cut[width].item< float >() > 0.5) { continue ; } std::vector< float > tesnor2vec(cut.data_ptr< float >(), cut.data_ptr< float >() + cut.numel()); for ( int s = 0; s < width; s++) { sum += tesnor2vec[s]; sum_w += tesnor2vec[s] * s; } lane_loc = (sum_w / sum) * w_scale; //std::tuple<torch::Tensor, torch::Tensor> result = cut.topk(2, -1); //auto top_scores = std::get<0>(result).view(-1);//{1,10} 变成 {10} //auto top_idxs = std::get<1>(result).view(-1); /*if (cut[width].item<float>() > 0.5) { continue; } if(top_idxs[0].item().toInt()!=width) { lane_loc = top_idxs[0].item().toInt() * w_scale; } else { lane_loc= top_idxs[1].item().toInt() * w_scale; }*/ cv::Scalar temp; switch (j) { case 0: temp = cv::Scalar(0, 0, 0); break ; case 1: temp = cv::Scalar(255, 0, 0); break ; case 2: temp = cv::Scalar(0, 255, 0); break ; case 3: temp = cv::Scalar(0, 0, 255); break ; } cv::circle(img, cv::Point(lane_loc, tusimple_row_anchor[i]), 5, temp, -1); } } // void culane_lane(at::Tensor& input, cv::Mat& img) { int lane_num = 4; int length_cell = 18; int width = 200; int topk_n =3; double w_scale = img.cols/( double )width; double h_scale = img.rows/( double )length_cell; input = input.to(torch::kCPU); //CPU for Ergodic computation,CUDA for Parallel computation for ( int i=0;i<length_cell;i++) for ( int j = 0; j < lane_num; j++) { double lane_loc = 0; double sum_w = 0; double sum = 0; auto cut = input.index({ Slice(torch::indexing::None),Slice(torch::indexing::None), Slice(i,i + 1), Slice(j,j + 1) }); cut = torch::softmax(cut, 1).view(-1); if (cut[width].item< float >() > 0.5) { continue ; } std::vector< float > tesnor2vec(cut.data_ptr< float >(), cut.data_ptr< float >() + cut.numel()); for ( int s = 0; s < width; s++) { sum += tesnor2vec[s]; sum_w += tesnor2vec[s] * s; } lane_loc = (sum_w / sum )* w_scale; cv::Scalar temp; switch (j) { case 0: temp = cv::Scalar(0, 0, 0); break ; case 1: temp = cv::Scalar(255, 0, 0); break ; case 2: temp = cv::Scalar(0, 255, 0); break ; case 3: temp = cv::Scalar(0, 0, 255); break ; } cv::circle(img, cv::Point(lane_loc, culane_row_anchor[i]), 5, temp, -1); } } int main() { torch::jit::script::Module module = torch::jit::load( "C:\\Users\\lvdon\\source\\repos\\C_test\\lane_det\\lane.torchscript_gpu.pt" ); torch::DeviceType device_type = torch::kCUDA; module.to(device_type); module.eval(); cv::VideoCapture cap( "G:\\视频\\弯道\\cv2_curve.mp4" ); //cv::VideoWriter vw; //vw.open("C:\\Users\\lvdon\\source\\repos\\C_test\\test.mp4", //路径 // CV_FOURCC('m', 'p', '4', 'v'), //编码格式 // 30, //帧率 // cv::Size(800, // 288));//尺寸 cv::Mat img; cv::Mat ori; cap.read(img); int width = img.size().width; int height = img.size().height; int count = 0; while ( true ) { clock_t start = clock (); count++; cap >> img; if (img.empty()) { cout << "play end" << endl; break ; } else if (count % 1 != 0) { continue ; } //img = cv::imread("E:\\train_lane\\driver_182_30frame\\05312333_0003.MP4\\00000.jpg"); cv::resize(img, img, cv::Size(600, 288)); cv::copyMakeBorder(img, img,0, 0, 100, 100, cv::BORDER_CONSTANT, cv::Scalar(0, 0,0)); cv::cvtColor(img, img, cv::COLOR_BGR2RGB); // BGR -> RGB img.convertTo(img, CV_32FC3, 1.0f / 255.0f); // normalization 1/255 auto tensor_img = torch::from_blob(img.data, { 1, img.rows, img.cols, img.channels() },torch::kFloat).to(device_type); tensor_img = tensor_img.permute({ 0, 3, 1, 2 }).contiguous(); // BHWC -> BCHW (Batch, Channel, Height, Width) std::vector<torch::jit::IValue> inputs; inputs.emplace_back(tensor_img); auto output = module.forward(inputs).toTensor(); culane_lane(output,img); cout << "cost time:" << clock () - start << endl; //cv::imshow("result", img); img.convertTo(img, CV_8UC3,255); //need to convert to CV_8UC3 for save the video cv::imshow( "result" , img); //vw << img; //cv::imshow("result", img); cv::waitKey(1); } return NULL; } |
模型链接: https://pan.baidu.com/s/1MZhkt87aWFb771gwUBxH6A?pwd=LDHL 提取码: LDHL
转载前,请注明来源。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理