mirror of
https://github.com/opencv/opencv.git
synced 2025-06-17 23:51:16 +08:00
dnn(onnx): fix Resize inputs handling
This commit is contained in:
parent
cc02fcd889
commit
51e65db715
@ -2348,6 +2348,7 @@ void ONNXImporter::parseConcat(LayerParams& layerParams, const opencv_onnx::Node
|
|||||||
addLayer(layerParams, node_proto);
|
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)
|
void ONNXImporter::parseResize(LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto)
|
||||||
{
|
{
|
||||||
for (int i = 1; i < node_proto.input_size(); i++)
|
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<String>("mode") == "linear" && framework_name == "pytorch")
|
if (layerParams.get<String>("mode") == "linear" && framework_name == "pytorch")
|
||||||
layerParams.set("mode", "opencv_linear");
|
layerParams.set("mode", "opencv_linear");
|
||||||
|
|
||||||
// input = [X, scales], [X, roi, scales] or [x, roi, scales, sizes]
|
// opset-10: input = [X, scales]
|
||||||
int foundScaleId = hasDynamicShapes ? node_proto.input_size() - 1
|
// opset-11: input = [X, roi, scales] or [x, roi, scales, sizes]
|
||||||
: node_proto.input_size() > 2 ? 2 : 1;
|
int scalesInputId = node_proto.input_size() == 2 ? 1 : 2;
|
||||||
|
|
||||||
Mat scales = getBlob(node_proto, foundScaleId);
|
Mat scales = getBlob(node_proto, scalesInputId);
|
||||||
if (scales.total() == 4)
|
if (!scales.empty())
|
||||||
{
|
{
|
||||||
|
CV_CheckEQ(scales.total(), (size_t)4, "HCHW layout is expected");
|
||||||
layerParams.set("zoom_factor_y", scales.at<float>(2));
|
layerParams.set("zoom_factor_y", scales.at<float>(2));
|
||||||
layerParams.set("zoom_factor_x", scales.at<float>(3));
|
layerParams.set("zoom_factor_x", scales.at<float>(3));
|
||||||
}
|
}
|
||||||
else
|
else if (node_proto.input_size() >= 4) // opset-11
|
||||||
{
|
{
|
||||||
const std::string& inputLast = node_proto.input(node_proto.input_size() - 1);
|
const std::string& inputSizes = node_proto.input(3);
|
||||||
if (constBlobs.find(inputLast) != constBlobs.end())
|
if (constBlobs.find(inputSizes) != constBlobs.end())
|
||||||
{
|
{
|
||||||
Mat shapes = getBlob(inputLast);
|
Mat shapes = getBlob(inputSizes);
|
||||||
CV_CheckEQ(shapes.size[0], 4, "");
|
CV_CheckEQ(shapes.total(), (size_t)4, "HCHW layout is expected");
|
||||||
CV_CheckEQ(shapes.size[1], 1, "");
|
|
||||||
CV_CheckDepth(shapes.depth(), shapes.depth() == CV_32S || shapes.depth() == CV_32F, "");
|
CV_CheckDepth(shapes.depth(), shapes.depth() == CV_32S || shapes.depth() == CV_32F, "");
|
||||||
if (shapes.depth() == CV_32F)
|
if (shapes.depth() == CV_32F)
|
||||||
shapes.convertTo(shapes, CV_32S);
|
shapes.convertTo(shapes, CV_32S);
|
||||||
layerParams.set("width", shapes.at<int>(3));
|
layerParams.set("width", shapes.at<int>(3));
|
||||||
layerParams.set("height", shapes.at<int>(2));
|
layerParams.set("height", shapes.at<int>(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");
|
replaceLayerParam(layerParams, "mode", "interpolation");
|
||||||
addLayer(layerParams, node_proto);
|
addLayer(layerParams, node_proto);
|
||||||
|
Loading…
Reference in New Issue
Block a user