Merge pull request #10854 from pengli:dnn
[platform/upstream/opencv.git] / modules / dnn / src / op_inf_engine.hpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 //
5 // Copyright (C) 2018, Intel Corporation, all rights reserved.
6 // Third party copyrights are property of their respective owners.
7
8 #ifndef __OPENCV_DNN_OP_INF_ENGINE_HPP__
9 #define __OPENCV_DNN_OP_INF_ENGINE_HPP__
10
11 #include "precomp.hpp"
12
13 #ifdef HAVE_INF_ENGINE
14 #include <inference_engine.hpp>
15 #endif  // HAVE_INF_ENGINE
16
17 namespace cv { namespace dnn {
18
19 #ifdef HAVE_INF_ENGINE
20
21 class InfEngineBackendNet : public InferenceEngine::ICNNNetwork
22 {
23 public:
24     virtual void Release() noexcept;
25
26     virtual InferenceEngine::Precision getPrecision() noexcept;
27
28     virtual void getOutputsInfo(InferenceEngine::OutputsDataMap &out) noexcept;
29
30     virtual void getInputsInfo(InferenceEngine::InputsDataMap &inputs) noexcept;
31
32     virtual void getInputsInfo(InferenceEngine::InputsDataMap &inputs) const noexcept;
33
34     virtual InferenceEngine::InputInfo::Ptr getInput(const std::string &inputName) noexcept;
35
36     virtual void getName(char *pName, size_t len) noexcept;
37
38     virtual size_t layerCount() noexcept;
39
40     virtual InferenceEngine::DataPtr& getData(const char *dname) noexcept;
41
42     virtual void addLayer(const InferenceEngine::CNNLayerPtr &layer) noexcept;
43
44     virtual InferenceEngine::StatusCode addOutput(const std::string &layerName,
45                                                   size_t outputIndex = 0,
46                                                   InferenceEngine::ResponseDesc *resp = nullptr) noexcept;
47
48     virtual InferenceEngine::StatusCode getLayerByName(const char *layerName,
49                                                        InferenceEngine::CNNLayerPtr &out,
50                                                        InferenceEngine::ResponseDesc *resp) noexcept;
51
52     virtual void setTargetDevice(InferenceEngine::TargetDevice device) noexcept;
53
54     virtual InferenceEngine::TargetDevice getTargetDevice() noexcept;
55
56     virtual InferenceEngine::StatusCode setBatchSize(const size_t size) noexcept;
57
58     virtual size_t getBatchSize() const noexcept;
59
60     void initEngine();
61
62     void addBlobs(const std::vector<Ptr<BackendWrapper> >& wrappers);
63
64     void forward();
65
66     bool isInitialized();
67
68 private:
69     std::vector<InferenceEngine::CNNLayerPtr> layers;
70     InferenceEngine::InputsDataMap inputs;
71     InferenceEngine::OutputsDataMap outputs;
72     InferenceEngine::BlobMap inpBlobs;
73     InferenceEngine::BlobMap outBlobs;
74     InferenceEngine::BlobMap allBlobs;
75     InferenceEngine::InferenceEnginePluginPtr engine;
76 };
77
78 class InfEngineBackendNode : public BackendNode
79 {
80 public:
81     InfEngineBackendNode(const InferenceEngine::CNNLayerPtr& layer);
82
83     void connect(std::vector<Ptr<BackendWrapper> >& inputs,
84                  std::vector<Ptr<BackendWrapper> >& outputs);
85
86     InferenceEngine::CNNLayerPtr layer;
87     // Inference Engine network object that allows to obtain the outputs of this layer.
88     Ptr<InfEngineBackendNet> net;
89 };
90
91 class InfEngineBackendWrapper : public BackendWrapper
92 {
93 public:
94     InfEngineBackendWrapper(int targetId, const Mat& m);
95
96     ~InfEngineBackendWrapper();
97
98     virtual void copyToHost();
99
100     virtual void setHostDirty();
101
102     InferenceEngine::DataPtr dataPtr;
103     InferenceEngine::TBlob<float>::Ptr blob;
104 };
105
106 InferenceEngine::TBlob<float>::Ptr wrapToInfEngineBlob(const Mat& m);
107
108 InferenceEngine::TBlob<float>::Ptr wrapToInfEngineBlob(const Mat& m, const std::vector<size_t>& shape);
109
110 InferenceEngine::DataPtr infEngineDataNode(const Ptr<BackendWrapper>& ptr);
111
112 // Fuses convolution weights and biases with channel-wise scales and shifts.
113 void fuseConvWeights(const std::shared_ptr<InferenceEngine::ConvolutionLayer>& conv,
114                      const Mat& w, const Mat& b = Mat());
115
116 #endif  // HAVE_INF_ENGINE
117
118 bool haveInfEngine();
119
120 void forwardInfEngine(Ptr<BackendNode>& node);
121
122 }}  // namespace dnn, namespace cv
123
124 #endif  // __OPENCV_DNN_OP_INF_ENGINE_HPP__