Main Content

使用Yolo V2代码生成对象检测

此示例显示了如何仅查看一次(YOLO)V2对象检测器来生成CUDA®MEX。Yolo V2对象检测网络由两个子网组成。功能提取网络,然后是检测网络。此示例为在该网络中生成代码使用Yolo V2深学习的对象检测example from Computer Vision Toolbox™. For more information, see使用Yolo V2深学习的对象检测(计算机视觉工具箱)。您可以修改此示例,以生成CUDA®MEX的导入网络Import Pretrained ONNX YOLO v2 Object Detectorexample from Computer Vision Toolbox™. For more information, seeImport Pretrained ONNX YOLO v2 Object Detector(计算机视觉工具箱)

Third-Party Prerequisites

Required

此示例生成CUDA MEX,并具有以下第三方要求。

  • CUDA® enabled NVIDIA® GPU and compatible driver.

选修的

For non-MEX builds such as static, dynamic libraries or executables, this example has the following additional requirements.

验证GPU环境

使用coder.checkGpuInstallfunction to verify that the compilers and libraries necessary for running this example are set up correctly.

envcfg = coder.gpuenvconfig('host');envcfg.deeplibtarget ='cudnn';envCfg.DeepCodegen = 1; envCfg.Quiet = 1; coder.checkGpuInstall(envCfg);

进行验证的dagnetwork

此示例使用yolov2ResNet50VehicleExampleMAT-file containing the pretrained network. The file is approximately 98MB in size. Download the file from the MathWorks website.

matfile = matlab.internal.examples.download金宝appsupportfile(“视觉/数据”,,,,'yolov2ResNet50VehicleExample.mat');VERICLEDETECTOR =负载(矩阵);net = vehicledetector.detector.network
net = DAGNetwork with properties: Layers: [150×1 nnet.cnn.layer.Layer] Connections: [162×2 table] InputNames: {'input_1'} OutputNames: {'yolov2OutputLayer'}

DAG网络包含150层,包括卷积,relu和批处理标准化层以及Yolo V2变换和Yolo V2输出层。要显示深度学习网络体系结构的互动可视化,请使用分析(深度学习工具箱)功能。

分析(NET);

yolov2_detect入口点功能

yolov2_detect.mentry-point function takes an image input and runs the detector on the image using the deep learning network saved in theyolov2ResNet50VehicleExample.mat文件。该功能从yolov2ResNet50VehicleExample.mat归档到持久变量yolov2Obj并在后续检测调用中重复持久对象。

类型('yolov2_detect.m'
matFile函数outImg = yolov2_detect () %right 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);

运行MEX代码生成

要为入口点函数生成CUDA代码,请为MEX目标创建GPU代码配置对象,并将目标语言设置为C ++。使用coder.DeepLearningConfig创建一个功能库丁深度学习配置对象并将其分配给DeepLearningConfigGPU代码配置对象的属性。跑过代码根命令指定输入大小为224 by-224-by-3。该值对应于Yolov2的输入层大小。

cfg = coder.gpuconfig('Mex');cfg.targetlang ='C ++';cfg.deeplearningconfig = coder.deeplearningconfig('cudnn');cfg.generatereport = true;inputargs = {一个(224,224,3,'uint8'),coder.constant(matfile)};代码根-configCFGyolov2_detect-args输入
代码生成成功:查看报告

Run Generated MEX

Set up the video file reader and read the input video. Create a video player to display the video and the output detections.

videoFile ='Highway_lanechange.mp4';videoFreader = vision.VideoFileReader(videoFile,“ videooututputdatatype”,,,,'uint8');DepVideOplayer = Vision.DeployableVideOplayer('尺寸',,,,'Custom',,,,'自定义大小',[640 480]);

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

cont = ~isDone(videoFreader);whilecont i = step(videofreader);in = imresize(i,[224,224]);out = yolov2_detect_mex(in,matfile);step(DepVideOplayer,out);如果关闭视频播放器窗口,则会退出循环cont = 〜ISDONE(videOfReader)&& isopen(depVideOplayer);结尾

References

[1] Redmon, Joseph, and Ali Farhadi. "YOLO9000: Better, Faster, Stronger." 2017 IEEE Conference on Computer Vision and Pattern Recognition (CVPR). IEEE, 2017.

版权2017-2021 The Mathworks,Inc。

也可以看看

职能

对象

相关示例

更多关于