Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
ec41a489
Commit
ec41a489
authored
Apr 03, 2019
by
Dmitry Kurtaev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove Switch and Merge nodes from TensorFlow networks
parent
df1f62b3
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
95 additions
and
1 deletion
+95
-1
tf_graph_simplifier.cpp
modules/dnn/src/tensorflow/tf_graph_simplifier.cpp
+80
-1
tf_graph_simplifier.hpp
modules/dnn/src/tensorflow/tf_graph_simplifier.hpp
+2
-0
tf_importer.cpp
modules/dnn/src/tensorflow/tf_importer.cpp
+3
-0
test_tf_importer.cpp
modules/dnn/test/test_tf_importer.cpp
+10
-0
No files found.
modules/dnn/src/tensorflow/tf_graph_simplifier.cpp
View file @
ec41a489
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
#ifdef HAVE_PROTOBUF
#ifdef HAVE_PROTOBUF
#include "tf_graph_simplifier.hpp"
#include "tf_graph_simplifier.hpp"
#include <queue>
namespace
cv
{
namespace
dnn
{
namespace
cv
{
namespace
dnn
{
CV__DNN_EXPERIMENTAL_NS_BEGIN
CV__DNN_EXPERIMENTAL_NS_BEGIN
...
@@ -883,7 +884,6 @@ void sortByExecutionOrder(tensorflow::GraphDef& net)
...
@@ -883,7 +884,6 @@ void sortByExecutionOrder(tensorflow::GraphDef& net)
nodesToAdd
.
pop_back
();
nodesToAdd
.
pop_back
();
permIds
.
push_back
(
nodeToAdd
);
permIds
.
push_back
(
nodeToAdd
);
// std::cout << net.node(nodeToAdd).name() << '\n';
for
(
int
i
=
0
;
i
<
edges
[
nodeToAdd
].
size
();
++
i
)
for
(
int
i
=
0
;
i
<
edges
[
nodeToAdd
].
size
();
++
i
)
{
{
...
@@ -902,6 +902,85 @@ void sortByExecutionOrder(tensorflow::GraphDef& net)
...
@@ -902,6 +902,85 @@ void sortByExecutionOrder(tensorflow::GraphDef& net)
permute
(
net
.
mutable_node
(),
permIds
);
permute
(
net
.
mutable_node
(),
permIds
);
}
}
// Remove training switches (Switch and Merge nodes and corresponding subgraphs).
void
removePhaseSwitches
(
tensorflow
::
GraphDef
&
net
)
{
std
::
vector
<
int
>
nodesToRemove
;
std
::
map
<
std
::
string
,
int
>
nodesMap
;
std
::
map
<
std
::
string
,
int
>::
iterator
nodesMapIt
;
std
::
queue
<
int
>
mergeOpSubgraphNodes
;
for
(
int
i
=
0
;
i
<
net
.
node_size
();
++
i
)
{
const
tensorflow
::
NodeDef
&
node
=
net
.
node
(
i
);
nodesMap
.
insert
(
std
::
make_pair
(
node
.
name
(),
i
));
if
(
node
.
op
()
==
"Switch"
||
node
.
op
()
==
"Merge"
)
{
CV_Assert
(
node
.
input_size
()
>
0
);
// Replace consumers' inputs.
for
(
int
j
=
0
;
j
<
net
.
node_size
();
++
j
)
{
tensorflow
::
NodeDef
*
consumer
=
net
.
mutable_node
(
j
);
for
(
int
k
=
0
;
k
<
consumer
->
input_size
();
++
k
)
{
std
::
string
inpName
=
consumer
->
input
(
k
);
inpName
=
inpName
.
substr
(
0
,
inpName
.
rfind
(
':'
));
if
(
inpName
==
node
.
name
())
{
consumer
->
set_input
(
k
,
node
.
input
(
0
));
}
}
}
nodesToRemove
.
push_back
(
i
);
if
(
node
.
op
()
==
"Merge"
)
mergeOpSubgraphNodes
.
push
(
i
);
}
}
std
::
vector
<
int
>
numConsumers
(
net
.
node_size
(),
0
);
for
(
int
i
=
0
;
i
<
net
.
node_size
();
++
i
)
{
const
tensorflow
::
NodeDef
&
node
=
net
.
node
(
i
);
for
(
int
j
=
0
;
j
<
node
.
input_size
();
++
j
)
{
std
::
string
inpName
=
node
.
input
(
j
);
inpName
=
inpName
.
substr
(
1
+
(
int
)
inpName
.
find
(
'^'
),
inpName
.
rfind
(
':'
));
nodesMapIt
=
nodesMap
.
find
(
inpName
);
CV_Assert
(
nodesMapIt
!=
nodesMap
.
end
());
numConsumers
[
nodesMapIt
->
second
]
+=
1
;
}
}
// Remove subgraphs of unused nodes which are terminated by Merge nodes.
while
(
!
mergeOpSubgraphNodes
.
empty
())
{
const
tensorflow
::
NodeDef
&
node
=
net
.
node
(
mergeOpSubgraphNodes
.
front
());
mergeOpSubgraphNodes
.
pop
();
for
(
int
i
=
0
;
i
<
node
.
input_size
();
++
i
)
{
std
::
string
inpName
=
node
.
input
(
i
);
inpName
=
inpName
.
substr
(
1
+
(
int
)
inpName
.
find
(
'^'
),
inpName
.
rfind
(
':'
));
nodesMapIt
=
nodesMap
.
find
(
inpName
);
CV_Assert
(
nodesMapIt
!=
nodesMap
.
end
());
int
inpNodeId
=
nodesMapIt
->
second
;
if
(
numConsumers
[
inpNodeId
]
==
1
)
{
mergeOpSubgraphNodes
.
push
(
inpNodeId
);
nodesToRemove
.
push_back
(
inpNodeId
);
}
else
if
(
numConsumers
[
inpNodeId
]
>
0
)
numConsumers
[
inpNodeId
]
-=
1
;
}
}
std
::
sort
(
nodesToRemove
.
begin
(),
nodesToRemove
.
end
());
for
(
int
i
=
nodesToRemove
.
size
()
-
1
;
i
>=
0
;
--
i
)
{
if
(
nodesToRemove
[
i
]
<
net
.
node_size
())
// Ids might be repeated.
net
.
mutable_node
()
->
DeleteSubrange
(
nodesToRemove
[
i
],
1
);
}
}
CV__DNN_EXPERIMENTAL_NS_END
CV__DNN_EXPERIMENTAL_NS_END
}}
// namespace dnn, namespace cv
}}
// namespace dnn, namespace cv
...
...
modules/dnn/src/tensorflow/tf_graph_simplifier.hpp
View file @
ec41a489
...
@@ -27,6 +27,8 @@ void releaseTensor(tensorflow::TensorProto* tensor);
...
@@ -27,6 +27,8 @@ void releaseTensor(tensorflow::TensorProto* tensor);
void
sortByExecutionOrder
(
tensorflow
::
GraphDef
&
net
);
void
sortByExecutionOrder
(
tensorflow
::
GraphDef
&
net
);
void
removePhaseSwitches
(
tensorflow
::
GraphDef
&
net
);
CV__DNN_EXPERIMENTAL_NS_END
CV__DNN_EXPERIMENTAL_NS_END
}}
// namespace dnn, namespace cv
}}
// namespace dnn, namespace cv
...
...
modules/dnn/src/tensorflow/tf_importer.cpp
View file @
ec41a489
...
@@ -657,6 +657,9 @@ static int predictOutputDataLayout(const tensorflow::GraphDef& net,
...
@@ -657,6 +657,9 @@ static int predictOutputDataLayout(const tensorflow::GraphDef& net,
void
TFImporter
::
populateNet
(
Net
dstNet
)
void
TFImporter
::
populateNet
(
Net
dstNet
)
{
{
if
(
!
netTxt
.
ByteSize
())
removePhaseSwitches
(
netBin
);
RemoveIdentityOps
(
netBin
);
RemoveIdentityOps
(
netBin
);
RemoveIdentityOps
(
netTxt
);
RemoveIdentityOps
(
netTxt
);
...
...
modules/dnn/test/test_tf_importer.cpp
View file @
ec41a489
...
@@ -185,6 +185,16 @@ TEST_P(Test_TensorFlow_layers, batch_norm)
...
@@ -185,6 +185,16 @@ TEST_P(Test_TensorFlow_layers, batch_norm)
runTensorFlowNet
(
"mvn_batch_norm_1x1"
);
runTensorFlowNet
(
"mvn_batch_norm_1x1"
);
}
}
TEST_P
(
Test_TensorFlow_layers
,
slim_batch_norm
)
{
if
(
backend
==
DNN_BACKEND_INFERENCE_ENGINE
)
throw
SkipTestException
(
"Test is disabled for DLIE"
);
// Output values range: [-40.0597, 207.827]
double
l1
=
(
target
==
DNN_TARGET_OPENCL_FP16
||
target
==
DNN_TARGET_MYRIAD
)
?
0.041
:
default_l1
;
double
lInf
=
(
target
==
DNN_TARGET_OPENCL_FP16
||
target
==
DNN_TARGET_MYRIAD
)
?
0.33
:
default_lInf
;
runTensorFlowNet
(
"slim_batch_norm"
,
false
,
l1
,
lInf
);
}
TEST_P
(
Test_TensorFlow_layers
,
pooling
)
TEST_P
(
Test_TensorFlow_layers
,
pooling
)
{
{
runTensorFlowNet
(
"max_pool_even"
);
runTensorFlowNet
(
"max_pool_even"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment