主要内容

Code Generation for Object Detection by Using YOLO v2

This example shows how to generate CUDA® MEX for a you only look once (YOLO) v2 object detector. A YOLO v2 object detection network is composed of two subnetworks. A feature extraction network followed by a detection network. This example generates code for the network trained in theObject Detection Using YOLO v2 Deep Learning计算机视觉工具箱™的示例。有关更多信息,请参阅Object Detection Using YOLO v2 Deep Learning(Computer Vision Toolbox). You can modify this example to generate CUDA® MEX for the network imported in the进口预估计的ONNX YOLO V2对象检测器计算机视觉工具箱™的示例。有关更多信息,请参阅进口预估计的ONNX YOLO V2对象检测器(Computer Vision Toolbox).

第三方先决条件

必需的

This example generates CUDA MEX and has the following third-party requirements.

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

Optional

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

Verify GPU Environment

Use theCoder.CheckgPuinstall函数以验证运行此示例所需的编译器和库是否正确设置。

envCfg = coder.gpuEnvConfig('主持人'); envCfg.DeepLibTarget ='cudnn';envcfg.deepcodegen = 1;envcfg.quiet = 1;coder.checkgpuinstall(envcfg);

Get Pretrained DAGNetwork

This example uses theyolov2Resnet50 vehiclesampame包含验证网络的垫子文件。该文件的大小约为98MB。从Mathworks网站下载文件。

matFile = matlab.internal.examples.downloadSupportFile('vision/data','yolov2Resnet50 vehicleexample.mat'); vehicleDetector = load(matFile); net = vehicleDetector.detector.Network
net =带有属性的dagnetwork:层:[150×1 nnet.cnn.layer.layer]连接:[162×2 table] inputNames:{'input_1'} outputnames:{'yolov2outputputlayer'}

The DAG network contains 150 layers including convolution, ReLU, and batch normalization layers and the YOLO v2 transform and YOLO v2 output layers. To display an interactive visualization of the deep learning network architecture, use theanalyzeNetwork(Deep Learning Toolbox)function.

analyzeNetwork(net);

Theyolov2_detectEntry-Point Function

Theyolov2_detect.m入口点功能采用图像输入,并使用保存在图像上的检测器在图像上运行检测器yolov2Resnet50 vehicleexample.matfile. The function loads the network object from theyolov2Resnet50 vehicleexample.matfile into a persistent variableyolov2obj和reuses the persistent object on subsequent detection calls.

type('yolov2_detect.m')
function outImg = yolov2_detect(in,matFile) % Copyright 2018-2021 The MathWorks, Inc. persistent yolov2Obj; if isempty(yolov2Obj) yolov2Obj = coder.loadDeepLearningNetwork(matFile); end % Call to detect method [bboxes,~,labels] = yolov2Obj.detect(in,'Threshold',0.5); % Convert categorical labels to cell array of charactor vectors labels = cellstr(labels); % Annotate detections in the image. outImg = insertObjectAnnotation(in,'rectangle',bboxes,labels);

Run MEX Code Generation

To generate CUDA code for the entry-point function, create a GPU code configuration object for a MEX target and set the target language to C++. Use thecoder.deeplearningconfigfunction to create aCuDNNdeep learning configuration object and assign it to the深度学习property of the GPU code configuration object. Run thecodegencommand specifying an input size of 224-by-224-by-3. This value corresponds to the input layer size of YOLOv2.

cfg = coder.gpuConfig('mex'); cfg.TargetLang ='C++';cfg.DeepLearningConfig = coder.DeepLearningConfig('cudnn'); cfg.GenerateReport = true; inputArgs = {ones(224,224,3,'uint8'),coder.Constant(matFile)}; codegen-configcfgyolov2_detect-argsinputArgs
Code generation successful: View report

运行产生的mex

设置视频文件读取器并读取输入视频。创建视频播放器以显示视频和输出检测。

视频='highway_lanechange.mp4';videofreader = vision.videofilereader(videofile,'VideoOutputDataType','uint8'); depVideoPlayer = vision.DeployableVideoPlayer('Size','风俗','CustomSize',[640 480]);

阅读视频限制框架,并使用检测器检测视频中的车辆。

续= 〜ISDONE(videofreader);尽管cont I = step(videoFreader); in = imresize(I,[224,224]); out = yolov2_detect_mex(in,matFile); step(depVideoPlayer, out);% Exit the loop if the video player figure window is closedcont = ~isDone(videoFreader) && isOpen(depVideoPlayer);end

参考

[1] Redmon,Joseph和Ali Farhadi。“ Yolo9000:更好,更快,更强壮。”2017 IEEE计算机视觉和模式识别会议(CVPR)。IEEE,2017年。

Copyright 2017-2021The MathWorks, Inc.

See Also

Functions

Objects

Related Examples

More About