主要内容

导出Yolo V2对象检测器到ONNX

此示例显示了如何将Yolo V2对象检测网络导出到ONNX™(开放神经网络交换)模型格式。导出Yolo V2网络后,您可以将网络导入到其他深度学习框架中进行推理。此示例还提供了您可以遵循的工作流程,以使用导入的ONNX模型执行推理。

导出Yolo V2网络

将检测网络导出到ONNX并收集生成对象检测结果所需的元数据。

First, load a pretrained YOLO v2 object detector into the workspace.

input = load('yolov2vehicledetector.mat');net = input.detector.network;

接下来,获取Yolo V2检测器元数据以用于推理。检测器元数据包括网络输入图像大小,锚固框和最后卷积层的激活大小。

从输入Yolo V2网络中读取网络输入图像大小。

inputimagesize = net.layers(1,1).inputsize;

阅读用于训练输入检测器的锚箱。

AnchorBoxes = input.detector.andorboxes;

Get the activation size of the last convolution layer in the input network by using the分析功能。

分析(net);

最终激活= [16 16 24];

导出到ONNX模型格式

Export the YOLO v2 object detection network as an ONNX format file by using theexportONNXNetwork(Deep Learning Toolbox)功能。将文件名指定为yolov2.onnx. The function saves the exported ONNX file to the current working folder.

filename ='yolov2.onnx';ExportOnnXnetwork(net,f​​ilename);

TheexportONNXNetwork功能maps theyolov2TransformLayeryolov2OutputLayer在输入YOLO V2网络中,分别是基本的ONX运算符和身份操作员。导出网络后,您可以导入yolov2.onnxfile into any deep learning framework that supports ONNX import.

Using theexportONNXNetwork, requires Deep Learning Toolbox™ and the Deep Learning Toolbox Converter for ONNX Model Format support package. If this support package is not installed, then the function provides a download link.

使用导出的Yolo V2网络检测对象检测

When exporting is complete, you can import the ONNX model into any deep learning framework and use the following workflow to perform object detection. Along with the ONNX network, this workflow also requires the YOLO v2 detector metadatainputimagesize,anchorBoxes, 和最终激活从MATLAB工作区获得。以下代码是工作流的MATLAB实现,您必须将其转换为选择框架的等效代码。

预处理输入图像

Preprocess the image to use for inference. The image must be an RGB image and must be resized to the network input image size, and its pixel values must lie in the interval [0 1].

i = imread('Highway.png');resizedi = iMresize(i,inputimagesize(1:2));Rescaledi = recrepale(resizeiziei);

Pass Input and Run ONNX Model

在您选择的深度学习框架中运行ONNX模型,并使用预处理图像作为导入ONNX模型的输入。

从ONNX模型的输出中提取预测

该模型预测以下内容:

  • Intersection over union (IoU) with ground truth boxes

  • x,y,w, 和h每个锚点的边界框参数

  • 每个锚点的课程概率

ONNX模型的输出是包含预测且大小的特征映射Predivionsperanchor-经过-numAnchors-经过-numGrids.

  • numAnchors是锚盒的数量。

  • numGrids是计算为最后卷积层高度和宽度的乘积的网格数。

  • Predivionsperanchor是形式的输出预测IoU;x;y;w;h;class probabilities]。

  • The first row in the feature map contains IoU predictions for each anchor box.

  • 特征图中的第二行和第三行包含每个锚盒的质心坐标(x,y)的预测。

  • The fourth and fifth rows in the feature map contain the predictions for the width and height of each anchor box.

  • 功能映射中的第六行包含每个锚框的类概率的预测。

Compute Final Detections

要计算预处理测试图像的最终检测,您必须:

  • 与网络输入层的大小相对于边界框参数。

  • 从预测中计算对象置信度得分。

  • Obtain predictions with high object confidence scores.

  • Perform nonmaximum suppression.

作为实施指南,使用代码Yolov2PostProcess功能在后处理功能.

