MATLAB: Does the function ‘exportONNXNetwork’ reports warnings about incompatibility while trying to convert the pre-trained yolov3 to onnx

featureresize2MATLABonnxyolo

I would like to deploy the pre-trained yolov3 detector to an ONNX model for inference. The function 'exportONNXNetwork' reports warnings that some layers may change the behavior. I tried the following example to download the yolov3 pretrained network:
After downloading the mat file in the yolov3 example, below is the step to reproduce the warning:
>> load('yolov3SqueezeNetVehicleExample_21aSPKG.mat')
>> exportONNXNetwork(detector.Network, 'test.onnx')
Warning: Cannot exactly export Layer 'featureResize2' with 'GeometricTransformMode' set to
'half-pixel' for 'OpsetVersion' '8'. Exported network may produce different results.
> In nnet.internal.cnn.onnx/ConverterForResizeLayers/toOnnx (line 57)
In nnet.internal.cnn.onnx/ConverterForNetwork/networkToGraphProto (line 114)
In nnet.internal.cnn.onnx/ConverterForNetwork/toOnnx (line 44)
In nnet.internal.cnn.onnx.exportONNXNetwork (line 36)
In exportONNXNetwork (line 38)
Warning: Cannot exactly export Layer 'featureResize2' with 'NearestRoundingMode' set to
'round' for 'OpsetVersion' '8'. Exported network may produce different results.
> In nnet.internal.cnn.onnx/ConverterForResizeLayers/toOnnx (line 60)
In nnet.internal.cnn.onnx/ConverterForNetwork/networkToGraphProto (line 114)
In nnet.internal.cnn.onnx/ConverterForNetwork/toOnnx (line 44)
In nnet.internal.cnn.onnx.exportONNXNetwork (line 36)
Is there a workaround for this problem? It seems like 'featureResize2' is not compatible with onnx format.

Best Answer

Currently, MATLAB export support is limited to opset version 9, but the parameters used in the 'resize2Dlayer '(featureResize2) of 'yolov3ObjectDetector' requires export support of opset >=10. As a workaround, you can replace the 'featureResize2' with a new 'resize2Dlayer' that can be exported even with opset version 9. Here is an example:
lgraph= layerGraph(detector.Network);
layer = resize2dLayer('OutputSize',[28,28],'Name','resize2DLayer','GeometricTransformMode','asymmetric','NearestRoundingMode','onnx-10');
lgraph = replaceLayer(lgraph,'featureResize2',layer);
net = dlnetwork(lgraph);
The above net is a yolov3 network that can be exported without warnings.
To verify the mAP, you can create a detector as below and follow the steps mentioned in Evaluate Model section of the "Object Detection Using YOLO v3 Deep Learning" example.
yolov3Detector = yolov3ObjectDetector(net, detector.ClassNames, detector.AnchorBoxes);