mirror of
https://github.com/opencv/opencv.git
synced 2025-06-12 20:42:53 +08:00
Merge pull request #9772 from dkurt:fix_caffe_eltwise_and_fc_layers
This commit is contained in:
commit
21bd834a59
@ -119,6 +119,8 @@ public:
|
|||||||
EltwiseOp op;
|
EltwiseOp op;
|
||||||
int nstripes;
|
int nstripes;
|
||||||
const ActivationLayer* activ;
|
const ActivationLayer* activ;
|
||||||
|
int channels;
|
||||||
|
size_t planeSize;
|
||||||
|
|
||||||
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) {}
|
||||||
|
|
||||||
@ -126,7 +128,7 @@ public:
|
|||||||
const std::vector<float>& 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(1 < dst.dims && dst.dims <= 4, dst.type() == CV_32F, dst.isContinuous());
|
||||||
CV_Assert(coeffs.empty() || coeffs.size() == (size_t)nsrcs);
|
CV_Assert(coeffs.empty() || coeffs.size() == (size_t)nsrcs);
|
||||||
|
|
||||||
for( int i = 0; i > nsrcs; i++ )
|
for( int i = 0; i > nsrcs; i++ )
|
||||||
@ -142,6 +144,11 @@ public:
|
|||||||
p.dst = &dst;
|
p.dst = &dst;
|
||||||
p.op = op;
|
p.op = op;
|
||||||
p.nstripes = nstripes;
|
p.nstripes = nstripes;
|
||||||
|
p.channels = (dst.dims == 4 ? dst.size[1] : 1);
|
||||||
|
p.planeSize = (dst.dims >= 3 ? dst.size[dst.dims - 1] * dst.size[dst.dims - 2] :
|
||||||
|
dst.size[dst.dims - 1]);
|
||||||
|
CV_Assert(dst.total() == dst.size[0] * p.channels * p.planeSize);
|
||||||
|
|
||||||
bool simpleCoeffs = true;
|
bool simpleCoeffs = true;
|
||||||
if( op == EltwiseLayer::SUM && !coeffs.empty() )
|
if( op == EltwiseLayer::SUM && !coeffs.empty() )
|
||||||
{
|
{
|
||||||
@ -162,13 +169,11 @@ public:
|
|||||||
|
|
||||||
void operator()(const Range& r) const
|
void operator()(const Range& r) const
|
||||||
{
|
{
|
||||||
size_t planeSize = dst->size[2]*dst->size[3];
|
|
||||||
size_t total = dst->size[0]*planeSize;
|
size_t total = dst->size[0]*planeSize;
|
||||||
size_t stripeSize = (total + nstripes - 1)/nstripes;
|
size_t stripeSize = (total + nstripes - 1)/nstripes;
|
||||||
size_t stripeStart = r.start*stripeSize;
|
size_t stripeStart = r.start*stripeSize;
|
||||||
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];
|
|
||||||
const float* 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;
|
||||||
|
@ -107,14 +107,18 @@ public:
|
|||||||
std::vector<MatShape> &outputs,
|
std::vector<MatShape> &outputs,
|
||||||
std::vector<MatShape> &) const
|
std::vector<MatShape> &) const
|
||||||
{
|
{
|
||||||
CV_Assert(inputs.size() > 0);
|
CV_Assert(inputs.size() == 1);
|
||||||
CV_Assert(1 <= blobs.size() && blobs.size() <= 2);
|
CV_Assert(1 <= blobs.size() && blobs.size() <= 2);
|
||||||
CV_Assert(blobs[0].dims == 2);
|
CV_Assert(blobs[0].dims == 2);
|
||||||
|
|
||||||
int cAxis = clamp(axis, inputs[0]);
|
int cAxis = clamp(axis, inputs[0]);
|
||||||
int outerSize = total(inputs[0], 0, cAxis);
|
|
||||||
int numOutput = blobs[0].size[0];
|
int numOutput = blobs[0].size[0];
|
||||||
outputs.resize(inputs.size(), shape(outerSize, numOutput));
|
MatShape outShape(cAxis + 1);
|
||||||
|
for (int i = 0; i < cAxis; ++i)
|
||||||
|
outShape[i] = inputs[0][i];
|
||||||
|
outShape.back() = numOutput;
|
||||||
|
|
||||||
|
outputs.resize(inputs.size(), outShape);
|
||||||
|
|
||||||
CV_Assert(!bias || (size_t)numOutput == blobs[1].total());
|
CV_Assert(!bias || (size_t)numOutput == blobs[1].total());
|
||||||
return false;
|
return false;
|
||||||
@ -278,8 +282,8 @@ public:
|
|||||||
for (size_t i = 0; i < input.size(); i++)
|
for (size_t i = 0; i < input.size(); i++)
|
||||||
{
|
{
|
||||||
UMat srcMat, dstMat;
|
UMat srcMat, dstMat;
|
||||||
srcMat = input[i]->getUMat(ACCESS_READ);
|
srcMat = input[i]->reshape(1, outerSize).getUMat(ACCESS_READ);
|
||||||
dstMat = output[i].getUMat(ACCESS_WRITE);
|
dstMat = output[i].reshape(1, outerSize).getUMat(ACCESS_WRITE);
|
||||||
dstMat.setTo(0.0f);
|
dstMat.setTo(0.0f);
|
||||||
|
|
||||||
if (!innerProductOp->Forward(srcMat, umat_blobs[0], (bias) ? umat_blobs[1] : UMat(), dstMat))
|
if (!innerProductOp->Forward(srcMat, umat_blobs[0], (bias) ? umat_blobs[1] : UMat(), dstMat))
|
||||||
|
@ -274,6 +274,11 @@ OCL_TEST(Layer_Test_Concat, Accuracy)
|
|||||||
testLayerUsingCaffeModels("layer_concat", DNN_TARGET_OPENCL);
|
testLayerUsingCaffeModels("layer_concat", DNN_TARGET_OPENCL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Layer_Test_Eltwise, Accuracy)
|
||||||
|
{
|
||||||
|
testLayerUsingCaffeModels("layer_eltwise");
|
||||||
|
}
|
||||||
|
|
||||||
//template<typename XMat>
|
//template<typename XMat>
|
||||||
//static void test_Layer_Concat()
|
//static void test_Layer_Concat()
|
||||||
//{
|
//{
|
||||||
|
Loading…
Reference in New Issue
Block a user