主要内容

车道检测和GPU编码器优化

这个例子展示了如何开发一个深度学习车道检测应用程序运行在NVIDIA®gpu。

pretrained车道检测网络可以检测并输出车道标记从一个图像和基于边界AlexNet网络。最后几层的AlexNet取而代之的是一个较小的完全连接网络层和回归输出层。生成一个CUDA的例子可执行,运行在主机上的一个人GPU。

先决条件

验证GPU环境

使用coder.checkGpuInstall函数来确认所需的编译器和库运行这个例子是正确设置。

envCfg = coder.gpuEnvConfig (“主机”);envCfg。DeepLibTarget =“cudnn”;envCfg。DeepCodegen = 1;envCfg。安静= 1;coder.checkGpuInstall (envCfg);

得到Pretrained车道检测网络

下面的例子使用了trainedLaneNet包含pretrained MAT-file车道检测网络。这个文件是大约143 MB的大小。从MathWorks网站下载文件。

laneNetFile = matlab.internal.examples.download金宝appSupportFile (“gpucoder / cnn_models / lane_detection”,“trainedLaneNet.mat”);

这个网络需要一个图像作为输入和输出两个车道边界对应于自我的左和右车道车辆。每个车道边界由抛物型方程表示: y = 一个 x 2 + b x + c x, y是横向偏移量和纵向距离车辆。网络输出三个参数a、b和c /巷。网络体系结构是类似的AlexNet除了最后几层取而代之的是一个更小的完全连接层和回归输出层。

负载(laneNetFile);disp (laneNet)
SeriesNetwork属性:层:[23×1 nnet.cnn.layer.Layer] InputNames:{“数据”}OutputNames:{“输出”}

查看网络体系结构,使用analyzeNetwork函数。

analyzeNetwork (laneNet)

下载测试视频

测试模型,从加州理工学院车道的例子使用了一个视频文件数据集。大约是8 MB的文件。从MathWorks网站下载文件。

videoFile = matlab.internal.examples.download金宝appSupportFile (“gpucoder /媒体”,“caltech_cordova1.avi”);

主要入口点函数

detectLanesInVideo.m文件的主要入口点函数代码生成。的detectLanesInVideo函数使用vision.VideoFileReader(计算机视觉工具箱)系统对象读取从输入视频帧调用LaneNet网络对象的预测方法,并将输入视频的检测出车道。一个vision.DeployableVideoPlayer(计算机视觉工具箱)系统对象用于显示车道检测视频输出。

类型detectLanesInVideo.m
函数detectLanesInVideo (videoFile,净、laneCoeffMeans laneCoeffsStds) % detectLanesInVideo入口点函数的车道检测优化与GPU编码器例子% % % detectLanesInVideo (videoFile,净,laneCoeffMeans laneCoeffsStds)使用% VideoFileReader系统对象阅读从输入视频帧,调用% LaneNet网络对象的预测方法,并将检测到的%车道对输入视频。DeployableVideoPlayer系统对象用于%显示车道检测视频输出。% 2022年版权MathWorks公司% # codegen % %创建视频读者和视频播放器对象videoFReader = vision.VideoFileReader (videoFile);depVideoPlayer =愿景。DeployableVideoPlayer (Name =“GPU车道检测”);% %视频帧处理循环而~结束(videoFReader) videoFrame = videoFReader ();scaledFrame = 255。* (imresize (videoFrame (227 227)));[laneFound, ltPts rtPts] = laneNetPredict(净,scaledFrame,…laneCoeffMeans laneCoeffsStds); if(laneFound) pts = [reshape(ltPts',1,[]);reshape(rtPts',1,[])]; videoFrame = insertShape(videoFrame, 'Line', pts, 'LineWidth', 4); end depVideoPlayer(videoFrame); end end

LaneNet预测函数

laneNetPredict函数计算左右车道位置在单个视频帧。的laneNet网络计算参数a、b和c的抛物型方程描述左和右车道边界。从这些参数,计算出x和y坐标对应通道的位置。坐标必须映射到图像坐标。

