主要内容

交通标志检测和识别

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

在此交通标志检测和识别示例中,您将执行三个步骤-检测、非最大抑制(NMS)和识别。首先,该示例使用对象检测网络检测输入图像上的交通标志,该网络是you Only Look Once(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层阵列层: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与步幅卷积[11]和填充[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 1]和填充[0 0 0 0] 9 'relu3' 漏RELU漏RELU带刻度0.1 10 'CONV4' 卷积256轮3×3×128匝与步幅[1 1]和填补[1 1 1 1] 11 'relu4' 漏RELU漏RELU带刻度0.1 12 'conv5' 卷积256轮1×1×256匝与步幅[1 1]和填充[0 0 0 0] 13 'relu5'漏泄RELU漏泄RELU带刻度0.1 14 'conv6' 卷积512轮3×3×256匝与步幅[1 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 1]和填补[0 0 0 0] 18 'relu7' 漏RELU漏RELU带刻度0.1 19 'conv8' 卷积512轮3×3×256匝与步幅[1 1]和填充[1 1 1 1] 20 'relu8' 漏RELU漏RELU带刻度0.1 21 'conv9' 卷积256轮1×1×512匝与步幅[1 1]和填充[0 0 0 0] 22 'relu9' 漏RELU漏RELU带刻度0.1 23 'conv10' 卷积512 3×3×256匝与步幅[1 1]和填充[1 1 1 1] 24 'relu10' 漏RELU漏RELU带刻度0.1 25 'conv11' 卷积256轮1×1×512匝与步幅[1 1]和填充[0 0 0 0] 26 'relu11' 漏RELU漏RELU带刻度0.1 27 'conv12' 卷积512轮3×3×256匝与步幅[1 1]和填充[1 1 1 1] 28 'relu12' 漏RELU漏RELU带刻度0.1 29 'conv13' 卷积256 1×1×512匝与步幅[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”);convnet.Layers
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_入口点函数

这个tsdr_predict.m入口点功能接收图像输入,并使用检测网络检测图像中的交通标志。该功能使用选择strongestbbox并利用识别网络对交通标志进行识别。函数从YOLO_TSR.MAT.变成一个持久变量detectionnetRecognitionNet.mat变成一个持久变量认可。该函数在后续调用中重用持久对象。

类型(“tsdr_.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 MEX为tsdr_功能

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

cfg=coder.gpuConfig('MEX');cfg.TargetLang=“C++”;cfg。DeepLearningConfig =编码器。DeepLearningConfig (“cudnn”); 编码基因-配置cfgtsdr_-args{one(480704,3,'uint8')}报告
代码生成成功:要查看报告,打开('codegen/mex/tsdr_predict/html/report.mldatx')。

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

运行生成的墨西哥人

加载一个输入图像。

im=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”,“不要通过”,“十字路口”,...“保持正确”,“laneEnds”,“合并”,“noLeftTurn”,“北转弯”,“停止”,“pedestrianCrossing”,...“stopAhead”,“rampSpeedAdvisory20”,“rampSpeedAdvisory45”,'truckSpeedLimit55',...“rampSpeedAdvisory50”,“左转”,'rampSpeedAdvisoryUrdbl','右转','rightlanemustturn',...'屈服',“yieldAhead”,'学校',‘学校速度限制25’,“45区”,“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,“文本颜色”,“红色”);结束imshow(输出图像);

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

所包含的帮助文件TSDR_TESTVIDEO.M.抓住从测试视频,进行交通标志的检测和识别,和绘图的测试视频的每一帧上的结果的帧。

% Input video v = VideoReader('stop.avi'); / /输入视频fps = 0;
hasFrame(v)%Take a frame picture=readFrame(v);picture=imresize(picture,[9201632]);%Call MEX函数用于交通标志检测和识别tic;[bboxes,clases]=tsdr_predict_MEX(picture);newt=toc;
%fps fps=.9*fps+.1*(1/牛顿);
%展示
displayDetections(图画、bboxes一堂课,fps);结束

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

清除梅克斯

也可以看看

功能

对象

相关话题