OpenCV 4.12.0
开源计算机视觉
加载中...
搜索中...
无匹配项
cv::dnn::LSTMLayer 类参考abstract

LSTM 循环层。更多...

#include <opencv2/dnn/all_layers.hpp>

cv::dnn::LSTMLayer 协作图

公共成员函数

int inputNameToIndex (String inputName) CV_OVERRIDE
 返回输入 blob 在输入数组中的索引。
 
int outputNameToIndex (const String &outputName) CV_OVERRIDE
 返回输出 blob 在输出数组中的索引。
 
virtual void setOutShape (const MatShape &outTailShape=MatShape())=0
 指定输出 blob 的形状,其将为 [[T], N] + outTailShape
 
virtual void setProduceCellOutput (bool produce=false)=0
 如果此标志设置为 true,则层将生成 \( c_t \) 作为第二个输出。
 
virtual void setUseTimstampsDim (bool use=true)=0
 指定是将输入 blob 的第一维度解释为时间戳维度还是样本维度。
 
virtual void setWeights (const Mat &Wh, const Mat &Wx, const Mat &b)=0
 为 LSTM 层设置已训练的权重。
 
- 继承自 cv::dnn::Layer 的公共成员函数
 Layer ()
 
 Layer (const LayerParams &params)
 仅初始化 nametypeblobs 字段。
 
virtual ~Layer ()
 
virtual void applyHalideScheduler (Ptr< BackendNode > &node, const std::vector< Mat * > &inputs, const std::vector< Mat > &outputs, int targetId) const
 基于层超参数的 Halide 自动调度。
 
virtual void finalize (const std::vector< Mat * > &input, std::vector< Mat > &output)
 根据输入、输出和 blob 计算并设置内部参数。
 
std::vector< Matfinalize (const std::vector< Mat > &inputs)
 这是一个重载成员函数,为方便起见而提供。它与上述函数的区别仅在于它接受的参数。
 
void finalize (const std::vector< Mat > &inputs, std::vector< Mat > &outputs)
 这是一个重载成员函数,为方便起见而提供。它与上述函数的区别仅在于它接受的参数。
 
virtual void finalize (InputArrayOfArrays inputs, OutputArrayOfArrays outputs)
 根据输入、输出和 blob 计算并设置内部参数。
 
virtual void forward (InputArrayOfArrays inputs, OutputArrayOfArrays outputs, OutputArrayOfArrays internals)
 给定 input blobs,计算输出 blobs
 
virtual void forward (std::vector< Mat * > &input, std::vector< Mat > &output, std::vector< Mat > &internals)
 给定 input blobs,计算输出 blobs
 
void forward_fallback (InputArrayOfArrays inputs, OutputArrayOfArrays outputs, OutputArrayOfArrays internals)
 给定 input blobs,计算输出 blobs
 
virtual int64 getFLOPS (const std::vector< MatShape > &inputs, const std::vector< MatShape > &outputs) const
 
virtual bool getMemoryShapes (const std::vector< MatShape > &inputs, const int requiredOutputs, std::vector< MatShape > &outputs, std::vector< MatShape > &internals) const
 
virtual void getScaleShift (Mat &scale, Mat &shift) const
 返回具有逐通道乘法和加法操作的层的参数。
 
virtual void getScaleZeropoint (float &scale, int &zeropoint) const
 返回层的缩放和零点。
 
virtual Ptr< BackendNodeinitCann (const std::vector< Ptr< BackendWrapper > > &inputs, const std::vector< Ptr< BackendWrapper > > &outputs, const std::vector< Ptr< BackendNode > > &nodes)
 返回一个 CANN 后端节点。
 
virtual Ptr< BackendNodeinitCUDA (void *context, const std::vector< Ptr< BackendWrapper > > &inputs, const std::vector< Ptr< BackendWrapper > > &outputs)
 返回一个 CUDA 后端节点。
 
virtual Ptr< BackendNodeinitHalide (const std::vector< Ptr< BackendWrapper > > &inputs)
 返回 Halide 后端节点。
 
virtual Ptr< BackendNodeinitNgraph (const std::vector< Ptr< BackendWrapper > > &inputs, const std::vector< Ptr< BackendNode > > &nodes)
 
