Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / ie_bridges / python / src / openvino / inference_engine / ie_api_impl.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //        http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #pragma once
16
17 #include <ie_extension.h>
18 #include <iterator>
19
20 #include <string>
21 #include <utility>
22 #include <map>
23 #include <vector>
24 #include <set>
25
26
27 #include <iostream>
28 #include <algorithm>
29
30 #include <sstream>
31 #include <chrono>
32 #include "inference_engine.hpp"
33
34 typedef std::chrono::high_resolution_clock Time;
35 typedef std::chrono::nanoseconds ns;
36
37 namespace InferenceEnginePython {
38 struct IENetLayer {
39     InferenceEngine::CNNLayerPtr layer_ptr;
40     InferenceEngine::CNNNetwork network_ptr;
41     std::string name;
42     std::string type;
43     std::string precision;
44     std::string shape;
45     std::string layout;
46     std::vector<std::string> children;
47     std::vector<std::string> parents;
48     std::string affinity;
49     std::map<std::string, std::string> params;
50
51     void setAffinity(const std::string &target_affinity);
52
53     void setParams(const std::map<std::string, std::string> &params_map);
54
55     std::map<std::string, InferenceEngine::Blob::Ptr> getWeights();
56
57     void setPrecision(std::string precision);
58 };
59
60 struct InputInfo {
61     InferenceEngine::InputInfo actual;
62     std::vector<size_t> dims;
63     std::string precision;
64     std::string layout;
65
66     void setPrecision(std::string precision);
67
68     void setLayout(std::string layout);
69 };
70
71 struct OutputInfo {
72     InferenceEngine::DataPtr actual;
73     std::vector<size_t> dims;
74     std::string precision;
75     std::string layout;
76
77     void setPrecision(std::string precision);
78 };
79
80 struct ProfileInfo {
81     std::string status;
82     std::string exec_type;
83     std::string layer_type;
84     int64_t real_time;
85     int64_t cpu_time;
86     unsigned execution_index;
87 };
88
89 struct IENetwork {
90     InferenceEngine::CNNNetwork actual;
91     std::string name;
92     std::size_t batch_size;
93
94     void setBatch(const size_t size);
95
96     void addOutputs(const std::vector<std::string> &out_layers, const std::string &precision);
97
98     const std::vector<std::pair<std::string, InferenceEnginePython::IENetLayer>> getLayers();
99
100     const std::map<std::string, InferenceEnginePython::InputInfo> getInputs();
101
102     const std::map<std::string, InferenceEnginePython::OutputInfo> getOutputs();
103
104     void reshape(const std::map<std::string, std::vector<size_t>> &input_shapes);
105
106     void serialize(const std::string &path_to_xml, const std::string &path_to_bin);
107
108     void setStats(const std::map<std::string, std::map<std::string, std::vector<float>>> &stats);
109
110     const std::map<std::string, std::map<std::string, std::vector<float>>> getStats();
111
112     IENetwork(const std::string &model, const std::string &weights);
113
114     IENetwork() = default;
115 };
116
117 struct InferRequestWrap {
118     InferenceEngine::IInferRequest::Ptr request_ptr;
119     Time::time_point start_time;
120     double exec_time;
121     void infer();
122
123     void infer_async();
124
125     int  wait(int64_t timeout);
126
127     void getBlobPtr(const std::string &blob_name, InferenceEngine::Blob::Ptr &blob_ptr);
128
129     void setBatch(int size);
130
131     std::map<std::string, InferenceEnginePython::ProfileInfo> getPerformanceCounts();
132 };
133
134
135 struct IEExecNetwork {
136     InferenceEngine::IExecutableNetwork::Ptr actual;
137     std::vector<InferRequestWrap> infer_requests;
138     std::string name;
139
140     IEExecNetwork(const std::string &name, size_t num_requests);
141
142     void infer();
143 };
144
145
146 struct IEPlugin {
147     std::unique_ptr<InferenceEnginePython::IEExecNetwork> load(const InferenceEnginePython::IENetwork &net,
148                                                                int num_requests,
149                                                                const std::map<std::string, std::string> &config);
150
151     std::string device_name;
152     std::string version;
153
154     void setConfig(const std::map<std::string, std::string> &);
155
156     void addCpuExtension(const std::string &extension_path);
157
158     void setInitialAffinity(const InferenceEnginePython::IENetwork &net);
159
160     IEPlugin(const std::string &device, const std::vector<std::string> &plugin_dirs);
161
162     IEPlugin() = default;
163
164     std::set<std::string> queryNetwork(const InferenceEnginePython::IENetwork &net);
165
166     InferenceEngine::InferenceEnginePluginPtr actual;
167 };
168
169 template<class T>
170 T *get_buffer(InferenceEngine::Blob &blob) {
171     return blob.buffer().as<T *>();
172 }
173
174 template<class T, class... Args>
175 std::unique_ptr<T> make_unique(Args &&... args) {
176     return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
177 }
178
179 std::string get_version();
180 };  // namespace InferenceEnginePython