主要内容

交通标志检测和识别

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

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

第三方的先决条件

必需的

本例生成CUDA MEX,并具有以下第三方需求。

  • CUDA®支持NVIDIA®GPU和兼容驱动程序。

可选

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

验证GPU环境

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

envCfg = coder.gpuEnvConfig ('主持人');envCfg。DeepLibTarget =“cudnn”;envcfg.deepcodegen = 1;Envcf​​g.quiet = 1;Coder.CheckGpuInstall(Envcf​​g);

检测与识别网络

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

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

利用MATLAB对同一图像进行网络训练。

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

获得预先训练过的系列网络

下载检测和识别网络。

getTsdr ();

检测网络由卷积层、ReLU泄漏层、全连接层等58层组成。

负载(“yolo_tsr.mat”);yolo。意思层
ans = 58×1 Layer array with layers:1的输入图像输入448×448×3图片2 conv1卷积64 7×7×3旋转步(2 - 2)和填充[3 3 3 3]3‘relu1漏ReLU漏ReLU规模0.1 4“pool1”马克斯池2×2马克斯池步(2 - 2)和填充[0 0 0 0]5 conv2卷积192 3×3×64旋转步[1]和填充[1 1 1 1]6 relu2漏水的ReLU漏ReLU规模0.1 7“pool2”马克斯池2×2马克斯池步(2 - 2)和填充[0 0 0 0]8 conv3卷积128 1×1×192旋转步[1]和填充[0 0 0 0]9“relu3”漏ReLU漏ReLU规模0.1 10 conv4卷积256 3×3×128旋转步[1]和填充[1 1 1 1]11 relu4漏水的ReLU漏ReLU规模0.1 12 conv5卷积256 1×1×256旋转步[1]和填充[0 0 0 0]13的relu5漏ReLU漏ReLU规模0.1 14 conv6卷积512 3×3×256旋转步[1]和填充[1 1 1 1]15“relu6”漏ReLU漏ReLU规模0.1 16“pool6”马克斯池2×2马克斯池步伐[2 2]和填充[0 0 0 0]17 conv7卷积256 1×1×512旋转步[1]和填充[0 0 0 0]18“relu7”漏ReLU漏ReLU规模0.1 19 conv8卷积512 3×3×256旋转步[1]和填充[1 1 1 1]20 ' relu8漏ReLU漏ReLU规模0.1 21 ' conv9卷积256 1×1×512旋转步[1]和填充[0 0 0 0]22“relu9”漏ReLU漏ReLU规模0.1 23 conv10卷积512 3×3×256旋转步[1]和填充[1 1 1 1]24的relu10 ReLU漏水漏ReLU规模0.1 25 conv11卷积256 1×1×512旋转步[1]和填充[0 0 0 0]26 relu11 ReLU漏水Leaky ReLU with scale 0.1 27 'conv12' Convolution 512 3×3×256 convolutions with stride [1 1] and padding [1 1 1 1] 28 'relu12' Leaky ReLU Leaky ReLU with scale 0.1 29 'conv13' Convolution 256 1×1×512 convolutions with stride [1 1] and padding [0 0 0 0] 30 'relu13' Leaky ReLU Leaky ReLU with scale 0.1 31 'conv14' Convolution 512 3×3×256 convolutions with stride [1 1] and padding [1 1 1 1] 32 'relu14' Leaky ReLU Leaky ReLU with scale 0.1 33 'conv15' Convolution 512 1×1×512 convolutions with stride [1 1] and padding [0 0 0 0] 34 'relu15' Leaky ReLU Leaky ReLU with scale 0.1 35 'conv16' Convolution 1024 3×3×512 convolutions with stride [1 1] and padding [1 1 1 1] 36 'relu16' Leaky ReLU Leaky ReLU with scale 0.1 37 'pool16' Max Pooling 2×2 max pooling with stride [2 2] and padding [0 0 0 0] 38 'conv17' Convolution 512 1×1×1024 convolutions with stride [1 1] and padding [0 0 0 0] 39 'relu17' Leaky ReLU Leaky ReLU with scale 0.1 40 'conv18' Convolution 1024 3×3×512 convolutions with stride [1 1] and padding [1 1 1 1] 41 'relu18' Leaky ReLU Leaky ReLU with scale 0.1 42 'conv19' Convolution 512 1×1×1024 convolutions with stride [1 1] and padding [0 0 0 0] 43 'relu19' Leaky ReLU Leaky ReLU with scale 0.1 44 'conv20' Convolution 1024 3×3×512 convolutions with stride [1 1] and padding [1 1 1 1] 45 'relu20' Leaky ReLU Leaky ReLU with scale 0.1 46 'conv21' Convolution 1024 3×3×1024 convolutions with stride [1 1] and padding [1 1 1 1] 47 'relu21' Leaky ReLU Leaky ReLU with scale 0.1 48 'conv22' Convolution 1024 3×3×1024 convolutions with stride [2 2] and padding [1 1 1 1] 49 'relu22' Leaky ReLU Leaky ReLU with scale 0.1 50 'conv23' Convolution 1024 3×3×1024 convolutions with stride [1 1] and padding [1 1 1 1] 51 'relu23' Leaky ReLU Leaky ReLU with scale 0.1 52 'conv24' Convolution 1024 3×3×1024 convolutions with stride [1 1] and padding [1 1 1 1] 53 'relu24' Leaky ReLU Leaky ReLU with scale 0.1 54 'fc25' Fully Connected 4096 fully connected layer 55 'relu25' Leaky ReLU Leaky ReLU with scale 0.1 56 'fc26' Fully Connected 539 fully connected layer 57 'softmax' Softmax softmax 58 'classoutput' Classification Output crossentropyex with '1' and 538 other classes

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

