主要内容

交通标志检测与识别

本示例展示了如何为使用深度学习的交通标志检测和识别应用程序生成CUDA®MEX代码。交通标志检测与识别是辅助驾驶系统的重要应用,为驾驶员提供有关道路标志的信息。

在这个交通标志检测和识别示例中,您将执行三个步骤——检测、非最大抑制(NMS)和识别。首先,该示例使用对象检测网络检测输入图像上的交通标志,该网络是You Only Look Once (YOLO)网络的变体。然后利用NMS算法抑制重叠检测。最后,识别网络对检测到的交通标志进行分类。

第三方的先决条件

要求

本例生成CUDA MEX,第三方需求如下。

  • CUDA®启用NVIDIA®GPU和兼容驱动程序。

可选

对于非mex构建,例如静态、动态库或可执行文件,此示例具有以下附加要求。

检查GPU环境

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

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

检测与识别网络

检测网络在Darknet框架中训练,并导入到MATLAB®中进行推理。由于交通标志相对于图像的大小较小,并且训练数据中每类训练样本的数量较少,因此将所有的交通标志视为一个单一的类来训练检测网络。

检测网络将输入图像划分为7 × 7的网格。如果交通标志的中心落在每个网格单元格内,则每个网格单元格检测一个交通标志。每个单元格预测两个边界框和这些边界框的置信度分数。置信度分数表示框中是否包含对象。每个单元格预测在网格单元格中找到交通标志的概率。最后的分数是前面分数的乘积。在最终分数上应用0.2的阈值来选择检测。

利用MATLAB在相同图像上训练识别网络。

trainRecognitionnet.m助手脚本显示识别网络训练。

获得预训练的检测器和识别网络

本例使用yolo_tsr而且RecognitionNetmat -包含预训练网络的文件。文件大小分别约为6MB和992MB。从MathWorks网站下载这些文件。

detectorNet = matlab.internal.examples.download万博1manbetxSupportFile(“gpucoder / cnn_models traffic_sign_detection / v001 '“yolo_tsr.mat”);识别码= matlab.internal.examples.downloadSupportFile(万博1manbetx“gpucoder / cnn_models traffic_sign_detection / v001 '“RecognitionNet.mat”);

检测网络包含58层,包括卷积层、漏ReLU层和全连接层。

负载(detectorNet);yolo
yolo = SeriesNetwork with properties: Layers: [58×1 nnet.cnn.layer.Layer] InputNames: {'input'} OutputNames: {'classoutput'}

要查看网络体系结构,请使用analyzeNetwork(深度学习工具箱)函数。

analyzeNetwork (yolo)的意思

识别网络包含14层,包括卷积层、全连接层和分类输出层。

负载(recognitionNet);事先
convnet = SeriesNetwork with properties: Layers: [14×1 nnet.cnn.layer.Layer] InputNames: {'imageinput'} OutputNames: {'classoutput'}

tsdr_predict入口点函数

tsdr_predict.m入口点函数接受图像输入,利用检测网络检测图像中的交通标志。该功能通过使用来抑制重叠检测selectStrongestBbox并利用识别网络对交通标志进行识别。该函数从加载网络对象yolo_tsr.mat变成一个持久变量detectionnetRecognitionNet.mat变成一个持久变量recognitionnet.该函数在后续调用中重用持久对象。

类型(“tsdr_predict.m”
function [seltedbbox,idx] = tsdr_predict(img,detectorMATFile,recogMATFile) %#codegen code .gpu.kernelfun;= imresize(img,[448,448]);%转换成BGR格式img_rz = img_rz(:,:,3:-1:1);Img_rz = im2single(Img_rz);%% TSD持久检测网;if isempty(detectionnet) detectionnet = code . loaddeeplearningnetwork (detectorMATFile,'Detection');end forecasts = detectionnet.activations(img_rz,56,'OutputAs','channels');将预测转换为边界框属性类= 1;Num = 2;Side = 7; thresh = 0.2; [h,w,~] = size(img); boxes = single(zeros(0,4)); probs = single(zeros(0,1)); for i = 0:(side*side)-1 for n = 0:num-1 p_index = side*side*classes + i*num + n + 1; scale = predictions(p_index); prob = zeros(1,classes+1); for j = 0:classes class_index = i*classes + 1; tempProb = scale*predictions(class_index+j); if tempProb > thresh row = floor(i / side); col = mod(i,side); box_index = side*side*(classes + num) + (i*num + n)*4 + 1; bxX = (predictions(box_index + 0) + col) / side; bxY = (predictions(box_index + 1) + row) / side; bxW = (predictions(box_index + 2)^2); bxH = (predictions(box_index + 3)^2); prob(j+1) = tempProb; probs = [probs;tempProb]; boxX = (bxX-bxW/2)*w+1; boxY = (bxY-bxH/2)*h+1; boxW = bxW*w; boxH = bxH*h; boxes = [boxes; boxX,boxY,boxW,boxH]; end end end end %% Run Non-Maximal Suppression on the detected bounding boxess coder.varsize('selectedBbox',[98, 4],[1 0]); [selectedBbox,~] = selectStrongestBbox(round(boxes),probs); %% Recognition persistent recognitionnet; if isempty(recognitionnet) recognitionnet = coder.loadDeepLearningNetwork(recogMATFile,'Recognition'); end idx = zeros(size(selectedBbox,1),1); inpImg = coder.nullcopy(zeros(48,48,3,size(selectedBbox,1))); for i = 1:size(selectedBbox,1) ymin = selectedBbox(i,2); ymax = ymin+selectedBbox(i,4); xmin = selectedBbox(i,1); xmax = xmin+selectedBbox(i,3); % Resize Image inpImg(:,:,:,i) = imresize(img(ymin:ymax,xmin:xmax,:),[48,48]); end for i = 1:size(selectedBbox,1) output = recognitionnet.predict(inpImg(:,:,:,i)); [~,idx(i)]=max(output); end % Copyright 2017-2022 The MathWorks, Inc.

生成CUDA MEXtsdr_predict函数

创建MEX目标的GPU配置对象,并选择目标语言为c++。使用编码器。DeepLearningConfig函数创建CuDNN深度学习配置对象,并将其分配给DeepLearningConfigGPU代码配置对象的属性。要生成CUDA MEX,请使用codegen命令并指定输入的大小为[480,704,3]。的输入图像大小对应tsdr_predict函数。

cfg = code . gpuconfig (墨西哥人的);cfg。TargetLang =“c++”;cfg。DeepLearningConfig =编码器。DeepLearningConfig (“cudnn”);inputArgs = {ones(480,704,3,“uint8”), coder.Constant (detectorNet),...coder.Constant (recognitionNet)};codegen配置cfgtsdr_predictarg游戏inputArgs报告
代码生成成功:查看报告

使用TensorRT生成代码,通过coder.DeepLearningConfig(“tensorrt”)作为编码器配置对象的选项,而不是“cudnn”

运行生成的MEX

加载一个输入图像。

Im = imread(“stop.jpg”);imshow (im);

调用tsdr_predict_mex在输入图像上。

Im = imresize(Im, [480,704]);[bboxes,classes] = tsdr_predict_mex(im,detectorNet,recognitionNet);

将类号映射到类字典中的交通标志名。

classNames = {...“addedLane”“慢”“下降”“speedLimit25”“speedLimit35”“speedLimit40”...“speedLimit45”“speedLimit50”“speedLimit55”“speedLimit65”...“speedLimitUrdbl”“doNotPass”“十字路口”“keepRight”“laneEnds”...“合并”“noLeftTurn”“noRightTurn”“停止”“pedestrianCrossing”...“stopAhead”“rampSpeedAdvisory20”“rampSpeedAdvisory45”...“truckSpeedLimit55”“rampSpeedAdvisory50”“turnLeft”...“rampSpeedAdvisoryUrdbl”“turnRight”“rightLaneMustTurn”“收益”...“yieldAhead”“学校”“schoolSpeedLimit25”“zoneAhead45”“signalAhead”};classRec = classNames(类);

显示检测到的交通标志。

outputImage = insertShape(im,“矩形”bboxes,“线宽”3);i = 1:size(bboxes,1) outputImage = insertText(outputImage,[bboxes(i,1)+...bboxes(我,3)bboxes(我,2)-20],classRec {},“字形大小”, 20岁,...“输入TextColor”“红色”);结束imshow (outputImage);

基于视频的交通标志检测与识别

包含的帮助文件tsdr_testVideo.m从测试视频中抓取帧,进行交通标志检测和识别,并将结果绘制在测试视频的每一帧上。

类型tsdr_testVideo
The MathWorks, Inc. %输入视频v = VideoReader('stop.avi');%%生成交通标志检测识别代码%为MEX目标创建GPU配置对象设置目标语言%为c++。执行|codegen|命令,指定输入视频%帧大小。这对应于tsdr_predict %函数的输入图像大小。cfg = code . gpuconfig ('mex');cfg。TargetLang = ' c++ ';cfg。DeepLearningConfig = code .DeepLearningConfig('cudnn');inputArgs = {ones(480,704,3,'uint8'),code .constant(detectorNet),… coder.Constant(recognitionNet)}; codegen -config cfg tsdr_predict -args inputArgs -report fps = 0; while hasFrame(v) % Take a frame picture = readFrame(v); picture = imresize(picture,[480,704]); % Call MEX function for Traffic Sign Detection and Recognition tic; [bboxes,clases] = tsdr_predict_mex(picture,detectorNet,recognitionNet); newt = toc; % fps fps = .9*fps + .1*(1/newt); % display diplayDetections(picture,bboxes,clases,fps); end end function diplayDetections(im,boundingBoxes,classIndices,fps) % Function for inserting the detected bounding boxes and recognized classes % and displaying the result % % Inputs : % % im : Input test image % boundingBoxes : Detected bounding boxes % classIndices : Corresponding classes % % Traffic Signs (35) classNames = {'addedLane','slow','dip','speedLimit25','speedLimit35',... 'speedLimit40','speedLimit45','speedLimit50','speedLimit55',... 'speedLimit65','speedLimitUrdbl','doNotPass','intersection',... 'keepRight','laneEnds','merge','noLeftTurn','noRightTurn','stop',... 'pedestrianCrossing','stopAhead','rampSpeedAdvisory20',... 'rampSpeedAdvisory45','truckSpeedLimit55','rampSpeedAdvisory50',... 'turnLeft','rampSpeedAdvisoryUrdbl','turnRight','rightLaneMustTurn',... 'yield','yieldAhead','school','schoolSpeedLimit25','zoneAhead45',... 'signalAhead'}; outputImage = insertShape(im,'Rectangle',boundingBoxes,'LineWidth',3); for i = 1:size(boundingBoxes,1) ymin = boundingBoxes(i,2); xmin = boundingBoxes(i,1); xmax = xmin+boundingBoxes(i,3); % inserting class as text at YOLO detection classRec = classNames{classIndices(i)}; outputImage = insertText(outputImage,[xmax ymin-20],classRec,... 'FontSize',20,'TextColor','red'); end outputImage = insertText(outputImage,... round(([size(outputImage,1) 40]/2)-20),... ['Frame Rate: ',num2str(fps)],'FontSize',20,'TextColor','red'); imshow(outputImage); end

另请参阅

功能

对象

相关的话题