|
|
|
@ -93,6 +93,7 @@ class LSTMLayerImpl CV_FINAL : public LSTMLayer |
|
|
|
|
float forgetBias, cellClip; |
|
|
|
|
bool useCellClip, usePeephole; |
|
|
|
|
bool reverse; // If true, go in negative direction along the time axis
|
|
|
|
|
bool bidirectional; // If true, produces both forward and reversed directions along time axis
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
|
@ -101,6 +102,7 @@ public: |
|
|
|
|
{ |
|
|
|
|
setParamsFrom(params); |
|
|
|
|
|
|
|
|
|
bidirectional = params.get<bool>("bidirectional", false); |
|
|
|
|
if (!blobs.empty()) |
|
|
|
|
{ |
|
|
|
|
CV_Assert(blobs.size() >= 3); |
|
|
|
@ -113,7 +115,7 @@ public: |
|
|
|
|
CV_CheckEQ(Wh.dims, 2, ""); |
|
|
|
|
CV_CheckEQ(Wx.dims, 2, ""); |
|
|
|
|
CV_CheckEQ(Wh.rows, Wx.rows, ""); |
|
|
|
|
CV_CheckEQ(Wh.rows, 4*Wh.cols, ""); |
|
|
|
|
CV_CheckEQ(Wh.rows, (1 + static_cast<int>(bidirectional))*4*Wh.cols, ""); |
|
|
|
|
CV_CheckEQ(Wh.rows, (int)bias.total(), ""); |
|
|
|
|
CV_Assert(Wh.type() == Wx.type() && Wx.type() == bias.type()); |
|
|
|
|
|
|
|
|
@ -136,6 +138,7 @@ public: |
|
|
|
|
useCellClip = params.get<bool>("use_cell_clip", false); |
|
|
|
|
usePeephole = params.get<bool>("use_peephole", false); |
|
|
|
|
reverse = params.get<bool>("reverse", false); |
|
|
|
|
CV_Assert(!reverse || !bidirectional); |
|
|
|
|
|
|
|
|
|
allocated = false; |
|
|
|
|
outTailShape.clear(); |
|
|
|
@ -207,6 +210,7 @@ public: |
|
|
|
|
|
|
|
|
|
outResShape.push_back(_numSamples); |
|
|
|
|
outResShape.insert(outResShape.end(), outTailShape_.begin(), outTailShape_.end()); |
|
|
|
|
outResShape.back() *= (1 + static_cast<int>(bidirectional)); |
|
|
|
|
|
|
|
|
|
size_t noutputs = produceCellOutput ? 2 : 1; |
|
|
|
|
outputs.assign(noutputs, outResShape); |
|
|
|
@ -253,6 +257,7 @@ public: |
|
|
|
|
outTsShape.clear(); |
|
|
|
|
outTsShape.push_back(numSamples); |
|
|
|
|
outTsShape.insert(outTsShape.end(), outTailShape.begin(), outTailShape.end()); |
|
|
|
|
outTsShape.back() *= (1 + static_cast<int>(bidirectional)); |
|
|
|
|
|
|
|
|
|
allocated = true; |
|
|
|
|
} |
|
|
|
@ -273,9 +278,12 @@ public: |
|
|
|
|
outputs_arr.getMatVector(output); |
|
|
|
|
internals_arr.getMatVector(internals); |
|
|
|
|
|
|
|
|
|
const Mat &Wh = blobs[0]; |
|
|
|
|
const Mat &Wx = blobs[1]; |
|
|
|
|
const Mat &bias = blobs[2]; |
|
|
|
|
const int numDirs = 1 + static_cast<int>(bidirectional); |
|
|
|
|
for (int i = 0; i < numDirs; ++i) |
|
|
|
|
{ |
|
|
|
|
const Mat &Wh = blobs[0].rowRange(i * blobs[0].rows / numDirs, (i + 1) * blobs[0].rows / numDirs); |
|
|
|
|
const Mat &Wx = blobs[1].rowRange(i * blobs[1].rows / numDirs, (i + 1) * blobs[1].rows / numDirs); |
|
|
|
|
const Mat &bias = blobs[2].colRange(i * blobs[2].cols / numDirs, (i + 1) * blobs[2].cols / numDirs); |
|
|
|
|
|
|
|
|
|
int numOut = Wh.size[1]; |
|
|
|
|
|
|
|
|
@ -289,10 +297,11 @@ public: |
|
|
|
|
Mat xTs = input[0].reshape(1, numSamplesTotal); |
|
|
|
|
|
|
|
|
|
Mat hOutTs = output[0].reshape(1, numSamplesTotal); |
|
|
|
|
hOutTs = hOutTs.colRange(i * hOutTs.cols / numDirs, (i + 1) * hOutTs.cols / numDirs); |
|
|
|
|
Mat cOutTs = produceCellOutput ? output[1].reshape(1, numSamplesTotal) : Mat(); |
|
|
|
|
|
|
|
|
|
int tsStart, tsEnd, tsInc; |
|
|
|
|
if (reverse) { |
|
|
|
|
if (reverse || i == 1) { |
|
|
|
|
tsStart = numTimeStamps - 1; |
|
|
|
|
tsEnd = -1; |
|
|
|
|
tsInc = -1; |
|
|
|
@ -360,6 +369,7 @@ public: |
|
|
|
|
cInternal.copyTo(cOutTs.rowRange(curRowRange)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Ptr<LSTMLayer> LSTMLayer::create(const LayerParams& params) |
|
|
|
|