类型laneNetPredict.m
函数[laneFound、ltPts rtPts] = laneNetPredict(净,框架,意味着,性病)% laneNetPredict预测车道标记输入图像帧使用% % %车道检测网络版权2017 - 2022 MathWorks, Inc . % # codegen %持久对象lanenet用于加载网络对象。%第一次调用这个函数,构造持久对象和%设置。只有在函数被调用时,随后的时期,%重用相同的对象调用预测输入,从而避免网络重构和%重新加载对象。持久lanenet;如果isempty (lanenet) lanenet =编码器。loadDeepLearningNetwork(网络,“lanenet”);结束lanecoeffsNetworkOutput =预测(lanenet,框架);%恢复原始多项式系数通过逆转标准化的步骤。params = lanecoeffsNetworkOutput。*性病+意味着;% ' c '应该超过0.5是一个车道。 isRightLaneFound = abs(params(6)) > 0.5; isLeftLaneFound = abs(params(3)) > 0.5; % From the networks output, compute left and right lane points in the image % coordinates. vehicleXPoints = 3:30; ltPts = coder.nullcopy(zeros(28,2,'single')); rtPts = coder.nullcopy(zeros(28,2,'single')); if isRightLaneFound && isLeftLaneFound rtBoundary = params(4:6); rt_y = computeBoundaryModel(rtBoundary, vehicleXPoints); ltBoundary = params(1:3); lt_y = computeBoundaryModel(ltBoundary, vehicleXPoints); % Visualize lane boundaries of the ego vehicle. tform = get_tformToImage; % Map vehicle to image coordinates. ltPts = tform.transformPointsInverse([vehicleXPoints', lt_y']); rtPts = tform.transformPointsInverse([vehicleXPoints', rt_y']); laneFound = true; else laneFound = false; end end %% Helper Functions % Compute boundary model. function yWorld = computeBoundaryModel(model, xWorld) yWorld = polyval(model, xWorld); end % Compute extrinsics. function tform = get_tformToImage %The camera coordinates are described by the caltech mono % camera model. yaw = 0; pitch = 14; % Pitch of the camera in degrees roll = 0; translation = translationVector(yaw, pitch, roll); rotation = rotationMatrix(yaw, pitch, roll); % Construct a camera matrix. focalLength = [309.4362, 344.2161]; principalPoint = [318.9034, 257.5352]; Skew = 0; camMatrix = [rotation; translation] * intrinsicMatrix(focalLength, ... Skew, principalPoint); % Turn camMatrix into 2-D homography. tform2D = [camMatrix(1,:); camMatrix(2,:); camMatrix(4,:)]; % drop Z tform = projective2d(tform2D); tform = tform.invert(); end % Translate to image co-ordinates. function translation = translationVector(yaw, pitch, roll) SensorLocation = [0 0]; Height = 2.1798; % mounting height in meters from the ground rotationMatrix = (... rotZ(yaw)*... % last rotation rotX(90-pitch)*... rotZ(roll)... % first rotation ); % Adjust for the SensorLocation by adding a translation. sl = SensorLocation; translationInWorldUnits = [sl(2), sl(1), Height]; translation = translationInWorldUnits*rotationMatrix; end % Rotation around X-axis. function R = rotX(a) a = deg2rad(a); R = [... 1 0 0; 0 cos(a) -sin(a); 0 sin(a) cos(a)]; end % Rotation around Y-axis. function R = rotY(a) a = deg2rad(a); R = [... cos(a) 0 sin(a); 0 1 0; -sin(a) 0 cos(a)]; end % Rotation around Z-axis. function R = rotZ(a) a = deg2rad(a); R = [... cos(a) -sin(a) 0; sin(a) cos(a) 0; 0 0 1]; end % Given the Yaw, Pitch, and Roll, determine the appropriate Euler angles % and the sequence in which they are applied to align the camera's % coordinate system with the vehicle coordinate system. The resulting % matrix is a Rotation matrix that together with the Translation vector % defines the extrinsic parameters of the camera. function rotation = rotationMatrix(yaw, pitch, roll) rotation = (... rotY(180)*... % last rotation: point Z up rotZ(-90)*... % X-Y swap rotZ(yaw)*... % point the camera forward rotX(90-pitch)*... % "un-pitch" rotZ(roll)... % 1st rotation: "un-roll" ); end % Intrinsic matrix computation. function intrinsicMat = intrinsicMatrix(FocalLength, Skew, PrincipalPoint) intrinsicMat = ... [FocalLength(1) , 0 , 0; ... Skew , FocalLength(2) , 0; ... PrincipalPoint(1), PrincipalPoint(2), 1]; end

生成CUDA执行

生成一个独立的CUDA可执行的detectLanesInVideo入口点函数,创建一个GPU代码配置对象exe”目标和目标语言设置为c++。使用coder.DeepLearningConfig函数创建一个CuDNN深度学习配置对象,并将其分配给DeepLearningConfigGPU代码配置对象的属性。

cfg = coder.gpuConfig (exe”);cfg。DeepLearningConfig = coder.DeepLearningConfig (“cudnn”);cfg。GenerateReport = true;cfg。GenerateExampleMain =“GenerateCodeAndCompile”;cfg。TargetLang =“c++”;输入= {coder.Constant (videoFile) coder.Constant (laneNetFile),coder.Constant (laneCoeffMeans) coder.Constant (laneCoeffsStds)};

运行codegen命令。

codegenarg游戏输入配置cfgdetectLanesInVideo
代码生成成功:查看报告

生成的代码描述

系列网络生成c++类包含一个数组的18层类(后层融合优化)。的设置()类的方法设置处理,每一层对象分配内存。的预测()方法调用中每个18层的预测网络。

lanenet0_0{公众:lanenet0_0 ();无效setSize ();无效resetState ();无效设置();无效预测();无效清理();浮动layerIndex * getLayerOutput (int, int portIndex);intlayerIndex getLayerOutputSize (int, int portIndex);浮动* getInputDataPointer (int b_index);浮动* getInputDataPointer ();浮动* getOutputDataPointer (int b_index);浮动* getOutputDataPointer ();intgetBatchSize ();~ lanenet0_0 ();私人:空白分配();无效postsetup ();无效释放();公众:boolean_TisInitialized;boolean_TmatlabCodegenIsDeleted;私人:intnumLayers;MWTensorBase* inputTensors [1];MWTensorBase* outputTensors [1];MWCNNLayer*层[18];MWCudnnTarget: MWTargetNetworkImpl * targetImpl;};

cnn_lanenet * _conv * _w和cnn_lanenet * _conv * _b文件是二进制卷积层的重量和偏见文件网络。cnn_lanenet * _fc * _w和cnn_lanenet * _fc * _b文件是二进制文件完全连接层的重量和偏见网络。

codegendir = fullfile (“codegen”,exe”,“detectLanesInVideo”);dir ([codegendir filesep,“*。斌”])
cnn_lanenet0_0_conv1_b。bin cnn_lanenet0_0_conv3_b.bin cnn_lanenet0_0_conv5_b.bin cnn_lanenet0_0_fc6_b.bin cnn_lanenet0_0_fcLane2_b.bin cnn_lanenet0_0_conv1_w.bin cnn_lanenet0_0_conv3_w.bin cnn_lanenet0_0_conv5_w.bin cnn_lanenet0_0_fc6_w.bin cnn_lanenet0_0_fcLane2_w.bin cnn_lanenet0_0_conv2_b.bin cnn_lanenet0_0_conv4_b.bin cnn_lanenet0_0_data_offset.bin cnn_lanenet0_0_fcLane1_b.bin networkParamsInfo_lanenet0_0.bin cnn_lanenet0_0_conv2_w.bin cnn_lanenet0_0_conv4_w.bin cnn_lanenet0_0_data_scale.bin cnn_lanenet0_0_fcLane1_w.bin

运行可执行文件

运行可执行文件,取消注释以下行代码。

如果ispc[地位,cmdout] =系统(“detectLanesInVideo.exe”);其他的(地位、cmdout) =系统(”。/ detectLanesInVideo”);结束

gpuLaneDetectionOutput.png

另请参阅

功能

对象

相关的话题