Skip to content

Commit 21bd834

Browse files
committed
Merge pull request opencv#9772 from dkurt:fix_caffe_eltwise_and_fc_layers
2 parents b969d86 + ad8bbaf commit 21bd834

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

modules/dnn/src/layers/eltwise_layer.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,16 @@ class EltwiseLayerImpl : public EltwiseLayer
119119
EltwiseOp op;
120120
int nstripes;
121121
const ActivationLayer* activ;
122+
int channels;
123+
size_t planeSize;
122124

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

125127
static void run(const Mat** srcs, int nsrcs, Mat& dst,
126128
const std::vector<float>& coeffs, EltwiseOp op,
127129
const ActivationLayer* activ, int nstripes)
128130
{
129-
CV_Assert(dst.dims == 4 && dst.type() == CV_32F && dst.isContinuous());
131+
CV_Assert(1 < dst.dims && dst.dims <= 4, dst.type() == CV_32F, dst.isContinuous());
130132
CV_Assert(coeffs.empty() || coeffs.size() == (size_t)nsrcs);
131133

132134
for( int i = 0; i > nsrcs; i++ )
@@ -142,6 +144,11 @@ class EltwiseLayerImpl : public EltwiseLayer
142144
p.dst = &dst;
143145
p.op = op;
144146
p.nstripes = nstripes;
147+
p.channels = (dst.dims == 4 ? dst.size[1] : 1);
148+
p.planeSize = (dst.dims >= 3 ? dst.size[dst.dims - 1] * dst.size[dst.dims - 2] :
149+
dst.size[dst.dims - 1]);
150+
CV_Assert(dst.total() == dst.size[0] * p.channels * p.planeSize);
151+
145152
bool simpleCoeffs = true;
146153
if( op == EltwiseLayer::SUM && !coeffs.empty() )
147154
{
@@ -162,13 +169,11 @@ class EltwiseLayerImpl : public EltwiseLayer
162169

163170
void operator()(const Range& r) const
164171
{
165-
size_t planeSize = dst->size[2]*dst->size[3];
166172
size_t total = dst->size[0]*planeSize;
167173
size_t stripeSize = (total + nstripes - 1)/nstripes;
168174
size_t stripeStart = r.start*stripeSize;
169175
size_t stripeEnd = std::min(r.end*stripeSize, total);
170176
int c, j, k, n = nsrcs;
171-
int channels = dst->size[1];
172177
const float* coeffsptr = coeffs && !coeffs->empty() ? &coeffs->at(0) : 0;
173178
float* dstptr0 = dst->ptr<float>();
174179
int blockSize0 = 1 << 12, blockSize = blockSize0;

modules/dnn/src/layers/fully_connected_layer.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,18 @@ class FullyConnectedLayerImpl : public InnerProductLayer
107107
std::vector<MatShape> &outputs,
108108
std::vector<MatShape> &) const
109109
{
110-
CV_Assert(inputs.size() > 0);
110+
CV_Assert(inputs.size() == 1);
111111
CV_Assert(1 <= blobs.size() && blobs.size() <= 2);
112112
CV_Assert(blobs[0].dims == 2);
113113

114114
int cAxis = clamp(axis, inputs[0]);
115-
int outerSize = total(inputs[0], 0, cAxis);
116115
int numOutput = blobs[0].size[0];
117-
outputs.resize(inputs.size(), shape(outerSize, numOutput));
116+
MatShape outShape(cAxis + 1);
117+
for (int i = 0; i < cAxis; ++i)
118+
outShape[i] = inputs[0][i];
119+
outShape.back() = numOutput;
120+
121+
outputs.resize(inputs.size(), outShape);
118122

119123
CV_Assert(!bias || (size_t)numOutput == blobs[1].total());
120124
return false;
@@ -278,8 +282,8 @@ class FullyConnectedLayerImpl : public InnerProductLayer
278282
for (size_t i = 0; i < input.size(); i++)
279283
{
280284
UMat srcMat, dstMat;
281-
srcMat = input[i]->getUMat(ACCESS_READ);
282-
dstMat = output[i].getUMat(ACCESS_WRITE);
285+
srcMat = input[i]->reshape(1, outerSize).getUMat(ACCESS_READ);
286+
dstMat = output[i].reshape(1, outerSize).getUMat(ACCESS_WRITE);
283287
dstMat.setTo(0.0f);
284288

285289
if (!innerProductOp->Forward(srcMat, umat_blobs[0], (bias) ? umat_blobs[1] : UMat(), dstMat))

modules/dnn/test/test_layers.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ OCL_TEST(Layer_Test_Concat, Accuracy)
274274
testLayerUsingCaffeModels("layer_concat", DNN_TARGET_OPENCL);
275275
}
276276

277+
TEST(Layer_Test_Eltwise, Accuracy)
278+
{
279+
testLayerUsingCaffeModels("layer_eltwise");
280+
}
281+
277282
//template<typename XMat>
278283
//static void test_Layer_Concat()
279284
//{

0 commit comments

Comments
 (0)