dnn.hpp 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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 = 0,
  64. DNN_BACKEND_HALIDE,
  65. DNN_BACKEND_INFERENCE_ENGINE, //!< Intel's Inference Engine computational backend
  66. //!< @sa setInferenceEngineBackendType
  67. DNN_BACKEND_OPENCV,
  68. DNN_BACKEND_VKCOM,
  69. DNN_BACKEND_CUDA,
  70. #ifdef __OPENCV_BUILD
  71. DNN_BACKEND_INFERENCE_ENGINE_NGRAPH = 1000000, // internal - use DNN_BACKEND_INFERENCE_ENGINE + setInferenceEngineBackendType()
  72. DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019, // internal - use DNN_BACKEND_INFERENCE_ENGINE + setInferenceEngineBackendType()
  73. #endif
  74. };
  75. /**
  76. * @brief Enum of target devices for computations.
  77. * @see Net::setPreferableTarget
  78. */
  79. enum Target
  80. {
  81. DNN_TARGET_CPU = 0,
  82. DNN_TARGET_OPENCL,
  83. DNN_TARGET_OPENCL_FP16,
  84. DNN_TARGET_MYRIAD,
  85. DNN_TARGET_VULKAN,
  86. DNN_TARGET_FPGA, //!< FPGA device with CPU fallbacks using Inference Engine's Heterogeneous plugin.
  87. DNN_TARGET_CUDA,
  88. DNN_TARGET_CUDA_FP16,
  89. DNN_TARGET_HDDL
  90. };
  91. CV_EXPORTS std::vector< std::pair<Backend, Target> > getAvailableBackends();
  92. CV_EXPORTS_W std::vector<Target> getAvailableTargets(dnn::Backend be);
  93. /** @brief This class provides all data needed to initialize layer.
  94. *
  95. * It includes dictionary with scalar params (which can be read by using Dict interface),
  96. * blob params #blobs and optional meta information: #name and #type of layer instance.
  97. */
  98. class CV_EXPORTS LayerParams : public Dict
  99. {
  100. public:
  101. //TODO: Add ability to name blob params
  102. std::vector<Mat> blobs; //!< List of learned parameters stored as blobs.
  103. String name; //!< Name of the layer instance (optional, can be used internal purposes).
  104. String type; //!< Type name which was used for creating layer by layer factory (optional).
  105. };
  106. /**
  107. * @brief Derivatives of this class encapsulates functions of certain backends.
  108. */
  109. class BackendNode
  110. {
  111. public:
  112. BackendNode(int backendId);
  113. virtual ~BackendNode(); //!< Virtual destructor to make polymorphism.
  114. int backendId; //!< Backend identifier.
  115. };
  116. /**
  117. * @brief Derivatives of this class wraps cv::Mat for different backends and targets.
  118. */
  119. class BackendWrapper
  120. {
  121. public:
  122. BackendWrapper(int backendId, int targetId);
  123. /**
  124. * @brief Wrap cv::Mat for specific backend and target.
  125. * @param[in] targetId Target identifier.
  126. * @param[in] m cv::Mat for wrapping.
  127. *
  128. * Make CPU->GPU data transfer if it's require for the target.
  129. */
  130. BackendWrapper(int targetId, const cv::Mat& m);
  131. /**
  132. * @brief Make wrapper for reused cv::Mat.
  133. * @param[in] base Wrapper of cv::Mat that will be reused.
  134. * @param[in] shape Specific shape.
  135. *
  136. * Initialize wrapper from another one. It'll wrap the same host CPU
  137. * memory and mustn't allocate memory on device(i.e. GPU). It might
  138. * has different shape. Use in case of CPU memory reusing for reuse
  139. * associated memory on device too.
  140. */
  141. BackendWrapper(const Ptr<BackendWrapper>& base, const MatShape& shape);
  142. virtual ~BackendWrapper(); //!< Virtual destructor to make polymorphism.
  143. /**
  144. * @brief Transfer data to CPU host memory.
  145. */
  146. virtual void copyToHost() = 0;
  147. /**
  148. * @brief Indicate that an actual data is on CPU.
  149. */
  150. virtual void setHostDirty() = 0;
  151. int backendId; //!< Backend identifier.
  152. int targetId; //!< Target identifier.
  153. };
  154. class CV_EXPORTS ActivationLayer;
  155. /** @brief This interface class allows to build new Layers - are building blocks of networks.
  156. *
  157. * Each class, derived from Layer, must implement allocate() methods to declare own outputs and forward() to compute outputs.
  158. * Also before using the new layer into networks you must register your layer by using one of @ref dnnLayerFactory "LayerFactory" macros.
  159. */
  160. class CV_EXPORTS_W Layer : public Algorithm
  161. {
  162. public:
  163. //! List of learned parameters must be stored here to allow read them by using Net::getParam().
  164. CV_PROP_RW std::vector<Mat> blobs;
  165. /** @brief Computes and sets internal parameters according to inputs, outputs and blobs.
  166. * @deprecated Use Layer::finalize(InputArrayOfArrays, OutputArrayOfArrays) instead
  167. * @param[in] input vector of already allocated input blobs
  168. * @param[out] output 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_DEPRECATED_EXTERNAL
  174. virtual void finalize(const std::vector<Mat*> &input, std::vector<Mat> &output);
  175. /** @brief Computes and sets internal parameters according to inputs, outputs and blobs.
  176. * @param[in] inputs vector of already allocated input blobs
  177. * @param[out] outputs vector of already allocated output blobs
  178. *
  179. * If this method is called after network has allocated all memory for input and output blobs
  180. * and before inferencing.
  181. */
  182. CV_WRAP virtual void finalize(InputArrayOfArrays inputs, OutputArrayOfArrays outputs);
  183. /** @brief Given the @p input blobs, computes the output @p blobs.
  184. * @deprecated Use Layer::forward(InputArrayOfArrays, OutputArrayOfArrays, OutputArrayOfArrays) instead
  185. * @param[in] input the input blobs.
  186. * @param[out] output allocated output blobs, which will store results of the computation.
  187. * @param[out] internals allocated internal blobs
  188. */
  189. CV_DEPRECATED_EXTERNAL
  190. virtual void forward(std::vector<Mat*> &input, std::vector<Mat> &output, std::vector<Mat> &internals);
  191. /** @brief Given the @p input blobs, computes the output @p blobs.
  192. * @param[in] inputs the input blobs.
  193. * @param[out] outputs allocated output blobs, which will store results of the computation.
  194. * @param[out] internals allocated internal blobs
  195. */
  196. virtual void forward(InputArrayOfArrays inputs, OutputArrayOfArrays outputs, OutputArrayOfArrays internals);
  197. /** @brief Given the @p input blobs, computes the output @p blobs.
  198. * @param[in] inputs the input blobs.
  199. * @param[out] outputs allocated output blobs, which will store results of the computation.
  200. * @param[out] internals allocated internal blobs
  201. */
  202. void forward_fallback(InputArrayOfArrays inputs, OutputArrayOfArrays outputs, OutputArrayOfArrays internals);
  203. /** @brief
  204. * @overload
  205. * @deprecated Use Layer::finalize(InputArrayOfArrays, OutputArrayOfArrays) instead
  206. */
  207. CV_DEPRECATED_EXTERNAL
  208. void finalize(const std::vector<Mat> &inputs, CV_OUT std::vector<Mat> &outputs);
  209. /** @brief
  210. * @overload
  211. * @deprecated Use Layer::finalize(InputArrayOfArrays, OutputArrayOfArrays) instead
  212. */
  213. CV_DEPRECATED std::vector<Mat> finalize(const std::vector<Mat> &inputs);
  214. /** @brief Allocates layer and computes output.
  215. * @deprecated This method will be removed in the future release.
  216. */
  217. CV_DEPRECATED CV_WRAP void run(const std::vector<Mat> &inputs, CV_OUT std::vector<Mat> &outputs,
  218. CV_IN_OUT std::vector<Mat> &internals);
  219. /** @brief Returns index of input blob into the input array.
  220. * @param inputName label of input blob
  221. *
  222. * Each layer input and output can be labeled to easily identify them using "%<layer_name%>[.output_name]" notation.
  223. * This method maps label of input blob to its index into input vector.
  224. */
  225. virtual int inputNameToIndex(String inputName);
  226. /** @brief Returns index of output blob in output array.
  227. * @see inputNameToIndex()
  228. */
  229. CV_WRAP virtual int outputNameToIndex(const String& outputName);
  230. /**
  231. * @brief Ask layer if it support specific backend for doing computations.
  232. * @param[in] backendId computation backend identifier.
  233. * @see Backend
  234. */
  235. virtual bool supportBackend(int backendId);
  236. /**
  237. * @brief Returns Halide backend node.
  238. * @param[in] inputs Input Halide buffers.
  239. * @see BackendNode, BackendWrapper
  240. *
  241. * Input buffers should be exactly the same that will be used in forward invocations.
  242. * Despite we can use Halide::ImageParam based on input shape only,
  243. * it helps prevent some memory management issues (if something wrong,
  244. * Halide tests will be failed).
  245. */
  246. virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs);
  247. virtual Ptr<BackendNode> initInfEngine(const std::vector<Ptr<BackendWrapper> > &inputs);
  248. virtual Ptr<BackendNode> initNgraph(const std::vector<Ptr<BackendWrapper> > &inputs, const std::vector<Ptr<BackendNode> >& nodes);
  249. virtual Ptr<BackendNode> initVkCom(const std::vector<Ptr<BackendWrapper> > &inputs);
  250. /**
  251. * @brief Returns a CUDA backend node
  252. *
  253. * @param context void pointer to CSLContext object
  254. * @param inputs layer inputs
  255. * @param outputs layer outputs
  256. */
  257. virtual Ptr<BackendNode> initCUDA(
  258. void *context,
  259. const std::vector<Ptr<BackendWrapper>>& inputs,
  260. const std::vector<Ptr<BackendWrapper>>& outputs
  261. );
  262. /**
  263. * @brief Automatic Halide scheduling based on layer hyper-parameters.
  264. * @param[in] node Backend node with Halide functions.
  265. * @param[in] inputs Blobs that will be used in forward invocations.
  266. * @param[in] outputs Blobs that will be used in forward invocations.
  267. * @param[in] targetId Target identifier
  268. * @see BackendNode, Target
  269. *
  270. * Layer don't use own Halide::Func members because we can have applied
  271. * layers fusing. In this way the fused function should be scheduled.
  272. */
  273. virtual void applyHalideScheduler(Ptr<BackendNode>& node,
  274. const std::vector<Mat*> &inputs,
  275. const std::vector<Mat> &outputs,
  276. int targetId) const;
  277. /**
  278. * @brief Implement layers fusing.
  279. * @param[in] node Backend node of bottom layer.
  280. * @see BackendNode
  281. *
  282. * Actual for graph-based backends. If layer attached successfully,
  283. * returns non-empty cv::Ptr to node of the same backend.
  284. * Fuse only over the last function.
  285. */
  286. virtual Ptr<BackendNode> tryAttach(const Ptr<BackendNode>& node);
  287. /**
  288. * @brief Tries to attach to the layer the subsequent activation layer, i.e. do the layer fusion in a partial case.
  289. * @param[in] layer The subsequent activation layer.
  290. *
  291. * Returns true if the activation layer has been attached successfully.
  292. */
  293. virtual bool setActivation(const Ptr<ActivationLayer>& layer);
  294. /**
  295. * @brief Try to fuse current layer with a next one
  296. * @param[in] top Next layer to be fused.
  297. * @returns True if fusion was performed.
  298. */
  299. virtual bool tryFuse(Ptr<Layer>& top);
  300. /**
  301. * @brief Returns parameters of layers with channel-wise multiplication and addition.
  302. * @param[out] scale Channel-wise multipliers. Total number of values should
  303. * be equal to number of channels.
  304. * @param[out] shift Channel-wise offsets. Total number of values should
  305. * be equal to number of channels.
  306. *
  307. * Some layers can fuse their transformations with further layers.
  308. * In example, convolution + batch normalization. This way base layer
  309. * use weights from layer after it. Fused layer is skipped.
  310. * By default, @p scale and @p shift are empty that means layer has no
  311. * element-wise multiplications or additions.
  312. */
  313. virtual void getScaleShift(Mat& scale, Mat& shift) const;
  314. /**
  315. * @brief "Deattaches" all the layers, attached to particular layer.
  316. */
  317. virtual void unsetAttached();
  318. virtual bool getMemoryShapes(const std::vector<MatShape> &inputs,
  319. const int requiredOutputs,
  320. std::vector<MatShape> &outputs,
  321. std::vector<MatShape> &internals) const;
  322. virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
  323. const std::vector<MatShape> &outputs) const {CV_UNUSED(inputs); CV_UNUSED(outputs); return 0;}
  324. virtual bool updateMemoryShapes(const std::vector<MatShape> &inputs);
  325. CV_PROP String name; //!< Name of the layer instance, can be used for logging or other internal purposes.
  326. CV_PROP String type; //!< Type name which was used for creating layer by layer factory.
  327. CV_PROP int preferableTarget; //!< prefer target for layer forwarding
  328. Layer();
  329. explicit Layer(const LayerParams &params); //!< Initializes only #name, #type and #blobs fields.
  330. void setParamsFrom(const LayerParams &params); //!< Initializes only #name, #type and #blobs fields.
  331. virtual ~Layer();
  332. };
  333. /** @brief This class allows to create and manipulate comprehensive artificial neural networks.
  334. *
  335. * Neural network is presented as directed acyclic graph (DAG), where vertices are Layer instances,
  336. * and edges specify relationships between layers inputs and outputs.
  337. *
  338. * Each network layer has unique integer id and unique string name inside its network.
  339. * LayerId can store either layer name or layer id.
  340. *
  341. * This class supports reference counting of its instances, i. e. copies point to the same instance.
  342. */
  343. class CV_EXPORTS_W_SIMPLE Net
  344. {
  345. public:
  346. CV_WRAP Net(); //!< Default constructor.
  347. CV_WRAP ~Net(); //!< Destructor frees the net only if there aren't references to the net anymore.
  348. /** @brief Create a network from Intel's Model Optimizer intermediate representation (IR).
  349. * @param[in] xml XML configuration file with network's topology.
  350. * @param[in] bin Binary file with trained weights.
  351. * Networks imported from Intel's Model Optimizer are launched in Intel's Inference Engine
  352. * backend.
  353. */
  354. CV_WRAP static Net readFromModelOptimizer(const String& xml, const String& bin);
  355. /** @brief Create a network from Intel's Model Optimizer in-memory buffers with intermediate representation (IR).
  356. * @param[in] bufferModelConfig buffer with model's configuration.
  357. * @param[in] bufferWeights buffer with model's trained weights.
  358. * @returns Net object.
  359. */
  360. CV_WRAP static
  361. Net readFromModelOptimizer(const std::vector<uchar>& bufferModelConfig, const std::vector<uchar>& bufferWeights);
  362. /** @brief Create a network from Intel's Model Optimizer in-memory buffers with intermediate representation (IR).
  363. * @param[in] bufferModelConfigPtr buffer pointer of model's configuration.
  364. * @param[in] bufferModelConfigSize buffer size of model's configuration.
  365. * @param[in] bufferWeightsPtr buffer pointer of model's trained weights.
  366. * @param[in] bufferWeightsSize buffer size of model's trained weights.
  367. * @returns Net object.
  368. */
  369. static
  370. Net readFromModelOptimizer(const uchar* bufferModelConfigPtr, size_t bufferModelConfigSize,
  371. const uchar* bufferWeightsPtr, size_t bufferWeightsSize);
  372. /** Returns true if there are no layers in the network. */
  373. CV_WRAP bool empty() const;
  374. /** @brief Dump net to String
  375. * @returns String with structure, hyperparameters, backend, target and fusion
  376. * Call method after setInput(). To see correct backend, target and fusion run after forward().
  377. */
  378. CV_WRAP String dump();
  379. /** @brief Dump net structure, hyperparameters, backend, target and fusion to dot file
  380. * @param path path to output file with .dot extension
  381. * @see dump()
  382. */
  383. CV_WRAP void dumpToFile(const String& path);
  384. /** @brief Adds new layer to the net.
  385. * @param name unique name of the adding layer.
  386. * @param type typename of the adding layer (type must be registered in LayerRegister).
  387. * @param params parameters which will be used to initialize the creating layer.
  388. * @returns unique identifier of created layer, or -1 if a failure will happen.
  389. */
  390. int addLayer(const String &name, const String &type, LayerParams &params);
  391. /** @brief Adds new layer and connects its first input to the first output of previously added layer.
  392. * @see addLayer()
  393. */
  394. int addLayerToPrev(const String &name, const String &type, LayerParams &params);
  395. /** @brief Converts string name of the layer to the integer identifier.
  396. * @returns id of the layer, or -1 if the layer wasn't found.
  397. */
  398. CV_WRAP int getLayerId(const String &layer);
  399. CV_WRAP std::vector<String> getLayerNames() const;
  400. /** @brief Container for strings and integers. */
  401. typedef DictValue LayerId;
  402. /** @brief Returns pointer to layer with specified id or name which the network use. */
  403. CV_WRAP Ptr<Layer> getLayer(LayerId layerId);
  404. /** @brief Returns pointers to input layers of specific layer. */
  405. std::vector<Ptr<Layer> > getLayerInputs(LayerId layerId); // FIXIT: CV_WRAP
  406. /** @brief Connects output of the first layer to input of the second layer.
  407. * @param outPin descriptor of the first layer output.
  408. * @param inpPin descriptor of the second layer input.
  409. *
  410. * Descriptors have the following template <DFN>&lt;layer_name&gt;[.input_number]</DFN>:
  411. * - the first part of the template <DFN>layer_name</DFN> is string name of the added layer.
  412. * If this part is empty then the network input pseudo layer will be used;
  413. * - the second optional part of the template <DFN>input_number</DFN>
  414. * is either number of the layer input, either label one.
  415. * If this part is omitted then the first layer input will be used.
  416. *
  417. * @see setNetInputs(), Layer::inputNameToIndex(), Layer::outputNameToIndex()
  418. */
  419. CV_WRAP void connect(String outPin, String inpPin);
  420. /** @brief Connects #@p outNum output of the first layer to #@p inNum input of the second layer.
  421. * @param outLayerId identifier of the first layer
  422. * @param outNum number of the first layer output
  423. * @param inpLayerId identifier of the second layer
  424. * @param inpNum number of the second layer input
  425. */
  426. void connect(int outLayerId, int outNum, int inpLayerId, int inpNum);
  427. /** @brief Sets outputs names of the network input pseudo layer.
  428. *
  429. * Each net always has special own the network input pseudo layer with id=0.
  430. * This layer stores the user blobs only and don't make any computations.
  431. * In fact, this layer provides the only way to pass user data into the network.
  432. * As any other layer, this layer can label its outputs and this function provides an easy way to do this.
  433. */
  434. CV_WRAP void setInputsNames(const std::vector<String> &inputBlobNames);
  435. /** @brief Specify shape of network input.
  436. */
  437. CV_WRAP void setInputShape(const String &inputName, const MatShape& shape);
  438. /** @brief Runs forward pass to compute output of layer with name @p outputName.
  439. * @param outputName name for layer which output is needed to get
  440. * @return blob for first output of specified layer.
  441. * @details By default runs forward pass for the whole network.
  442. */
  443. CV_WRAP Mat forward(const String& outputName = String());
  444. /** @brief Runs forward pass to compute output of layer with name @p outputName.
  445. * @param outputName name for layer which output is needed to get
  446. * @details By default runs forward pass for the whole network.
  447. *
  448. * This is an asynchronous version of forward(const String&).
  449. * dnn::DNN_BACKEND_INFERENCE_ENGINE backend is required.
  450. */
  451. CV_WRAP AsyncArray forwardAsync(const String& outputName = String());
  452. /** @brief Runs forward pass to compute output of layer with name @p outputName.
  453. * @param outputBlobs contains all output blobs for specified layer.
  454. * @param outputName name for layer which output is needed to get
  455. * @details If @p outputName is empty, runs forward pass for the whole network.
  456. */
  457. CV_WRAP void forward(OutputArrayOfArrays outputBlobs, const String& outputName = String());
  458. /** @brief Runs forward pass to compute outputs of layers listed in @p outBlobNames.
  459. * @param outputBlobs contains blobs for first outputs of specified layers.
  460. * @param outBlobNames names for layers which outputs are needed to get
  461. */
  462. CV_WRAP void forward(OutputArrayOfArrays outputBlobs,
  463. const std::vector<String>& outBlobNames);
  464. /** @brief Runs forward pass to compute outputs of layers listed in @p outBlobNames.
  465. * @param outputBlobs contains all output blobs for each layer specified in @p outBlobNames.
  466. * @param outBlobNames names for layers which outputs are needed to get
  467. */
  468. CV_WRAP_AS(forwardAndRetrieve) void forward(CV_OUT std::vector<std::vector<Mat> >& outputBlobs,
  469. const std::vector<String>& outBlobNames);
  470. /**
  471. * @brief Compile Halide layers.
  472. * @param[in] scheduler Path to YAML file with scheduling directives.
  473. * @see setPreferableBackend
  474. *
  475. * Schedule layers that support Halide backend. Then compile them for
  476. * specific target. For layers that not represented in scheduling file
  477. * or if no manual scheduling used at all, automatic scheduling will be applied.
  478. */
  479. CV_WRAP void setHalideScheduler(const String& scheduler);
  480. /**
  481. * @brief Ask network to use specific computation backend where it supported.
  482. * @param[in] backendId backend identifier.
  483. * @see Backend
  484. *
  485. * If OpenCV is compiled with Intel's Inference Engine library, DNN_BACKEND_DEFAULT
  486. * means DNN_BACKEND_INFERENCE_ENGINE. Otherwise it equals to DNN_BACKEND_OPENCV.
  487. */
  488. CV_WRAP void setPreferableBackend(int backendId);
  489. /**
  490. * @brief Ask network to make computations on specific target device.
  491. * @param[in] targetId target identifier.
  492. * @see Target
  493. *
  494. * List of supported combinations backend / target:
  495. * | | DNN_BACKEND_OPENCV | DNN_BACKEND_INFERENCE_ENGINE | DNN_BACKEND_HALIDE | DNN_BACKEND_CUDA |
  496. * |------------------------|--------------------|------------------------------|--------------------|-------------------|
  497. * | DNN_TARGET_CPU | + | + | + | |
  498. * | DNN_TARGET_OPENCL | + | + | + | |
  499. * | DNN_TARGET_OPENCL_FP16 | + | + | | |
  500. * | DNN_TARGET_MYRIAD | | + | | |
  501. * | DNN_TARGET_FPGA | | + | | |
  502. * | DNN_TARGET_CUDA | | | | + |
  503. * | DNN_TARGET_CUDA_FP16 | | | | + |
  504. * | DNN_TARGET_HDDL | | + | | |
  505. */
  506. CV_WRAP void setPreferableTarget(int targetId);
  507. /** @brief Sets the new input value for the network
  508. * @param blob A new blob. Should have CV_32F or CV_8U depth.
  509. * @param name A name of input layer.
  510. * @param scalefactor An optional normalization scale.
  511. * @param mean An optional mean subtraction values.
  512. * @see connect(String, String) to know format of the descriptor.
  513. *
  514. * If scale or mean values are specified, a final input blob is computed
  515. * as:
  516. * \f[input(n,c,h,w) = scalefactor \times (blob(n,c,h,w) - mean_c)\f]
  517. */
  518. CV_WRAP void setInput(InputArray blob, const String& name = "",
  519. double scalefactor = 1.0, const Scalar& mean = Scalar());
  520. /** @brief Sets the new value for the learned param of the layer.
  521. * @param layer name or id of the layer.
  522. * @param numParam index of the layer parameter in the Layer::blobs array.
  523. * @param blob the new value.
  524. * @see Layer::blobs
  525. * @note If shape of the new blob differs from the previous shape,
  526. * then the following forward pass may fail.
  527. */
  528. CV_WRAP void setParam(LayerId layer, int numParam, const Mat &blob);
  529. /** @brief Returns parameter blob of the layer.
  530. * @param layer name or id of the layer.
  531. * @param numParam index of the layer parameter in the Layer::blobs array.
  532. * @see Layer::blobs
  533. */
  534. CV_WRAP Mat getParam(LayerId layer, int numParam = 0);
  535. /** @brief Returns indexes of layers with unconnected outputs.
  536. */
  537. CV_WRAP std::vector<int> getUnconnectedOutLayers() const;
  538. /** @brief Returns names of layers with unconnected outputs.
  539. */
  540. CV_WRAP std::vector<String> getUnconnectedOutLayersNames() const;
  541. /** @brief Returns input and output shapes for all layers in loaded model;
  542. * preliminary inferencing isn't necessary.
  543. * @param netInputShapes shapes for all input blobs in net input layer.
  544. * @param layersIds output parameter for layer IDs.
  545. * @param inLayersShapes output parameter for input layers shapes;
  546. * order is the same as in layersIds
  547. * @param outLayersShapes output parameter for output layers shapes;
  548. * order is the same as in layersIds
  549. */
  550. CV_WRAP void getLayersShapes(const std::vector<MatShape>& netInputShapes,
  551. CV_OUT std::vector<int>& layersIds,
  552. CV_OUT std::vector<std::vector<MatShape> >& inLayersShapes,
  553. CV_OUT std::vector<std::vector<MatShape> >& outLayersShapes) const;
  554. /** @overload */
  555. CV_WRAP void getLayersShapes(const MatShape& netInputShape,
  556. CV_OUT std::vector<int>& layersIds,
  557. CV_OUT std::vector<std::vector<MatShape> >& inLayersShapes,
  558. CV_OUT std::vector<std::vector<MatShape> >& outLayersShapes) const;
  559. /** @brief Returns input and output shapes for layer with specified
  560. * id in loaded model; preliminary inferencing isn't necessary.
  561. * @param netInputShape shape input blob in net input layer.
  562. * @param layerId id for layer.
  563. * @param inLayerShapes output parameter for input layers shapes;
  564. * order is the same as in layersIds
  565. * @param outLayerShapes output parameter for output layers shapes;
  566. * order is the same as in layersIds
  567. */
  568. void getLayerShapes(const MatShape& netInputShape,
  569. const int layerId,
  570. CV_OUT std::vector<MatShape>& inLayerShapes,
  571. CV_OUT std::vector<MatShape>& outLayerShapes) const; // FIXIT: CV_WRAP
  572. /** @overload */
  573. void getLayerShapes(const std::vector<MatShape>& netInputShapes,
  574. const int layerId,
  575. CV_OUT std::vector<MatShape>& inLayerShapes,
  576. CV_OUT std::vector<MatShape>& outLayerShapes) const; // FIXIT: CV_WRAP
  577. /** @brief Computes FLOP for whole loaded model with specified input shapes.
  578. * @param netInputShapes vector of shapes for all net inputs.
  579. * @returns computed FLOP.
  580. */
  581. CV_WRAP int64 getFLOPS(const std::vector<MatShape>& netInputShapes) const;
  582. /** @overload */
  583. CV_WRAP int64 getFLOPS(const MatShape& netInputShape) const;
  584. /** @overload */
  585. CV_WRAP int64 getFLOPS(const int layerId,
  586. const std::vector<MatShape>& netInputShapes) const;
  587. /** @overload */
  588. CV_WRAP int64 getFLOPS(const int layerId,
  589. const MatShape& netInputShape) const;
  590. /** @brief Returns list of types for layer used in model.
  591. * @param layersTypes output parameter for returning types.
  592. */
  593. CV_WRAP void getLayerTypes(CV_OUT std::vector<String>& layersTypes) const;
  594. /** @brief Returns count of layers of specified type.
  595. * @param layerType type.
  596. * @returns count of layers
  597. */
  598. CV_WRAP int getLayersCount(const String& layerType) const;
  599. /** @brief Computes bytes number which are required to store
  600. * all weights and intermediate blobs for model.
  601. * @param netInputShapes vector of shapes for all net inputs.
  602. * @param weights output parameter to store resulting bytes for weights.
  603. * @param blobs output parameter to store resulting bytes for intermediate blobs.
  604. */
  605. void getMemoryConsumption(const std::vector<MatShape>& netInputShapes,
  606. CV_OUT size_t& weights, CV_OUT size_t& blobs) const; // FIXIT: CV_WRAP
  607. /** @overload */
  608. CV_WRAP void getMemoryConsumption(const MatShape& netInputShape,
  609. CV_OUT size_t& weights, CV_OUT size_t& blobs) const;
  610. /** @overload */
  611. CV_WRAP void getMemoryConsumption(const int layerId,
  612. const std::vector<MatShape>& netInputShapes,
  613. CV_OUT size_t& weights, CV_OUT size_t& blobs) const;
  614. /** @overload */
  615. CV_WRAP void getMemoryConsumption(const int layerId,
  616. const MatShape& netInputShape,
  617. CV_OUT size_t& weights, CV_OUT size_t& blobs) const;
  618. /** @brief Computes bytes number which are required to store
  619. * all weights and intermediate blobs for each layer.
  620. * @param netInputShapes vector of shapes for all net inputs.
  621. * @param layerIds output vector to save layer IDs.
  622. * @param weights output parameter to store resulting bytes for weights.
  623. * @param blobs output parameter to store resulting bytes for intermediate blobs.
  624. */
  625. void getMemoryConsumption(const std::vector<MatShape>& netInputShapes,
  626. CV_OUT std::vector<int>& layerIds,
  627. CV_OUT std::vector<size_t>& weights,
  628. CV_OUT std::vector<size_t>& blobs) const; // FIXIT: CV_WRAP
  629. /** @overload */
  630. void getMemoryConsumption(const MatShape& netInputShape,
  631. CV_OUT std::vector<int>& layerIds,
  632. CV_OUT std::vector<size_t>& weights,
  633. CV_OUT std::vector<size_t>& blobs) const; // FIXIT: CV_WRAP
  634. /** @brief Enables or disables layer fusion in the network.
  635. * @param fusion true to enable the fusion, false to disable. The fusion is enabled by default.
  636. */
  637. CV_WRAP void enableFusion(bool fusion);
  638. /** @brief Returns overall time for inference and timings (in ticks) for layers.
  639. * Indexes in returned vector correspond to layers ids. Some layers can be fused with others,
  640. * in this case zero ticks count will be return for that skipped layers.
  641. * @param timings vector for tick timings for all layers.
  642. * @return overall ticks for model inference.
  643. */
  644. CV_WRAP int64 getPerfProfile(CV_OUT std::vector<double>& timings);
  645. private:
  646. struct Impl;
  647. Ptr<Impl> impl;
  648. };
  649. /** @brief Reads a network model stored in <a href="https://pjreddie.com/darknet/">Darknet</a> model files.
  650. * @param cfgFile path to the .cfg file with text description of the network architecture.
  651. * @param darknetModel path to the .weights file with learned network.
  652. * @returns Network object that ready to do forward, throw an exception in failure cases.
  653. * @returns Net object.
  654. */
  655. CV_EXPORTS_W Net readNetFromDarknet(const String &cfgFile, const String &darknetModel = String());
  656. /** @brief Reads a network model stored in <a href="https://pjreddie.com/darknet/">Darknet</a> model files.
  657. * @param bufferCfg A buffer contains a content of .cfg file with text description of the network architecture.
  658. * @param bufferModel A buffer contains a content of .weights file with learned network.
  659. * @returns Net object.
  660. */
  661. CV_EXPORTS_W Net readNetFromDarknet(const std::vector<uchar>& bufferCfg,
  662. const std::vector<uchar>& bufferModel = std::vector<uchar>());
  663. /** @brief Reads a network model stored in <a href="https://pjreddie.com/darknet/">Darknet</a> model files.
  664. * @param bufferCfg A buffer contains a content of .cfg file with text description of the network architecture.
  665. * @param lenCfg Number of bytes to read from bufferCfg
  666. * @param bufferModel A buffer contains a content of .weights file with learned network.
  667. * @param lenModel Number of bytes to read from bufferModel
  668. * @returns Net object.
  669. */
  670. CV_EXPORTS Net readNetFromDarknet(const char *bufferCfg, size_t lenCfg,
  671. const char *bufferModel = NULL, size_t lenModel = 0);
  672. /** @brief Reads a network model stored in <a href="http://caffe.berkeleyvision.org">Caffe</a> framework's format.
  673. * @param prototxt path to the .prototxt file with text description of the network architecture.
  674. * @param caffeModel path to the .caffemodel file with learned network.
  675. * @returns Net object.
  676. */
  677. CV_EXPORTS_W Net readNetFromCaffe(const String &prototxt, const String &caffeModel = String());
  678. /** @brief Reads a network model stored in Caffe model in memory.
  679. * @param bufferProto buffer containing the content of the .prototxt file
  680. * @param bufferModel buffer containing the content of the .caffemodel file
  681. * @returns Net object.
  682. */
  683. CV_EXPORTS_W Net readNetFromCaffe(const std::vector<uchar>& bufferProto,
  684. const std::vector<uchar>& bufferModel = std::vector<uchar>());
  685. /** @brief Reads a network model stored in Caffe model in memory.
  686. * @details This is an overloaded member function, provided for convenience.
  687. * It differs from the above function only in what argument(s) it accepts.
  688. * @param bufferProto buffer containing the content of the .prototxt file
  689. * @param lenProto length of bufferProto
  690. * @param bufferModel buffer containing the content of the .caffemodel file
  691. * @param lenModel length of bufferModel
  692. * @returns Net object.
  693. */
  694. CV_EXPORTS Net readNetFromCaffe(const char *bufferProto, size_t lenProto,
  695. const char *bufferModel = NULL, size_t lenModel = 0);
  696. /** @brief Reads a network model stored in <a href="https://www.tensorflow.org/">TensorFlow</a> framework's format.
  697. * @param model path to the .pb file with binary protobuf description of the network architecture
  698. * @param config path to the .pbtxt file that contains text graph definition in protobuf format.
  699. * Resulting Net object is built by text graph using weights from a binary one that
  700. * let us make it more flexible.
  701. * @returns Net object.
  702. */
  703. CV_EXPORTS_W Net readNetFromTensorflow(const String &model, const String &config = String());
  704. /** @brief Reads a network model stored in <a href="https://www.tensorflow.org/">TensorFlow</a> framework's format.
  705. * @param bufferModel buffer containing the content of the pb file
  706. * @param bufferConfig buffer containing the content of the pbtxt file
  707. * @returns Net object.
  708. */
  709. CV_EXPORTS_W Net readNetFromTensorflow(const std::vector<uchar>& bufferModel,
  710. const std::vector<uchar>& bufferConfig = std::vector<uchar>());
  711. /** @brief Reads a network model stored in <a href="https://www.tensorflow.org/">TensorFlow</a> framework's format.
  712. * @details This is an overloaded member function, provided for convenience.
  713. * It differs from the above function only in what argument(s) it accepts.
  714. * @param bufferModel buffer containing the content of the pb file
  715. * @param lenModel length of bufferModel
  716. * @param bufferConfig buffer containing the content of the pbtxt file
  717. * @param lenConfig length of bufferConfig
  718. */
  719. CV_EXPORTS Net readNetFromTensorflow(const char *bufferModel, size_t lenModel,
  720. const char *bufferConfig = NULL, size_t lenConfig = 0);
  721. /**
  722. * @brief Reads a network model stored in <a href="http://torch.ch">Torch7</a> framework's format.
  723. * @param model path to the file, dumped from Torch by using torch.save() function.
  724. * @param isBinary specifies whether the network was serialized in ascii mode or binary.
  725. * @param evaluate specifies testing phase of network. If true, it's similar to evaluate() method in Torch.
  726. * @returns Net object.
  727. *
  728. * @note Ascii mode of Torch serializer is more preferable, because binary mode extensively use `long` type of C language,
  729. * which has various bit-length on different systems.
  730. *
  731. * The loading file must contain serialized <a href="https://github.com/torch/nn/blob/master/doc/module.md">nn.Module</a> object
  732. * with importing network. Try to eliminate a custom objects from serialazing data to avoid importing errors.
  733. *
  734. * List of supported layers (i.e. object instances derived from Torch nn.Module class):
  735. * - nn.Sequential
  736. * - nn.Parallel
  737. * - nn.Concat
  738. * - nn.Linear
  739. * - nn.SpatialConvolution
  740. * - nn.SpatialMaxPooling, nn.SpatialAveragePooling
  741. * - nn.ReLU, nn.TanH, nn.Sigmoid
  742. * - nn.Reshape
  743. * - nn.SoftMax, nn.LogSoftMax
  744. *
  745. * Also some equivalents of these classes from cunn, cudnn, and fbcunn may be successfully imported.
  746. */
  747. CV_EXPORTS_W Net readNetFromTorch(const String &model, bool isBinary = true, bool evaluate = true);
  748. /**
  749. * @brief Read deep learning network represented in one of the supported formats.
  750. * @param[in] model Binary file contains trained weights. The following file
  751. * extensions are expected for models from different frameworks:
  752. * * `*.caffemodel` (Caffe, http://caffe.berkeleyvision.org/)
  753. * * `*.pb` (TensorFlow, https://www.tensorflow.org/)
  754. * * `*.t7` | `*.net` (Torch, http://torch.ch/)
  755. * * `*.weights` (Darknet, https://pjreddie.com/darknet/)
  756. * * `*.bin` (DLDT, https://software.intel.com/openvino-toolkit)
  757. * * `*.onnx` (ONNX, https://onnx.ai/)
  758. * @param[in] config Text file contains network configuration. It could be a
  759. * file with the following extensions:
  760. * * `*.prototxt` (Caffe, http://caffe.berkeleyvision.org/)
  761. * * `*.pbtxt` (TensorFlow, https://www.tensorflow.org/)
  762. * * `*.cfg` (Darknet, https://pjreddie.com/darknet/)
  763. * * `*.xml` (DLDT, https://software.intel.com/openvino-toolkit)
  764. * @param[in] framework Explicit framework name tag to determine a format.
  765. * @returns Net object.
  766. *
  767. * This function automatically detects an origin framework of trained model
  768. * and calls an appropriate function such @ref readNetFromCaffe, @ref readNetFromTensorflow,
  769. * @ref readNetFromTorch or @ref readNetFromDarknet. An order of @p model and @p config
  770. * arguments does not matter.
  771. */
  772. CV_EXPORTS_W Net readNet(const String& model, const String& config = "", const String& framework = "");
  773. /**
  774. * @brief Read deep learning network represented in one of the supported formats.
  775. * @details This is an overloaded member function, provided for convenience.
  776. * It differs from the above function only in what argument(s) it accepts.
  777. * @param[in] framework Name of origin framework.
  778. * @param[in] bufferModel A buffer with a content of binary file with weights
  779. * @param[in] bufferConfig A buffer with a content of text file contains network configuration.
  780. * @returns Net object.
  781. */
  782. CV_EXPORTS_W Net readNet(const String& framework, const std::vector<uchar>& bufferModel,
  783. const std::vector<uchar>& bufferConfig = std::vector<uchar>());
  784. /** @brief Loads blob which was serialized as torch.Tensor object of Torch7 framework.
  785. * @warning This function has the same limitations as readNetFromTorch().
  786. */
  787. CV_EXPORTS_W Mat readTorchBlob(const String &filename, bool isBinary = true);
  788. /** @brief Load a network from Intel's Model Optimizer intermediate representation.
  789. * @param[in] xml XML configuration file with network's topology.
  790. * @param[in] bin Binary file with trained weights.
  791. * @returns Net object.
  792. * Networks imported from Intel's Model Optimizer are launched in Intel's Inference Engine
  793. * backend.
  794. */
  795. CV_EXPORTS_W
  796. Net readNetFromModelOptimizer(const String &xml, const String &bin);
  797. /** @brief Load a network from Intel's Model Optimizer intermediate representation.
  798. * @param[in] bufferModelConfig Buffer contains XML configuration with network's topology.
  799. * @param[in] bufferWeights Buffer contains binary data with trained weights.
  800. * @returns Net object.
  801. * Networks imported from Intel's Model Optimizer are launched in Intel's Inference Engine
  802. * backend.
  803. */
  804. CV_EXPORTS_W
  805. Net readNetFromModelOptimizer(const std::vector<uchar>& bufferModelConfig, const std::vector<uchar>& bufferWeights);
  806. /** @brief Load a network from Intel's Model Optimizer intermediate representation.
  807. * @param[in] bufferModelConfigPtr Pointer to buffer which contains XML configuration with network's topology.
  808. * @param[in] bufferModelConfigSize Binary size of XML configuration data.
  809. * @param[in] bufferWeightsPtr Pointer to buffer which contains binary data with trained weights.
  810. * @param[in] bufferWeightsSize Binary size of trained weights data.
  811. * @returns Net object.
  812. * Networks imported from Intel's Model Optimizer are launched in Intel's Inference Engine
  813. * backend.
  814. */
  815. CV_EXPORTS
  816. Net readNetFromModelOptimizer(const uchar* bufferModelConfigPtr, size_t bufferModelConfigSize,
  817. const uchar* bufferWeightsPtr, size_t bufferWeightsSize);
  818. /** @brief Reads a network model <a href="https://onnx.ai/">ONNX</a>.
  819. * @param onnxFile path to the .onnx file with text description of the network architecture.
  820. * @returns Network object that ready to do forward, throw an exception in failure cases.
  821. */
  822. CV_EXPORTS_W Net readNetFromONNX(const String &onnxFile);
  823. /** @brief Reads a network model from <a href="https://onnx.ai/">ONNX</a>
  824. * in-memory buffer.
  825. * @param buffer memory address of the first byte of the buffer.
  826. * @param sizeBuffer size of the buffer.
  827. * @returns Network object that ready to do forward, throw an exception
  828. * in failure cases.
  829. */
  830. CV_EXPORTS Net readNetFromONNX(const char* buffer, size_t sizeBuffer);
  831. /** @brief Reads a network model from <a href="https://onnx.ai/">ONNX</a>
  832. * in-memory buffer.
  833. * @param buffer in-memory buffer that stores the ONNX model bytes.
  834. * @returns Network object that ready to do forward, throw an exception
  835. * in failure cases.
  836. */
  837. CV_EXPORTS_W Net readNetFromONNX(const std::vector<uchar>& buffer);
  838. /** @brief Creates blob from .pb file.
  839. * @param path to the .pb file with input tensor.
  840. * @returns Mat.
  841. */
  842. CV_EXPORTS_W Mat readTensorFromONNX(const String& path);
  843. /** @brief Creates 4-dimensional blob from image. Optionally resizes and crops @p image from center,
  844. * subtract @p mean values, scales values by @p scalefactor, swap Blue and Red channels.
  845. * @param image input image (with 1-, 3- or 4-channels).
  846. * @param size spatial size for output image
  847. * @param mean scalar with mean values which are subtracted from channels. Values are intended
  848. * to be in (mean-R, mean-G, mean-B) order if @p image has BGR ordering and @p swapRB is true.
  849. * @param scalefactor multiplier for @p image values.
  850. * @param swapRB flag which indicates that swap first and last channels
  851. * in 3-channel image is necessary.
  852. * @param crop flag which indicates whether image will be cropped after resize or not
  853. * @param ddepth Depth of output blob. Choose CV_32F or CV_8U.
  854. * @details if @p crop is true, input image is resized so one side after resize is equal to corresponding
  855. * dimension in @p size and another one is equal or larger. Then, crop from the center is performed.
  856. * If @p crop is false, direct resize without cropping and preserving aspect ratio is performed.
  857. * @returns 4-dimensional Mat with NCHW dimensions order.
  858. */
  859. CV_EXPORTS_W Mat blobFromImage(InputArray image, double scalefactor=1.0, const Size& size = Size(),
  860. const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false,
  861. int ddepth=CV_32F);
  862. /** @brief Creates 4-dimensional blob from image.
  863. * @details This is an overloaded member function, provided for convenience.
  864. * It differs from the above function only in what argument(s) it accepts.
  865. */
  866. CV_EXPORTS void blobFromImage(InputArray image, OutputArray blob, double scalefactor=1.0,
  867. const Size& size = Size(), const Scalar& mean = Scalar(),
  868. bool swapRB=false, bool crop=false, int ddepth=CV_32F);
  869. /** @brief Creates 4-dimensional blob from series of images. Optionally resizes and
  870. * crops @p images from center, subtract @p mean values, scales values by @p scalefactor,
  871. * swap Blue and Red channels.
  872. * @param images input images (all with 1-, 3- or 4-channels).
  873. * @param size spatial size for output image
  874. * @param mean scalar with mean values which are subtracted from channels. Values are intended
  875. * to be in (mean-R, mean-G, mean-B) order if @p image has BGR ordering and @p swapRB is true.
  876. * @param scalefactor multiplier for @p images values.
  877. * @param swapRB flag which indicates that swap first and last channels
  878. * in 3-channel image is necessary.
  879. * @param crop flag which indicates whether image will be cropped after resize or not
  880. * @param ddepth Depth of output blob. Choose CV_32F or CV_8U.
  881. * @details if @p crop is true, input image is resized so one side after resize is equal to corresponding
  882. * dimension in @p size and another one is equal or larger. Then, crop from the center is performed.
  883. * If @p crop is false, direct resize without cropping and preserving aspect ratio is performed.
  884. * @returns 4-dimensional Mat with NCHW dimensions order.
  885. */
  886. CV_EXPORTS_W Mat blobFromImages(InputArrayOfArrays images, double scalefactor=1.0,
  887. Size size = Size(), const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false,
  888. int ddepth=CV_32F);
  889. /** @brief Creates 4-dimensional blob from series of images.
  890. * @details This is an overloaded member function, provided for convenience.
  891. * It differs from the above function only in what argument(s) it accepts.
  892. */
  893. CV_EXPORTS void blobFromImages(InputArrayOfArrays images, OutputArray blob,
  894. double scalefactor=1.0, Size size = Size(),
  895. const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false,
  896. int ddepth=CV_32F);
  897. /** @brief Parse a 4D blob and output the images it contains as 2D arrays through a simpler data structure
  898. * (std::vector<cv::Mat>).
  899. * @param[in] blob_ 4 dimensional array (images, channels, height, width) in floating point precision (CV_32F) from
  900. * which you would like to extract the images.
  901. * @param[out] images_ array of 2D Mat containing the images extracted from the blob in floating point precision
  902. * (CV_32F). They are non normalized neither mean added. The number of returned images equals the first dimension
  903. * of the blob (batch size). Every image has a number of channels equals to the second dimension of the blob (depth).
  904. */
  905. CV_EXPORTS_W void imagesFromBlob(const cv::Mat& blob_, OutputArrayOfArrays images_);
  906. /** @brief Convert all weights of Caffe network to half precision floating point.
  907. * @param src Path to origin model from Caffe framework contains single
  908. * precision floating point weights (usually has `.caffemodel` extension).
  909. * @param dst Path to destination model with updated weights.
  910. * @param layersTypes Set of layers types which parameters will be converted.
  911. * By default, converts only Convolutional and Fully-Connected layers'
  912. * weights.
  913. *
  914. * @note Shrinked model has no origin float32 weights so it can't be used
  915. * in origin Caffe framework anymore. However the structure of data
  916. * is taken from NVidia's Caffe fork: https://github.com/NVIDIA/caffe.
  917. * So the resulting model may be used there.
  918. */
  919. CV_EXPORTS_W void shrinkCaffeModel(const String& src, const String& dst,
  920. const std::vector<String>& layersTypes = std::vector<String>());
  921. /** @brief Create a text representation for a binary network stored in protocol buffer format.
  922. * @param[in] model A path to binary network.
  923. * @param[in] output A path to output text file to be created.
  924. *
  925. * @note To reduce output file size, trained weights are not included.
  926. */
  927. CV_EXPORTS_W void writeTextGraph(const String& model, const String& output);
  928. /** @brief Performs non maximum suppression given boxes and corresponding scores.
  929. * @param bboxes a set of bounding boxes to apply NMS.
  930. * @param scores a set of corresponding confidences.
  931. * @param score_threshold a threshold used to filter boxes by score.
  932. * @param nms_threshold a threshold used in non maximum suppression.
  933. * @param indices the kept indices of bboxes after NMS.
  934. * @param eta a coefficient in adaptive threshold formula: \f$nms\_threshold_{i+1}=eta\cdot nms\_threshold_i\f$.
  935. * @param top_k if `>0`, keep at most @p top_k picked indices.
  936. */
  937. CV_EXPORTS void NMSBoxes(const std::vector<Rect>& bboxes, const std::vector<float>& scores,
  938. const float score_threshold, const float nms_threshold,
  939. CV_OUT std::vector<int>& indices,
  940. const float eta = 1.f, const int top_k = 0);
  941. CV_EXPORTS_W void NMSBoxes(const std::vector<Rect2d>& bboxes, const std::vector<float>& scores,
  942. const float score_threshold, const float nms_threshold,
  943. CV_OUT std::vector<int>& indices,
  944. const float eta = 1.f, const int top_k = 0);
  945. CV_EXPORTS_AS(NMSBoxesRotated) void NMSBoxes(const std::vector<RotatedRect>& bboxes, const std::vector<float>& scores,
  946. const float score_threshold, const float nms_threshold,
  947. CV_OUT std::vector<int>& indices,
  948. const float eta = 1.f, const int top_k = 0);
  949. /** @brief This class is presented high-level API for neural networks.
  950. *
  951. * Model allows to set params for preprocessing input image.
  952. * Model creates net from file with trained weights and config,
  953. * sets preprocessing input and runs forward pass.
  954. */
  955. class CV_EXPORTS_W_SIMPLE Model
  956. {
  957. public:
  958. CV_DEPRECATED_EXTERNAL // avoid using in C++ code, will be moved to "protected" (need to fix bindings first)
  959. Model();
  960. Model(const Model&) = default;
  961. Model(Model&&) = default;
  962. Model& operator=(const Model&) = default;
  963. Model& operator=(Model&&) = default;
  964. /**
  965. * @brief Create model from deep learning network represented in one of the supported formats.
  966. * An order of @p model and @p config arguments does not matter.
  967. * @param[in] model Binary file contains trained weights.
  968. * @param[in] config Text file contains network configuration.
  969. */
  970. CV_WRAP Model(const String& model, const String& config = "");
  971. /**
  972. * @brief Create model from deep learning network.
  973. * @param[in] network Net object.
  974. */
  975. CV_WRAP Model(const Net& network);
  976. /** @brief Set input size for frame.
  977. * @param[in] size New input size.
  978. * @note If shape of the new blob less than 0, then frame size not change.
  979. */
  980. CV_WRAP Model& setInputSize(const Size& size);
  981. /** @overload
  982. * @param[in] width New input width.
  983. * @param[in] height New input height.
  984. */
  985. CV_WRAP inline
  986. Model& setInputSize(int width, int height) { return setInputSize(Size(width, height)); }
  987. /** @brief Set mean value for frame.
  988. * @param[in] mean Scalar with mean values which are subtracted from channels.
  989. */
  990. CV_WRAP Model& setInputMean(const Scalar& mean);
  991. /** @brief Set scalefactor value for frame.
  992. * @param[in] scale Multiplier for frame values.
  993. */
  994. CV_WRAP Model& setInputScale(double scale);
  995. /** @brief Set flag crop for frame.
  996. * @param[in] crop Flag which indicates whether image will be cropped after resize or not.
  997. */
  998. CV_WRAP Model& setInputCrop(bool crop);
  999. /** @brief Set flag swapRB for frame.
  1000. * @param[in] swapRB Flag which indicates that swap first and last channels.
  1001. */
  1002. CV_WRAP Model& setInputSwapRB(bool swapRB);
  1003. /** @brief Set preprocessing parameters for frame.
  1004. * @param[in] size New input size.
  1005. * @param[in] mean Scalar with mean values which are subtracted from channels.
  1006. * @param[in] scale Multiplier for frame values.
  1007. * @param[in] swapRB Flag which indicates that swap first and last channels.
  1008. * @param[in] crop Flag which indicates whether image will be cropped after resize or not.
  1009. * blob(n, c, y, x) = scale * resize( frame(y, x, c) ) - mean(c) )
  1010. */
  1011. CV_WRAP void setInputParams(double scale = 1.0, const Size& size = Size(),
  1012. const Scalar& mean = Scalar(), bool swapRB = false, bool crop = false);
  1013. /** @brief Given the @p input frame, create input blob, run net and return the output @p blobs.
  1014. * @param[in] frame The input image.
  1015. * @param[out] outs Allocated output blobs, which will store results of the computation.
  1016. */
  1017. CV_WRAP void predict(InputArray frame, OutputArrayOfArrays outs) const;
  1018. // ============================== Net proxy methods ==============================
  1019. // Never expose methods with network implementation details, like:
  1020. // - addLayer, addLayerToPrev, connect, setInputsNames, setInputShape, setParam, getParam
  1021. // - getLayer*, getUnconnectedOutLayers, getUnconnectedOutLayersNames, getLayersShapes
  1022. // - forward* methods, setInput
  1023. /// @sa Net::setPreferableBackend
  1024. CV_WRAP Model& setPreferableBackend(dnn::Backend backendId);
  1025. /// @sa Net::setPreferableTarget
  1026. CV_WRAP Model& setPreferableTarget(dnn::Target targetId);
  1027. CV_DEPRECATED_EXTERNAL
  1028. operator Net&() const { return getNetwork_(); }
  1029. //protected: - internal/tests usage only
  1030. Net& getNetwork_() const;
  1031. inline Net& getNetwork_() { return const_cast<const Model*>(this)->getNetwork_(); }
  1032. struct Impl;
  1033. inline Impl* getImpl() const { return impl.get(); }
  1034. inline Impl& getImplRef() const { CV_DbgAssert(impl); return *impl.get(); }
  1035. protected:
  1036. Ptr<Impl> impl;
  1037. };
  1038. /** @brief This class represents high-level API for classification models.
  1039. *
  1040. * ClassificationModel allows to set params for preprocessing input image.
  1041. * ClassificationModel creates net from file with trained weights and config,
  1042. * sets preprocessing input, runs forward pass and return top-1 prediction.
  1043. */
  1044. class CV_EXPORTS_W_SIMPLE ClassificationModel : public Model
  1045. {
  1046. public:
  1047. /**
  1048. * @brief Create classification model from network represented in one of the supported formats.
  1049. * An order of @p model and @p config arguments does not matter.
  1050. * @param[in] model Binary file contains trained weights.
  1051. * @param[in] config Text file contains network configuration.
  1052. */
  1053. CV_WRAP ClassificationModel(const String& model, const String& config = "");
  1054. /**
  1055. * @brief Create model from deep learning network.
  1056. * @param[in] network Net object.
  1057. */
  1058. CV_WRAP ClassificationModel(const Net& network);
  1059. /** @brief Given the @p input frame, create input blob, run net and return top-1 prediction.
  1060. * @param[in] frame The input image.
  1061. */
  1062. std::pair<int, float> classify(InputArray frame);
  1063. /** @overload */
  1064. CV_WRAP void classify(InputArray frame, CV_OUT int& classId, CV_OUT float& conf);
  1065. };
  1066. /** @brief This class represents high-level API for keypoints models
  1067. *
  1068. * KeypointsModel allows to set params for preprocessing input image.
  1069. * KeypointsModel creates net from file with trained weights and config,
  1070. * sets preprocessing input, runs forward pass and returns the x and y coordinates of each detected keypoint
  1071. */
  1072. class CV_EXPORTS_W KeypointsModel: public Model
  1073. {
  1074. public:
  1075. /**
  1076. * @brief Create keypoints model from network represented in one of the supported formats.
  1077. * An order of @p model and @p config arguments does not matter.
  1078. * @param[in] model Binary file contains trained weights.
  1079. * @param[in] config Text file contains network configuration.
  1080. */
  1081. CV_WRAP KeypointsModel(const String& model, const String& config = "");
  1082. /**
  1083. * @brief Create model from deep learning network.
  1084. * @param[in] network Net object.
  1085. */
  1086. CV_WRAP KeypointsModel(const Net& network);
  1087. /** @brief Given the @p input frame, create input blob, run net
  1088. * @param[in] frame The input image.
  1089. * @param thresh minimum confidence threshold to select a keypoint
  1090. * @returns a vector holding the x and y coordinates of each detected keypoint
  1091. *
  1092. */
  1093. CV_WRAP std::vector<Point2f> estimate(InputArray frame, float thresh=0.5);
  1094. };
  1095. /** @brief This class represents high-level API for segmentation models
  1096. *
  1097. * SegmentationModel allows to set params for preprocessing input image.
  1098. * SegmentationModel creates net from file with trained weights and config,
  1099. * sets preprocessing input, runs forward pass and returns the class prediction for each pixel.
  1100. */
  1101. class CV_EXPORTS_W SegmentationModel: public Model
  1102. {
  1103. public:
  1104. /**
  1105. * @brief Create segmentation model from network represented in one of the supported formats.
  1106. * An order of @p model and @p config arguments does not matter.
  1107. * @param[in] model Binary file contains trained weights.
  1108. * @param[in] config Text file contains network configuration.
  1109. */
  1110. CV_WRAP SegmentationModel(const String& model, const String& config = "");
  1111. /**
  1112. * @brief Create model from deep learning network.
  1113. * @param[in] network Net object.
  1114. */
  1115. CV_WRAP SegmentationModel(const Net& network);
  1116. /** @brief Given the @p input frame, create input blob, run net
  1117. * @param[in] frame The input image.
  1118. * @param[out] mask Allocated class prediction for each pixel
  1119. */
  1120. CV_WRAP void segment(InputArray frame, OutputArray mask);
  1121. };
  1122. /** @brief This class represents high-level API for object detection networks.
  1123. *
  1124. * DetectionModel allows to set params for preprocessing input image.
  1125. * DetectionModel creates net from file with trained weights and config,
  1126. * sets preprocessing input, runs forward pass and return result detections.
  1127. * For DetectionModel SSD, Faster R-CNN, YOLO topologies are supported.
  1128. */
  1129. class CV_EXPORTS_W_SIMPLE DetectionModel : public Model
  1130. {
  1131. public:
  1132. /**
  1133. * @brief Create detection model from network represented in one of the supported formats.
  1134. * An order of @p model and @p config arguments does not matter.
  1135. * @param[in] model Binary file contains trained weights.
  1136. * @param[in] config Text file contains network configuration.
  1137. */
  1138. CV_WRAP DetectionModel(const String& model, const String& config = "");
  1139. /**
  1140. * @brief Create model from deep learning network.
  1141. * @param[in] network Net object.
  1142. */
  1143. CV_WRAP DetectionModel(const Net& network);
  1144. CV_DEPRECATED_EXTERNAL // avoid using in C++ code (need to fix bindings first)
  1145. DetectionModel();
  1146. /**
  1147. * @brief nmsAcrossClasses defaults to false,
  1148. * such that when non max suppression is used during the detect() function, it will do so per-class.
  1149. * This function allows you to toggle this behaviour.
  1150. * @param[in] value The new value for nmsAcrossClasses
  1151. */
  1152. CV_WRAP DetectionModel& setNmsAcrossClasses(bool value);
  1153. /**
  1154. * @brief Getter for nmsAcrossClasses. This variable defaults to false,
  1155. * such that when non max suppression is used during the detect() function, it will do so only per-class
  1156. */
  1157. CV_WRAP bool getNmsAcrossClasses();
  1158. /** @brief Given the @p input frame, create input blob, run net and return result detections.
  1159. * @param[in] frame The input image.
  1160. * @param[out] classIds Class indexes in result detection.
  1161. * @param[out] confidences A set of corresponding confidences.
  1162. * @param[out] boxes A set of bounding boxes.
  1163. * @param[in] confThreshold A threshold used to filter boxes by confidences.
  1164. * @param[in] nmsThreshold A threshold used in non maximum suppression.
  1165. */
  1166. CV_WRAP void detect(InputArray frame, CV_OUT std::vector<int>& classIds,
  1167. CV_OUT std::vector<float>& confidences, CV_OUT std::vector<Rect>& boxes,
  1168. float confThreshold = 0.5f, float nmsThreshold = 0.0f);
  1169. };
  1170. /** @brief This class represents high-level API for text recognition networks.
  1171. *
  1172. * TextRecognitionModel allows to set params for preprocessing input image.
  1173. * TextRecognitionModel creates net from file with trained weights and config,
  1174. * sets preprocessing input, runs forward pass and return recognition result.
  1175. * For TextRecognitionModel, CRNN-CTC is supported.
  1176. */
  1177. class CV_EXPORTS_W_SIMPLE TextRecognitionModel : public Model
  1178. {
  1179. public:
  1180. CV_DEPRECATED_EXTERNAL // avoid using in C++ code, will be moved to "protected" (need to fix bindings first)
  1181. TextRecognitionModel();
  1182. /**
  1183. * @brief Create Text Recognition model from deep learning network
  1184. * Call setDecodeType() and setVocabulary() after constructor to initialize the decoding method
  1185. * @param[in] network Net object
  1186. */
  1187. CV_WRAP TextRecognitionModel(const Net& network);
  1188. /**
  1189. * @brief Create text recognition model from network represented in one of the supported formats
  1190. * Call setDecodeType() and setVocabulary() after constructor to initialize the decoding method
  1191. * @param[in] model Binary file contains trained weights
  1192. * @param[in] config Text file contains network configuration
  1193. */
  1194. CV_WRAP inline
  1195. TextRecognitionModel(const std::string& model, const std::string& config = "")
  1196. : TextRecognitionModel(readNet(model, config)) { /* nothing */ }
  1197. /**
  1198. * @brief Set the decoding method of translating the network output into string
  1199. * @param[in] decodeType The decoding method of translating the network output into string: {'CTC-greedy': greedy decoding for the output of CTC-based methods}
  1200. */
  1201. CV_WRAP
  1202. TextRecognitionModel& setDecodeType(const std::string& decodeType);
  1203. /**
  1204. * @brief Get the decoding method
  1205. * @return the decoding method
  1206. */
  1207. CV_WRAP
  1208. const std::string& getDecodeType() const;
  1209. /**
  1210. * @brief Set the vocabulary for recognition.
  1211. * @param[in] vocabulary the associated vocabulary of the network.
  1212. */
  1213. CV_WRAP
  1214. TextRecognitionModel& setVocabulary(const std::vector<std::string>& vocabulary);
  1215. /**
  1216. * @brief Get the vocabulary for recognition.
  1217. * @return vocabulary the associated vocabulary
  1218. */
  1219. CV_WRAP
  1220. const std::vector<std::string>& getVocabulary() const;
  1221. /**
  1222. * @brief Given the @p input frame, create input blob, run net and return recognition result
  1223. * @param[in] frame The input image
  1224. * @return The text recognition result
  1225. */
  1226. CV_WRAP
  1227. std::string recognize(InputArray frame) const;
  1228. /**
  1229. * @brief Given the @p input frame, create input blob, run net and return recognition result
  1230. * @param[in] frame The input image
  1231. * @param[in] roiRects List of text detection regions of interest (cv::Rect, CV_32SC4). ROIs is be cropped as the network inputs
  1232. * @param[out] results A set of text recognition results.
  1233. */
  1234. CV_WRAP
  1235. void recognize(InputArray frame, InputArrayOfArrays roiRects, CV_OUT std::vector<std::string>& results) const;
  1236. };
  1237. /** @brief Base class for text detection networks
  1238. */
  1239. class CV_EXPORTS_W TextDetectionModel : public Model
  1240. {
  1241. protected:
  1242. CV_DEPRECATED_EXTERNAL // avoid using in C++ code, will be moved to "protected" (need to fix bindings first)
  1243. TextDetectionModel();
  1244. public:
  1245. /** @brief Performs detection
  1246. *
  1247. * Given the input @p frame, prepare network input, run network inference, post-process network output and return result detections.
  1248. *
  1249. * Each result is quadrangle's 4 points in this order:
  1250. * - bottom-left
  1251. * - top-left
  1252. * - top-right
  1253. * - bottom-right
  1254. *
  1255. * Use cv::getPerspectiveTransform function to retrive image region without perspective transformations.
  1256. *
  1257. * @note If DL model doesn't support that kind of output then result may be derived from detectTextRectangles() output.
  1258. *
  1259. * @param[in] frame The input image
  1260. * @param[out] detections array with detections' quadrangles (4 points per result)
  1261. * @param[out] confidences array with detection confidences
  1262. */
  1263. CV_WRAP
  1264. void detect(
  1265. InputArray frame,
  1266. CV_OUT std::vector< std::vector<Point> >& detections,
  1267. CV_OUT std::vector<float>& confidences
  1268. ) const;
  1269. /** @overload */
  1270. CV_WRAP
  1271. void detect(
  1272. InputArray frame,
  1273. CV_OUT std::vector< std::vector<Point> >& detections
  1274. ) const;
  1275. /** @brief Performs detection
  1276. *
  1277. * Given the input @p frame, prepare network input, run network inference, post-process network output and return result detections.
  1278. *
  1279. * Each result is rotated rectangle.
  1280. *
  1281. * @note Result may be inaccurate in case of strong perspective transformations.
  1282. *
  1283. * @param[in] frame the input image
  1284. * @param[out] detections array with detections' RotationRect results
  1285. * @param[out] confidences array with detection confidences
  1286. */
  1287. CV_WRAP
  1288. void detectTextRectangles(
  1289. InputArray frame,
  1290. CV_OUT std::vector<cv::RotatedRect>& detections,
  1291. CV_OUT std::vector<float>& confidences
  1292. ) const;
  1293. /** @overload */
  1294. CV_WRAP
  1295. void detectTextRectangles(
  1296. InputArray frame,
  1297. CV_OUT std::vector<cv::RotatedRect>& detections
  1298. ) const;
  1299. };
  1300. /** @brief This class represents high-level API for text detection DL networks compatible with EAST model.
  1301. *
  1302. * Configurable parameters:
  1303. * - (float) confThreshold - used to filter boxes by confidences, default: 0.5f
  1304. * - (float) nmsThreshold - used in non maximum suppression, default: 0.0f
  1305. */
  1306. class CV_EXPORTS_W_SIMPLE TextDetectionModel_EAST : public TextDetectionModel
  1307. {
  1308. public:
  1309. CV_DEPRECATED_EXTERNAL // avoid using in C++ code, will be moved to "protected" (need to fix bindings first)
  1310. TextDetectionModel_EAST();
  1311. /**
  1312. * @brief Create text detection algorithm from deep learning network
  1313. * @param[in] network Net object
  1314. */
  1315. CV_WRAP TextDetectionModel_EAST(const Net& network);
  1316. /**
  1317. * @brief Create text detection model from network represented in one of the supported formats.
  1318. * An order of @p model and @p config arguments does not matter.
  1319. * @param[in] model Binary file contains trained weights.
  1320. * @param[in] config Text file contains network configuration.
  1321. */
  1322. CV_WRAP inline
  1323. TextDetectionModel_EAST(const std::string& model, const std::string& config = "")
  1324. : TextDetectionModel_EAST(readNet(model, config)) { /* nothing */ }
  1325. /**
  1326. * @brief Set the detection confidence threshold
  1327. * @param[in] confThreshold A threshold used to filter boxes by confidences
  1328. */
  1329. CV_WRAP
  1330. TextDetectionModel_EAST& setConfidenceThreshold(float confThreshold);
  1331. /**
  1332. * @brief Get the detection confidence threshold
  1333. */
  1334. CV_WRAP
  1335. float getConfidenceThreshold() const;
  1336. /**
  1337. * @brief Set the detection NMS filter threshold
  1338. * @param[in] nmsThreshold A threshold used in non maximum suppression
  1339. */
  1340. CV_WRAP
  1341. TextDetectionModel_EAST& setNMSThreshold(float nmsThreshold);
  1342. /**
  1343. * @brief Get the detection confidence threshold
  1344. */
  1345. CV_WRAP
  1346. float getNMSThreshold() const;
  1347. };
  1348. /** @brief This class represents high-level API for text detection DL networks compatible with DB model.
  1349. *
  1350. * Related publications: @cite liao2020real
  1351. * Paper: https://arxiv.org/abs/1911.08947
  1352. * For more information about the hyper-parameters setting, please refer to https://github.com/MhLiao/DB
  1353. *
  1354. * Configurable parameters:
  1355. * - (float) binaryThreshold - The threshold of the binary map. It is usually set to 0.3.
  1356. * - (float) polygonThreshold - The threshold of text polygons. It is usually set to 0.5, 0.6, and 0.7. Default is 0.5f
  1357. * - (double) unclipRatio - The unclip ratio of the detected text region, which determines the output size. It is usually set to 2.0.
  1358. * - (int) maxCandidates - The max number of the output results.
  1359. */
  1360. class CV_EXPORTS_W_SIMPLE TextDetectionModel_DB : public TextDetectionModel
  1361. {
  1362. public:
  1363. CV_DEPRECATED_EXTERNAL // avoid using in C++ code, will be moved to "protected" (need to fix bindings first)
  1364. TextDetectionModel_DB();
  1365. /**
  1366. * @brief Create text detection algorithm from deep learning network.
  1367. * @param[in] network Net object.
  1368. */
  1369. CV_WRAP TextDetectionModel_DB(const Net& network);
  1370. /**
  1371. * @brief Create text detection model from network represented in one of the supported formats.
  1372. * An order of @p model and @p config arguments does not matter.
  1373. * @param[in] model Binary file contains trained weights.
  1374. * @param[in] config Text file contains network configuration.
  1375. */
  1376. CV_WRAP inline
  1377. TextDetectionModel_DB(const std::string& model, const std::string& config = "")
  1378. : TextDetectionModel_DB(readNet(model, config)) { /* nothing */ }
  1379. CV_WRAP TextDetectionModel_DB& setBinaryThreshold(float binaryThreshold);
  1380. CV_WRAP float getBinaryThreshold() const;
  1381. CV_WRAP TextDetectionModel_DB& setPolygonThreshold(float polygonThreshold);
  1382. CV_WRAP float getPolygonThreshold() const;
  1383. CV_WRAP TextDetectionModel_DB& setUnclipRatio(double unclipRatio);
  1384. CV_WRAP double getUnclipRatio() const;
  1385. CV_WRAP TextDetectionModel_DB& setMaxCandidates(int maxCandidates);
  1386. CV_WRAP int getMaxCandidates() const;
  1387. };
  1388. //! @}
  1389. CV__DNN_INLINE_NS_END
  1390. }
  1391. }
  1392. #include <opencv2/dnn/layer.hpp>
  1393. #include <opencv2/dnn/dnn.inl.hpp>
  1394. /// @deprecated Include this header directly from application. Automatic inclusion will be removed
  1395. #include <opencv2/dnn/utils/inference_engine.hpp>
  1396. #endif /* OPENCV_DNN_DNN_HPP */