Skip to content

Commit 8a1caa0

Browse files
committed
Merge pull request opencv#18554 from alalek:issue_17945
2 parents 083727b + cdcf7e6 commit 8a1caa0

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

modules/dnn/src/dnn.cpp

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,14 +2413,42 @@ struct Net::Impl : public detail::NetImplBase
24132413
}
24142414

24152415
// fuse convolution layer followed by eltwise + relu
2416-
if ( IS_DNN_OPENCL_TARGET(preferableTarget) && ld.layerInstance->type == "Convolution" )
2416+
while (nextData && IS_DNN_OPENCL_TARGET(preferableTarget) && ld.layerInstance->type == "Convolution") // semantic of 'if'
24172417
{
2418-
Ptr<EltwiseLayer> nextEltwiseLayer;
2419-
if( nextData )
2420-
nextEltwiseLayer = nextData->layerInstance.dynamicCast<EltwiseLayer>();
2418+
Ptr<EltwiseLayer> nextEltwiseLayer = nextData->layerInstance.dynamicCast<EltwiseLayer>();
2419+
if (nextEltwiseLayer.empty())
2420+
break;
2421+
2422+
if (pinsToKeep.count(lpNext) != 0)
2423+
break;
2424+
if (nextData->inputBlobsId.size() != 2)
2425+
break;
2426+
2427+
if (!nextData->params.has("operation") || nextData->params.get<String>("operation").toLowerCase() == "sum")
2428+
{
2429+
if (nextData->params.has("coeff"))
2430+
{
2431+
DictValue paramCoeff = nextData->params.get("coeff");
2432+
int n = paramCoeff.size();
2433+
bool isCoeffOneOne = (n == 2);
2434+
for (int i = 0; isCoeffOneOne && i < n; i++)
2435+
{
2436+
float c = paramCoeff.get<float>(i);
2437+
isCoeffOneOne &= (c == 1.0f);
2438+
}
2439+
if (!isCoeffOneOne)
2440+
{
2441+
CV_LOG_DEBUG(NULL, "DNN/OpenCL: fusion of 'Sum' without coeffs (or {1.0, 1.0}) is supported only");
2442+
break;
2443+
}
2444+
}
2445+
}
2446+
else
2447+
{
2448+
CV_LOG_DEBUG(NULL, "DNN/OpenCL: fusion with eltwise operation is not supported: " << nextData->params.get<String>("operation"));
2449+
break;
2450+
}
24212451

2422-
if( !nextEltwiseLayer.empty() && pinsToKeep.count(lpNext) == 0 &&
2423-
nextData && nextData->inputBlobsId.size() == 2 )
24242452
{
24252453
LayerData *eltwiseData = nextData;
24262454

@@ -2517,6 +2545,8 @@ struct Net::Impl : public detail::NetImplBase
25172545
}
25182546
}
25192547
}
2548+
2549+
break;
25202550
}
25212551
}
25222552

@@ -2698,11 +2728,11 @@ struct Net::Impl : public detail::NetImplBase
26982728

26992729
Ptr<Layer> layer = ld.layerInstance;
27002730

2701-
TickMeter tm;
2702-
tm.start();
2703-
27042731
if( !ld.skip )
27052732
{
2733+
TickMeter tm;
2734+
tm.start();
2735+
27062736
std::map<int, Ptr<BackendNode> >::iterator it = ld.backendNodes.find(preferableBackend);
27072737
if (preferableBackend == DNN_BACKEND_OPENCV || it == ld.backendNodes.end() || it->second.empty())
27082738
{
@@ -2881,12 +2911,15 @@ struct Net::Impl : public detail::NetImplBase
28812911
CV_Error(Error::StsNotImplemented, "Unknown backend identifier");
28822912
}
28832913
}
2914+
2915+
tm.stop();
2916+
int64 t = tm.getTimeTicks();
2917+
layersTimings[ld.id] = (t > 0) ? t : t + 1; // zero for skipped layers only
28842918
}
28852919
else
2886-
tm.reset();
2887-
2888-
tm.stop();
2889-
layersTimings[ld.id] = tm.getTimeTicks();
2920+
{
2921+
layersTimings[ld.id] = 0;
2922+
}
28902923

28912924
ld.flag = 1;
28922925
}

modules/dnn/src/layers/convolution_layer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
376376
if (activ_power->scale != 1.0f) // not supported well by implementation, #17964
377377
{
378378
// FIXIT no way to check number of blobs (like, eltwise input)
379-
CV_LOG_INFO(NULL, "DNN/OpenCL: can't configure Power activation (scale != 1.0f)");
379+
CV_LOG_DEBUG(NULL, "DNN/OpenCL: can't configure Power activation (scale != 1.0f)");
380380
activ.release();
381381
newActiv = false;
382382
return false;

modules/dnn/test/test_layers.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,10 +2341,6 @@ TEST_P(ConvolutionEltwiseActivationFusion, Accuracy)
23412341
Backend backendId = get<0>(get<4>(GetParam()));
23422342
Target targetId = get<1>(get<4>(GetParam()));
23432343

2344-
// bug: https://github.com/opencv/opencv/issues/17945
2345-
if ((eltwiseOp != "sum" || weightedEltwise) && backendId == DNN_BACKEND_OPENCV && (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16))
2346-
applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL);
2347-
23482344
Net net;
23492345
int convId = net.addLayer(convParams.name, convParams.type, convParams);
23502346
int eltwiseId = net.addLayer(eltwiseParams.name, eltwiseParams.type, eltwiseParams);
@@ -2361,7 +2357,9 @@ TEST_P(ConvolutionEltwiseActivationFusion, Accuracy)
23612357
expectedFusedLayers.push_back(activId); // activation is fused with eltwise layer
23622358
else if (targetId == DNN_TARGET_OPENCL || targetId == DNN_TARGET_OPENCL_FP16)
23632359
{
2364-
if (actType == "ReLU" || actType == "ChannelsPReLU" /*|| actType == "Power"*/)
2360+
if (eltwiseOp == "sum" && !weightedEltwise &&
2361+
(actType == "ReLU" || actType == "ChannelsPReLU" /*|| actType == "Power"*/)
2362+
)
23652363
{
23662364
expectedFusedLayers.push_back(eltwiseId);
23672365
expectedFusedLayers.push_back(activId);

0 commit comments

Comments
 (0)