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
7b36e575
Commit
7b36e575
authored
Apr 20, 2018
by
Alexander Alekhin
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #11362 from dkurt:dnn_keras_deconv
parents
6f675ae7
d959d7b9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
125 additions
and
2 deletions
+125
-2
tf_graph_simplifier.cpp
modules/dnn/src/tensorflow/tf_graph_simplifier.cpp
+121
-0
tf_importer.cpp
modules/dnn/src/tensorflow/tf_importer.cpp
+2
-2
test_tf_importer.cpp
modules/dnn/test/test_tf_importer.cpp
+2
-0
No files found.
modules/dnn/src/tensorflow/tf_graph_simplifier.cpp
View file @
7b36e575
...
...
@@ -419,6 +419,125 @@ public:
}
};
class
DeconvolutionValidKerasSubgraph
:
public
Subgraph
{
public
:
DeconvolutionValidKerasSubgraph
()
{
int
input
=
addNodeToMatch
(
""
);
int
shape
=
addNodeToMatch
(
"Shape"
,
input
);
int
kernel
=
addNodeToMatch
(
"Const"
);
int
stack
=
addNodeToMatch
(
"Const"
);
int
stack_1
=
addNodeToMatch
(
"Const"
);
int
stack_2
=
addNodeToMatch
(
"Const"
);
int
strided_slice
=
addNodeToMatch
(
"StridedSlice"
,
shape
,
stack
,
stack_1
,
stack_2
);
stack
=
addNodeToMatch
(
"Const"
);
stack_1
=
addNodeToMatch
(
"Const"
);
stack_2
=
addNodeToMatch
(
"Const"
);
int
strided_slice_1
=
addNodeToMatch
(
"StridedSlice"
,
shape
,
stack
,
stack_1
,
stack_2
);
stack
=
addNodeToMatch
(
"Const"
);
stack_1
=
addNodeToMatch
(
"Const"
);
stack_2
=
addNodeToMatch
(
"Const"
);
int
strided_slice_2
=
addNodeToMatch
(
"StridedSlice"
,
shape
,
stack
,
stack_1
,
stack_2
);
int
mul
=
addNodeToMatch
(
"Mul"
,
strided_slice_1
,
addNodeToMatch
(
"Const"
));
int
add
=
addNodeToMatch
(
"Add"
,
mul
,
addNodeToMatch
(
"Const"
));
int
mul_1
=
addNodeToMatch
(
"Mul"
,
strided_slice_2
,
addNodeToMatch
(
"Const"
));
int
add_1
=
addNodeToMatch
(
"Add"
,
mul_1
,
addNodeToMatch
(
"Const"
));
int
pack
=
addNodeToMatch
(
"Pack"
,
strided_slice
,
add
,
add_1
,
addNodeToMatch
(
"Const"
));
addNodeToMatch
(
"Conv2DBackpropInput"
,
pack
,
kernel
,
input
);
// Put any unused Const op to the first input.
setFusedNode
(
"Conv2DBackpropInput"
,
stack
,
kernel
,
input
);
}
virtual
void
finalize
(
tensorflow
::
GraphDef
&
,
tensorflow
::
NodeDef
*
fusedNode
,
std
::
vector
<
tensorflow
::
NodeDef
*>&
inputNodes
)
CV_OVERRIDE
{
// Disable adjusted paddings (see Conv2DBackpropInput layer at tf_importer.cpp)
// adj_w = (outW - (pad == "SAME") ? 1 : kernelW) % strideX;
// adj_h = (outH - (pad == "SAME") ? 1 : kernelH) % strideY;
// Where outH and outW are 1st and 2nd dimensions (NHWC) or 2nd and third (NCHW).
std
::
string
padMode
=
fusedNode
->
attr
().
at
(
"padding"
).
s
();
CV_Assert
(
padMode
==
"VALID"
);
const
tensorflow
::
TensorShapeProto
&
kernelShape
=
inputNodes
[
1
]
->
mutable_attr
()
->
at
(
"value"
).
tensor
().
tensor_shape
();
CV_Assert
(
kernelShape
.
dim_size
()
==
4
);
const
int
kernelHeight
=
kernelShape
.
dim
(
0
).
size
();
const
int
kernelWidth
=
kernelShape
.
dim
(
1
).
size
();
tensorflow
::
TensorProto
*
outShape
=
inputNodes
[
0
]
->
mutable_attr
()
->
at
(
"value"
).
mutable_tensor
();
outShape
->
clear_int_val
();
outShape
->
add_int_val
(
-
1
);
outShape
->
add_int_val
(
kernelHeight
);
outShape
->
add_int_val
(
kernelWidth
);
outShape
->
add_int_val
(
-
1
);
}
};
class
DeconvolutionSameKerasSubgraph
:
public
Subgraph
{
public
:
DeconvolutionSameKerasSubgraph
()
{
int
input
=
addNodeToMatch
(
""
);
int
shape
=
addNodeToMatch
(
"Shape"
,
input
);
int
kernel
=
addNodeToMatch
(
"Const"
);
int
stack
=
addNodeToMatch
(
"Const"
);
int
stack_1
=
addNodeToMatch
(
"Const"
);
int
stack_2
=
addNodeToMatch
(
"Const"
);
int
strided_slice
=
addNodeToMatch
(
"StridedSlice"
,
shape
,
stack
,
stack_1
,
stack_2
);
stack
=
addNodeToMatch
(
"Const"
);
stack_1
=
addNodeToMatch
(
"Const"
);
stack_2
=
addNodeToMatch
(
"Const"
);
int
strided_slice_1
=
addNodeToMatch
(
"StridedSlice"
,
shape
,
stack
,
stack_1
,
stack_2
);
stack
=
addNodeToMatch
(
"Const"
);
stack_1
=
addNodeToMatch
(
"Const"
);
stack_2
=
addNodeToMatch
(
"Const"
);
int
strided_slice_2
=
addNodeToMatch
(
"StridedSlice"
,
shape
,
stack
,
stack_1
,
stack_2
);
int
mul
=
addNodeToMatch
(
"Mul"
,
strided_slice_1
,
addNodeToMatch
(
"Const"
));
int
mul_1
=
addNodeToMatch
(
"Mul"
,
strided_slice_2
,
addNodeToMatch
(
"Const"
));
int
pack
=
addNodeToMatch
(
"Pack"
,
strided_slice
,
mul
,
mul_1
,
addNodeToMatch
(
"Const"
));
addNodeToMatch
(
"Conv2DBackpropInput"
,
pack
,
kernel
,
input
);
// Put any unused Const op to the first input.
setFusedNode
(
"Conv2DBackpropInput"
,
stack
,
kernel
,
input
);
}
virtual
void
finalize
(
tensorflow
::
GraphDef
&
,
tensorflow
::
NodeDef
*
fusedNode
,
std
::
vector
<
tensorflow
::
NodeDef
*>&
inputNodes
)
CV_OVERRIDE
{
// Disable adjusted paddings (see Conv2DBackpropInput layer at tf_importer.cpp)
// adj_w = (outW - (pad == "SAME") ? 1 : kernelW) % strideX;
// adj_h = (outH - (pad == "SAME") ? 1 : kernelH) % strideY;
// Where outH and outW are 1st and 2nd dimensions (NHWC) or 2nd and third (NCHW).
std
::
string
padMode
=
fusedNode
->
attr
().
at
(
"padding"
).
s
();
CV_Assert
(
padMode
==
"SAME"
);
const
tensorflow
::
AttrValue_ListValue
&
strides
=
fusedNode
->
attr
().
at
(
"strides"
).
list
();
CV_Assert
(
strides
.
i_size
()
==
4
);
const
int
strideY
=
strides
.
i
(
1
);
const
int
strideX
=
strides
.
i
(
2
);
tensorflow
::
TensorProto
*
outShape
=
inputNodes
[
0
]
->
mutable_attr
()
->
at
(
"value"
).
mutable_tensor
();
outShape
->
clear_int_val
();
outShape
->
add_int_val
(
-
1
);
outShape
->
add_int_val
(
strideY
);
outShape
->
add_int_val
(
strideX
);
outShape
->
add_int_val
(
-
1
);
}
};
void
simplifySubgraphs
(
tensorflow
::
GraphDef
&
net
)
{
std
::
vector
<
Ptr
<
Subgraph
>
>
subgraphs
;
...
...
@@ -430,6 +549,8 @@ void simplifySubgraphs(tensorflow::GraphDef& net)
subgraphs
.
push_back
(
Ptr
<
Subgraph
>
(
new
ReLU6KerasSubgraph
()));
subgraphs
.
push_back
(
Ptr
<
Subgraph
>
(
new
ReshapeKerasSubgraph
(
3
)));
subgraphs
.
push_back
(
Ptr
<
Subgraph
>
(
new
L2NormalizeSubgraph
()));
subgraphs
.
push_back
(
Ptr
<
Subgraph
>
(
new
DeconvolutionValidKerasSubgraph
()));
subgraphs
.
push_back
(
Ptr
<
Subgraph
>
(
new
DeconvolutionSameKerasSubgraph
()));
int
numNodes
=
net
.
node_size
();
std
::
vector
<
int
>
matchedNodesIds
;
...
...
modules/dnn/src/tensorflow/tf_importer.cpp
View file @
7b36e575
...
...
@@ -1303,8 +1303,8 @@ void TFImporter::populateNet(Net dstNet)
const
int
strideY
=
layerParams
.
get
<
int
>
(
"stride_h"
);
const
int
strideX
=
layerParams
.
get
<
int
>
(
"stride_w"
);
Mat
outShape
=
getTensorContent
(
getConstBlob
(
layer
,
value_id
,
0
));
const
int
outH
=
outShape
.
at
<
int
>
(
2
);
const
int
outW
=
outShape
.
at
<
int
>
(
1
);
const
int
outH
=
outShape
.
at
<
int
>
(
1
);
const
int
outW
=
outShape
.
at
<
int
>
(
2
);
if
(
layerParams
.
get
<
String
>
(
"pad_mode"
)
==
"SAME"
)
{
layerParams
.
set
(
"adj_w"
,
(
outW
-
1
)
%
strideX
);
...
...
modules/dnn/test/test_tf_importer.cpp
View file @
7b36e575
...
...
@@ -173,6 +173,8 @@ TEST_P(Test_TensorFlow_layers, deconvolution)
runTensorFlowNet
(
"deconvolution_stride_2_same"
,
targetId
);
runTensorFlowNet
(
"deconvolution_adj_pad_valid"
,
targetId
);
runTensorFlowNet
(
"deconvolution_adj_pad_same"
,
targetId
);
runTensorFlowNet
(
"keras_deconv_valid"
,
targetId
);
runTensorFlowNet
(
"keras_deconv_same"
,
targetId
);
}
TEST_P
(
Test_TensorFlow_layers
,
matmul
)
...
...
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