From 51e65db715826931b67d3128cf5aaab5b3d655f8 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 23 Dec 2021 01:59:34 +0000 Subject: [PATCH] dnn(onnx): fix Resize inputs handling --- modules/dnn/src/onnx/onnx_importer.cpp | 31 +++++++++++++++++--------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/modules/dnn/src/onnx/onnx_importer.cpp b/modules/dnn/src/onnx/onnx_importer.cpp index 0d3313c039..defec7978b 100644 --- a/modules/dnn/src/onnx/onnx_importer.cpp +++ b/modules/dnn/src/onnx/onnx_importer.cpp @@ -2348,6 +2348,7 @@ void ONNXImporter::parseConcat(LayerParams& layerParams, const opencv_onnx::Node addLayer(layerParams, node_proto); } +// https://github.com/onnx/onnx/blob/master/docs/Operators.md#Resize void ONNXImporter::parseResize(LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto) { for (int i = 1; i < node_proto.input_size(); i++) @@ -2368,30 +2369,38 @@ void ONNXImporter::parseResize(LayerParams& layerParams, const opencv_onnx::Node if (layerParams.get("mode") == "linear" && framework_name == "pytorch") layerParams.set("mode", "opencv_linear"); - // input = [X, scales], [X, roi, scales] or [x, roi, scales, sizes] - int foundScaleId = hasDynamicShapes ? node_proto.input_size() - 1 - : node_proto.input_size() > 2 ? 2 : 1; + // opset-10: input = [X, scales] + // opset-11: input = [X, roi, scales] or [x, roi, scales, sizes] + int scalesInputId = node_proto.input_size() == 2 ? 1 : 2; - Mat scales = getBlob(node_proto, foundScaleId); - if (scales.total() == 4) + Mat scales = getBlob(node_proto, scalesInputId); + if (!scales.empty()) { + CV_CheckEQ(scales.total(), (size_t)4, "HCHW layout is expected"); layerParams.set("zoom_factor_y", scales.at(2)); layerParams.set("zoom_factor_x", scales.at(3)); } - else + else if (node_proto.input_size() >= 4) // opset-11 { - const std::string& inputLast = node_proto.input(node_proto.input_size() - 1); - if (constBlobs.find(inputLast) != constBlobs.end()) + const std::string& inputSizes = node_proto.input(3); + if (constBlobs.find(inputSizes) != constBlobs.end()) { - Mat shapes = getBlob(inputLast); - CV_CheckEQ(shapes.size[0], 4, ""); - CV_CheckEQ(shapes.size[1], 1, ""); + Mat shapes = getBlob(inputSizes); + CV_CheckEQ(shapes.total(), (size_t)4, "HCHW layout is expected"); CV_CheckDepth(shapes.depth(), shapes.depth() == CV_32S || shapes.depth() == CV_32F, ""); if (shapes.depth() == CV_32F) shapes.convertTo(shapes, CV_32S); layerParams.set("width", shapes.at(3)); layerParams.set("height", shapes.at(2)); } + else + { + CV_Error(Error::StsNotImplemented, cv::format("ONNX/Resize: doesn't support dynamic non-constant 'sizes' input: %s", inputSizes.c_str())); + } + } + else + { + CV_Error(Error::StsNotImplemented, "ONNX/Resize: can't find neither 'scale' nor destination sizes parameters"); } replaceLayerParam(layerParams, "mode", "interpolation"); addLayer(layerParams, node_proto);