主要内容

Generate C++ Code for Object Detection Using YOLO v2 and Intel MKL-DNN

This example shows how to generate C++ code for the YOLO v2 Object detection network on an Intel® processor. The generated code uses the Intel Math Kernel Library for Deep Neural Networks (MKL-DNN).

For more information, seeObject Detection Using YOLO v2 Deep Learning(计算机视觉工具箱)

Prerequisites

  • 在tel Math Kernel Library for Deep Neural Networks (MKL-DNN)

  • ReferMKLDNN CPU Supportto know the list of processors that supports MKL-DNN library

  • MATLAB® Coder™ for C++ code generation

  • MATLAB编码器界面为深学习支持包金宝app

  • 深度学习工具箱™使用DAGNetworkobject

  • Computer Vision Toolbox™ for video I/O operations

For more information on the supported versions of the compilers and libraries, see第三方硬件和软件

此示例支持Linux®,Wind金宝appows®和MacOS平台,不支持Matlab Online。

托料DAGNetworkObject

DAG网络包含150层,包括卷积,Relu和批量归一化层和YOLO V2变换和YOLO V2输出层。

net = getYOLOv2();
下载掠夺探测器(98 MB)......

Use the commandNet.Layers.to see all the layers of the network.

Net.Layers.

Code Generation foryolov2_detection功能

Theyolov2_detection附加的功能随图占用图像输入并使用保存的网络运行图像上的检测器YOLOV2RESNET50VEHICEEXAMPLEMAT.。该函数加载网络对象YOLOV2RESNET50VEHICEEXAMPLEMAT.into a persistent variableyolov2obj.。后续调用函数重用持久对象进行检测。

type('yolov2_detection.m')
function outImg = yolov2_detection(in) % Copyright 2018-2019 The MathWorks, Inc. % A persistent object yolov2Obj is used to load the YOLOv2ObjectDetector object. % At the first call to this function, the persistent object is constructed and % set up. Subsequent calls to the function reuse the same object to call detection % on inputs, thus avoiding having to reconstruct and reload the % network object. persistent yolov2Obj; if isempty(yolov2Obj) yolov2Obj = coder.loadDeepLearningNetwork('yolov2ResNet50VehicleExample.mat'); end % pass in input [bboxes,~,labels] = yolov2Obj.detect(in,'Threshold',0.5); outImg = in; % convert categorical labels to cell array of character vectors labels = cellstr(labels); if ~(isempty(bboxes) && isempty(labels)) % Annotate detections in the image. outImg = insertObjectAnnotation(in,'rectangle',bboxes,labels); end

To generate code, create a code configuration object for a MEX target and set the target language to C++. Use thecoder.deeplearningconfigfunction to create a MKL-DNN deep learning configuration object. Assign this object to theDeeplearningConfigproperty of the code configuration object. Specify the input size as an argument to thecodegencommand. In this example, the input layer size of the YOLO v2 network is[224,224,3]

cfg = coder.config('mex'); cfg.TargetLang ='C++';cfg.DeepLearningConfig = coder.DeepLearningConfig('mkldnn'); codegen-configcfgyolov2_detection-args{ones(224,224,3,'uint8')}-报告
Code generation successful: To view the report, open('codegen/mex/yolov2_detection/html/report.mldatx').

在示例输入上运行生成的mex函数

Set up a video file reader and read the example input videohighway_lanechange.mp4。创建视频播放器以显示视频和输出检测。

VideoFile =.'highway_lanechange.mp4';Videofreader = Vision.videofilereader(VideoFile,'VideoOutputDataType',“uint8”); depVideoPlayer = vision.DeployableVideoPlayer('Size','风俗','CustomSize',[640 480]);

通过帧读取视频输入框架并通过使用探测器检测视频中的车辆。

cont =〜ISDONE(Videofreader);尽管cont i = step(videofreader);In = Imresize(I,[224,224]);out = yolov2_detection_mex(in);DEPVIDEOPLAYER(OUT);cont =〜ISDONE(VideoFreader)&& Inopen(depvideoplayer);% Exit the loop if the video player figure window is closedend

参考

[1] Redmon,Joseph和Ali Farhadi。“YOLO9000:更好,更快,更强。”在2017 IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 6517–25. Honolulu, HI: IEEE, 2017.

See Also

|

相关话题