dnn.hpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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_HPP
  42. #define OPENCV_DNN_DNN_HPP
  43. #include <vector>
  44. #include <opencv2/core.hpp>
  45. #include "opencv2/core/async.hpp"
  46. #include "../dnn/version.hpp"
  47. #include <opencv2/dnn/dict.hpp>
  48. namespace cv {
  49. namespace dnn {
  50. CV__DNN_INLINE_NS_BEGIN
  51. //! @addtogroup dnn
  52. //! @{
  53. typedef std::vector<int> MatShape;
  54. /**
  55. * @brief Enum of computation backends supported by layers.
  56. * @see Net::setPreferableBackend
  57. */
  58. enum Backend
  59. {
  60. //! DNN_BACKEND_DEFAULT equals to DNN_BACKEND_INFERENCE_ENGINE if
  61. //! OpenCV is built with Intel's Inference Engine library or
  62. //! DNN_BACKEND_OPENCV otherwise.
  63. DNN_BACKEND_DEFAULT,
  64. DNN_BACKEND_HALIDE,
  65. DNN_BACKEND_INFERENCE_ENGINE, //!< Intel's Inference Engine computational backend.
  66. DNN_BACKEND_OPENCV,
  67. DNN_BACKEND_VKCOM
  68. };
  69. /**
  70. * @brief Enum of target devices for computations.
  71. * @see Net::setPreferableTarget
  72. */
  73. enum Target
  74. {
  75. DNN_TARGET_CPU,
  76. DNN_TARGET_OPENCL,
  77. DNN_TARGET_OPENCL_FP16,
  78. DNN_TARGET_MYRIAD,
  79. DNN_TARGET_VULKAN,
  80. DNN_TARGET_FPGA //!< FPGA device with CPU fallbacks using Inference Engine's Heterogeneous plugin.
  81. };
  82. CV_EXPORTS std::vector< std::pair<Backend, Target> > getAvailableBackends();
  83. CV_EXPORTS std::vector<Target> getAvailableTargets(Backend be);
  84. /** @brief This class provides all data needed to initialize layer.
  85. *
  86. * It includes dictionary with scalar params (which can be read by using Dict interface),
  87. * blob params #blobs and optional meta information: #name and #type of layer instance.
  88. */
  89. class CV_EXPORTS LayerParams : public Dict
  90. {
  91. public:
  92. //TODO: Add ability to name blob params
  93. std::vector<Mat> blobs; //!< List of learned parameters stored as blobs.
  94. String name; //!< Name of the layer instance (optional, can be used internal purposes).
  95. String type; //!< Type name which was used for creating layer by layer factory (optional).
  96. };
  97. /**
  98. * @brief Derivatives of this class encapsulates functions of certain backends.
  99. */
  100. class BackendNode
  101. {
  102. public:
  103. BackendNode(int backendId);
  104. virtual ~BackendNode(); //!< Virtual destructor to make polymorphism.
  105. int backendId; //!< Backend identifier.
  106. };
  107. /**
  108. * @brief Derivatives of this class wraps cv::Mat for different backends and targets.
  109. */
  110. class BackendWrapper
  111. {
  112. public:
  113. BackendWrapper(int backendId, int targetId);
  114. /**
  115. * @brief Wrap cv::Mat for specific backend and target.
  116. * @param[in] targetId Target identifier.
  117. * @param[in] m cv::Mat for wrapping.
  118. *
  119. * Make CPU->GPU data transfer if it's require for the target.
  120. */
  121. BackendWrapper(int targetId, const cv::Mat& m);
  122. /**
  123. * @brief Make wrapper for reused cv::Mat.
  124. * @param[in] base Wrapper of cv::Mat that will be reused.
  125. * @param[in] shape Specific shape.
  126. *
  127. * Initialize wrapper from another one. It'll wrap the same host CPU
  128. * memory and mustn't allocate memory on device(i.e. GPU). It might
  129. * has different shape. Use in case of CPU memory reusing for reuse
  130. * associated memory on device too.
  131. */
  132. BackendWrapper(const Ptr<BackendWrapper>& base, const MatShape& shape);
  133. virtual ~BackendWrapper(); //!< Virtual destructor to make polymorphism.
  134. /**
  135. * @brief Transfer data to CPU host memory.
  136. */
  137. virtual void copyToHost() = 0;
  138. /**
  139. * @brief Indicate that an actual data is on CPU.
  140. */
  141. virtual void setHostDirty() = 0;
  142. int backendId; //!< Backend identifier.
  143. int targetId; //!< Target identifier.
  144. };
  145. class CV_EXPORTS ActivationLayer;
  146. /** @brief This interface class allows to build new Layers - are building blocks of networks.
  147. *
  148. * Each class, derived from Layer, must implement allocate() methods to declare own outputs and forward() to compute outputs.
  149. * Also before using the new layer into networks you must register your layer by using one of @ref dnnLayerFactory "LayerFactory" macros.
  150. */
  151. class CV_EXPORTS_W Layer : public Algorithm
  152. {
  153. public:
  154. //! List of learned parameters must be stored here to allow read them by using Net::getParam().
  155. CV_PROP_RW std::vector<Mat> blobs;
  156. /** @brief Computes and sets internal parameters according to inputs, outputs and blobs.
  157. * @deprecated Use Layer::finalize(InputArrayOfArrays, OutputArrayOfArrays) instead
  158. * @param[in] input vector of already allocated input blobs
  159. * @param[out] output vector of already allocated output blobs
  160. *
  161. * If this method is called after network has allocated all memory for input and output blobs
  162. * and before inferencing.
  163. */
  164. CV_DEPRECATED_EXTERNAL
  165. virtual void finalize(const std::vector<Mat*> &input, std::vector<Mat> &output);
  166. /** @brief Computes and sets internal parameters according to inputs, outputs and blobs.
  167. * @param[in] inputs vector of already allocated input blobs
  168. * @param[out] outputs vector of already allocated output blobs
  169. *
  170. * If this method is called after network has allocated all memory for input and output blobs
  171. * and before inferencing.
  172. */
  173. CV_WRAP virtual void finalize(InputArrayOfArrays inputs, OutputArrayOfArrays outputs);
  174. /** @brief Given the @p input blobs, computes the output @p blobs.
  175. * @deprecated Use Layer::forward(InputArrayOfArrays, OutputArrayOfArrays, OutputArrayOfArrays) instead
  176. * @param[in] input the input blobs.
  177. * @param[out] output allocated output blobs, which will store results of the computation.
  178. * @param[out] internals allocated internal blobs
  179. */
  180. CV_DEPRECATED_EXTERNAL
  181. virtual void forward(std::vector<Mat*> &input, std::vector<Mat> &output, std::vector<Mat> &internals);
  182. /** @brief Given the @p input blobs, computes the output @p blobs.
  183. * @param[in] inputs the input blobs.
  184. * @param[out] outputs allocated output blobs, which will store results of the computation.
  185. * @param[out] internals allocated internal blobs
  186. */
  187. virtual void forward(InputArrayOfArrays inputs, OutputArrayOfArrays outputs, OutputArrayOfArrays internals);
  188. /** @brief Given the @p input blobs, computes the output @p blobs.
  189. * @param[in] inputs the input blobs.
  190. * @param[out] outputs allocated output blobs, which will store results of the computation.
  191. * @param[out] internals allocated internal blobs
  192. */
  193. void forward_fallback(InputArrayOfArrays inputs, OutputArrayOfArrays outputs, OutputArrayOfArrays internals);
  194. /** @brief
  195. * @overload
  196. * @deprecated Use Layer::finalize(InputArrayOfArrays, OutputArrayOfArrays) instead
  197. */
  198. CV_DEPRECATED_EXTERNAL
  199. void finalize(const std::vector<Mat> &inputs, CV_OUT std::vector<Mat> &outputs);
  200. /** @brief
  201. * @overload
  202. * @deprecated Use Layer::finalize(InputArrayOfArrays, OutputArrayOfArrays) instead
  203. */
  204. CV_DEPRECATED std::vector<Mat> finalize(const std::vector<Mat> &inputs);
  205. /** @brief Allocates layer and computes output.
  206. * @deprecated This method will be removed in the future release.
  207. */
  208. CV_DEPRECATED CV_WRAP void run(const std::vector<Mat> &inputs, CV_OUT std::vector<Mat> &outputs,
  209. CV_IN_OUT std::vector<Mat> &internals);
  210. /** @brief Returns index of input blob into the input array.
  211. * @param inputName label of input blob
  212. *
  213. * Each layer input and output can be labeled to easily identify them using "%<layer_name%>[.output_name]" notation.
  214. * This method maps label of input blob to its index into input vector.
  215. */
  216. virtual int inputNameToIndex(String inputName);
  217. /** @brief Returns index of output blob in output array.
  218. * @see inputNameToIndex()
  219. */
  220. CV_WRAP virtual int outputNameToIndex(const String& outputName);
  221. /**
  222. * @brief Ask layer if it support specific backend for doing computations.
  223. * @param[in] backendId computation backend identifier.
  224. * @see Backend
  225. */
  226. virtual bool supportBackend(int backendId);
  227. /**
  228. * @brief Returns Halide backend node.
  229. * @param[in] inputs Input Halide buffers.
  230. * @see BackendNode, BackendWrapper
  231. *
  232. * Input buffers should be exactly the same that will be used in forward invocations.
  233. * Despite we can use Halide::ImageParam based on input shape only,
  234. * it helps prevent some memory management issues (if something wrong,
  235. * Halide tests will be failed).
  236. */
  237. virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs);
  238. virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> > &inputs);
  239. virtual Ptr<BackendNode> initVkCom(const std::vector<Ptr<BackendWrapper> > &inputs);
  240. /**
  241. * @brief Automatic Halide scheduling based on layer hyper-parameters.
  242. * @param[in] node Backend node with Halide functions.
  243. * @param[in] inputs Blobs that will be used in forward invocations.
  244. * @param[in] outputs Blobs that will be used in forward invocations.
  245. * @param[in] targetId Target identifier
  246. * @see BackendNode, Target
  247. *
  248. * Layer don't use own Halide::Func members because we can have applied
  249. * layers fusing. In this way the fused function should be scheduled.
  250. */
  251. virtual void applyHalideScheduler(Ptr<BackendNode>& node,
  252. const std::vector<Mat*> &inputs,
  253. const std::vector<Mat> &outputs,
  254. int targetId) const;
  255. /**
  256. * @brief Implement layers fusing.
  257. * @param[in] node Backend node of bottom layer.
  258. * @see BackendNode
  259. *
  260. * Actual for graph-based backends. If layer attached successfully,
  261. * returns non-empty cv::Ptr to node of the same backend.
  262. * Fuse only over the last function.
  263. */
  264. virtual Ptr<BackendNode> tryAttach(const Ptr<BackendNode>& node);
  265. /**
  266. * @brief Tries to attach to the layer the subsequent activation layer, i.e. do the layer fusion in a partial case.
  267. * @param[in] layer The subsequent activation layer.
  268. *
  269. * Returns true if the activation layer has been attached successfully.
  270. */
  271. virtual bool setActivation(const Ptr<ActivationLayer>& layer);
  272. /**
  273. * @brief Try to fuse current layer with a next one
  274. * @param[in] top Next layer to be fused.
  275. * @returns True if fusion was performed.
  276. */
  277. virtual bool tryFuse(Ptr<Layer>& top);
  278. /**
  279. * @brief Returns parameters of layers with channel-wise multiplication and addition.
  280. * @param[out] scale Channel-wise multipliers. Total number of values should
  281. * be equal to number of channels.
  282. * @param[out] shift Channel-wise offsets. Total number of values should
  283. * be equal to number of channels.
  284. *
  285. * Some layers can fuse their transformations with further layers.
  286. * In example, convolution + batch normalization. This way base layer
  287. * use weights from layer after it. Fused layer is skipped.
  288. * By default, @p scale and @p shift are empty that means layer has no
  289. * element-wise multiplications or additions.
  290. */
  291. virtual void getScaleShift(Mat& scale, Mat& shift) const;
  292. /**
  293. * @brief "Deattaches" all the layers, attached to particular layer.
  294. */
  295. virtual void unsetAttached();
  296. virtual bool getMemoryShapes(const std::vector<MatShape> &inputs,
  297. const int requiredOutputs,
  298. std::vector<MatShape> &outputs,
  299. std::vector<MatShape> &internals) const;
  300. virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
  301. const std::vector<MatShape> &outputs) const {CV_UNUSED(inputs); CV_UNUSED(outputs); return 0;}
  302. CV_PROP String name; //!< Name of the layer instance, can be used for logging or other internal purposes.
  303. CV_PROP String type; //!< Type name which was used for creating layer by layer factory.
  304. CV_PROP int preferableTarget; //!< prefer target for layer forwarding
  305. Layer();
  306. explicit Layer(const LayerParams &params); //!< Initializes only #name, #type and #blobs fields.
  307. void setParamsFrom(const LayerParams &params); //!< Initializes only #name, #type and #blobs fields.
  308. virtual ~Layer();
  309. };
  310. /** @brief This class allows to create and manipulate comprehensive artificial neural networks.
  311. *
  312. * Neural network is presented as directed acyclic graph (DAG), where vertices are Layer instances,
  313. * and edges specify relationships between layers inputs and outputs.
  314. *
  315. * Each network layer has unique integer id and unique string name inside its network.
  316. * LayerId can store either layer name or layer id.
  317. *
  318. * This class supports reference counting of its instances, i. e. copies point to the same instance.
  319. */
  320. class CV_EXPORTS_W_SIMPLE Net
  321. {
  322. public:
  323. CV_WRAP Net(); //!< Default constructor.
  324. CV_WRAP ~Net(); //!< Destructor frees the net only if there aren't references to the net anymore.
  325. /** @brief Create a network from Intel's Model Optimizer intermediate representation.
  326. * @param[in] xml XML configuration file with network's topology.
  327. * @param[in] bin Binary file with trained weights.
  328. * Networks imported from Intel's Model Optimizer are launched in Intel's Inference Engine
  329. * backend.
  330. */
  331. CV_WRAP static Net readFromModelOptimizer(const String& xml, const String& bin);
  332. /** Returns true if there are no layers in the network. */
  333. CV_WRAP bool empty() const;
  334. /** @brief Dump net to String
  335. * @returns String with structure, hyperparameters, backend, target and fusion
  336. * Call method after setInput(). To see correct backend, target and fusion run after forward().
  337. */
  338. CV_WRAP String dump();
  339. /** @brief Dump net structure, hyperparameters, backend, target and fusion to dot file
  340. * @param path path to output file with .dot extension
  341. * @see dump()
  342. */
  343. CV_WRAP void dumpToFile(const String& path);
  344. /** @brief Adds new layer to the net.
  345. * @param name unique name of the adding layer.
  346. * @param type typename of the adding layer (type must be registered in LayerRegister).
  347. * @param params parameters which will be used to initialize the creating layer.
  348. * @returns unique identifier of created layer, or -1 if a failure will happen.
  349. */
  350. int addLayer(const String &name, const String &type, LayerParams &params);
  351. /** @brief Adds new layer and connects its first input to the first output of previously added layer.
  352. * @see addLayer()
  353. */
  354. int addLayerToPrev(const String &name, const String &type, LayerParams &params);
  355. /** @brief Converts string name of the layer to the integer identifier.
  356. * @returns id of the layer, or -1 if the layer wasn't found.
  357. */
  358. CV_WRAP int getLayerId(const String &layer);
  359. CV_WRAP std::vector<String> getLayerNames() const;
  360. /** @brief Container for strings and integers. */
  361. typedef DictValue LayerId;
  362. /** @brief Returns pointer to layer with specified id or name which the network use. */
  363. CV_WRAP Ptr<Layer> getLayer(LayerId layerId);
  364. /** @brief Returns pointers to input layers of specific layer. */
  365. std::vector<Ptr<Layer> > getLayerInputs(LayerId layerId); // FIXIT: CV_WRAP
  366. /** @brief Connects output of the first layer to input of the second layer.
  367. * @param outPin descriptor of the first layer output.
  368. * @param inpPin descriptor of the second layer input.
  369. *
  370. * Descriptors have the following template <DFN>&lt;layer_name&gt;[.input_number]</DFN>:
  371. * - the first part of the template <DFN>layer_name</DFN> is sting name of the added layer.
  372. * If this part is empty then the network input pseudo layer will be used;
  373. * - the second optional part of the template <DFN>input_number</DFN>
  374. * is either number of the layer input, either label one.
  375. * If this part is omitted then the first layer input will be used.
  376. *
  377. * @see setNetInputs(), Layer::inputNameToIndex(), Layer::outputNameToIndex()
  378. */
  379. CV_WRAP void connect(String outPin, String inpPin);
  380. /** @brief Connects #@p outNum output of the first layer to #@p inNum input of the second layer.
  381. * @param outLayerId identifier of the first layer
  382. * @param outNum number of the first layer output
  383. * @param inpLayerId identifier of the second layer
  384. * @param inpNum number of the second layer input
  385. */
  386. void connect(int outLayerId, int outNum, int inpLayerId, int inpNum);
  387. /** @brief Sets outputs names of the network input pseudo layer.
  388. *
  389. * Each net always has special own the network input pseudo layer with id=0.
  390. * This layer stores the user blobs only and don't make any computations.
  391. * In fact, this layer provides the only way to pass user data into the network.
  392. * As any other layer, this layer can label its outputs and this function provides an easy way to do this.
  393. */
  394. CV_WRAP void setInputsNames(const std::vector<String> &inputBlobNames);
  395. /** @brief Runs forward pass to compute output of layer with name @p outputName.
  396. * @param outputName name for layer which output is needed to get
  397. * @return blob for first output of specified layer.
  398. * @details By default runs forward pass for the whole network.
  399. */
  400. CV_WRAP Mat forward(const String& outputName = String());
  401. /** @brief Runs forward pass to compute output of layer with name @p outputName.
  402. * @param outputName name for layer which output is needed to get
  403. * @details By default runs forward pass for the whole network.
  404. *
  405. * This is an asynchronous version of forward(const String&).
  406. * dnn::DNN_BACKEND_INFERENCE_ENGINE backend is required.
  407. */
  408. CV_WRAP AsyncArray forwardAsync(const String& outputName = String());
  409. /** @brief Runs forward pass to compute output of layer with name @p outputName.
  410. * @param outputBlobs contains all output blobs for specified layer.
  411. * @param outputName name for layer which output is needed to get
  412. * @details If @p outputName is empty, runs forward pass for the whole network.
  413. */
  414. CV_WRAP void forward(OutputArrayOfArrays outputBlobs, const String& outputName = String());
  415. /** @brief Runs forward pass to compute outputs of layers listed in @p outBlobNames.
  416. * @param outputBlobs contains blobs for first outputs of specified layers.
  417. * @param outBlobNames names for layers which outputs are needed to get
  418. */
  419. CV_WRAP void forward(OutputArrayOfArrays outputBlobs,
  420. const std::vector<String>& outBlobNames);
  421. /** @brief Runs forward pass to compute outputs of layers listed in @p outBlobNames.
  422. * @param outputBlobs contains all output blobs for each layer specified in @p outBlobNames.
  423. * @param outBlobNames names for layers which outputs are needed to get
  424. */
  425. CV_WRAP_AS(forwardAndRetrieve) void forward(CV_OUT std::vector<std::vector<Mat> >& outputBlobs,
  426. const std::vector<String>& outBlobNames);
  427. /**
  428. * @brief Compile Halide layers.
  429. * @param[in] scheduler Path to YAML file with scheduling directives.
  430. * @see setPreferableBackend
  431. *
  432. * Schedule layers that support Halide backend. Then compile them for
  433. * specific target. For layers that not represented in scheduling file
  434. * or if no manual scheduling used at all, automatic scheduling will be applied.
  435. */
  436. CV_WRAP void setHalideScheduler(const String& scheduler);
  437. /**
  438. * @brief Ask network to use specific computation backend where it supported.
  439. * @param[in] backendId backend identifier.
  440. * @see Backend
  441. *
  442. * If OpenCV is compiled with Intel's Inference Engine library, DNN_BACKEND_DEFAULT
  443. * means DNN_BACKEND_INFERENCE_ENGINE. Otherwise it equals to DNN_BACKEND_OPENCV.
  444. */
  445. CV_WRAP void setPreferableBackend(int backendId);
  446. /**
  447. * @brief Ask network to make computations on specific target device.
  448. * @param[in] targetId target identifier.
  449. * @see Target
  450. *
  451. * List of supported combinations backend / target:
  452. * | | DNN_BACKEND_OPENCV | DNN_BACKEND_INFERENCE_ENGINE | DNN_BACKEND_HALIDE |
  453. * |------------------------|--------------------|------------------------------|--------------------|
  454. * | DNN_TARGET_CPU | + | + | + |
  455. * | DNN_TARGET_OPENCL | + | + | + |
  456. * | DNN_TARGET_OPENCL_FP16 | + | + | |
  457. * | DNN_TARGET_MYRIAD | | + | |
  458. * | DNN_TARGET_FPGA | | + | |
  459. */
  460. CV_WRAP void setPreferableTarget(int targetId);
  461. /** @brief Sets the new input value for the network
  462. * @param blob A new blob. Should have CV_32F or CV_8U depth.
  463. * @param name A name of input layer.
  464. * @param scalefactor An optional normalization scale.
  465. * @param mean An optional mean subtraction values.
  466. * @see connect(String, String) to know format of the descriptor.
  467. *
  468. * If scale or mean values are specified, a final input blob is computed
  469. * as:
  470. * \f[input(n,c,h,w) = scalefactor \times (blob(n,c,h,w) - mean_c)\f]
  471. */
  472. CV_WRAP void setInput(InputArray blob, const String& name = "",
  473. double scalefactor = 1.0, const Scalar& mean = Scalar());
  474. /** @brief Sets the new value for the learned param of the layer.
  475. * @param layer name or id of the layer.
  476. * @param numParam index of the layer parameter in the Layer::blobs array.
  477. * @param blob the new value.
  478. * @see Layer::blobs
  479. * @note If shape of the new blob differs from the previous shape,
  480. * then the following forward pass may fail.
  481. */
  482. CV_WRAP void setParam(LayerId layer, int numParam, const Mat &blob);
  483. /** @brief Returns parameter blob of the layer.
  484. * @param layer name or id of the layer.
  485. * @param numParam index of the layer parameter in the Layer::blobs array.
  486. * @see Layer::blobs
  487. */
  488. CV_WRAP Mat getParam(LayerId layer, int numParam = 0);
  489. /** @brief Returns indexes of layers with unconnected outputs.
  490. */
  491. CV_WRAP std::vector<int> getUnconnectedOutLayers() const;
  492. /** @brief Returns names of layers with unconnected outputs.
  493. */
  494. CV_WRAP std::vector<String> getUnconnectedOutLayersNames() const;
  495. /** @brief Returns input and output shapes for all layers in loaded model;
  496. * preliminary inferencing isn't necessary.
  497. * @param netInputShapes shapes for all input blobs in net input layer.
  498. * @param layersIds output parameter for layer IDs.
  499. * @param inLayersShapes output parameter for input layers shapes;
  500. * order is the same as in layersIds
  501. * @param outLayersShapes output parameter for output layers shapes;
  502. * order is the same as in layersIds
  503. */
  504. CV_WRAP void getLayersShapes(const std::vector<MatShape>& netInputShapes,
  505. CV_OUT std::vector<int>& layersIds,
  506. CV_OUT std::vector<std::vector<MatShape> >& inLayersShapes,
  507. CV_OUT std::vector<std::vector<MatShape> >& outLayersShapes) const;
  508. /** @overload */
  509. CV_WRAP void getLayersShapes(const MatShape& netInputShape,
  510. CV_OUT std::vector<int>& layersIds,
  511. CV_OUT std::vector<std::vector<MatShape> >& inLayersShapes,
  512. CV_OUT std::vector<std::vector<MatShape> >& outLayersShapes) const;
  513. /** @brief Returns input and output shapes for layer with specified
  514. * id in loaded model; preliminary inferencing isn't necessary.
  515. * @param netInputShape shape input blob in net input layer.
  516. * @param layerId id for layer.
  517. * @param inLayerShapes output parameter for input layers shapes;
  518. * order is the same as in layersIds
  519. * @param outLayerShapes output parameter for output layers shapes;
  520. * order is the same as in layersIds
  521. */
  522. void getLayerShapes(const MatShape& netInputShape,
  523. const int layerId,
  524. CV_OUT std::vector<MatShape>& inLayerShapes,
  525. CV_OUT std::vector<MatShape>& outLayerShapes) const; // FIXIT: CV_WRAP
  526. /** @overload */
  527. void getLayerShapes(const std::vector<MatShape>& netInputShapes,
  528. const int layerId,
  529. CV_OUT std::vector<MatShape>& inLayerShapes,
  530. CV_OUT std::vector<MatShape>& outLayerShapes) const; // FIXIT: CV_WRAP
  531. /** @brief Computes FLOP for whole loaded model with specified input shapes.
  532. * @param netInputShapes vector of shapes for all net inputs.
  533. * @returns computed FLOP.
  534. */
  535. CV_WRAP int64 getFLOPS(const std::vector<MatShape>& netInputShapes) const;
  536. /** @overload */
  537. CV_WRAP int64 getFLOPS(const MatShape& netInputShape) const;
  538. /** @overload */
  539. CV_WRAP int64 getFLOPS(const int layerId,
  540. const std::vector<MatShape>& netInputShapes) const;
  541. /** @overload */
  542. CV_WRAP int64 getFLOPS(const int layerId,
  543. const MatShape& netInputShape) const;
  544. /** @brief Returns list of types for layer used in model.
  545. * @param layersTypes output parameter for returning types.
  546. */
  547. CV_WRAP void getLayerTypes(CV_OUT std::vector<String>& layersTypes) const;
  548. /** @brief Returns count of layers of specified type.
  549. * @param layerType type.
  550. * @returns count of layers
  551. */
  552. CV_WRAP int getLayersCount(const String& layerType) const;
  553. /** @brief Computes bytes number which are required to store
  554. * all weights and intermediate blobs for model.
  555. * @param netInputShapes vector of shapes for all net inputs.
  556. * @param weights output parameter to store resulting bytes for weights.
  557. * @param blobs output parameter to store resulting bytes for intermediate blobs.
  558. */
  559. void getMemoryConsumption(const std::vector<MatShape>& netInputShapes,
  560. CV_OUT size_t& weights, CV_OUT size_t& blobs) const; // FIXIT: CV_WRAP
  561. /** @overload */
  562. CV_WRAP void getMemoryConsumption(const MatShape& netInputShape,
  563. CV_OUT size_t& weights, CV_OUT size_t& blobs) const;
  564. /** @overload */
  565. CV_WRAP void getMemoryConsumption(const int layerId,
  566. const std::vector<MatShape>& netInputShapes,
  567. CV_OUT size_t& weights, CV_OUT size_t& blobs) const;
  568. /** @overload */
  569. CV_WRAP void getMemoryConsumption(const int layerId,
  570. const MatShape& netInputShape,
  571. CV_OUT size_t& weights, CV_OUT size_t& blobs) const;
  572. /** @brief Computes bytes number which are required to store
  573. * all weights and intermediate blobs for each layer.
  574. * @param netInputShapes vector of shapes for all net inputs.
  575. * @param layerIds output vector to save layer IDs.
  576. * @param weights output parameter to store resulting bytes for weights.
  577. * @param blobs output parameter to store resulting bytes for intermediate blobs.
  578. */
  579. void getMemoryConsumption(const std::vector<MatShape>& netInputShapes,
  580. CV_OUT std::vector<int>& layerIds,
  581. CV_OUT std::vector<size_t>& weights,
  582. CV_OUT std::vector<size_t>& blobs) const; // FIXIT: CV_WRAP
  583. /** @overload */
  584. void getMemoryConsumption(const MatShape& netInputShape,
  585. CV_OUT std::vector<int>& layerIds,
  586. CV_OUT std::vector<size_t>& weights,
  587. CV_OUT std::vector<size_t>& blobs) const; // FIXIT: CV_WRAP
  588. /** @brief Enables or disables layer fusion in the network.
  589. * @param fusion true to enable the fusion, false to disable. The fusion is enabled by default.
  590. */
  591. CV_WRAP void enableFusion(bool fusion);
  592. /** @brief Returns overall time for inference and timings (in ticks) for layers.
  593. * Indexes in returned vector correspond to layers ids. Some layers can be fused with others,
  594. * in this case zero ticks count will be return for that skipped layers.
  595. * @param timings vector for tick timings for all layers.
  596. * @return overall ticks for model inference.
  597. */
  598. CV_WRAP int64 getPerfProfile(CV_OUT std::vector<double>& timings);
  599. private:
  600. struct Impl;
  601. Ptr<Impl> impl;
  602. };
  603. /** @brief Reads a network model stored in <a href="https://pjreddie.com/darknet/">Darknet</a> model files.
  604. * @param cfgFile path to the .cfg file with text description of the network architecture.
  605. * @param darknetModel path to the .weights file with learned network.
  606. * @returns Network object that ready to do forward, throw an exception in failure cases.
  607. * @returns Net object.
  608. */
  609. CV_EXPORTS_W Net readNetFromDarknet(const String &cfgFile, const String &darknetModel = String());
  610. /** @brief Reads a network model stored in <a href="https://pjreddie.com/darknet/">Darknet</a> model files.
  611. * @param bufferCfg A buffer contains a content of .cfg file with text description of the network architecture.
  612. * @param bufferModel A buffer contains a content of .weights file with learned network.
  613. * @returns Net object.
  614. */
  615. CV_EXPORTS_W Net readNetFromDarknet(const std::vector<uchar>& bufferCfg,
  616. const std::vector<uchar>& bufferModel = std::vector<uchar>());
  617. /** @brief Reads a network model stored in <a href="https://pjreddie.com/darknet/">Darknet</a> model files.
  618. * @param bufferCfg A buffer contains a content of .cfg file with text description of the network architecture.
  619. * @param lenCfg Number of bytes to read from bufferCfg
  620. * @param bufferModel A buffer contains a content of .weights file with learned network.
  621. * @param lenModel Number of bytes to read from bufferModel
  622. * @returns Net object.
  623. */
  624. CV_EXPORTS Net readNetFromDarknet(const char *bufferCfg, size_t lenCfg,
  625. const char *bufferModel = NULL, size_t lenModel = 0);
  626. /** @brief Reads a network model stored in <a href="http://caffe.berkeleyvision.org">Caffe</a> framework's format.
  627. * @param prototxt path to the .prototxt file with text description of the network architecture.
  628. * @param caffeModel path to the .caffemodel file with learned network.
  629. * @returns Net object.
  630. */
  631. CV_EXPORTS_W Net readNetFromCaffe(const String &prototxt, const String &caffeModel = String());
  632. /** @brief Reads a network model stored in Caffe model in memory.
  633. * @param bufferProto buffer containing the content of the .prototxt file
  634. * @param bufferModel buffer containing the content of the .caffemodel file
  635. * @returns Net object.
  636. */
  637. CV_EXPORTS_W Net readNetFromCaffe(const std::vector<uchar>& bufferProto,
  638. const std::vector<uchar>& bufferModel = std::vector<uchar>());
  639. /** @brief Reads a network model stored in Caffe model in memory.
  640. * @details This is an overloaded member function, provided for convenience.
  641. * It differs from the above function only in what argument(s) it accepts.
  642. * @param bufferProto buffer containing the content of the .prototxt file
  643. * @param lenProto length of bufferProto
  644. * @param bufferModel buffer containing the content of the .caffemodel file
  645. * @param lenModel length of bufferModel
  646. * @returns Net object.
  647. */
  648. CV_EXPORTS Net readNetFromCaffe(const char *bufferProto, size_t lenProto,
  649. const char *bufferModel = NULL, size_t lenModel = 0);
  650. /** @brief Reads a network model stored in <a href="https://www.tensorflow.org/">TensorFlow</a> framework's format.
  651. * @param model path to the .pb file with binary protobuf description of the network architecture
  652. * @param config path to the .pbtxt file that contains text graph definition in protobuf format.
  653. * Resulting Net object is built by text graph using weights from a binary one that
  654. * let us make it more flexible.
  655. * @returns Net object.
  656. */
  657. CV_EXPORTS_W Net readNetFromTensorflow(const String &model, const String &config = String());
  658. /** @brief Reads a network model stored in <a href="https://www.tensorflow.org/">TensorFlow</a> framework's format.
  659. * @param bufferModel buffer containing the content of the pb file
  660. * @param bufferConfig buffer containing the content of the pbtxt file
  661. * @returns Net object.
  662. */
  663. CV_EXPORTS_W Net readNetFromTensorflow(const std::vector<uchar>& bufferModel,
  664. const std::vector<uchar>& bufferConfig = std::vector<uchar>());
  665. /** @brief Reads a network model stored in <a href="https://www.tensorflow.org/">TensorFlow</a> framework's format.
  666. * @details This is an overloaded member function, provided for convenience.
  667. * It differs from the above function only in what argument(s) it accepts.
  668. * @param bufferModel buffer containing the content of the pb file
  669. * @param lenModel length of bufferModel
  670. * @param bufferConfig buffer containing the content of the pbtxt file
  671. * @param lenConfig length of bufferConfig
  672. */
  673. CV_EXPORTS Net readNetFromTensorflow(const char *bufferModel, size_t lenModel,
  674. const char *bufferConfig = NULL, size_t lenConfig = 0);
  675. /**
  676. * @brief Reads a network model stored in <a href="http://torch.ch">Torch7</a> framework's format.
  677. * @param model path to the file, dumped from Torch by using torch.save() function.
  678. * @param isBinary specifies whether the network was serialized in ascii mode or binary.
  679. * @param evaluate specifies testing phase of network. If true, it's similar to evaluate() method in Torch.
  680. * @returns Net object.
  681. *
  682. * @note Ascii mode of Torch serializer is more preferable, because binary mode extensively use `long` type of C language,
  683. * which has various bit-length on different systems.
  684. *
  685. * The loading file must contain serialized <a href="https://github.com/torch/nn/blob/master/doc/module.md">nn.Module</a> object
  686. * with importing network. Try to eliminate a custom objects from serialazing data to avoid importing errors.
  687. *
  688. * List of supported layers (i.e. object instances derived from Torch nn.Module class):
  689. * - nn.Sequential
  690. * - nn.Parallel
  691. * - nn.Concat
  692. * - nn.Linear
  693. * - nn.SpatialConvolution
  694. * - nn.SpatialMaxPooling, nn.SpatialAveragePooling
  695. * - nn.ReLU, nn.TanH, nn.Sigmoid
  696. * - nn.Reshape
  697. * - nn.SoftMax, nn.LogSoftMax
  698. *
  699. * Also some equivalents of these classes from cunn, cudnn, and fbcunn may be successfully imported.
  700. */
  701. CV_EXPORTS_W Net readNetFromTorch(const String &model, bool isBinary = true, bool evaluate = true);
  702. /**
  703. * @brief Read deep learning network represented in one of the supported formats.
  704. * @param[in] model Binary file contains trained weights. The following file
  705. * extensions are expected for models from different frameworks:
  706. * * `*.caffemodel` (Caffe, http://caffe.berkeleyvision.org/)
  707. * * `*.pb` (TensorFlow, https://www.tensorflow.org/)
  708. * * `*.t7` | `*.net` (Torch, http://torch.ch/)
  709. * * `*.weights` (Darknet, https://pjreddie.com/darknet/)
  710. * * `*.bin` (DLDT, https://software.intel.com/openvino-toolkit)
  711. * * `*.onnx` (ONNX, https://onnx.ai/)
  712. * @param[in] config Text file contains network configuration. It could be a
  713. * file with the following extensions:
  714. * * `*.prototxt` (Caffe, http://caffe.berkeleyvision.org/)
  715. * * `*.pbtxt` (TensorFlow, https://www.tensorflow.org/)
  716. * * `*.cfg` (Darknet, https://pjreddie.com/darknet/)
  717. * * `*.xml` (DLDT, https://software.intel.com/openvino-toolkit)
  718. * @param[in] framework Explicit framework name tag to determine a format.
  719. * @returns Net object.
  720. *
  721. * This function automatically detects an origin framework of trained model
  722. * and calls an appropriate function such @ref readNetFromCaffe, @ref readNetFromTensorflow,
  723. * @ref readNetFromTorch or @ref readNetFromDarknet. An order of @p model and @p config
  724. * arguments does not matter.
  725. */
  726. CV_EXPORTS_W Net readNet(const String& model, const String& config = "", const String& framework = "");
  727. /**
  728. * @brief Read deep learning network represented in one of the supported formats.
  729. * @details This is an overloaded member function, provided for convenience.
  730. * It differs from the above function only in what argument(s) it accepts.
  731. * @param[in] framework Name of origin framework.
  732. * @param[in] bufferModel A buffer with a content of binary file with weights
  733. * @param[in] bufferConfig A buffer with a content of text file contains network configuration.
  734. * @returns Net object.
  735. */
  736. CV_EXPORTS_W Net readNet(const String& framework, const std::vector<uchar>& bufferModel,
  737. const std::vector<uchar>& bufferConfig = std::vector<uchar>());
  738. /** @brief Loads blob which was serialized as torch.Tensor object of Torch7 framework.
  739. * @warning This function has the same limitations as readNetFromTorch().
  740. */
  741. CV_EXPORTS_W Mat readTorchBlob(const String &filename, bool isBinary = true);
  742. /** @brief Load a network from Intel's Model Optimizer intermediate representation.
  743. * @param[in] xml XML configuration file with network's topology.
  744. * @param[in] bin Binary file with trained weights.
  745. * @returns Net object.
  746. * Networks imported from Intel's Model Optimizer are launched in Intel's Inference Engine
  747. * backend.
  748. */
  749. CV_EXPORTS_W Net readNetFromModelOptimizer(const String &xml, const String &bin);
  750. /** @brief Reads a network model <a href="https://onnx.ai/">ONNX</a>.
  751. * @param onnxFile path to the .onnx file with text description of the network architecture.
  752. * @returns Network object that ready to do forward, throw an exception in failure cases.
  753. */
  754. CV_EXPORTS_W Net readNetFromONNX(const String &onnxFile);
  755. /** @brief Reads a network model from <a href="https://onnx.ai/">ONNX</a>
  756. * in-memory buffer.
  757. * @param buffer memory address of the first byte of the buffer.
  758. * @param sizeBuffer size of the buffer.
  759. * @returns Network object that ready to do forward, throw an exception
  760. * in failure cases.
  761. */
  762. CV_EXPORTS Net readNetFromONNX(const char* buffer, size_t sizeBuffer);
  763. /** @brief Reads a network model from <a href="https://onnx.ai/">ONNX</a>
  764. * in-memory buffer.
  765. * @param buffer in-memory buffer that stores the ONNX model bytes.
  766. * @returns Network object that ready to do forward, throw an exception
  767. * in failure cases.
  768. */
  769. CV_EXPORTS_W Net readNetFromONNX(const std::vector<uchar>& buffer);
  770. /** @brief Creates blob from .pb file.
  771. * @param path to the .pb file with input tensor.
  772. * @returns Mat.
  773. */
  774. CV_EXPORTS_W Mat readTensorFromONNX(const String& path);
  775. /** @brief Creates 4-dimensional blob from image. Optionally resizes and crops @p image from center,
  776. * subtract @p mean values, scales values by @p scalefactor, swap Blue and Red channels.
  777. * @param image input image (with 1-, 3- or 4-channels).
  778. * @param size spatial size for output image
  779. * @param mean scalar with mean values which are subtracted from channels. Values are intended
  780. * to be in (mean-R, mean-G, mean-B) order if @p image has BGR ordering and @p swapRB is true.
  781. * @param scalefactor multiplier for @p image values.
  782. * @param swapRB flag which indicates that swap first and last channels
  783. * in 3-channel image is necessary.
  784. * @param crop flag which indicates whether image will be cropped after resize or not
  785. * @param ddepth Depth of output blob. Choose CV_32F or CV_8U.
  786. * @details if @p crop is true, input image is resized so one side after resize is equal to corresponding
  787. * dimension in @p size and another one is equal or larger. Then, crop from the center is performed.
  788. * If @p crop is false, direct resize without cropping and preserving aspect ratio is performed.
  789. * @returns 4-dimensional Mat with NCHW dimensions order.
  790. */
  791. CV_EXPORTS_W Mat blobFromImage(InputArray image, double scalefactor=1.0, const Size& size = Size(),
  792. const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false,
  793. int ddepth=CV_32F);
  794. /** @brief Creates 4-dimensional blob from image.
  795. * @details This is an overloaded member function, provided for convenience.
  796. * It differs from the above function only in what argument(s) it accepts.
  797. */
  798. CV_EXPORTS void blobFromImage(InputArray image, OutputArray blob, double scalefactor=1.0,
  799. const Size& size = Size(), const Scalar& mean = Scalar(),
  800. bool swapRB=false, bool crop=false, int ddepth=CV_32F);
  801. /** @brief Creates 4-dimensional blob from series of images. Optionally resizes and
  802. * crops @p images from center, subtract @p mean values, scales values by @p scalefactor,
  803. * swap Blue and Red channels.
  804. * @param images input images (all with 1-, 3- or 4-channels).
  805. * @param size spatial size for output image
  806. * @param mean scalar with mean values which are subtracted from channels. Values are intended
  807. * to be in (mean-R, mean-G, mean-B) order if @p image has BGR ordering and @p swapRB is true.
  808. * @param scalefactor multiplier for @p images values.
  809. * @param swapRB flag which indicates that swap first and last channels
  810. * in 3-channel image is necessary.
  811. * @param crop flag which indicates whether image will be cropped after resize or not
  812. * @param ddepth Depth of output blob. Choose CV_32F or CV_8U.
  813. * @details if @p crop is true, input image is resized so one side after resize is equal to corresponding
  814. * dimension in @p size and another one is equal or larger. Then, crop from the center is performed.
  815. * If @p crop is false, direct resize without cropping and preserving aspect ratio is performed.
  816. * @returns 4-dimensional Mat with NCHW dimensions order.
  817. */
  818. CV_EXPORTS_W Mat blobFromImages(InputArrayOfArrays images, double scalefactor=1.0,
  819. Size size = Size(), const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false,
  820. int ddepth=CV_32F);
  821. /** @brief Creates 4-dimensional blob from series of images.
  822. * @details This is an overloaded member function, provided for convenience.
  823. * It differs from the above function only in what argument(s) it accepts.
  824. */
  825. CV_EXPORTS void blobFromImages(InputArrayOfArrays images, OutputArray blob,
  826. double scalefactor=1.0, Size size = Size(),
  827. const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false,
  828. int ddepth=CV_32F);
  829. /** @brief Parse a 4D blob and output the images it contains as 2D arrays through a simpler data structure
  830. * (std::vector<cv::Mat>).
  831. * @param[in] blob_ 4 dimensional array (images, channels, height, width) in floating point precision (CV_32F) from
  832. * which you would like to extract the images.
  833. * @param[out] images_ array of 2D Mat containing the images extracted from the blob in floating point precision
  834. * (CV_32F). They are non normalized neither mean added. The number of returned images equals the first dimension
  835. * of the blob (batch size). Every image has a number of channels equals to the second dimension of the blob (depth).
  836. */
  837. CV_EXPORTS_W void imagesFromBlob(const cv::Mat& blob_, OutputArrayOfArrays images_);
  838. /** @brief Convert all weights of Caffe network to half precision floating point.
  839. * @param src Path to origin model from Caffe framework contains single
  840. * precision floating point weights (usually has `.caffemodel` extension).
  841. * @param dst Path to destination model with updated weights.
  842. * @param layersTypes Set of layers types which parameters will be converted.
  843. * By default, converts only Convolutional and Fully-Connected layers'
  844. * weights.
  845. *
  846. * @note Shrinked model has no origin float32 weights so it can't be used
  847. * in origin Caffe framework anymore. However the structure of data
  848. * is taken from NVidia's Caffe fork: https://github.com/NVIDIA/caffe.
  849. * So the resulting model may be used there.
  850. */
  851. CV_EXPORTS_W void shrinkCaffeModel(const String& src, const String& dst,
  852. const std::vector<String>& layersTypes = std::vector<String>());
  853. /** @brief Create a text representation for a binary network stored in protocol buffer format.
  854. * @param[in] model A path to binary network.
  855. * @param[in] output A path to output text file to be created.
  856. *
  857. * @note To reduce output file size, trained weights are not included.
  858. */
  859. CV_EXPORTS_W void writeTextGraph(const String& model, const String& output);
  860. /** @brief Performs non maximum suppression given boxes and corresponding scores.
  861. * @param bboxes a set of bounding boxes to apply NMS.
  862. * @param scores a set of corresponding confidences.
  863. * @param score_threshold a threshold used to filter boxes by score.
  864. * @param nms_threshold a threshold used in non maximum suppression.
  865. * @param indices the kept indices of bboxes after NMS.
  866. * @param eta a coefficient in adaptive threshold formula: \f$nms\_threshold_{i+1}=eta\cdot nms\_threshold_i\f$.
  867. * @param top_k if `>0`, keep at most @p top_k picked indices.
  868. */
  869. CV_EXPORTS_W void NMSBoxes(const std::vector<Rect>& bboxes, const std::vector<float>& scores,
  870. const float score_threshold, const float nms_threshold,
  871. CV_OUT std::vector<int>& indices,
  872. const float eta = 1.f, const int top_k = 0);
  873. CV_EXPORTS_W void NMSBoxes(const std::vector<Rect2d>& bboxes, const std::vector<float>& scores,
  874. const float score_threshold, const float nms_threshold,
  875. CV_OUT std::vector<int>& indices,
  876. const float eta = 1.f, const int top_k = 0);
  877. CV_EXPORTS_AS(NMSBoxesRotated) void NMSBoxes(const std::vector<RotatedRect>& bboxes, const std::vector<float>& scores,
  878. const float score_threshold, const float nms_threshold,
  879. CV_OUT std::vector<int>& indices,
  880. const float eta = 1.f, const int top_k = 0);
  881. //! @}
  882. CV__DNN_INLINE_NS_END
  883. }
  884. }
  885. #include <opencv2/dnn/layer.hpp>
  886. #include <opencv2/dnn/dnn.inl.hpp>
  887. /// @deprecated Include this header directly from application. Automatic inclusion will be removed
  888. #include <opencv2/dnn/utils/inference_engine.hpp>
  889. #endif /* OPENCV_DNN_DNN_HPP */