From 3203635765891c52385afa667472f994d53ef276 Mon Sep 17 00:00:00 2001 From: dkurt Date: Mon, 10 Jul 2017 12:58:11 +0300 Subject: [PATCH] Eltwise layer fixes --- modules/dnn/src/layers/eltwise_layer.cpp | 18 +++++++++--------- modules/dnn/src/layers/layers_common.cpp | 3 ++- modules/dnn/test/test_halide_layers.cpp | 16 ++++++++++++++-- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/modules/dnn/src/layers/eltwise_layer.cpp b/modules/dnn/src/layers/eltwise_layer.cpp index 12069e7..fa49109 100644 --- a/modules/dnn/src/layers/eltwise_layer.cpp +++ b/modules/dnn/src/layers/eltwise_layer.cpp @@ -53,7 +53,7 @@ class EltwiseLayerImpl : public EltwiseLayer { public: EltwiseOp op; - std::vector coeffs; + std::vector coeffs; EltwiseLayerImpl(const LayerParams& params) { @@ -79,7 +79,7 @@ public: coeffs.resize(n); for (i = 0; i < n; i++) { - coeffs[i] = paramCoeff.get(i); + coeffs[i] = paramCoeff.get(i); } } } @@ -115,7 +115,7 @@ public: const Mat** srcs; int nsrcs; Mat* dst; - const std::vector* coeffs; + const std::vector* coeffs; EltwiseOp op; int nstripes; const ActivationLayer* activ; @@ -123,7 +123,7 @@ public: EltwiseInvoker() : srcs(0), nsrcs(0), dst(0), coeffs(0), op(EltwiseLayer::PROD), nstripes(0), activ(0) {} static void run(const Mat** srcs, int nsrcs, Mat& dst, - const std::vector& coeffs, EltwiseOp op, + const std::vector& coeffs, EltwiseOp op, const ActivationLayer* activ, int nstripes) { CV_Assert(dst.dims == 4 && dst.type() == CV_32F && dst.isContinuous()); @@ -143,7 +143,7 @@ public: p.op = op; p.nstripes = nstripes; bool simpleCoeffs = true; - if( op != EltwiseLayer::SUM && !coeffs.empty() ) + if( op == EltwiseLayer::SUM && !coeffs.empty() ) { CV_Assert( coeffs.size() == (size_t)nsrcs ); @@ -169,7 +169,7 @@ public: size_t stripeEnd = std::min(r.end*stripeSize, total); int c, j, k, n = nsrcs; int channels = dst->size[1]; - const int* coeffsptr = coeffs && !coeffs->empty() ? &coeffs->at(0) : 0; + const float* coeffsptr = coeffs && !coeffs->empty() ? &coeffs->at(0) : 0; float* dstptr0 = dst->ptr(); int blockSize0 = 1 << 12, blockSize = blockSize0; @@ -203,7 +203,7 @@ public: { for( k = 1; k < n; k++ ) { - const float* srcptr1 = srcs[0]->ptr() + globalDelta; + const float* srcptr1 = srcs[k]->ptr() + globalDelta; for( j = 0; j < blockSize; j++ ) { dstptr[j] = std::max(srcptr0[j], srcptr1[j]); @@ -225,11 +225,11 @@ public: } else { - int c0 = coeffsptr[0]; + float c0 = coeffsptr[0]; for( k = 1; k < n; k++ ) { const float* srcptr1 = srcs[k]->ptr() + globalDelta; - int c1 = coeffsptr[k]; + float c1 = coeffsptr[k]; for( j = 0; j < blockSize; j++ ) { dstptr[j] = c0*srcptr0[j] + c1*srcptr1[j]; diff --git a/modules/dnn/src/layers/layers_common.cpp b/modules/dnn/src/layers/layers_common.cpp index 4e1142f..d11739a 100644 --- a/modules/dnn/src/layers/layers_common.cpp +++ b/modules/dnn/src/layers/layers_common.cpp @@ -125,7 +125,8 @@ void getPoolingKernelParams(const LayerParams ¶ms, int &kernelH, int &kernel { util::getStrideAndPadding(params, padH, padW, strideH, strideW, padMode); - globalPooling = params.has("global_pooling"); + globalPooling = params.has("global_pooling") && + params.get("global_pooling"); if (globalPooling) { diff --git a/modules/dnn/test/test_halide_layers.cpp b/modules/dnn/test/test_halide_layers.cpp index 64056dc..b2edf3a 100644 --- a/modules/dnn/test/test_halide_layers.cpp +++ b/modules/dnn/test/test_halide_layers.cpp @@ -575,12 +575,13 @@ INSTANTIATE_TEST_CASE_P(Layer_Test_Halide, Concat, Combine( // `--- conv ----^ ^ ^ // `---- ... ------' ' // `-----------------' -typedef TestWithParam > Eltwise; +typedef TestWithParam > Eltwise; TEST_P(Eltwise, Accuracy) { Vec3i inSize = get<0>(GetParam()); std::string op = get<1>(GetParam()); int numConv = get<2>(GetParam()); + bool weighted = get<3>(GetParam()); Net net; @@ -606,6 +607,16 @@ TEST_P(Eltwise, Accuracy) } LayerParams eltwiseParam; + eltwiseParam.set("operation", op); + if (op == "sum" && weighted) + { + std::vector coeff(1 + numConv); + for (int i = 0; i < coeff.size(); ++i) + { + coeff[i] = ((float)rand() / RAND_MAX) * 4 - 2; + } + eltwiseParam.set("coeff", DictValue::arrayReal(&coeff[0], coeff.size())); + } eltwiseParam.type = "Eltwise"; eltwiseParam.name = "testLayer"; int eltwiseId = net.addLayer(eltwiseParam.name, eltwiseParam.type, eltwiseParam); @@ -629,7 +640,8 @@ TEST_P(Eltwise, Accuracy) INSTANTIATE_TEST_CASE_P(Layer_Test_Halide, Eltwise, Combine( /*input size*/ Values(Vec3i(1, 4, 5), Vec3i(2, 8, 6)), /*operation*/ Values("prod", "sum", "max"), -/*num convs*/ Values(1, 2, 3) +/*num convs*/ Values(1, 2, 3), +/*weighted(for sum only)*/ Bool() )); #endif // HAVE_HALIDE -- 2.7.4