Commit 32036357 authored by dkurt's avatar dkurt

Eltwise layer fixes

parent cddf8685
...@@ -53,7 +53,7 @@ class EltwiseLayerImpl : public EltwiseLayer ...@@ -53,7 +53,7 @@ class EltwiseLayerImpl : public EltwiseLayer
{ {
public: public:
EltwiseOp op; EltwiseOp op;
std::vector<int> coeffs; std::vector<float> coeffs;
EltwiseLayerImpl(const LayerParams& params) EltwiseLayerImpl(const LayerParams& params)
{ {
...@@ -79,7 +79,7 @@ public: ...@@ -79,7 +79,7 @@ public:
coeffs.resize(n); coeffs.resize(n);
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
coeffs[i] = paramCoeff.get<int>(i); coeffs[i] = paramCoeff.get<float>(i);
} }
} }
} }
...@@ -115,7 +115,7 @@ public: ...@@ -115,7 +115,7 @@ public:
const Mat** srcs; const Mat** srcs;
int nsrcs; int nsrcs;
Mat* dst; Mat* dst;
const std::vector<int>* coeffs; const std::vector<float>* coeffs;
EltwiseOp op; EltwiseOp op;
int nstripes; int nstripes;
const ActivationLayer* activ; const ActivationLayer* activ;
...@@ -123,7 +123,7 @@ public: ...@@ -123,7 +123,7 @@ public:
EltwiseInvoker() : srcs(0), nsrcs(0), dst(0), coeffs(0), op(EltwiseLayer::PROD), nstripes(0), activ(0) {} 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, static void run(const Mat** srcs, int nsrcs, Mat& dst,
const std::vector<int>& coeffs, EltwiseOp op, const std::vector<float>& coeffs, EltwiseOp op,
const ActivationLayer* activ, int nstripes) const ActivationLayer* activ, int nstripes)
{ {
CV_Assert(dst.dims == 4 && dst.type() == CV_32F && dst.isContinuous()); CV_Assert(dst.dims == 4 && dst.type() == CV_32F && dst.isContinuous());
...@@ -143,7 +143,7 @@ public: ...@@ -143,7 +143,7 @@ public:
p.op = op; p.op = op;
p.nstripes = nstripes; p.nstripes = nstripes;
bool simpleCoeffs = true; bool simpleCoeffs = true;
if( op != EltwiseLayer::SUM && !coeffs.empty() ) if( op == EltwiseLayer::SUM && !coeffs.empty() )
{ {
CV_Assert( coeffs.size() == (size_t)nsrcs ); CV_Assert( coeffs.size() == (size_t)nsrcs );
...@@ -169,7 +169,7 @@ public: ...@@ -169,7 +169,7 @@ public:
size_t stripeEnd = std::min(r.end*stripeSize, total); size_t stripeEnd = std::min(r.end*stripeSize, total);
int c, j, k, n = nsrcs; int c, j, k, n = nsrcs;
int channels = dst->size[1]; 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<float>(); float* dstptr0 = dst->ptr<float>();
int blockSize0 = 1 << 12, blockSize = blockSize0; int blockSize0 = 1 << 12, blockSize = blockSize0;
...@@ -203,7 +203,7 @@ public: ...@@ -203,7 +203,7 @@ public:
{ {
for( k = 1; k < n; k++ ) for( k = 1; k < n; k++ )
{ {
const float* srcptr1 = srcs[0]->ptr<float>() + globalDelta; const float* srcptr1 = srcs[k]->ptr<float>() + globalDelta;
for( j = 0; j < blockSize; j++ ) for( j = 0; j < blockSize; j++ )
{ {
dstptr[j] = std::max(srcptr0[j], srcptr1[j]); dstptr[j] = std::max(srcptr0[j], srcptr1[j]);
...@@ -225,11 +225,11 @@ public: ...@@ -225,11 +225,11 @@ public:
} }
else else
{ {
int c0 = coeffsptr[0]; float c0 = coeffsptr[0];
for( k = 1; k < n; k++ ) for( k = 1; k < n; k++ )
{ {
const float* srcptr1 = srcs[k]->ptr<float>() + globalDelta; const float* srcptr1 = srcs[k]->ptr<float>() + globalDelta;
int c1 = coeffsptr[k]; float c1 = coeffsptr[k];
for( j = 0; j < blockSize; j++ ) for( j = 0; j < blockSize; j++ )
{ {
dstptr[j] = c0*srcptr0[j] + c1*srcptr1[j]; dstptr[j] = c0*srcptr0[j] + c1*srcptr1[j];
......
...@@ -125,7 +125,8 @@ void getPoolingKernelParams(const LayerParams &params, int &kernelH, int &kernel ...@@ -125,7 +125,8 @@ void getPoolingKernelParams(const LayerParams &params, int &kernelH, int &kernel
{ {
util::getStrideAndPadding(params, padH, padW, strideH, strideW, padMode); util::getStrideAndPadding(params, padH, padW, strideH, strideW, padMode);
globalPooling = params.has("global_pooling"); globalPooling = params.has("global_pooling") &&
params.get<bool>("global_pooling");
if (globalPooling) if (globalPooling)
{ {
......
...@@ -575,12 +575,13 @@ INSTANTIATE_TEST_CASE_P(Layer_Test_Halide, Concat, Combine( ...@@ -575,12 +575,13 @@ INSTANTIATE_TEST_CASE_P(Layer_Test_Halide, Concat, Combine(
// `--- conv ----^ ^ ^ // `--- conv ----^ ^ ^
// `---- ... ------' ' // `---- ... ------' '
// `-----------------' // `-----------------'
typedef TestWithParam<tuple<Vec3i, std::string, int> > Eltwise; typedef TestWithParam<tuple<Vec3i, std::string, int, bool> > Eltwise;
TEST_P(Eltwise, Accuracy) TEST_P(Eltwise, Accuracy)
{ {
Vec3i inSize = get<0>(GetParam()); Vec3i inSize = get<0>(GetParam());
std::string op = get<1>(GetParam()); std::string op = get<1>(GetParam());
int numConv = get<2>(GetParam()); int numConv = get<2>(GetParam());
bool weighted = get<3>(GetParam());
Net net; Net net;
...@@ -606,6 +607,16 @@ TEST_P(Eltwise, Accuracy) ...@@ -606,6 +607,16 @@ TEST_P(Eltwise, Accuracy)
} }
LayerParams eltwiseParam; LayerParams eltwiseParam;
eltwiseParam.set("operation", op);
if (op == "sum" && weighted)
{
std::vector<float> coeff(1 + numConv);
for (int i = 0; i < coeff.size(); ++i)
{
coeff[i] = ((float)rand() / RAND_MAX) * 4 - 2;
}
eltwiseParam.set("coeff", DictValue::arrayReal<float*>(&coeff[0], coeff.size()));
}
eltwiseParam.type = "Eltwise"; eltwiseParam.type = "Eltwise";
eltwiseParam.name = "testLayer"; eltwiseParam.name = "testLayer";
int eltwiseId = net.addLayer(eltwiseParam.name, eltwiseParam.type, eltwiseParam); int eltwiseId = net.addLayer(eltwiseParam.name, eltwiseParam.type, eltwiseParam);
...@@ -629,7 +640,8 @@ TEST_P(Eltwise, Accuracy) ...@@ -629,7 +640,8 @@ TEST_P(Eltwise, Accuracy)
INSTANTIATE_TEST_CASE_P(Layer_Test_Halide, Eltwise, Combine( INSTANTIATE_TEST_CASE_P(Layer_Test_Halide, Eltwise, Combine(
/*input size*/ Values(Vec3i(1, 4, 5), Vec3i(2, 8, 6)), /*input size*/ Values(Vec3i(1, 4, 5), Vec3i(2, 8, 6)),
/*operation*/ Values("prod", "sum", "max"), /*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 #endif // HAVE_HALIDE
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment