Skip to content

Commit f0bef94

Browse files
committed
dnn: update network dump code, include ngraph serialization
1 parent d5e8792 commit f0bef94

File tree

5 files changed

+91
-26
lines changed

5 files changed

+91
-26
lines changed

modules/dnn/src/dnn.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,17 +1082,26 @@ static Ptr<BackendWrapper> wrapMat(int backendId, int targetId, cv::Mat& m)
10821082

10831083
static int g_networkId = 0;
10841084

1085-
struct Net::Impl
1085+
detail::NetImplBase::NetImplBase()
1086+
: networkId(CV_XADD(&g_networkId, 1))
1087+
, networkDumpCounter(0)
1088+
, dumpLevel(DNN_NETWORK_DUMP)
1089+
{
1090+
// nothing
1091+
}
1092+
1093+
std::string detail::NetImplBase::getDumpFileNameBase()
1094+
{
1095+
std::string dumpFileNameBase = cv::format("ocv_dnn_net_%05d_%02d", networkId, networkDumpCounter++);
1096+
return dumpFileNameBase;
1097+
}
1098+
1099+
struct Net::Impl : public detail::NetImplBase
10861100
{
10871101
typedef std::map<int, LayerShapes> LayersShapesMap;
10881102
typedef std::map<int, LayerData> MapIdToLayerData;
10891103

1090-
const int networkId; // network global identifier
1091-
int networkDumpCounter; // dump counter
1092-
10931104
Impl()
1094-
: networkId(CV_XADD(&g_networkId, 1))
1095-
, networkDumpCounter(0)
10961105
{
10971106
//allocate fake net input layer
10981107
netInputLayer = Ptr<DataLayer>(new DataLayer());
@@ -1256,7 +1265,7 @@ struct Net::Impl
12561265
{
12571266
CV_TRACE_FUNCTION();
12581267

1259-
if (DNN_NETWORK_DUMP > 0 && networkDumpCounter == 0)
1268+
if (dumpLevel && networkDumpCounter == 0)
12601269
{
12611270
dumpNetworkToFile();
12621271
}
@@ -1339,7 +1348,7 @@ struct Net::Impl
13391348

13401349
netWasAllocated = true;
13411350

1342-
if (DNN_NETWORK_DUMP > 0)
1351+
if (dumpLevel)
13431352
{
13441353
dumpNetworkToFile();
13451354
}
@@ -2043,7 +2052,7 @@ struct Net::Impl
20432052
}
20442053

20452054
if (net.empty()) {
2046-
net = Ptr<InfEngineNgraphNet>(new InfEngineNgraphNet());
2055+
net = Ptr<InfEngineNgraphNet>(new InfEngineNgraphNet(*this));
20472056
}
20482057

20492058
if (!fused) {
@@ -2087,7 +2096,7 @@ struct Net::Impl
20872096
}
20882097
}
20892098
else {
2090-
net = Ptr<InfEngineNgraphNet>(new InfEngineNgraphNet());
2099+
net = Ptr<InfEngineNgraphNet>(new InfEngineNgraphNet(*this));
20912100
}
20922101

20932102
if (!fused)
@@ -3126,7 +3135,8 @@ struct Net::Impl
31263135
void dumpNetworkToFile()
31273136
{
31283137
#ifndef OPENCV_DNN_DISABLE_NETWORK_AUTO_DUMP
3129-
String dumpFileName = cv::format("ocv_dnn_net_%05d_%02d.dot", networkId, networkDumpCounter++);
3138+
string dumpFileNameBase = getDumpFileNameBase();
3139+
string dumpFileName = dumpFileNameBase + ".dot";
31303140
try
31313141
{
31323142
string dumpStr = dump();
@@ -3185,7 +3195,7 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe
31853195
{
31863196
auto fake_node = std::make_shared<ngraph::op::Parameter>(ngraph::element::f32, ngraph::Shape{});
31873197
Ptr<InfEngineNgraphNode> backendNodeNGraph(new InfEngineNgraphNode(fake_node));
3188-
backendNodeNGraph->net = Ptr<InfEngineNgraphNet>(new InfEngineNgraphNet(ieNet));
3198+
backendNodeNGraph->net = Ptr<InfEngineNgraphNet>(new InfEngineNgraphNet(*(cvNet.impl), ieNet));
31893199
backendNode = backendNodeNGraph;
31903200
}
31913201
else

modules/dnn/src/dnn_common.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#ifndef __OPENCV_DNN_COMMON_HPP__
6+
#define __OPENCV_DNN_COMMON_HPP__
7+
8+
#include <opencv2/dnn.hpp>
9+
10+
namespace cv { namespace dnn {
11+
CV__DNN_EXPERIMENTAL_NS_BEGIN
12+
#define IS_DNN_OPENCL_TARGET(id) (id == DNN_TARGET_OPENCL || id == DNN_TARGET_OPENCL_FP16)
13+
Mutex& getInitializationMutex();
14+
void initializeLayerFactory();
15+
16+
namespace detail {
17+
18+
struct NetImplBase
19+
{
20+
const int networkId; // network global identifier
21+
int networkDumpCounter; // dump counter
22+
int dumpLevel; // level of information dumps (initialized through OPENCV_DNN_NETWORK_DUMP parameter)
23+
24+
NetImplBase();
25+
26+
std::string getDumpFileNameBase();
27+
};
28+
29+
} // namespace detail
30+
31+
CV__DNN_EXPERIMENTAL_NS_END
32+
}} // namespace
33+
34+
#endif // __OPENCV_DNN_COMMON_HPP__

modules/dnn/src/ie_ngraph.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// Third party copyrights are property of their respective owners.
77

88
#include "precomp.hpp"
9+
10+
#include <fstream>
11+
912
#include "ie_ngraph.hpp"
1013

1114
#include <opencv2/dnn/shape_utils.hpp>
@@ -22,6 +25,8 @@ namespace cv { namespace dnn {
2225

2326
#ifdef HAVE_DNN_NGRAPH
2427

28+
static bool DNN_IE_SERIALIZE = utils::getConfigurationParameterBool("OPENCV_DNN_IE_SERIALIZE", false);
29+
2530
// For networks with input layer which has an empty name, IE generates a name id[some_number].
2631
// OpenCV lets users use an empty input name and to prevent unexpected naming,
2732
// we can use some predefined name.
@@ -295,13 +300,16 @@ void InfEngineNgraphNode::setName(const std::string& name) {
295300
node->set_friendly_name(name);
296301
}
297302

298-
InfEngineNgraphNet::InfEngineNgraphNet()
303+
InfEngineNgraphNet::InfEngineNgraphNet(detail::NetImplBase& netImpl)
304+
: netImpl_(netImpl)
299305
{
300306
hasNetOwner = false;
301307
device_name = "CPU";
302308
}
303309

304-
InfEngineNgraphNet::InfEngineNgraphNet(InferenceEngine::CNNNetwork& net) : cnn(net)
310+
InfEngineNgraphNet::InfEngineNgraphNet(detail::NetImplBase& netImpl, InferenceEngine::CNNNetwork& net)
311+
: netImpl_(netImpl)
312+
, cnn(net)
305313
{
306314
hasNetOwner = true;
307315
device_name = "CPU";
@@ -440,9 +448,27 @@ void InfEngineNgraphNet::init(Target targetId)
440448
ngraph_function->validate_nodes_and_infer_types();
441449
}
442450
cnn = InferenceEngine::CNNNetwork(ngraph_function);
443-
#ifdef _DEBUG // TODO
444-
//cnn.serialize("/tmp/cnn.xml", "/tmp/cnn.bin");
451+
452+
if (DNN_IE_SERIALIZE)
453+
{
454+
#ifndef OPENCV_DNN_DISABLE_NETWORK_AUTO_DUMP
455+
std::string dumpFileNameBase = netImpl_.getDumpFileNameBase();
456+
try
457+
{
458+
cnn.serialize(dumpFileNameBase + "_ngraph.xml", dumpFileNameBase + "_ngraph.bin");
459+
}
460+
catch (const std::exception& e)
461+
{
462+
std::ofstream out((dumpFileNameBase + "_ngraph.error").c_str(), std::ios::out);
463+
out << "Exception: " << e.what() << std::endl;
464+
}
465+
catch (...)
466+
{
467+
std::ofstream out((dumpFileNameBase + "_ngraph.error").c_str(), std::ios::out);
468+
out << "Can't dump: unknown exception" << std::endl;
469+
}
445470
#endif
471+
}
446472
}
447473

448474
switch (targetId)

modules/dnn/src/ie_ngraph.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class InfEngineNgraphNode;
3434
class InfEngineNgraphNet
3535
{
3636
public:
37-
InfEngineNgraphNet();
38-
InfEngineNgraphNet(InferenceEngine::CNNNetwork& net);
37+
InfEngineNgraphNet(detail::NetImplBase& netImpl);
38+
InfEngineNgraphNet(detail::NetImplBase& netImpl, InferenceEngine::CNNNetwork& net);
3939

4040
void addOutput(const std::string& name);
4141

@@ -55,6 +55,8 @@ class InfEngineNgraphNet
5555

5656
void reset();
5757
private:
58+
detail::NetImplBase& netImpl_;
59+
5860
void release();
5961
int getNumComponents();
6062
void dfs(std::shared_ptr<ngraph::Node>& node, std::vector<std::shared_ptr<ngraph::Node>>& comp,

modules/dnn/src/precomp.hpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,4 @@
6161
#include <opencv2/dnn.hpp>
6262
#include <opencv2/dnn/all_layers.hpp>
6363

64-
65-
namespace cv { namespace dnn {
66-
CV__DNN_EXPERIMENTAL_NS_BEGIN
67-
#define IS_DNN_OPENCL_TARGET(id) (id == DNN_TARGET_OPENCL || id == DNN_TARGET_OPENCL_FP16)
68-
Mutex& getInitializationMutex();
69-
void initializeLayerFactory();
70-
CV__DNN_EXPERIMENTAL_NS_END
71-
}} // namespace
64+
#include "dnn_common.hpp"

0 commit comments

Comments
 (0)