Skip to content

Commit 9365817

Browse files
committed
Merge pull request opencv#9131 from dkurt:fix_eltwise_layer
2 parents d656d39 + 3203635 commit 9365817

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

modules/dnn/src/layers/eltwise_layer.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class EltwiseLayerImpl : public EltwiseLayer
5353
{
5454
public:
5555
EltwiseOp op;
56-
std::vector<int> coeffs;
56+
std::vector<float> coeffs;
5757

5858
EltwiseLayerImpl(const LayerParams& params)
5959
{
@@ -79,7 +79,7 @@ class EltwiseLayerImpl : public EltwiseLayer
7979
coeffs.resize(n);
8080
for (i = 0; i < n; i++)
8181
{
82-
coeffs[i] = paramCoeff.get<int>(i);
82+
coeffs[i] = paramCoeff.get<float>(i);
8383
}
8484
}
8585
}
@@ -115,15 +115,15 @@ class EltwiseLayerImpl : public EltwiseLayer
115115
const Mat** srcs;
116116
int nsrcs;
117117
Mat* dst;
118-
const std::vector<int>* coeffs;
118+
const std::vector<float>* coeffs;
119119
EltwiseOp op;
120120
int nstripes;
121121
const ActivationLayer* activ;
122122

123123
EltwiseInvoker() : srcs(0), nsrcs(0), dst(0), coeffs(0), op(EltwiseLayer::PROD), nstripes(0), activ(0) {}
124124

125125
static void run(const Mat** srcs, int nsrcs, Mat& dst,
126-
const std::vector<int>& coeffs, EltwiseOp op,
126+
const std::vector<float>& coeffs, EltwiseOp op,
127127
const ActivationLayer* activ, int nstripes)
128128
{
129129
CV_Assert(dst.dims == 4 && dst.type() == CV_32F && dst.isContinuous());
@@ -143,7 +143,7 @@ class EltwiseLayerImpl : public EltwiseLayer
143143
p.op = op;
144144
p.nstripes = nstripes;
145145
bool simpleCoeffs = true;
146-
if( op != EltwiseLayer::SUM && !coeffs.empty() )
146+
if( op == EltwiseLayer::SUM && !coeffs.empty() )
147147
{
148148
CV_Assert( coeffs.size() == (size_t)nsrcs );
149149

@@ -169,7 +169,7 @@ class EltwiseLayerImpl : public EltwiseLayer
169169
size_t stripeEnd = std::min(r.end*stripeSize, total);
170170
int c, j, k, n = nsrcs;
171171
int channels = dst->size[1];
172-
const int* coeffsptr = coeffs && !coeffs->empty() ? &coeffs->at(0) : 0;
172+
const float* coeffsptr = coeffs && !coeffs->empty() ? &coeffs->at(0) : 0;
173173
float* dstptr0 = dst->ptr<float>();
174174
int blockSize0 = 1 << 12, blockSize = blockSize0;
175175

@@ -203,7 +203,7 @@ class EltwiseLayerImpl : public EltwiseLayer
203203
{
204204
for( k = 1; k < n; k++ )
205205
{
206-
const float* srcptr1 = srcs[0]->ptr<float>() + globalDelta;
206+
const float* srcptr1 = srcs[k]->ptr<float>() + globalDelta;
207207
for( j = 0; j < blockSize; j++ )
208208
{
209209
dstptr[j] = std::max(srcptr0[j], srcptr1[j]);
@@ -225,11 +225,11 @@ class EltwiseLayerImpl : public EltwiseLayer
225225
}
226226
else
227227
{
228-
int c0 = coeffsptr[0];
228+
float c0 = coeffsptr[0];
229229
for( k = 1; k < n; k++ )
230230
{
231231
const float* srcptr1 = srcs[k]->ptr<float>() + globalDelta;
232-
int c1 = coeffsptr[k];
232+
float c1 = coeffsptr[k];
233233
for( j = 0; j < blockSize; j++ )
234234
{
235235
dstptr[j] = c0*srcptr0[j] + c1*srcptr1[j];

modules/dnn/src/layers/layers_common.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ void getPoolingKernelParams(const LayerParams &params, int &kernelH, int &kernel
125125
{
126126
util::getStrideAndPadding(params, padH, padW, strideH, strideW, padMode);
127127

128-
globalPooling = params.has("global_pooling");
128+
globalPooling = params.has("global_pooling") &&
129+
params.get<bool>("global_pooling");
129130

130131
if (globalPooling)
131132
{

modules/dnn/test/test_halide_layers.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,13 @@ INSTANTIATE_TEST_CASE_P(Layer_Test_Halide, Concat, Combine(
575575
// `--- conv ----^ ^ ^
576576
// `---- ... ------' '
577577
// `-----------------'
578-
typedef TestWithParam<tuple<Vec3i, std::string, int> > Eltwise;
578+
typedef TestWithParam<tuple<Vec3i, std::string, int, bool> > Eltwise;
579579
TEST_P(Eltwise, Accuracy)
580580
{
581581
Vec3i inSize = get<0>(GetParam());
582582
std::string op = get<1>(GetParam());
583583
int numConv = get<2>(GetParam());
584+
bool weighted = get<3>(GetParam());
584585

585586
Net net;
586587

@@ -606,6 +607,16 @@ TEST_P(Eltwise, Accuracy)
606607
}
607608

608609
LayerParams eltwiseParam;
610+
eltwiseParam.set("operation", op);
611+
if (op == "sum" && weighted)
612+
{
613+
std::vector<float> coeff(1 + numConv);
614+
for (int i = 0; i < coeff.size(); ++i)
615+
{
616+
coeff[i] = ((float)rand() / RAND_MAX) * 4 - 2;
617+
}
618+
eltwiseParam.set("coeff", DictValue::arrayReal<float*>(&coeff[0], coeff.size()));
619+
}
609620
eltwiseParam.type = "Eltwise";
610621
eltwiseParam.name = "testLayer";
611622
int eltwiseId = net.addLayer(eltwiseParam.name, eltwiseParam.type, eltwiseParam);
@@ -629,7 +640,8 @@ TEST_P(Eltwise, Accuracy)
629640
INSTANTIATE_TEST_CASE_P(Layer_Test_Halide, Eltwise, Combine(
630641
/*input size*/ Values(Vec3i(1, 4, 5), Vec3i(2, 8, 6)),
631642
/*operation*/ Values("prod", "sum", "max"),
632-
/*num convs*/ Values(1, 2, 3)
643+
/*num convs*/ Values(1, 2, 3),
644+
/*weighted(for sum only)*/ Bool()
633645
));
634646
#endif // HAVE_HALIDE
635647

0 commit comments

Comments
 (0)