Package: deform-conv2d-onnx-exporter
deform_conv2d_onnx_exporterOverviewThis module enables you to exportdeform_conv2dto ONNX in PyTorch.At this moment, in August 2021, PyTorch 1.9.0 and torchvision 0.10.0 does not support exportingdeform_conv2dinto ONNX, so I implemented this module.This module implements Deformable Convolution v2, described in a paper,Deformable ConvNets v2: More Deformable, Better Results <https://arxiv.org/abs/1811.11168>, using ONNX operators.The implementation is straightforward, but may not be efficient.Installation$pipinstalldeform_conv2d_onnx_exporterUsageimporttorch.onnxfromtorchvision.ops.deform_convimportDeformConv2dimportdeform_conv2d_onnx_exporterdeform_conv2d_onnx_exporter.register_deform_conv2d_onnx_op()model=DeformConv2d(...)input_names=["input","offset"]output_names=["output"]input_params=(torch.rand([1,x,x,x]),# inputtorch.randn([1,x,x,x]),# offset)torch.onnx.export(model,input_params,"output.onnx",input_names=input_names,output_names=output_names,opset_version=12)Note that you have to setopset_versionto12or later.TestsInstall dependent libraries.$pipinstall-rrequirements.txtRununittest.$python-munittestdiscover-stestsDevelopment notesOptions fordeform_conv2d_onnx_exporter.register_deform_conv2d_onnx_op()You can specify 2 options for this function.use_gathernd:IfTrue, useGatherNDoperator. Otherwise, useGatherElementsoperator.enable_openvino_patch:IfTrue, enable patch for OpenVINO.Referenced paperThis module implements Deformable Convolution v2, described in a paper, "Deformable ConvNets v2: More Deformable, Better Results", using ONNX operators.Some of the variable names in the module, such aspandp_0, are based on the paper.Memory layout ofoffsetThe detail ofdeform_conv2dimplementation in PyTorch is not fully documented.Therefore, I investigated theimplementationto understand memory layout of some variables, such asoffset.offsetThe shape is(batch, 2 * group * kernel_h * kernel_w, out_h, out_w)according to thereference.The internal memory layout of2 * group * kernel_h * kernel_wis not clear.According to the source code, it seems to be(batch, group, kernel_h, kernel_w, 2, out_h, out_w).The size2means "y-coords and x-coords".Padding ofinputEven ifpaddingis set to0, this module adds at least 1 padding internally.This is necessary to handle out-of-boundsoffsetappropriately.PerformanceTo be honest, the performance is not so good because the current version of ONNX, version 15, does not supportdeform_conv2dnatively.Therefore, this module implements it usingGatherNDand other operators.As a result, the performance is not so good, but good enough for me.Of course, I implemented this module carefully to reduce unnecessary or duplicated calculations.Opset versionVersion12or later is required because of the following reasons:Clip:Version 12 and later supportsClipoperaetor fortensor(int64). This module uses it.GatherND:Versoin 12 and later supportsGatherNDoperator withbatch_dimsattribute. This module also uses it.LicenseYou can use this module under the MIT License.Copyright 2021 Masamitsu MURASEPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.