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
b88435fd
Commit
b88435fd
authored
Sep 25, 2019
by
Andrew Ryrie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dnn: Allow LSTM layer to operate in reverse direction
This is useful for bidirectional LSTMs.
parent
3289a0af
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
1 deletion
+63
-1
recurrent_layers.cpp
modules/dnn/src/layers/recurrent_layers.cpp
+14
-1
test_layers.cpp
modules/dnn/test/test_layers.cpp
+49
-0
No files found.
modules/dnn/src/layers/recurrent_layers.cpp
View file @
b88435fd
...
...
@@ -92,6 +92,7 @@ class LSTMLayerImpl CV_FINAL : public LSTMLayer
bool
produceCellOutput
;
float
forgetBias
,
cellClip
;
bool
useCellClip
,
usePeephole
;
bool
reverse
;
// If true, go in negative direction along the time axis
public
:
...
...
@@ -133,6 +134,7 @@ public:
cellClip
=
params
.
get
<
float
>
(
"cell_clip"
,
0.0
f
);
useCellClip
=
params
.
get
<
bool
>
(
"use_cell_clip"
,
false
);
usePeephole
=
params
.
get
<
bool
>
(
"use_peephole"
,
false
);
reverse
=
params
.
get
<
bool
>
(
"reverse"
,
false
);
allocated
=
false
;
outTailShape
.
clear
();
...
...
@@ -288,7 +290,18 @@ public:
Mat
hOutTs
=
output
[
0
].
reshape
(
1
,
numSamplesTotal
);
Mat
cOutTs
=
produceCellOutput
?
output
[
1
].
reshape
(
1
,
numSamplesTotal
)
:
Mat
();
for
(
int
ts
=
0
;
ts
<
numTimeStamps
;
ts
++
)
int
tsStart
,
tsEnd
,
tsInc
;
if
(
reverse
)
{
tsStart
=
numTimeStamps
-
1
;
tsEnd
=
-
1
;
tsInc
=
-
1
;
}
else
{
tsStart
=
0
;
tsEnd
=
numTimeStamps
;
tsInc
=
1
;
}
for
(
int
ts
=
tsStart
;
ts
!=
tsEnd
;
ts
+=
tsInc
)
{
Range
curRowRange
(
ts
*
numSamples
,
(
ts
+
1
)
*
numSamples
);
Mat
xCurr
=
xTs
.
rowRange
(
curRowRange
);
...
...
modules/dnn/test/test_layers.cpp
View file @
b88435fd
...
...
@@ -489,6 +489,55 @@ TEST(Layer_RNN_Test_Accuracy_with_, CaffeRecurrent)
normAssert
(
h_ref
,
output
[
0
]);
}
TEST
(
Layer_LSTM_Test_Accuracy_
,
Reverse
)
{
// This handcrafted setup calculates (approximately) the prefix sum of the
// input, assuming the inputs are suitably small.
cv
::
Mat
input
(
2
,
1
,
CV_32FC1
);
input
.
at
<
float
>
(
0
,
0
)
=
1e-5
f
;
input
.
at
<
float
>
(
1
,
0
)
=
2e-5
f
;
cv
::
Mat
Wx
(
4
,
1
,
CV_32FC1
);
Wx
.
at
<
float
>
(
0
,
0
)
=
0.
f
;
// Input gate
Wx
.
at
<
float
>
(
1
,
0
)
=
0.
f
;
// Forget gate
Wx
.
at
<
float
>
(
2
,
0
)
=
0.
f
;
// Output gate
Wx
.
at
<
float
>
(
3
,
0
)
=
1.
f
;
// Update signal
cv
::
Mat
Wh
(
4
,
1
,
CV_32FC1
);
Wh
.
at
<
float
>
(
0
,
0
)
=
0.
f
;
// Input gate
Wh
.
at
<
float
>
(
1
,
0
)
=
0.
f
;
// Forget gate
Wh
.
at
<
float
>
(
2
,
0
)
=
0.
f
;
// Output gate
Wh
.
at
<
float
>
(
3
,
0
)
=
0.
f
;
// Update signal
cv
::
Mat
bias
(
4
,
1
,
CV_32FC1
);
bias
.
at
<
float
>
(
0
,
0
)
=
1e10
f
;
// Input gate - always allows input to c
bias
.
at
<
float
>
(
1
,
0
)
=
1e10
f
;
// Forget gate - never forget anything on c
bias
.
at
<
float
>
(
2
,
0
)
=
1e10
f
;
// Output gate - always output everything
bias
.
at
<
float
>
(
3
,
0
)
=
0.
f
;
// Update signal
LayerParams
lp
;
lp
.
set
(
"reverse"
,
true
);
lp
.
set
(
"use_timestamp_dim"
,
true
);
lp
.
blobs
.
clear
();
lp
.
blobs
.
push_back
(
Wh
);
lp
.
blobs
.
push_back
(
Wx
);
lp
.
blobs
.
push_back
(
bias
);
cv
::
Ptr
<
cv
::
dnn
::
LSTMLayer
>
layer
=
LSTMLayer
::
create
(
lp
);
std
::
vector
<
cv
::
Mat
>
outputs
;
std
::
vector
<
cv
::
Mat
>
inputs
;
inputs
.
push_back
(
input
);
runLayer
(
layer
,
inputs
,
outputs
);
ASSERT_EQ
(
1
,
outputs
.
size
());
cv
::
Mat
out
=
outputs
[
0
];
ASSERT_EQ
(
3
,
out
.
dims
);
ASSERT_EQ
(
shape
(
2
,
1
,
1
),
shape
(
out
));
float
*
data
=
reinterpret_cast
<
float
*>
(
out
.
data
);
EXPECT_NEAR
(
std
::
tanh
(
1e-5
f
)
+
std
::
tanh
(
2e-5
f
),
data
[
0
],
1e-10
);
EXPECT_NEAR
(
std
::
tanh
(
2e-5
f
),
data
[
1
],
1e-10
);
}
class
Layer_RNN_Test
:
public
::
testing
::
Test
{
...
...
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