[bboxes,scores,labels] = yolov2PostProcess(featureMap,inputImageSize,finalActivationsSize,anchorBoxes);

Display Detection Results

Idisp = insertObjectAnnotation(resizedI,'长方形',bboxes,scores); figure imshow(Idisp)

参考

[1] Redmon, Joseph, and Ali Farhadi. “YOLO9000: Better, Faster, Stronger.” In 2017IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 6517–25. Honolulu, HI: IEEE, 2017. https://doi.org/10.1109/CVPR.2017.690.

后处理功能

功能[bboxes,scores,labels] = yolov2PostProcess(featureMap,inputImageSize,finalActivationsSize,anchorBoxes)%从特征图提取预测值。iouPred = featureMap(1,:,:); xyPred = featureMap(2:3,:,:); whPred = featureMap(4:5,:,:); probPred = featureMap(6,:,:);%缩放边界框参数。bBoxes = rescaleBbox(xyPred,whPred,anchorBoxes,finalActivationsSize,inputImageSize);%重新排列特征图作为二维矩阵,用于有效处理。宠物= [bboxes; ioupred; probpred];宠物= reshape(宠物,大小(宠物,1),[]);%计算重新排列的预测值的对象置信度得分。[confScore,idx] = computeObjectScore(predval);% Obtain predictions with high object confidence scores.[bboxpred,Scorepred,classPred] = selectmaximumpredictions(confScore,idx,宠儿);%要获得最终检测,请执行非临时抑制,重叠阈值为0.5。[bboxes,分数,标签] = selectstrongestbboxmulticlass(bboxpred',scorpred',classpred','ratiotype','Union','OverlapThreshold',0.5);end
功能bBoxes = rescaleBbox(xyPred,whPred,anchorBoxes,finalActivationsSize,inputImageSize)%要重新缩放边界框参数,请使用网络参数InputImagesize和最终激活来计算缩放因子。scaleY = inputImageSize(1)/finalActivationsSize(1); scaleX = inputImageSize(2)/finalActivationsSize(2); scaleFactor = [scaleY scaleX]; bBoxes = zeros(size(xyPred,1)+size(whPred,1),size(anchors,1),size(xyPred,3),'像',xyPred);forrowIdx=0:finalActivationsSize(1,1)-1forCOLIDX = 0:最终激活size(1,2)-1 ind = rowidx*finalActivationsSize(1,2)+colidx+1;forAnchorIdx = 1:size(Anchorboxes,1)% Compute the center with respect to image.cx = (xyPred(1,anchorIdx,ind)+colIdx)* scaleFactor(1,2); cy = (xyPred(2,anchorIdx,ind)+rowIdx)* scaleFactor(1,1);%计算宽度和高度的image.bw = whPred(1,anchorIdx,ind)* anchorBoxes(anchorIdx,1); bh = whPred(2,anchorIdx,ind)* anchorBoxes(anchorIdx,2); bBoxes(1,anchorIdx,ind) = (cx-bw/2); bBoxes(2,anchorIdx,ind) = (cy-bh/2); bBoxes(3,anchorIdx,ind) = bw; bBoxes(4,anchorIdx,ind) = bh;endendendend
功能[confScore,idx] = computeObjectScore(predval)ioupred = pestval(5,:);probpred =宠物(6:end,:);[imax,idx] = max(probpred,[],1);confScore = ioupred。*imax;end
功能[bboxpred,Scorepred,classPred] = selectmaximumpredictions(confScore,idx,宠儿)%指定置信分数的阈值。confScoreId = confScore> = 0.5;%获得大于或等于0.5的置信分数。ScorePred = confScore(:,confScoreId);% Obtain the class IDs for predictions with confidence scores greater than%或等于0.5。classPred = idx(:,confScoreId);% Obtain the bounding box parameters for predictions with confidence scores% greater than or equal to 0.5.bboxesXYWH = predVal(1:4,:); bboxPred = bboxesXYWH(:,confScoreId);end