代码生成的深度学习仿真软件模型,执行巷和车辆检测万博1manbetx
这个例子展示了如何从一个仿真软件生成c++代码®模型执行巷和车辆检测利用卷积神经网络(cnn)。万博1manbetx的例子以交通视频的帧作为输入,输出两个车道边界对应的左和右车道自我车辆,和检测车辆的框架。这个示例使用pretrained车道检测网络和pretrained车辆检测网络的对象检测使用YOLO v2意思深入学习计算机视觉的工具箱™的例子。有关更多信息,请参见对象检测使用YOLO v2意思深入学习(计算机视觉工具箱)。
这个例子说明了以下概念:
模型仿真软件中的车道检测应用程序。万博1manbetx首先处理交通视频通过调整到227 - 227 - 3和255乘以一个常数因子。随后,你处理交通视频使用pretrained网络装载在预测块的深度学习工具箱™。最后,如果检测到左和右车道边界,抛物线系数模型的轨迹车道边界。
模型仿真软件中的车辆检测应用程序通过处理交通视频使用pretrained YOLO v万博1manbetx2意思探测器。这个网络视频中检测到车辆和输出边界框的坐标为这些车辆和他们的信心得分。
为代码生成配置模型。
先决条件
英特尔数学内核库深层神经网络(MKL-DNN)。
指MKLDNN CPU支万博1manbetx持的处理器列表支持MKL-DNN图书馆。万博1manbetx
深度学习工具箱™使用
DAGNetwork
对象。计算机视觉工具箱™视频I / O操作。
算法流程
算法的框图模型的工作流模型。万博1manbetx
Pretrained车道,车辆检测网络
下面的例子使用了trainedLaneNet
和yolov2ResNet50VehicleExample
垫文件包含pretrained网络。大约143 MB和98 MB的文件大小,分别。下载的文件。
lanenetFile = matlab.internal.examples.download万博1manbetxSupportFile (“gpucoder / cnn_models / lane_detection”,“trainedLaneNet.mat”);vehiclenetFile = matlab.internal.examples.download万博1manbetxSupportFile (“视觉/数据”,“yolov2ResNet50VehicleExample.mat”);
交通视频下载测试
测试模型,示例使用加州理工学院车道数据集。大约是16 MB的文件大小。下载这个文件。
mediaFile = matlab.internal.examples.download万博1manbetxSupportFile (“gpucoder /媒体”,“caltech_washington1.avi”);
车道,车辆检测仿真软件模型万博1manbetx
下图显示了仿真软件模型进行车道,车辆在交通视频检测。万博1manbetx模型运行时,视频查看器块显示交通视频注释巷和车辆。
模型=“laneAndVehicleDetection”;open_system(模型);
的文件路径设置下载的网络模型预测和探测器模块的仿真软件模型。万博1manbetx
set_param (“laneAndVehicleDetection /车道检测”,“NetworkFilePath”lanenetFile) set_param (“laneAndVehicleDetection /车辆检测器”,“DetectorFilePath”vehiclenetFile)
设置测试视频,你的位置加载仿真软件模型。万博1manbetx
set_param (“laneAndVehicleDetection /交通视频”,“inputFileName”mediaFile)
车道检测
预测块加载pretrained车道检测网络的trainedLaneNet.mat
文件。这个网络需要一个图像作为输入和输出两个车道边界对应于自我的左和右车道车辆。每个车道边界由抛物型方程表示:
这里,y是横向偏移和x是纵向的距离。网络输出三个参数a、b和c /巷。的LaneDetectionCoordinates
MATLAB函数块定义了一个函数lane_detection_coordinates
这需要预测块的输出和输出三个参数:laneFound
,ltPts
,rtPts
。块使用阈值来确定左和右车道边界都是发现。如果被发现,laneFound
将真实边界的轨迹计算和存储在吗ltPts
和rtPts
。
类型lane_detection_coordinates
函数[laneFound、ltPts rtPts] = lane_detection_coordinates (laneNetOut) % 2020年版权MathWorks公司持久laneCoeffMeans;如果isempty (laneCoeffMeans) laneCoeffMeans = (-0.0002 0.0002 1.4740 -0.0002 0.0045 - -1.3787);结束持久laneCoeffStds;如果isempty (laneCoeffStds) laneCoeffStds = (0.0030 0.0766 0.6313 0.0026 0.0736 - 0.9846);params = laneNetOut结束。* laneCoeffStds + laneCoeffMeans;isRightLaneFound = abs (params (6)) > 0.5;% c应超过0.5是一个右车道isLeftLaneFound = abs (params (3)) > 0.5;持久vehicleXPoints;如果isempty (vehicleXPoints) vehicleXPoints = 3;%米,在传感器端ltPts = coder.nullcopy(0(28日2,'单')); 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
车辆检测
YOLO v2意思对象检测网络由两个子网:特征提取网络之后,检测网络。这pretrained网络使用ResNet-50
特征提取。检测子是一个小的CNN与特征提取网络和由几个卷积的一层又一层特定于YOLO v2的意思。执行车辆万博1manbetx检测使用的仿真软件模型对象探测器。物体以一个图像作为输入和输出边界框坐标以及信心得分的车辆图像。
注释的车辆碰撞盒子和巷轨道交通视频中
的LaneVehicleAnnotation
MATLAB函数块定义了一个函数lane_vehicle_annotation
注释,车辆边界框得分的信心。同样,如果laneFound
是真的,那么左和右车道边界存储在吗ltPts
和rtPts
在交通视频注释。
类型lane_vehicle_annotation
函数= lane_vehicle_annotation (laneFound、ltPts rtPts, bboxes,分数,)%版权2020年MathWorks公司如果~ isempty (bboxes) = insertObjectAnnotation(“矩形”,bboxes分数);最后分=编码器。nullcopy(0(28 4 '单'));如果laneFound prevpt = [ltPts (1,1) ltPts (1、2)];k = 2:1:28 pts (k, 1:4) = (prevpt ltPts (k, 1) ltPts (k, 2)];prevpt = [ltPts (k, 1) ltPts (k, 2)];结束= insertShape(分,“行”,“线宽”,2);prevpt = [rtPts (1,1) rtPts (1、2)];k = 2:1:28 pts (k, 1:4) = (prevpt rtPts (k, 1) rtPts (k, 2)];prevpt = [rtPts (k, 1) rtPts (k, 2)]; end In = insertShape(In, 'Line', pts, 'LineWidth', 2); In = insertMarker(In, ltPts); In = insertMarker(In, rtPts); end end
Pretrained车道,车辆检测网络
这个函数下载yolov2ResNet50VehicleExample.mat
文件。
getVehicleDetectionAndLaneDetectionNetworks ()
运行仿真
配置参数对话框打开。在模拟目标窗格中,在深度学习组,选择目标库MKL-DNN
。
set_param(模型,“SimDLTargetLibrary”,“MKL-DNN”);
在接口窗格中,在深度学习组,选择目标库作为MKL-DNN
。
set_param(模型,“DLTargetLibrary”,“MKL-DNN”);
验证巷和车辆检测算法和显示巷轨迹,车辆边界框,和许多交通视频中加载仿真软件模型,仿真运行。万博1manbetx
set_param (“laneAndVehicleDetection”,“SimulationMode”,“正常”);sim卡(“laneAndVehicleDetection”);
生成和构建仿真软件模型万博1manbetx
在代码生成窗格中,选择语言作为c++。
set_param(模型,“TargetLang”,“c++”);
生成和构建仿真软件模型使用万博1manbetxslbuild
命令。代码生成器中的文件的地方laneAndVehicleDetection_grt_rtw
在当前工作目录下建立文件夹。
currentDir = pwd;状态= evalc (“slbuild (laneAndVehicleDetection)”);
生成c++代码
子文件夹命名laneAndVehicleDetection_grt_rtw
包含生成c++代码对应不同的块仿真软件模型和所执行的特定操作的块。万博1manbetx例如,该文件trainedLaneNet0_0.h
包含c++类,其中包含属性和成员函数代表pretrained车道检测网络。
hfile = fullfile (currentDir,“laneAndVehicleDetection_grt_rtw”,…“trainedLaneNet0_0.h”);rtwdemodbtype (hfile“ifndef RTW_HEADER_trainedLaneNet0_0_h_”,…“# endif”)
的ifndef RTW_HEADER_trainedLaneNet0_0_h_ # define RTW_HEADER_trainedLaneNet0_0_h_ MWOnednnTargetNetworkImpl # include”。hpp rtwtypes # include。h MWTensorBase # include。hpp MWTensor # include。hpp MWCNNLayer # include。hpp MWInputLayer # include。hpp MWElementwiseAffineLayer # include。hpp MWFusedConvActivationLayer # include。hpp MWNormLayer # include。hpp MWMaxPoolingLayer # include。hpp MWFCLayer # include。hpp MWReLULayer # include。hpp MWOutputLayer # include。hpp MWConvLayer # include。hpp MWYoloExtractionLayer # include。hpp MWSigmoidLayer # include。hpp" #include "MWExponentialLayer.hpp" #include "MWYoloSoftmaxLayer.hpp" #include "MWConcatenationLayer.hpp" #include "MWActivationFunctionType.hpp" #include "MWRNNParameterTypes.hpp" #include "MWTargetTypes.hpp" #include "shared_layers_export_macros.hpp" #include "MWOnednnUtils.hpp" #include "MWOnednnCustomLayerBase.hpp" #include "MWOnednnCommonHeaders.hpp" class trainedLaneNet0_0 { public: boolean_T isInitialized; boolean_T matlabCodegenIsDeleted; trainedLaneNet0_0(); void setSize(); void resetState(); void setup(); void predict(); void cleanup(); real32_T *getLayerOutput(int32_T layerIndex, int32_T portIndex); int32_T getLayerOutputSize(int32_T layerIndex, int32_T portIndex); real32_T *getInputDataPointer(int32_T index); real32_T *getInputDataPointer(); real32_T *getOutputDataPointer(int32_T index); real32_T *getOutputDataPointer(); int32_T getBatchSize(); ~trainedLaneNet0_0(); private: int32_T numLayers; MWTensorBase *inputTensors; MWTensorBase *outputTensors; MWCNNLayer *layers[18]; MWOnednnTarget::MWTargetNetworkImpl *targetImpl; void allocate(); void postsetup(); void deallocate(); };
同样,该文件yolov2ResNet50VehicleExample0_0.h
包含c++类,这代表了pretrained YOLO v2意思检测网络。
hfile = fullfile (currentDir,“laneAndVehicleDetection_grt_rtw”,…“yolov2ResNet50VehicleExample0_0.h”);rtwdemodbtype (hfile“ifndef RTW_HEADER_yolov2ResNet50VehicleExample0_0_h_”,…“# endif”)
的ifndef RTW_HEADER_yolov2ResNet50VehicleExample0_0_h_ # define RTW_HEADER_yolov2ResNet50VehicleExample0_0_h_ MWOnednnTargetNetworkImpl # include”。hpp rtwtypes # include。h MWTensorBase # include。hpp MWTensor # include。hpp MWCNNLayer # include。hpp MWInputLayer # include。hpp MWElementwiseAffineLayer # include。hpp MWFusedConvActivationLayer # include。hpp MWNormLayer # include。hpp MWMaxPoolingLayer # include。hpp MWFCLayer # include。hpp MWReLULayer # include。hpp MWOutputLayer # include。hpp MWConvLayer # include。hpp MWYoloExtractionLayer # include。hpp MWSigmoidLayer # include。hpp" #include "MWExponentialLayer.hpp" #include "MWYoloSoftmaxLayer.hpp" #include "MWConcatenationLayer.hpp" #include "MWActivationFunctionType.hpp" #include "MWRNNParameterTypes.hpp" #include "MWTargetTypes.hpp" #include "shared_layers_export_macros.hpp" #include "MWOnednnUtils.hpp" #include "MWOnednnCustomLayerBase.hpp" #include "MWOnednnCommonHeaders.hpp" class yolov2ResNet50VehicleExample0_0 { public: boolean_T isInitialized; boolean_T matlabCodegenIsDeleted; yolov2ResNet50VehicleExample0_0(); void setSize(); void resetState(); void setup(); void predict(); void activations(int32_T layerIdx); void cleanup(); real32_T *getLayerOutput(int32_T layerIndex, int32_T portIndex); int32_T getLayerOutputSize(int32_T layerIndex, int32_T portIndex); real32_T *getInputDataPointer(int32_T index); real32_T *getInputDataPointer(); real32_T *getOutputDataPointer(int32_T index); real32_T *getOutputDataPointer(); int32_T getBatchSize(); ~yolov2ResNet50VehicleExample0_0(); private: int32_T numLayers; MWTensorBase *inputTensors; MWTensorBase *outputTensors; MWCNNLayer *layers[57]; MWOnednnTarget::MWTargetNetworkImpl *targetImpl; void allocate(); void postsetup(); void deallocate(); };
注意:如果系统目标文件参数设置为grt.tlc
,你必须选择只生成代码模型配置参数。如果你设置系统目标文件来ert.tlc
,您可以清除只生成代码参数,但创建一个可执行文件,您必须生成一个主程序示例。
清理
关闭仿真软件模型。万博1manbetx
save_system (“laneAndVehicleDetection”);close_system (“laneAndVehicleDetection”);