Main Content

使用YOLO V2和Intel MKL-DNN生成用于对象检测的C ++代码

此示例显示如何在英特尔®处理器上生成Yolo V2对象检测网络的C ++代码。生成的代码使用Intel Math Kernel库进行深度神经网络(MKL-DNN)。

有关更多信息,请参阅对象检测使用YOLO V2深度学习(电脑视觉工具箱)

先决条件

  • 深度神经网络的英特尔数学内核库(MKL-DNN)

  • 参考Mkldnn CPU支金宝app持要了解支持MKL-DNN库的处理器列表金宝app

  • MATLAB®Coder™用于C ++代码生成

  • Matlab编码器Interface for Deep Learning support package

  • Deep Learning Toolbox™ for using theDagnetwork.目的

  • 用于视频I / O操作的计算机Vision Tool™

有关编译器和库的支持版本的更多信息,请参阅金宝appThird-Party Hardware and Software

This example is supported on Linux®, Windows®, and macOS platforms and not supported for MATLAB Online.

Get the PretrainedDagnetwork.目的

DAG网络contains 150 layers including convolution, ReLU, and batch normalization layers and the YOLO v2 transform and YOLO v2 output layers.

net = getyolov2();
Downloading pretrained detector (98 MB)...

使用命令net.Layers要查看网络的所有层。

net.Layers

代码生成Yolov2_Detection.Function

Yolov2_Detection.function attached with the example takes an image input and runs the detector on the image using the network saved inyolov2ResNet50VehicleExample.mat。这function loads the network object fromyolov2ResNet50VehicleExample.mat进入持久性变量yolov2Obj。Subsequent calls to the function reuse the persistent object for detection.

类型('yolov2_detection.m'的)
函数outimg = yolov2_detection(in)%copyright 2018-2019 Mathworks,Inc.%持久对象yolov2obj用于加载Yolov2ObjectDetector对象。%在第一次调用此函数时,构建持久对象并为%设置。随后对函数的调用重用相同的对象在输入上调用检测%,从而避免重建和重新加载%网络对象。持久的yolov2obj;如果是isempty(yolov2obj)yolov2obj = coder.loaddeeplearningnetwork('yolov2resnet50vehiceexample.mat');输入%输入[bboxes,〜,标签] = yolov2obj.detect(in,'threshold',0.5);Outimg = In;%将分类标签转换为字符向量标签= CellStr(标签)的单元格数组;if〜(isempty(bboxes)&& isempty(标签))%注释图像中的检测。Outimg = InsertObjectAnnotation(IN,'矩形',Bboxes,标签); end

要生成代码,为MEX目标创建代码配置对象,并将目标语言设置为C ++。使用coder.DeepLearningConfig功能创建MKL-DNN Deep Learing配置对象。将此对象分配给DeepLearningConfig代码配置对象的属性。将输入大小指定为参数Codegen.命令。在此示例中,YOLO V2网络的输入层大小是[224,224,3]

cfg = coder.config('mex');cfg.targetlang ='c ++';cfg.deeplearningconfig = coder.deeplearningconfig('mkldnn');Codegen.-Config.CFG.Yolov2_Detection.-  args.{ONE(224,224,3,'UINT8')}-report
代码生成成功:要查看报告,请打开('codegen / mex / yolov2_detection / html / export.mldatx')。

Run the Generated MEX Function on Example Input

设置视频文件阅读器并阅读示例输入视频Highway_Lanechange.mp4.。Create a video player to display the video and the output detections.

videoFile ='highway_lanechange.mp4';videoFreader = vision.VideoFileReader(videoFile,'VideoOutputDatatype''uint8');depvideoplayer = Vision.DeployapleableDoplayer('尺寸''Custom''自定义大小',[640 480]);

Read the video input frame by frame and detect the vehicles in the video by using the detector.

cont = ~isDone(videoFreader);whilecont I = step(videoFreader); in = imresize(I,[224,224]); out = yolov2_detection_mex(in); depVideoPlayer(out); cont = ~isDone(videoFreader) && isOpen(depVideoPlayer);如果关闭视频播放器图窗口,则%退出循环结尾

参考ences

[1] Redmon, Joseph, and Ali Farhadi. "YOLO9000: Better, Faster, Stronger." In2017年电脑视觉和模式识别(CVPR)的IEEE会议,6517-25。檀香山,嗨:IEEE,2017年。

也可以看看

|

Related Topics