all_layers.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #ifndef OPENCV_DNN_DNN_ALL_LAYERS_HPP
  42. #define OPENCV_DNN_DNN_ALL_LAYERS_HPP
  43. #include <opencv2/dnn.hpp>
  44. namespace cv {
  45. namespace dnn {
  46. CV__DNN_INLINE_NS_BEGIN
  47. //! @addtogroup dnn
  48. //! @{
  49. /** @defgroup dnnLayerList Partial List of Implemented Layers
  50. @{
  51. This subsection of dnn module contains information about built-in layers and their descriptions.
  52. Classes listed here, in fact, provides C++ API for creating instances of built-in layers.
  53. In addition to this way of layers instantiation, there is a more common factory API (see @ref dnnLayerFactory), it allows to create layers dynamically (by name) and register new ones.
  54. You can use both API, but factory API is less convenient for native C++ programming and basically designed for use inside importers (see @ref readNetFromCaffe(), @ref readNetFromTorch(), @ref readNetFromTensorflow()).
  55. Built-in layers partially reproduce functionality of corresponding Caffe and Torch7 layers.
  56. In particular, the following layers and Caffe importer were tested to reproduce <a href="http://caffe.berkeleyvision.org/tutorial/layers.html">Caffe</a> functionality:
  57. - Convolution
  58. - Deconvolution
  59. - Pooling
  60. - InnerProduct
  61. - TanH, ReLU, Sigmoid, BNLL, Power, AbsVal
  62. - Softmax
  63. - Reshape, Flatten, Slice, Split
  64. - LRN
  65. - MVN
  66. - Dropout (since it does nothing on forward pass -))
  67. */
  68. class CV_EXPORTS BlankLayer : public Layer
  69. {
  70. public:
  71. static Ptr<Layer> create(const LayerParams &params);
  72. };
  73. /**
  74. * Constant layer produces the same data blob at an every forward pass.
  75. */
  76. class CV_EXPORTS ConstLayer : public Layer
  77. {
  78. public:
  79. static Ptr<Layer> create(const LayerParams &params);
  80. };
  81. //! LSTM recurrent layer
  82. class CV_EXPORTS LSTMLayer : public Layer
  83. {
  84. public:
  85. /** Creates instance of LSTM layer */
  86. static Ptr<LSTMLayer> create(const LayerParams& params);
  87. /** @deprecated Use LayerParams::blobs instead.
  88. @brief Set trained weights for LSTM layer.
  89. LSTM behavior on each step is defined by current input, previous output, previous cell state and learned weights.
  90. Let @f$x_t@f$ be current input, @f$h_t@f$ be current output, @f$c_t@f$ be current state.
  91. Than current output and current cell state is computed as follows:
  92. @f{eqnarray*}{
  93. h_t &= o_t \odot tanh(c_t), \\
  94. c_t &= f_t \odot c_{t-1} + i_t \odot g_t, \\
  95. @f}
  96. where @f$\odot@f$ is per-element multiply operation and @f$i_t, f_t, o_t, g_t@f$ is internal gates that are computed using learned weights.
  97. Gates are computed as follows:
  98. @f{eqnarray*}{
  99. i_t &= sigmoid&(W_{xi} x_t + W_{hi} h_{t-1} + b_i), \\
  100. f_t &= sigmoid&(W_{xf} x_t + W_{hf} h_{t-1} + b_f), \\
  101. o_t &= sigmoid&(W_{xo} x_t + W_{ho} h_{t-1} + b_o), \\
  102. g_t &= tanh &(W_{xg} x_t + W_{hg} h_{t-1} + b_g), \\
  103. @f}
  104. where @f$W_{x?}@f$, @f$W_{h?}@f$ and @f$b_{?}@f$ are learned weights represented as matrices:
  105. @f$W_{x?} \in R^{N_h \times N_x}@f$, @f$W_{h?} \in R^{N_h \times N_h}@f$, @f$b_? \in R^{N_h}@f$.
  106. For simplicity and performance purposes we use @f$ W_x = [W_{xi}; W_{xf}; W_{xo}, W_{xg}] @f$
  107. (i.e. @f$W_x@f$ is vertical concatenation of @f$ W_{x?} @f$), @f$ W_x \in R^{4N_h \times N_x} @f$.
  108. The same for @f$ W_h = [W_{hi}; W_{hf}; W_{ho}, W_{hg}], W_h \in R^{4N_h \times N_h} @f$
  109. and for @f$ b = [b_i; b_f, b_o, b_g]@f$, @f$b \in R^{4N_h} @f$.
  110. @param Wh is matrix defining how previous output is transformed to internal gates (i.e. according to above mentioned notation is @f$ W_h @f$)
  111. @param Wx is matrix defining how current input is transformed to internal gates (i.e. according to above mentioned notation is @f$ W_x @f$)
  112. @param b is bias vector (i.e. according to above mentioned notation is @f$ b @f$)
  113. */
  114. CV_DEPRECATED virtual void setWeights(const Mat &Wh, const Mat &Wx, const Mat &b) = 0;
  115. /** @brief Specifies shape of output blob which will be [[`T`], `N`] + @p outTailShape.
  116. * @details If this parameter is empty or unset then @p outTailShape = [`Wh`.size(0)] will be used,
  117. * where `Wh` is parameter from setWeights().
  118. */
  119. virtual void setOutShape(const MatShape &outTailShape = MatShape()) = 0;
  120. /** @deprecated Use flag `produce_cell_output` in LayerParams.
  121. * @brief Specifies either interpret first dimension of input blob as timestamp dimension either as sample.
  122. *
  123. * If flag is set to true then shape of input blob will be interpreted as [`T`, `N`, `[data dims]`] where `T` specifies number of timestamps, `N` is number of independent streams.
  124. * In this case each forward() call will iterate through `T` timestamps and update layer's state `T` times.
  125. *
  126. * If flag is set to false then shape of input blob will be interpreted as [`N`, `[data dims]`].
  127. * In this case each forward() call will make one iteration and produce one timestamp with shape [`N`, `[out dims]`].
  128. */
  129. CV_DEPRECATED virtual void setUseTimstampsDim(bool use = true) = 0;
  130. /** @deprecated Use flag `use_timestamp_dim` in LayerParams.
  131. * @brief If this flag is set to true then layer will produce @f$ c_t @f$ as second output.
  132. * @details Shape of the second output is the same as first output.
  133. */
  134. CV_DEPRECATED virtual void setProduceCellOutput(bool produce = false) = 0;
  135. /* In common case it use single input with @f$x_t@f$ values to compute output(s) @f$h_t@f$ (and @f$c_t@f$).
  136. * @param input should contain packed values @f$x_t@f$
  137. * @param output contains computed outputs: @f$h_t@f$ (and @f$c_t@f$ if setProduceCellOutput() flag was set to true).
  138. *
  139. * If setUseTimstampsDim() is set to true then @p input[0] should has at least two dimensions with the following shape: [`T`, `N`, `[data dims]`],
  140. * where `T` specifies number of timestamps, `N` is number of independent streams (i.e. @f$ x_{t_0 + t}^{stream} @f$ is stored inside @p input[0][t, stream, ...]).
  141. *
  142. * If setUseTimstampsDim() is set to false then @p input[0] should contain single timestamp, its shape should has form [`N`, `[data dims]`] with at least one dimension.
  143. * (i.e. @f$ x_{t}^{stream} @f$ is stored inside @p input[0][stream, ...]).
  144. */
  145. int inputNameToIndex(String inputName) CV_OVERRIDE;
  146. int outputNameToIndex(const String& outputName) CV_OVERRIDE;
  147. };
  148. /** @brief GRU recurrent one-layer
  149. *
  150. * Accepts input sequence and computes the final hidden state for each element in the batch.
  151. *
  152. * - input[0] containing the features of the input sequence.
  153. * input[0] should have shape [`T`, `N`, `data_dims`] where `T` is sequence length, `N` is batch size, `data_dims` is input size
  154. * - output would have shape [`T`, `N`, `D` * `hidden_size`] where `D = 2` if layer is bidirectional otherwise `D = 1`
  155. *
  156. * Depends on the following attributes:
  157. * - hidden_size - Number of neurons in the hidden layer
  158. * - direction - RNN could be bidirectional or forward
  159. *
  160. * The final hidden state @f$ h_t @f$ computes by the following formulas:
  161. *
  162. @f{eqnarray*}{
  163. r_t = \sigma(W_{ir} x_t + b_{ir} + W_{hr} h_{(t-1)} + b_{hr}) \\
  164. z_t = \sigma(W_{iz} x_t + b_{iz} + W_{hz} h_{(t-1)} + b_{hz}) \\
  165. n_t = \tanh(W_{in} x_t + b_{in} + r_t \odot (W_{hn} h_{(t-1)}+ b_{hn})) \\
  166. h_t = (1 - z_t) \odot n_t + z_t \odot h_{(t-1)} \\
  167. @f}
  168. * Where @f$x_t@f$ is current input, @f$h_{(t-1)}@f$ is previous or initial hidden state.
  169. *
  170. * @f$W_{x?}@f$, @f$W_{h?}@f$ and @f$b_{?}@f$ are learned weights represented as matrices:
  171. * @f$W_{x?} \in R^{N_h \times N_x}@f$, @f$W_{h?} \in R^{N_h \times N_h}@f$, @f$b_? \in R^{N_h}@f$.
  172. *
  173. * @f$\odot@f$ is per-element multiply operation.
  174. */
  175. class CV_EXPORTS GRULayer : public Layer
  176. {
  177. public:
  178. /** Creates instance of GRU layer */
  179. static Ptr<GRULayer> create(const LayerParams& params);
  180. };
  181. /** @brief Classical recurrent layer
  182. Accepts two inputs @f$x_t@f$ and @f$h_{t-1}@f$ and compute two outputs @f$o_t@f$ and @f$h_t@f$.
  183. - input: should contain packed input @f$x_t@f$.
  184. - output: should contain output @f$o_t@f$ (and @f$h_t@f$ if setProduceHiddenOutput() is set to true).
  185. input[0] should have shape [`T`, `N`, `data_dims`] where `T` and `N` is number of timestamps and number of independent samples of @f$x_t@f$ respectively.
  186. output[0] will have shape [`T`, `N`, @f$N_o@f$], where @f$N_o@f$ is number of rows in @f$ W_{xo} @f$ matrix.
  187. If setProduceHiddenOutput() is set to true then @p output[1] will contain a Mat with shape [`T`, `N`, @f$N_h@f$], where @f$N_h@f$ is number of rows in @f$ W_{hh} @f$ matrix.
  188. */
  189. class CV_EXPORTS RNNLayer : public Layer
  190. {
  191. public:
  192. /** Creates instance of RNNLayer */
  193. static Ptr<RNNLayer> create(const LayerParams& params);
  194. /** Setups learned weights.
  195. Recurrent-layer behavior on each step is defined by current input @f$ x_t @f$, previous state @f$ h_t @f$ and learned weights as follows:
  196. @f{eqnarray*}{
  197. h_t &= tanh&(W_{hh} h_{t-1} + W_{xh} x_t + b_h), \\
  198. o_t &= tanh&(W_{ho} h_t + b_o),
  199. @f}
  200. @param Wxh is @f$ W_{xh} @f$ matrix
  201. @param bh is @f$ b_{h} @f$ vector
  202. @param Whh is @f$ W_{hh} @f$ matrix
  203. @param Who is @f$ W_{xo} @f$ matrix
  204. @param bo is @f$ b_{o} @f$ vector
  205. */
  206. virtual void setWeights(const Mat &Wxh, const Mat &bh, const Mat &Whh, const Mat &Who, const Mat &bo) = 0;
  207. /** @brief If this flag is set to true then layer will produce @f$ h_t @f$ as second output.
  208. * @details Shape of the second output is the same as first output.
  209. */
  210. virtual void setProduceHiddenOutput(bool produce = false) = 0;
  211. };
  212. class CV_EXPORTS BaseConvolutionLayer : public Layer
  213. {
  214. public:
  215. CV_DEPRECATED_EXTERNAL Size kernel, stride, pad, dilation, adjustPad;
  216. std::vector<size_t> adjust_pads;
  217. std::vector<size_t> kernel_size, strides, dilations;
  218. std::vector<size_t> pads_begin, pads_end;
  219. String padMode;
  220. int numOutput;
  221. };
  222. class CV_EXPORTS ConvolutionLayer : public BaseConvolutionLayer
  223. {
  224. public:
  225. static Ptr<BaseConvolutionLayer> create(const LayerParams& params);
  226. };
  227. class CV_EXPORTS ConvolutionLayerInt8 : public BaseConvolutionLayer
  228. {
  229. public:
  230. int input_zp, output_zp;
  231. float input_sc, output_sc;
  232. static Ptr<BaseConvolutionLayer> create(const LayerParams& params);
  233. };
  234. class CV_EXPORTS DeconvolutionLayer : public BaseConvolutionLayer
  235. {
  236. public:
  237. static Ptr<BaseConvolutionLayer> create(const LayerParams& params);
  238. };
  239. class CV_EXPORTS LRNLayer : public Layer
  240. {
  241. public:
  242. int type;
  243. int size;
  244. float alpha, beta, bias;
  245. bool normBySize;
  246. static Ptr<LRNLayer> create(const LayerParams& params);
  247. };
  248. /** @brief ArgMax/ArgMin layer
  249. * @note returns indices as floats, which means the supported range is [-2^24; 2^24]
  250. */
  251. class CV_EXPORTS ArgLayer : public Layer
  252. {
  253. public:
  254. static Ptr<ArgLayer> create(const LayerParams& params);
  255. };
  256. class CV_EXPORTS PoolingLayer : public Layer
  257. {
  258. public:
  259. int type;
  260. std::vector<size_t> kernel_size, strides;
  261. std::vector<size_t> pads_begin, pads_end;
  262. bool globalPooling; //!< Flag is true if at least one of the axes is global pooled.
  263. std::vector<bool> isGlobalPooling;
  264. bool computeMaxIdx;
  265. String padMode;
  266. bool ceilMode;
  267. // If true for average pooling with padding, divide an every output region
  268. // by a whole kernel area. Otherwise exclude zero padded values and divide
  269. // by number of real values.
  270. bool avePoolPaddedArea;
  271. // ROIPooling parameters.
  272. Size pooledSize;
  273. float spatialScale;
  274. // PSROIPooling parameters.
  275. int psRoiOutChannels;
  276. static Ptr<PoolingLayer> create(const LayerParams& params);
  277. };
  278. class CV_EXPORTS PoolingLayerInt8 : public PoolingLayer
  279. {
  280. public:
  281. int input_zp, output_zp;
  282. float input_sc, output_sc;
  283. static Ptr<PoolingLayerInt8> create(const LayerParams& params);
  284. };
  285. class CV_EXPORTS ReduceLayer : public Layer
  286. {
  287. public:
  288. int reduceType;
  289. std::vector<size_t> reduceDims;
  290. static Ptr<ReduceLayer> create(const LayerParams& params);
  291. };
  292. class CV_EXPORTS ReduceLayerInt8 : public ReduceLayer
  293. {
  294. public:
  295. static Ptr<ReduceLayerInt8> create(const LayerParams& params);
  296. };
  297. class CV_EXPORTS SoftmaxLayer : public Layer
  298. {
  299. public:
  300. bool logSoftMax;
  301. static Ptr<SoftmaxLayer> create(const LayerParams& params);
  302. };
  303. class CV_EXPORTS SoftmaxLayerInt8 : public SoftmaxLayer
  304. {
  305. public:
  306. float output_sc;
  307. int output_zp;
  308. static Ptr<SoftmaxLayerInt8> create(const LayerParams& params);
  309. };
  310. class CV_EXPORTS InnerProductLayer : public Layer
  311. {
  312. public:
  313. int axis;
  314. static Ptr<InnerProductLayer> create(const LayerParams& params);
  315. };
  316. class CV_EXPORTS InnerProductLayerInt8 : public InnerProductLayer
  317. {
  318. public:
  319. int input_zp, output_zp;
  320. float input_sc, output_sc;
  321. static Ptr<InnerProductLayerInt8> create(const LayerParams& params);
  322. };
  323. class CV_EXPORTS MVNLayer : public Layer
  324. {
  325. public:
  326. float eps;
  327. bool normVariance, acrossChannels;
  328. static Ptr<MVNLayer> create(const LayerParams& params);
  329. };
  330. /* Reshaping */
  331. class CV_EXPORTS ReshapeLayer : public Layer
  332. {
  333. public:
  334. MatShape newShapeDesc;
  335. Range newShapeRange;
  336. static Ptr<ReshapeLayer> create(const LayerParams& params);
  337. };
  338. class CV_EXPORTS FlattenLayer : public Layer
  339. {
  340. public:
  341. static Ptr<FlattenLayer> create(const LayerParams &params);
  342. };
  343. class CV_EXPORTS QuantizeLayer : public Layer
  344. {
  345. public:
  346. float scale;
  347. int zeropoint;
  348. static Ptr<QuantizeLayer> create(const LayerParams &params);
  349. };
  350. class CV_EXPORTS DequantizeLayer : public Layer
  351. {
  352. public:
  353. float scale;
  354. int zeropoint;
  355. static Ptr<DequantizeLayer> create(const LayerParams &params);
  356. };
  357. class CV_EXPORTS RequantizeLayer : public Layer
  358. {
  359. public:
  360. float scale, shift;
  361. static Ptr<RequantizeLayer> create(const LayerParams &params);
  362. };
  363. class CV_EXPORTS ConcatLayer : public Layer
  364. {
  365. public:
  366. int axis;
  367. /**
  368. * @brief Add zero padding in case of concatenation of blobs with different
  369. * spatial sizes.
  370. *
  371. * Details: https://github.com/torch/nn/blob/master/doc/containers.md#depthconcat
  372. */
  373. bool padding;
  374. int paddingValue;
  375. static Ptr<ConcatLayer> create(const LayerParams &params);
  376. };
  377. class CV_EXPORTS SplitLayer : public Layer
  378. {
  379. public:
  380. int outputsCount; //!< Number of copies that will be produced (is ignored when negative).
  381. static Ptr<SplitLayer> create(const LayerParams &params);
  382. };
  383. /**
  384. * Slice layer has several modes:
  385. * 1. Caffe mode
  386. * @param[in] axis Axis of split operation
  387. * @param[in] slice_point Array of split points
  388. *
  389. * Number of output blobs equals to number of split points plus one. The
  390. * first blob is a slice on input from 0 to @p slice_point[0] - 1 by @p axis,
  391. * the second output blob is a slice of input from @p slice_point[0] to
  392. * @p slice_point[1] - 1 by @p axis and the last output blob is a slice of
  393. * input from @p slice_point[-1] up to the end of @p axis size.
  394. *
  395. * 2. TensorFlow mode
  396. * @param begin Vector of start indices
  397. * @param size Vector of sizes
  398. *
  399. * More convenient numpy-like slice. One and only output blob
  400. * is a slice `input[begin[0]:begin[0]+size[0], begin[1]:begin[1]+size[1], ...]`
  401. *
  402. * 3. Torch mode
  403. * @param axis Axis of split operation
  404. *
  405. * Split input blob on the equal parts by @p axis.
  406. */
  407. class CV_EXPORTS SliceLayer : public Layer
  408. {
  409. public:
  410. /**
  411. * @brief Vector of slice ranges.
  412. *
  413. * The first dimension equals number of output blobs.
  414. * Inner vector has slice ranges for the first number of input dimensions.
  415. */
  416. std::vector<std::vector<Range> > sliceRanges;
  417. std::vector<std::vector<int> > sliceSteps;
  418. int axis;
  419. int num_split;
  420. static Ptr<SliceLayer> create(const LayerParams &params);
  421. };
  422. class CV_EXPORTS PermuteLayer : public Layer
  423. {
  424. public:
  425. static Ptr<PermuteLayer> create(const LayerParams& params);
  426. };
  427. /**
  428. * Permute channels of 4-dimensional input blob.
  429. * @param group Number of groups to split input channels and pick in turns
  430. * into output blob.
  431. *
  432. * \f[ groupSize = \frac{number\ of\ channels}{group} \f]
  433. * \f[ output(n, c, h, w) = input(n, groupSize \times (c \% group) + \lfloor \frac{c}{group} \rfloor, h, w) \f]
  434. * Read more at https://arxiv.org/pdf/1707.01083.pdf
  435. */
  436. class CV_EXPORTS ShuffleChannelLayer : public Layer
  437. {
  438. public:
  439. static Ptr<Layer> create(const LayerParams& params);
  440. int group;
  441. };
  442. /**
  443. * @brief Adds extra values for specific axes.
  444. * @param paddings Vector of paddings in format
  445. * @code
  446. * [ pad_before, pad_after, // [0]th dimension
  447. * pad_before, pad_after, // [1]st dimension
  448. * ...
  449. * pad_before, pad_after ] // [n]th dimension
  450. * @endcode
  451. * that represents number of padded values at every dimension
  452. * starting from the first one. The rest of dimensions won't
  453. * be padded.
  454. * @param value Value to be padded. Defaults to zero.
  455. * @param type Padding type: 'constant', 'reflect'
  456. * @param input_dims Torch's parameter. If @p input_dims is not equal to the
  457. * actual input dimensionality then the `[0]th` dimension
  458. * is considered as a batch dimension and @p paddings are shifted
  459. * to a one dimension. Defaults to `-1` that means padding
  460. * corresponding to @p paddings.
  461. */
  462. class CV_EXPORTS PaddingLayer : public Layer
  463. {
  464. public:
  465. static Ptr<PaddingLayer> create(const LayerParams& params);
  466. };
  467. /* Activations */
  468. class CV_EXPORTS ActivationLayer : public Layer
  469. {
  470. public:
  471. virtual void forwardSlice(const float* src, float* dst, int len,
  472. size_t outPlaneSize, int cn0, int cn1) const {};
  473. virtual void forwardSlice(const int* src, const int* lut, int* dst, int len,
  474. size_t outPlaneSize, int cn0, int cn1) const {};
  475. virtual void forwardSlice(const int8_t* src, const int8_t* lut, int8_t* dst, int len,
  476. size_t outPlaneSize, int cn0, int cn1) const {};
  477. };
  478. class CV_EXPORTS ReLULayer : public ActivationLayer
  479. {
  480. public:
  481. float negativeSlope;
  482. static Ptr<ReLULayer> create(const LayerParams &params);
  483. };
  484. class CV_EXPORTS ReLU6Layer : public ActivationLayer
  485. {
  486. public:
  487. float minValue, maxValue;
  488. static Ptr<ReLU6Layer> create(const LayerParams &params);
  489. };
  490. class CV_EXPORTS ChannelsPReLULayer : public ActivationLayer
  491. {
  492. public:
  493. static Ptr<Layer> create(const LayerParams& params);
  494. };
  495. class CV_EXPORTS ELULayer : public ActivationLayer
  496. {
  497. public:
  498. float alpha;
  499. static Ptr<ELULayer> create(const LayerParams &params);
  500. };
  501. class CV_EXPORTS TanHLayer : public ActivationLayer
  502. {
  503. public:
  504. static Ptr<TanHLayer> create(const LayerParams &params);
  505. };
  506. class CV_EXPORTS SwishLayer : public ActivationLayer
  507. {
  508. public:
  509. static Ptr<SwishLayer> create(const LayerParams &params);
  510. };
  511. class CV_EXPORTS MishLayer : public ActivationLayer
  512. {
  513. public:
  514. static Ptr<MishLayer> create(const LayerParams &params);
  515. };
  516. class CV_EXPORTS SigmoidLayer : public ActivationLayer
  517. {
  518. public:
  519. static Ptr<SigmoidLayer> create(const LayerParams &params);
  520. };
  521. class CV_EXPORTS BNLLLayer : public ActivationLayer
  522. {
  523. public:
  524. static Ptr<BNLLLayer> create(const LayerParams &params);
  525. };
  526. class CV_EXPORTS AbsLayer : public ActivationLayer
  527. {
  528. public:
  529. static Ptr<AbsLayer> create(const LayerParams &params);
  530. };
  531. class CV_EXPORTS PowerLayer : public ActivationLayer
  532. {
  533. public:
  534. float power, scale, shift;
  535. static Ptr<PowerLayer> create(const LayerParams &params);
  536. };
  537. class CV_EXPORTS ExpLayer : public ActivationLayer
  538. {
  539. public:
  540. float base, scale, shift;
  541. static Ptr<ExpLayer> create(const LayerParams &params);
  542. };
  543. class CV_EXPORTS CeilLayer : public ActivationLayer
  544. {
  545. public:
  546. static Ptr<CeilLayer> create(const LayerParams &params);
  547. };
  548. class CV_EXPORTS FloorLayer : public ActivationLayer
  549. {
  550. public:
  551. static Ptr<FloorLayer> create(const LayerParams &params);
  552. };
  553. class CV_EXPORTS LogLayer : public ActivationLayer
  554. {
  555. public:
  556. static Ptr<LogLayer> create(const LayerParams &params);
  557. };
  558. class CV_EXPORTS RoundLayer : public ActivationLayer
  559. {
  560. public:
  561. static Ptr<RoundLayer> create(const LayerParams &params);
  562. };
  563. class CV_EXPORTS SqrtLayer : public ActivationLayer
  564. {
  565. public:
  566. static Ptr<SqrtLayer> create(const LayerParams &params);
  567. };
  568. class CV_EXPORTS NotLayer : public ActivationLayer
  569. {
  570. public:
  571. static Ptr<NotLayer> create(const LayerParams &params);
  572. };
  573. class CV_EXPORTS AcosLayer : public ActivationLayer
  574. {
  575. public:
  576. static Ptr<AcosLayer> create(const LayerParams &params);
  577. };
  578. class CV_EXPORTS AcoshLayer : public ActivationLayer
  579. {
  580. public:
  581. static Ptr<AcoshLayer> create(const LayerParams &params);
  582. };
  583. class CV_EXPORTS AsinLayer : public ActivationLayer
  584. {
  585. public:
  586. static Ptr<AsinLayer> create(const LayerParams &params);
  587. };
  588. class CV_EXPORTS AsinhLayer : public ActivationLayer
  589. {
  590. public:
  591. static Ptr<AsinhLayer> create(const LayerParams &params);
  592. };
  593. class CV_EXPORTS AtanLayer : public ActivationLayer
  594. {
  595. public:
  596. static Ptr<AtanLayer> create(const LayerParams &params);
  597. };
  598. class CV_EXPORTS AtanhLayer : public ActivationLayer
  599. {
  600. public:
  601. static Ptr<AtanhLayer> create(const LayerParams &params);
  602. };
  603. class CV_EXPORTS CosLayer : public ActivationLayer
  604. {
  605. public:
  606. static Ptr<CosLayer> create(const LayerParams &params);
  607. };
  608. class CV_EXPORTS CoshLayer : public ActivationLayer
  609. {
  610. public:
  611. static Ptr<CoshLayer> create(const LayerParams &params);
  612. };
  613. class CV_EXPORTS ErfLayer : public ActivationLayer
  614. {
  615. public:
  616. static Ptr<ErfLayer> create(const LayerParams &params);
  617. };
  618. class CV_EXPORTS HardSwishLayer : public ActivationLayer
  619. {
  620. public:
  621. static Ptr<HardSwishLayer> create(const LayerParams &params);
  622. };
  623. class CV_EXPORTS SinLayer : public ActivationLayer
  624. {
  625. public:
  626. static Ptr<SinLayer> create(const LayerParams &params);
  627. };
  628. class CV_EXPORTS SinhLayer : public ActivationLayer
  629. {
  630. public:
  631. static Ptr<SinhLayer> create(const LayerParams &params);
  632. };
  633. class CV_EXPORTS SoftplusLayer : public ActivationLayer
  634. {
  635. public:
  636. static Ptr<SoftplusLayer> create(const LayerParams &params);
  637. };
  638. class CV_EXPORTS SoftsignLayer : public ActivationLayer
  639. {
  640. public:
  641. static Ptr<SoftsignLayer> create(const LayerParams &params);
  642. };
  643. class CV_EXPORTS TanLayer : public ActivationLayer
  644. {
  645. public:
  646. static Ptr<TanLayer> create(const LayerParams &params);
  647. };
  648. class CV_EXPORTS CeluLayer : public ActivationLayer
  649. {
  650. public:
  651. float alpha;
  652. static Ptr<CeluLayer> create(const LayerParams &params);
  653. };
  654. class CV_EXPORTS HardSigmoidLayer : public ActivationLayer
  655. {
  656. public:
  657. float alpha;
  658. float beta;
  659. static Ptr<HardSigmoidLayer> create(const LayerParams &params);
  660. };
  661. class CV_EXPORTS SeluLayer : public ActivationLayer
  662. {
  663. public:
  664. float alpha;
  665. float gamma;
  666. static Ptr<SeluLayer> create(const LayerParams &params);
  667. };
  668. class CV_EXPORTS ThresholdedReluLayer : public ActivationLayer
  669. {
  670. public:
  671. float alpha;
  672. static Ptr<ThresholdedReluLayer> create(const LayerParams &params);
  673. };
  674. class CV_EXPORTS ActivationLayerInt8 : public ActivationLayer
  675. {
  676. public:
  677. static Ptr<ActivationLayerInt8> create(const LayerParams &params);
  678. };
  679. class CV_EXPORTS SignLayer : public ActivationLayer
  680. {
  681. public:
  682. static Ptr<SignLayer> create(const LayerParams &params);
  683. };
  684. class CV_EXPORTS ShrinkLayer : public ActivationLayer
  685. {
  686. public:
  687. float bias;
  688. float lambd;
  689. static Ptr<ShrinkLayer> create(const LayerParams &params);
  690. };
  691. class CV_EXPORTS ReciprocalLayer : public ActivationLayer
  692. {
  693. public:
  694. static Ptr<ReciprocalLayer> create(const LayerParams &params);
  695. };
  696. /* Layers used in semantic segmentation */
  697. class CV_EXPORTS CropLayer : public Layer
  698. {
  699. public:
  700. static Ptr<Layer> create(const LayerParams &params);
  701. };
  702. /** @brief Element wise operation on inputs
  703. Extra optional parameters:
  704. - "operation" as string. Values are "sum" (default), "prod", "max", "div", "min"
  705. - "coeff" as float array. Specify weights of inputs for SUM operation
  706. - "output_channels_mode" as string. Values are "same" (default, all input must have the same layout), "input_0", "input_0_truncate", "max_input_channels"
  707. */
  708. class CV_EXPORTS EltwiseLayer : public Layer
  709. {
  710. public:
  711. static Ptr<EltwiseLayer> create(const LayerParams &params);
  712. };
  713. class CV_EXPORTS EltwiseLayerInt8 : public Layer
  714. {
  715. public:
  716. static Ptr<EltwiseLayerInt8> create(const LayerParams &params);
  717. };
  718. class CV_EXPORTS BatchNormLayer : public ActivationLayer
  719. {
  720. public:
  721. bool hasWeights, hasBias;
  722. float epsilon;
  723. static Ptr<BatchNormLayer> create(const LayerParams &params);
  724. };
  725. class CV_EXPORTS BatchNormLayerInt8 : public BatchNormLayer
  726. {
  727. public:
  728. float input_sc, output_sc;
  729. int input_zp, output_zp;
  730. static Ptr<BatchNormLayerInt8> create(const LayerParams &params);
  731. };
  732. class CV_EXPORTS MaxUnpoolLayer : public Layer
  733. {
  734. public:
  735. Size poolKernel;
  736. Size poolPad;
  737. Size poolStride;
  738. static Ptr<MaxUnpoolLayer> create(const LayerParams &params);
  739. };
  740. class CV_EXPORTS ScaleLayer : public Layer
  741. {
  742. public:
  743. bool hasBias;
  744. int axis;
  745. String mode;
  746. static Ptr<ScaleLayer> create(const LayerParams& params);
  747. };
  748. class CV_EXPORTS ScaleLayerInt8 : public ScaleLayer
  749. {
  750. public:
  751. float output_sc;
  752. int output_zp;
  753. static Ptr<ScaleLayerInt8> create(const LayerParams &params);
  754. };
  755. class CV_EXPORTS ShiftLayer : public Layer
  756. {
  757. public:
  758. static Ptr<Layer> create(const LayerParams& params);
  759. };
  760. class CV_EXPORTS ShiftLayerInt8 : public Layer
  761. {
  762. public:
  763. static Ptr<Layer> create(const LayerParams& params);
  764. };
  765. class CV_EXPORTS CompareLayer : public Layer
  766. {
  767. public:
  768. static Ptr<Layer> create(const LayerParams& params);
  769. };
  770. class CV_EXPORTS DataAugmentationLayer : public Layer
  771. {
  772. public:
  773. static Ptr<DataAugmentationLayer> create(const LayerParams& params);
  774. };
  775. class CV_EXPORTS CorrelationLayer : public Layer
  776. {
  777. public:
  778. static Ptr<CorrelationLayer> create(const LayerParams& params);
  779. };
  780. class CV_EXPORTS AccumLayer : public Layer
  781. {
  782. public:
  783. static Ptr<AccumLayer> create(const LayerParams& params);
  784. };
  785. class CV_EXPORTS FlowWarpLayer : public Layer
  786. {
  787. public:
  788. static Ptr<FlowWarpLayer> create(const LayerParams& params);
  789. };
  790. class CV_EXPORTS PriorBoxLayer : public Layer
  791. {
  792. public:
  793. static Ptr<PriorBoxLayer> create(const LayerParams& params);
  794. };
  795. class CV_EXPORTS ReorgLayer : public Layer
  796. {
  797. public:
  798. static Ptr<ReorgLayer> create(const LayerParams& params);
  799. };
  800. class CV_EXPORTS RegionLayer : public Layer
  801. {
  802. public:
  803. float nmsThreshold;
  804. static Ptr<RegionLayer> create(const LayerParams& params);
  805. };
  806. /**
  807. * @brief Detection output layer.
  808. *
  809. * The layer size is: @f$ (1 \times 1 \times N \times 7) @f$
  810. * where N is [keep_top_k] parameter multiplied by batch size. Each row is:
  811. * [image_id, label, confidence, xmin, ymin, xmax, ymax]
  812. * where image_id is the index of image input in the batch.
  813. */
  814. class CV_EXPORTS DetectionOutputLayer : public Layer
  815. {
  816. public:
  817. static Ptr<DetectionOutputLayer> create(const LayerParams& params);
  818. };
  819. /**
  820. * @brief \f$ L_p \f$ - normalization layer.
  821. * @param p Normalization factor. The most common `p = 1` for \f$ L_1 \f$ -
  822. * normalization or `p = 2` for \f$ L_2 \f$ - normalization or a custom one.
  823. * @param eps Parameter \f$ \epsilon \f$ to prevent a division by zero.
  824. * @param across_spatial If true, normalize an input across all non-batch dimensions.
  825. * Otherwise normalize an every channel separately.
  826. *
  827. * Across spatial:
  828. * @f[
  829. * norm = \sqrt[p]{\epsilon + \sum_{x, y, c} |src(x, y, c)|^p } \\
  830. * dst(x, y, c) = \frac{ src(x, y, c) }{norm}
  831. * @f]
  832. *
  833. * Channel wise normalization:
  834. * @f[
  835. * norm(c) = \sqrt[p]{\epsilon + \sum_{x, y} |src(x, y, c)|^p } \\
  836. * dst(x, y, c) = \frac{ src(x, y, c) }{norm(c)}
  837. * @f]
  838. *
  839. * Where `x, y` - spatial coordinates, `c` - channel.
  840. *
  841. * An every sample in the batch is normalized separately. Optionally,
  842. * output is scaled by the trained parameters.
  843. */
  844. class CV_EXPORTS NormalizeBBoxLayer : public Layer
  845. {
  846. public:
  847. float pnorm, epsilon;
  848. CV_DEPRECATED_EXTERNAL bool acrossSpatial;
  849. static Ptr<NormalizeBBoxLayer> create(const LayerParams& params);
  850. };
  851. /**
  852. * @brief Resize input 4-dimensional blob by nearest neighbor or bilinear strategy.
  853. *
  854. * Layer is used to support TensorFlow's resize_nearest_neighbor and resize_bilinear ops.
  855. */
  856. class CV_EXPORTS ResizeLayer : public Layer
  857. {
  858. public:
  859. static Ptr<ResizeLayer> create(const LayerParams& params);
  860. };
  861. /**
  862. * @brief Bilinear resize layer from https://github.com/cdmh/deeplab-public-ver2
  863. *
  864. * It differs from @ref ResizeLayer in output shape and resize scales computations.
  865. */
  866. class CV_EXPORTS InterpLayer : public Layer
  867. {
  868. public:
  869. static Ptr<Layer> create(const LayerParams& params);
  870. };
  871. class CV_EXPORTS ProposalLayer : public Layer
  872. {
  873. public:
  874. static Ptr<ProposalLayer> create(const LayerParams& params);
  875. };
  876. class CV_EXPORTS CropAndResizeLayer : public Layer
  877. {
  878. public:
  879. static Ptr<Layer> create(const LayerParams& params);
  880. };
  881. class CV_EXPORTS CumSumLayer : public Layer
  882. {
  883. public:
  884. int exclusive;
  885. int reverse;
  886. static Ptr<CumSumLayer> create(const LayerParams& params);
  887. };
  888. //! @}
  889. //! @}
  890. CV__DNN_INLINE_NS_END
  891. }
  892. }
  893. #endif