virtual Ptr< BackendNodeinitTimVX (void *timVxInfo, const std::vector< Ptr< BackendWrapper > > &inputsWrapper, const std::vector< Ptr< BackendWrapper > > &outputsWrapper, bool isLast)
 返回一个 TimVX 后端节点。
 
virtual Ptr< BackendNodeinitVkCom (const std::vector< Ptr< BackendWrapper > > &inputs, std::vector< Ptr< BackendWrapper > > &outputs)
 
virtual Ptr< BackendNodeinitWebnn (const std::vector< Ptr< BackendWrapper > > &inputs, const std::vector< Ptr< BackendNode > > &nodes)
 
void run (const std::vector< Mat > &inputs, std::vector< Mat > &outputs, std::vector< Mat > &internals)
 分配层并计算输出。
 
virtual bool setActivation (const Ptr< ActivationLayer > &layer)
 尝试将后续激活层附加到当前层,即在部分情况下进行层融合。
 
void setParamsFrom (const LayerParams &params)
 仅初始化 nametypeblobs 字段。
 
virtual bool supportBackend (int backendId)
 询问层是否支持特定的后端进行计算。
 
virtual Ptr< BackendNodetryAttach (const Ptr< BackendNode > &node)
 实现层融合。
 
virtual bool tryFuse (Ptr< Layer > &top)
 尝试将当前层与下一层融合。
 
virtual bool tryQuantize (const std::vector< std::vector< float > > &scales, const std::vector< std::vector< int > > &zeropoints, LayerParams &params)
 尝试量化给定层并计算定点实现所需的量化参数。
 
virtual void unsetAttached ()
 “分离”所有附加到特定层的层。
 
virtual bool updateMemoryShapes (const std::vector< MatShape > &inputs)
 
- 继承自 cv::Algorithm 的公共成员函数
 Algorithm ()
 
virtual ~Algorithm ()
 
virtual void clear ()
 清除算法状态。
 
virtual bool empty () const
 如果 Algorithm 为空(例如在最开始或读取不成功后),则返回 true。
 
virtual String getDefaultName () const
 
virtual void read (const FileNode &fn)
 从文件存储中读取算法参数。
 
virtual void save (const String &filename) const
 
void write (const Ptr< FileStorage > &fs, const String &name=String()) const
 
virtual void write (FileStorage &fs) const
 将算法参数存储到文件存储中。
 
void write (FileStorage &fs, const String &name) const
 

静态公共成员函数

static Ptr< LSTMLayercreate (const LayerParams &params)
 
- 继承自 cv::Algorithm 的静态公共成员函数
template<typename _Tp >
static Ptr< _Tpload (const String &filename, const String &objname=String())
 从文件中加载算法。
 
template<typename _Tp >
static Ptr< _TploadFromString (const String &strModel, const String &objname=String())
 从字符串加载算法。
 
template<typename _Tp >
static Ptr< _Tpread (const FileNode &fn)
 从文件节点读取算法。
 

额外继承的成员

- 继承自 cv::dnn::Layer 的公共属性
std::vector< Matblobs
 学习到的参数列表必须存储在此处,以便使用 Net::getParam() 读取。
 
String name
 层实例的名称,可用于日志记录或其他内部目的。
 
int preferableTarget
 层转发的首选目标
 
String type
 通过层工厂创建层时使用的类型名称。
 
- 继承自 cv::Algorithm 的保护成员函数
void writeFormat (FileStorage &fs) const
 

详细描述

LSTM 循环层。

成员函数文档

◆ create()

static Ptr< LSTMLayer > cv::dnn::LSTMLayer::create ( const LayerParams & params)
static

创建 LSTM 层实例

◆ inputNameToIndex()

int cv::dnn::LSTMLayer::inputNameToIndex ( String inputName)
virtual

返回输入 blob 在输入数组中的索引。

参数
inputName输入 blob 的标签

每个层的输入和输出都可以标记,以便使用“%<layer_name%>[.output_name]”符号轻松识别它们。此方法将输入 blob 的标签映射到其在输入向量中的索引。

重新实现自 cv::dnn::Layer

◆ outputNameToIndex()

int cv::dnn::LSTMLayer::outputNameToIndex ( const String & outputName)
virtual

