主要内容

交通标志检测与识别

这个例子展示了如何生成CUDA®墨西哥人的交通标志检测与识别应用程序代码使用深度学习。交通标志检测与识别是一个重要的应用程序对于驾驶员辅助系统,帮助驾驶员对道路标志和提供信息。

在这个交通标志检测与识别的例子执行三个步骤——检测,非最大抑制(NMS),和承认。首先,检测交通标志的例子一个输入图像通过使用一个对象检测网络,你只看一次的一种变体(YOLO)意思网络。然后,重叠使用NMS算法检测被抑制。最后,识别网络分类检测交通标志。

第三方的先决条件

要求

这个示例中生成CUDA墨西哥人,有以下第三方的要求。

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

可选

等non-MEX构建静态、动态库或可执行文件,这个例子有以下额外的需求。

验证GPU环境

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

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

检测和识别网络

检测网络训练Darknet框架和导入MATLAB®的推理。因为交通标志的大小是相对较小的对图像和每个类的训练样本数量较少的训练数据,所有的交通标志都视为一个单独的类培训检测网络。

检测网络将输入图像划分为7-by-7网格。每个网格单元检测到一个交通标志如果交通标志的中心落在网格内的细胞。每个单元预测两个边界框和信心得分为这些边界框。信心的分数表明是否框包含一个对象。每个单元预测概率寻找网格中的交通标志细胞。前的最后得分是产品分数。你用一个阈值在这最后得分0.2选择检测。

识别网络训练采用MATLAB在相同的图像。

trainRecognitionnet.m辅助脚本显示了识别网络培训。

得到Pretrained SeriesNetwork

下载检测和识别网络。

getTsdr ();

检测网络包含58层包括卷积,漏水的ReLU,充分连接层。

负载(“yolo_tsr.mat”);yolo
yolo =意思SeriesNetwork属性:层:[58×1 nnet.cnn.layer.Layer] InputNames:{“输入”}OutputNames: {“classoutput”}

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

analyzeNetwork (yolo)的意思

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

负载(“RecognitionNet.mat”);事先
事先与属性= SeriesNetwork:层:[14×1 nnet.cnn.layer.Layer] InputNames: {“imageinput”} OutputNames: {“classoutput”}

tsdr_predict入口点函数

tsdr_predict.m入口点函数接受一个图像输入和检测的交通标志图像通过使用检测网络。函数抑制重叠检测(NMS)通过使用selectStrongestBbox和识别交通标志的识别网络。函数加载网络对象yolo_tsr.mat到一个持续的变量detectionnetRecognitionNet.mat到一个持续的变量recognitionnet。在随后的调用函数重用持久对象。

类型(“tsdr_predict.m”)
函数[selectedBbox idx] = tsdr_predict (img) % # codegen %这个函数图像中检测交通标志使用检测网络%(修改版Yolo)意思和识别(分类)使用识别网络输入% %:% % im:输入测试图像% %输出:% % selectedBbox:检测边界框% idx:相应的类% 2017 - 2021版权MathWorks, inc . coder.gpu.kernelfun;%调整图像img_rz = imresize (img, [448448]);%转换为BGR格式img_rz = img_rz (:,:, 3: 1:1);img_rz = im2single (img_rz);% % TSD持久detectionnet;如果isempty (detectionnet) detectionnet = coder.loadDeepLearningNetwork (“yolo_tsr.mat”、“检测”);56岁的最终预测= detectionnet.activations (img_rz OutputAs,“渠道”);% %转换预测边界框属性类= 1;num = 2;= 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('RecognitionNet.mat','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

生成CUDA的墨西哥人tsdr_predict函数

为一个墨西哥人的目标创建一个GPU配置对象并设置目标语言c++。使用coder.DeepLearningConfig(GPU编码器)函数创建一个CuDNN深度学习配置对象,并将其分配给DeepLearningConfigGPU代码配置对象的属性。生成CUDA墨西哥人使用codegen命令并指定输入的大小(480704 3)。这个值对应的输入图像大小tsdr_predict函数。

cfg = coder.gpuConfig (墨西哥人的);cfg。TargetLang =“c++”;cfg。DeepLearningConfig = coder.DeepLearningConfig (“cudnn”);codegen配置cfgtsdr_predictarg游戏{(480704 3 uint8)}报告
代码生成成功:查看报告

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

运行生成的墨西哥人

加载一个输入图像。

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

调用tsdr_predict_mex在输入图像。

[480704]im = imresize (im);[bboxes、类]= tsdr_predict_mex (im);

地图类数字交通标志名称类词典。

一会= {“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 =类名(类);

显示检测到的交通标志。

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

交通标志检测与识别视频

包括辅助文件tsdr_testVideo.m抓住从测试视频帧,执行交通标志检测与识别,和情节结果在测试视频的每一帧。

类型tsdr_testVideo
函数tsdr_testVideo % 2017 - 2021版权MathWorks, Inc . % v = VideoReader输入视频(“stop.avi”);% %集成codegeneration %的交通标志检测与识别生成墨西哥人cfg = coder.config(墨西哥人);cfg。GpuConfig = coder.gpu.config;cfg.GpuConfig。启用= true;cfg。GenerateReport = false;cfg。TargetLang =“c++”; % Create a GPU Configuration object for MEX target setting target language % to C++. Run the |codegen| command specifying an input of input video % frame size. This corresponds to the input image size of tsdr_predict % function. codegen -config cfg tsdr_predict -args {ones(480,704,3 ,'uint8')} 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); 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

相关的话题