From d825caf18e1c173ecbc203a83c8c790c9e776873 Mon Sep 17 00:00:00 2001 From: Liubov Batanina Date: Tue, 21 Jan 2020 10:09:24 +0300 Subject: [PATCH] Update check params --- modules/dnn/src/layers/layers_common.cpp | 55 +++--------------------- modules/dnn/src/layers/pooling_layer.cpp | 9 ++-- 2 files changed, 10 insertions(+), 54 deletions(-) diff --git a/modules/dnn/src/layers/layers_common.cpp b/modules/dnn/src/layers/layers_common.cpp index dbe69b4b81..cd34748398 100644 --- a/modules/dnn/src/layers/layers_common.cpp +++ b/modules/dnn/src/layers/layers_common.cpp @@ -162,6 +162,7 @@ void getPoolingKernelParams(const LayerParams ¶ms, std::vector& kern } else { + util::getStrideAndPadding(params, pads_begin, pads_end, strides, padMode); if ((globalPooling[0] && params.has("kernel_d")) || (globalPooling[1] && params.has("kernel_h")) || (globalPooling[2] && params.has("kernel_w")) || @@ -170,60 +171,18 @@ void getPoolingKernelParams(const LayerParams ¶ms, std::vector& kern } kernel.resize(3); - pads_begin.resize(3, 0); - pads_end.resize(3, 0); - strides.resize(3, 1); kernel[0] = params.get("kernel_d", 1); kernel[1] = params.get("kernel_h", 1); kernel[2] = params.get("kernel_w", 1); - pads_begin[1] = params.get("pad_t", 0); - pads_begin[2] = params.get("pad_l", 0); - pads_end[1] = params.get("pad_b", 0); - pads_end[2] = params.get("pad_r", 0); - if (params.has("pad_h")) { - pads_begin[1] = params.get("pad_h"); - pads_end[1] = params.get("pad_h"); + for (int i = 0, j = globalPooling.size() - pads_begin.size(); i < pads_begin.size(); i++, j++) { + if ((pads_begin[i] != 0 || pads_end[i] != 0) && globalPooling[j]) + CV_Error(cv::Error::StsBadArg, "In global_pooling mode, pads must be = 0"); } - if (params.has("pad_w")) { - pads_begin[2] = params.get("pad_w"); - pads_end[2] = params.get("pad_w"); + for (int i = 0, j = globalPooling.size() - strides.size(); i < strides.size(); i++, j++) { + if (strides[i] != 1 && globalPooling[j]) + CV_Error(cv::Error::StsBadArg, "In global_pooling mode, strides must be = 1"); } - if (params.has("pad")) { - DictValue param = params.get("pad"); - if (param.size() == 1) { - std::fill(pads_begin.begin(), pads_begin.end(), param.get(0)); - pads_end = pads_begin; - } else if (param.size() <= pads_begin.size()) { - for (int i = param.size() - 1, j = pads_begin.size() - 1; i >= 0; i--, j--) { - pads_begin[j] = param.get(i); - } - pads_end = pads_begin; - } else { - for (int i = param.size() - 1, j = pads_begin.size() - 1; i >= param.size() / 2; i--, j--) { - pads_begin[j] = param.get(i); - } - for (int i = param.size() / 2 - 1, j = pads_end.size() / 2 - 1; i >= 0; i--, j--) { - pads_end[j] = param.get(i); - } - } - } - - strides[1] = params.get("stride_h", 1); - strides[2] = params.get("stride_w", 1); - if (params.has("stride")) { - DictValue param = params.get("stride"); - for (int i = param.size() - 1, j = strides.size() - 1; i >= 0; i--, j--) { - strides[j] = param.get(i); - } - if (param.size() == 1) - std::fill(strides.begin() + 1, strides.end(), strides[0]); - } - - for (int i = 0; i < pads_begin.size(); i++) { - if ((pads_begin[i] != 0 || pads_end[i] != 0 || strides[i] != 1) && globalPooling[i]) - CV_Error(cv::Error::StsBadArg, "In global_pooling mode, pads must be = 0 and strides must be = 1"); - } } } diff --git a/modules/dnn/src/layers/pooling_layer.cpp b/modules/dnn/src/layers/pooling_layer.cpp index eef091dd42..8d43dc3ebf 100644 --- a/modules/dnn/src/layers/pooling_layer.cpp +++ b/modules/dnn/src/layers/pooling_layer.cpp @@ -151,9 +151,6 @@ public: if (kernel_size.size() > inp.size()) { kernel_size.erase(kernel_size.begin()); - strides.erase(strides.begin()); - pads_begin.erase(pads_begin.begin()); - pads_end.erase(pads_end.begin()); } kernel_size.resize(out.size()); @@ -1025,16 +1022,16 @@ virtual Ptr initNgraph(const std::vector >& inp else if (padMode.empty()) { for (int i = 0, j = local_kernel.size() - inpShape.size(); i < inpShape.size(); i++, j++) { - float dst = (float)(inpShape[i] + pads_begin[j] + pads_end[j] - local_kernel[j]) / strides[j]; + float dst = (float)(inpShape[i] + pads_begin[i] + pads_end[i] - local_kernel[j]) / strides[i]; outShape.push_back(1 + (ceilMode ? ceil(dst) : floor(dst))); } // If we have padding, ensure that the last pooling starts strictly // inside the image (instead of at the padding); otherwise clip the last. for (int i = 0, j = local_kernel.size() - inpShape.size(); i < inpShape.size(); i++, j++) { - if (pads_end[j] && (outShape[2 + i] - 1) * strides[j] >= inpShape[i] + pads_end[j]) { + if (pads_end[i] && (outShape[2 + i] - 1) * strides[i] >= inpShape[i] + pads_end[i]) { --outShape[2 + i]; - CV_Assert((outShape[2 + i] - 1) * strides[j] < inpShape[i] + pads_end[j]); + CV_Assert((outShape[2 + i] - 1) * strides[i] < inpShape[i] + pads_end[i]); } } }