Eltwise layer fixes

This commit is contained in:
dkurt 2017-07-10 12:58:11 +03:00
parent cddf868572
commit 3203635765
3 changed files with 25 additions and 12 deletions

View File

@ -53,7 +53,7 @@ class EltwiseLayerImpl : public EltwiseLayer
{
public:
EltwiseOp op;
std::vector<int> coeffs;
std::vector<float> coeffs;
EltwiseLayerImpl(const LayerParams& params)
{
@ -79,7 +79,7 @@ public:
coeffs.resize(n);
for (i = 0; i < n; i++)
{
coeffs[i] = paramCoeff.get<int>(i);
coeffs[i] = paramCoeff.get<float>(i);
}
}
}
@ -115,7 +115,7 @@ public:
const Mat** srcs;
int nsrcs;
Mat* dst;
const std::vector<int>* coeffs;
const std::vector<float>* 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<int>& coeffs, EltwiseOp op,
const std::vector<float>& 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<float>();
int blockSize0 = 1 << 12, blockSize = blockSize0;
@ -203,7 +203,7 @@ public:
{
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++ )
{
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<float>() + globalDelta;
int c1 = coeffsptr[k];
float c1 = coeffsptr[k];
for( j = 0; j < blockSize; j++ )
{
dstptr[j] = c0*srcptr0[j] + c1*srcptr1[j];

View File

@ -125,7 +125,8 @@ void getPoolingKernelParams(const LayerParams &params, int &kernelH, int &kernel
{
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)
{

View File

@ -575,12 +575,13 @@ INSTANTIATE_TEST_CASE_P(Layer_Test_Halide, Concat, Combine(
// `--- conv ----^ ^ ^
// `---- ... ------' '
// `-----------------'
typedef TestWithParam<tuple<Vec3i, std::string, int> > Eltwise;
typedef TestWithParam<tuple<Vec3i, std::string, int, bool> > 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<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.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