主要内容

利用GPU编码器优化车道检测

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

预训练的车道检测网络可以从图像中检测并输出车道标记物边界AlexNet网络。最后几层AlexNet网络被更小的全连接层和回归输出层所取代。该示例生成一个CUDA可执行文件,它运行在主机上支持CUDA的GPU上。

先决条件

验证GPU环境

使用coder.checkGpuInstall函数来验证运行此示例所需的编译器和库是否已正确设置。

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

获得训练有素的车道检测网络

此示例使用trainedLaneNet包含预先训练的车道检测网络的mat文件。该文件的大小大约为143 MB。从MathWorks网站下载该文件。

laneNetFile = matlab.internal.examples.downloadSupportFile(“gpucoder / cnn_models / lane_detection”...“trainedLaneNet.mat”);

该网络以一个图像作为输入和输出两个车道边界,分别对应于自我车辆的左右车道。每个车道边界用抛物方程表示: y 一个 x 2 + b x + c ,其中y为横向偏移量,x为与车辆的纵向距离。网络输出每个车道的三个参数a、b和c。网络架构类似于AlexNet除了最后几层被一个更小的完全连接层和回归输出层所取代。

负载(laneNetFile);disp (laneNet)
层:[23×1 nnet.cnn.layer.Layer] InputNames: {'data'} OutputNames: {'output'}

若要查看网络架构,请使用analyzeNetwork函数。

analyzeNetwork (laneNet)

下载测试视频

为了测试该模型,该示例使用了来自Caltech lanes数据集的视频文件。该文件的大小大约为8 MB。从MathWorks网站下载该文件。

videoofile = matlab.internal.examples.downloadSupportFile(“gpucoder /媒体”“caltech_cordova1.avi”);

主要入口功能

detectLanesInVideo.mFile是代码生成的主要入口点函数。的detectLanesInVideo函数使用愿景。VideoFileReader(计算机视觉工具箱)系统对象从输入视频中读取帧,调用LaneNet网络对象的预测方法,并在输入视频上绘制出检测到的车道。一个愿景。DeployableVideoPlayer(计算机视觉工具箱)系统对象用于显示车道检测到的视频输出。

类型detectLanesInVideo.m
% % detectLanesInVideo(videoFile,net,laneCoeffMeans,laneCoeffsStds)使用% VideoFileReader系统对象从输入视频中读取帧,调用LaneNet网络对象的预测方法,并在输入视频上绘制检测到的% lanes。DeployableVideoPlayer系统对象用于显示检测到的通道视频输出。%版权所有The MathWorks, Inc. %#codegen %%创建视频阅读器和视频播放器对象videoreader = vision.VideoFileReader(videoFile);depVideoPlayer = vision.DeployableVideoPlayer(Name='Lane Detection on GPU'); %% Video Frame Processing Loop while ~isDone(videoFReader) videoFrame = videoFReader(); scaledFrame = 255.*(imresize(videoFrame,[227 227])); [laneFound,ltPts,rtPts] = laneNetPredict(net,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
function [laneFound,ltPts,rtPts] = laneNetPredict(net,frame,means,stds) % laneNetPredict使用%车道检测网络% %预测输入图像帧上的车道标记。版权所有the MathWorks, Inc. %#codegen %一个持久对象lanenet用于加载网络对象。第一次调用此函数时,将构造持久对象并设置%。当该函数随后被调用时,相同的对象%被重用以在输入上调用predict,从而避免重构和%重新加载网络对象。持久lanenet;如果isempty(lanenet) lanenet = coder。loadDeepLearningNetwork(网络,“lanenet”);end lanecoeffsNetworkOutput = predict(lanenet,frame);通过反转归一化步骤恢复原始系数。。params = lanecoeffsNetworkOutput .* stds + means;% 'c'应该大于0.5才能成为lane。 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++。使用编码器。DeepLearningConfig函数来创建CuDNN的深度学习配置对象,并将其分配给DeepLearningConfig属性的图形处理器代码配置对象。

cfg = code .gpu config (exe”);cfg。DeepLearningConfig =编码器。DeepLearningConfig (“cudnn”);cfg。GenerateReport = true;cfg。GenerateExampleMain =“GenerateCodeAndCompile”;cfg。TargetLang =“c++”;input = {code . constant (videoFile),code . constant (laneNetFile),...coder.Constant (laneCoeffMeans) coder.Constant (laneCoeffsStds)};

运行codegen命令。

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

生成的代码描述

串联网络生成为一个包含18个层类数组的c++类(经过层融合优化)。的设置()方法设置句柄并为每个层对象分配内存。的预测()方法为网络中的18层中的每一层调用预测。

lanenet0_0公众:lanenet0_0 ();无效setSize ();无效resetState ();无效设置();无效预测();无效清理();浮动*getLayerOutput(int layerIndex, int portIndex);intgetLayerOutputSize(int layerIndex, 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.bincnn_lanenet0_0_fcLane1_w.bin

运行可执行程序

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

如果Ispc [status,cmdout] = system(“detectLanesInVideo.exe”);其他的[status,cmdout] = system(”。/ detectLanesInVideo”);结束

gpuLaneDetectionOutput.png

另请参阅

功能

对象

相关的话题

Baidu
map