返回输出 blob 在输出数组中的索引。

另请参见
inputNameToIndex()

重新实现自 cv::dnn::Layer

◆ setOutShape()

virtual void cv::dnn::LSTMLayer::setOutShape ( const MatShape & outTailShape = MatShape())
纯虚函数

指定输出 blob 的形状,其将为 [[T], N] + outTailShape

如果此参数为空或未设置,则将使用 outTailShape = [Wh.size(0)],其中 Wh 是来自 setWeights() 的参数。

◆ setProduceCellOutput()

virtual void cv::dnn::LSTMLayer::setProduceCellOutput ( bool produce = false)
纯虚函数

如果此标志设置为 true,则层将生成 \( c_t \) 作为第二个输出。

已弃用
LayerParams 中使用标志 use_timestamp_dim

第二个输出的形状与第一个输出相同。

◆ setUseTimstampsDim()

virtual void cv::dnn::LSTMLayer::setUseTimstampsDim ( bool use = true)
纯虚函数

指定是将输入 blob 的第一维度解释为时间戳维度还是样本维度。

已弃用
LayerParams 中使用标志 produce_cell_output

如果标志设置为 true,则输入 blob 的形状将被解释为 [T, N, [data dims]],其中 T 指定时间戳的数量,N 是独立流的数量。在这种情况下,每次 forward() 调用将遍历 T 个时间戳并更新层的状态 T 次。

如果标志设置为 false,则输入 blob 的形状将被解释为 [N, [data dims]]。在这种情况下,每次 forward() 调用将进行一次迭代并生成一个形状为 [N, [out dims]] 的时间戳。

◆ setWeights()

virtual void cv::dnn::LSTMLayer::setWeights ( const Mat & Wh,
const Mat & Wx,
const Mat & b )
纯虚函数

为 LSTM 层设置已训练的权重。

已弃用
请改用 LayerParams::blobs

LSTM 在每个步骤的行为由当前输入、前一个输出、前一个单元状态和学习到的权重定义。

设 \(x_t\) 为当前输入,\(h_t\) 为当前输出,\(c_t\) 为当前状态。则当前输出和当前单元状态计算如下:

\begin{eqnarray*} h_t &= o_t \odot tanh(c_t), \\ c_t &= f_t \odot c_{t-1} + i_t \odot g_t, \\ \end{eqnarray*}

其中 \(\odot\) 是逐元素乘法运算,\(i_t, f_t, o_t, g_t\) 是使用学习到的权重计算的内部门。

门的计算方式如下:

\begin{eqnarray*} i_t &= sigmoid&(W_{xi} x_t + W_{hi} h_{t-1} + b_i), \\ f_t &= sigmoid&(W_{xf} x_t + W_{hf} h_{t-1} + b_f), \\ o_t &= sigmoid&(W_{xo} x_t + W_{ho} h_{t-1} + b_o), \\ g_t &= tanh &(W_{xg} x_t + W_{hg} h_{t-1} + b_g), \\ \end{eqnarray*}

其中 \(W_{x?}\)、\(W_{h?}\) 和 \(b_{?}\) 是以矩阵形式表示的学习权重:\(W_{x?} \in R^{N_h \times N_x}\),\(W_{h?} \in R^{N_h \times N_h}\),\(b_? \in R^{N_h}\)。

为了简化和性能目的,我们使用 \( W_x = [W_{xi}; W_{xf}; W_{xo}, W_{xg}] \)(即 \(W_x\) 是 \( W_{x?} \) 的垂直拼接),\( W_x \in R^{4N_h \times N_x} \)。对于 \( W_h = [W_{hi}; W_{hf}; W_{ho}, W_{hg}] \),\( W_h \in R^{4N_h \times N_h} \) 和对于 \( b = [b_i; b_f, b_o, b_g]\),\(b \in R^{4N_h} \) 也类似。

参数
Wh是定义前一个输出如何转换为内部门的矩阵(即根据上述符号表示为 \( W_h \))
Wx是定义当前输入如何转换为内部门的矩阵(即根据上述符号表示为 \( W_x \))
b是偏置向量(即根据上述符号表示为 \( b \))

此类的文档由以下文件生成: