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