负载(“RecognitionNet.mat”);事先。层
ans = 14×1 Layer array with layers:1“imageinput”图像输入48×48×3图片zerocenter正常化和randfliplr的扩增2 conv_1卷积100 7×7×3步[1]和填充的卷积[0 0 0 0]3‘relu_1 ReLU ReLU 4“maxpool_1”马克斯池2×2马克斯池步(2 - 2)和填充[0 0 0 0]5 conv_2卷积150 4×4×100旋转步[1]和填充[0 0 0 0]6‘relu_2 ReLU ReLU 7“maxpool_2”马克斯池2×2马克斯池步[2 2]和填充[0 0 0 0]8 conv_3卷积250 4×4×150旋转步[1]和填充[0 0 0 0]9“maxpool_3”马克斯池2×2马克斯池步[2 2]和填充[0 0 0 0]10“fc_1”完全连接300完全已连接层11 'dropout' dropout 90% dropout 12 'fc_2' Fully connected 35 Fully connected layer 13 'softmax' softmax' softmax 14 'classoutput' classoutput crossentropyex with '0'和34个其他类

tsdr_predict入口点函数

tsdr_predict.m入口点函数取图像输入,利用检测网络检测图像中的交通标志。该功能通过使用函数抑制重叠检测(NMS)selectStrongestBbox并利用识别网络对交通标志进行识别。函数从YOLO_TSR.MAT.变成一个持久变量detectionnetRecognitionNet.mat变成一个持久变量认可.该函数在后续调用中重用持久对象。

类型(“tsdr_predict.m”
函数[selectendbbox,idx] = tsdr_predict(img)%#codegen%此函数使用检测网络%(Modified版本的Yolo)检测图像中的交通标志,并使用识别网络%%输入识别(分类):%% IM:输入测试图像%%输出:%%selectedBox:检测到的绑定框%IDX:相应的类别版权所有2017-2019 MathWorks,Inc.Coder.GPU.Kernelfun;%调整图像IMG_RZ = IMResize(IMG,[448,448]);%转换为BGR格式IMG_RZ = IMG_RZ(:,:,3:-1:1);img_rz = im2single(img_rz);%% TSD持久性检测网络;如果是isempty(侦查网络)检测目录= coder.loaddeeplearningnetwork('yolo_tsr.mat','detection');结束预测=检测荷式.Activations(IMG_RZ,56,'Outputas','频道');%%将预测转换为边界框属性类= 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('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 MEXtsdr_predict功能

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

cfg = coder.gpuConfig (墨西哥人的);cfg。TargetLang =“c++”;cfg。DeepLearningConfig =编码器。DeepLearningConfig (“cudnn”);codegen配置cfgtsdr_predictarg游戏{(480704 3 uint8)}报告
代码生成成功:要查看报告,打开('codegen/mex/tsdr_predict/html/report.mldatx')。

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

运行生成的墨西哥人

加载一个输入图像。

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

调用tsdr_predict_mex在输入图像上。

IM = Imresize(IM,[480,704]);[bboxes,classes] = tsdr_predict_mex(IM);

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

ClassNames = {“addedLane”“慢”'蘸''speedlimit25'“speedLimit35”“speedLimit40”“speedLimit45”...“speedLimit50”“speedLimit55”“speedLimit65”“speedLimitUrdbl”“doNotPass”“十字路口”...“keepRight”“laneEnds”“合并”“noLeftTurn”“noRightTurn”“停止”'peistriancrossing'...“stopAhead”“rampSpeedAdvisory20”“rampSpeedAdvisory45”“truckSpeedLimit55”...'rampspeedadvisory50'“turnLeft”“rampSpeedAdvisoryUrdbl”'右转''rightlanemustturn'...“收益”“yieldAhead”“学校”“schoolSpeedLimit25”“zoneAhead45”“signalAhead”};classRec =类名(类);

显示检测到的交通标志。

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

视频交通标志检测与识别

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

% Input video v = VideoReader('stop.avi'); / /输入视频fps = 0;
while hasFrame(v) %拍摄帧图片= readFrame(v);照片= imresize(图片,(920、1632));% Call MEX功能用于交通标志检测与识别[bboxes,一堂课]= tsdr_predict_mex(图);纽特= toc;
% FPS = .9* FPS + .1*(1/newt);
%显示
displayDetections(图画、bboxes一堂课,fps);结束

清除加载到内存中的静态网络对象。

清除梅克斯

另请参阅

功能

对象

相关话题