Skip to content

Commit e7c5120

Browse files
committed
Merge pull request opencv#21259 from rogday:random_fixes
2 parents 19cfaa2 + e97c7e0 commit e7c5120

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

modules/dnn/src/layers/padding_layer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,13 @@ class PaddingLayerImpl CV_FINAL : public PaddingLayer
130130
outputs[0].setTo(paddingValue);
131131
inputs[0].copyTo(outputs[0](dstRanges));
132132
}
133-
else if (paddingType == "reflect")
133+
else if (paddingType == "reflect" || paddingType == "edge")
134134
{
135135
CV_Assert(inputs.size() == 1);
136136
CV_Assert(outputs.size() == 1);
137137
CV_Assert(inputs[0].dims == 4);
138138
CV_Assert(outputs[0].dims == 4);
139+
int borderType = paddingType == "reflect" ? BORDER_REFLECT_101 : BORDER_REPLICATE;
139140

140141
if (inputs[0].size[0] != outputs[0].size[0] || inputs[0].size[1] != outputs[0].size[1])
141142
CV_Error(Error::StsNotImplemented, "Only spatial reflection padding is supported.");
@@ -148,8 +149,8 @@ class PaddingLayerImpl CV_FINAL : public PaddingLayer
148149
const int padBottom = outHeight - dstRanges[2].end;
149150
const int padLeft = dstRanges[3].start;
150151
const int padRight = outWidth - dstRanges[3].end;
151-
CV_CheckLT(padTop, inpHeight, ""); CV_CheckLT(padBottom, inpHeight, "");
152-
CV_CheckLT(padLeft, inpWidth, ""); CV_CheckLT(padRight, inpWidth, "");
152+
CV_CheckLE(padTop, inpHeight, ""); CV_CheckLE(padBottom, inpHeight, "");
153+
CV_CheckLE(padLeft, inpWidth, ""); CV_CheckLE(padRight, inpWidth, "");
153154

154155
for (size_t n = 0; n < inputs[0].size[0]; ++n)
155156
{
@@ -158,7 +159,7 @@ class PaddingLayerImpl CV_FINAL : public PaddingLayer
158159
copyMakeBorder(getPlane(inputs[0], n, ch),
159160
getPlane(outputs[0], n, ch),
160161
padTop, padBottom, padLeft, padRight,
161-
BORDER_REFLECT_101);
162+
borderType);
162163
}
163164
}
164165
}

modules/dnn/src/onnx/onnx_importer.cpp

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class ONNXImporter
131131
typedef void (ONNXImporter::*ONNXImporterNodeParser)(LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto);
132132
typedef std::map<std::string, ONNXImporterNodeParser> DispatchMap;
133133

134+
void parseMaxUnpool (LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto);
134135
void parseMaxPool (LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto);
135136
void parseAveragePool (LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto);
136137
void parseReduce (LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto);
@@ -625,6 +626,41 @@ void setCeilMode(LayerParams& layerParams)
625626
}
626627
}
627628

629+
void ONNXImporter::parseMaxUnpool(LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto)
630+
{
631+
layerParams.type = "MaxUnpool";
632+
633+
DictValue kernel_shape = layerParams.get("kernel_size");
634+
CV_Assert(kernel_shape.size() == 2);
635+
layerParams.set("pool_k_w", kernel_shape.get<int>(0));
636+
layerParams.set("pool_k_h", kernel_shape.get<int>(1));
637+
638+
int pool_pad_w = 0, pool_pad_h = 0;
639+
if (layerParams.has("pad"))
640+
{
641+
DictValue pads = layerParams.get("pad");
642+
CV_CheckEQ(pads.size(), 2, "");
643+
pool_pad_w = pads.get<int>(0);
644+
pool_pad_h = pads.get<int>(1);
645+
}
646+
layerParams.set("pool_pad_w", pool_pad_w);
647+
layerParams.set("pool_pad_h", pool_pad_h);
648+
649+
650+
int pool_stride_w = 1, pool_stride_h = 1;
651+
if (layerParams.has("stride"))
652+
{
653+
DictValue strides = layerParams.get("stride");
654+
CV_CheckEQ(strides.size(), 2, "");
655+
pool_stride_w = strides.get<int>(0);
656+
pool_stride_h = strides.get<int>(1);
657+
}
658+
layerParams.set("pool_stride_w", pool_stride_w);
659+
layerParams.set("pool_stride_h", pool_stride_h);
660+
661+
addLayer(layerParams, node_proto);
662+
}
663+
628664
void ONNXImporter::parseMaxPool(LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto)
629665
{
630666
layerParams.type = "Pooling";
@@ -659,11 +695,11 @@ void ONNXImporter::parseReduce(LayerParams& layerParams, const opencv_onnx::Node
659695
pool = "AVE";
660696
layerParams.set("pool", pool);
661697
layerParams.set("global_pooling", !layerParams.has("axes"));
698+
bool keepdims = layerParams.get<int>("keepdims", 1) == 1;
662699
if (layerParams.has("axes") && (layer_type == "ReduceMean" || layer_type == "ReduceSum" || layer_type == "ReduceMax"))
663700
{
664701
MatShape inpShape = outShapes[node_proto.input(0)];
665702
DictValue axes = layerParams.get("axes");
666-
bool keepdims = layerParams.get<int>("keepdims");
667703
MatShape targetShape;
668704
std::vector<bool> shouldDelete(inpShape.size(), false);
669705
for (int i = 0; i < axes.size(); i++) {
@@ -771,7 +807,10 @@ void ONNXImporter::parseReduce(LayerParams& layerParams, const opencv_onnx::Node
771807
}
772808
else if (!layerParams.has("axes") && (layer_type == "ReduceMean" || layer_type == "ReduceSum" || layer_type == "ReduceMax"))
773809
{
774-
CV_CheckEQ(layerParams.get<int>("keepdims"), 0, "layer only supports keepdims = false");
810+
IterShape_t shapeIt = outShapes.find(node_proto.input(0));
811+
CV_Assert(shapeIt != outShapes.end());
812+
const size_t dims = keepdims ? shapeIt->second.size() : 1;
813+
775814
LayerParams reshapeLp;
776815
reshapeLp.name = layerParams.name + "/reshape";
777816
reshapeLp.type = "Reshape";
@@ -793,8 +832,8 @@ void ONNXImporter::parseReduce(LayerParams& layerParams, const opencv_onnx::Node
793832
addLayer(poolLp, node_proto);
794833

795834
layerParams.type = "Reshape";
796-
int targetShape[] = {1};
797-
layerParams.set("dim", DictValue::arrayInt(&targetShape[0], 1));
835+
std::vector<int> targetShape(dims, 1);
836+
layerParams.set("dim", DictValue::arrayInt(targetShape.data(), targetShape.size()));
798837

799838
node_proto.set_input(0, node_proto.output(0));
800839
node_proto.set_output(0, layerParams.name);
@@ -2341,6 +2380,7 @@ const ONNXImporter::DispatchMap ONNXImporter::buildDispatchMap()
23412380
{
23422381
DispatchMap dispatch;
23432382

2383+
dispatch["MaxUnpool"] = &ONNXImporter::parseMaxUnpool;
23442384
dispatch["MaxPool"] = &ONNXImporter::parseMaxPool;
23452385
dispatch["AveragePool"] = &ONNXImporter::parseAveragePool;
23462386
dispatch["GlobalAveragePool"] = dispatch["GlobalMaxPool"] = dispatch["ReduceMean"] = dispatch["ReduceSum"] =

modules/dnn/test/test_onnx_conformance_layer_parser_denylist.inl.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@
277277
"test_max_uint8",
278278
"test_maxpool_2d_uint8",
279279
"test_maxunpool_export_with_output_shape",
280-
"test_maxunpool_export_without_output_shape",
281280
"test_mean_example",
282281
"test_mean_one_input",
283282
"test_mean_two_inputs",
@@ -436,10 +435,6 @@
436435
"test_reduce_log_sum_exp_negative_axes_keepdims_example",
437436
"test_reduce_log_sum_exp_negative_axes_keepdims_random",
438437
"test_reduce_log_sum_negative_axes",
439-
"test_reduce_max_default_axes_keepdim_example",
440-
"test_reduce_max_default_axes_keepdims_random",
441-
"test_reduce_mean_default_axes_keepdims_example",
442-
"test_reduce_mean_default_axes_keepdims_random",
443438
"test_reduce_min_default_axes_keepdims_example",
444439
"test_reduce_min_default_axes_keepdims_random",
445440
"test_reduce_min_do_not_keepdims_example",

0 commit comments

Comments
 (0)