ie_network.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief a header file for the Inference Engine Network interface
7  * @file ie_inetwork.hpp
8  */
9 #pragma once
10 
11 #include <ie_blob.h>
12 #include <ie_layouts.h>
13 
14 #include <ie_context.hpp>
15 #include <ie_parameter.hpp>
16 #include <map>
17 #include <memory>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 namespace InferenceEngine {
23 
24 /**
25  * @deprecated Use ngraph API instead.
26  * @brief A type of network objects indexes.
27  */
28 using idx_t = size_t;
29 
30 /**
31  * @deprecated Use ngraph API instead.
32  * @brief This class contains a pair from layerId and port index
33  */
34 class INFERENCE_ENGINE_NN_BUILDER_API_CLASS(PortInfo) {
35 public:
36  /**
37  * @brief The constructor creates a PortInfo object for port 0
38  * @param layerID Layer id
39  */
40  PortInfo(idx_t layerID): layer(layerID), port(0) {} // NOLINT
41 
42  /**
43  * @brief The constructor creates a PortInfo object
44  * @param layerID Layer id
45  * @param portID Port id
46  */
47  PortInfo(idx_t layerID, idx_t portID): layer(layerID), port(portID) {}
48 
49  /**
50  * @brief Get layer id
51  * @return Layer id
52  */
53  idx_t layerId() const {
54  return layer;
55  }
56 
57  /**
58  * @brief Get port id
59  * @return Port id
60  */
61  idx_t portId() const {
62  return port;
63  }
64 
65  IE_SUPPRESS_DEPRECATED_START
66 
67  /**
68  * @brief Compares the given PortInfo object with the current one
69  * @param portInfo PortInfo object to compare with
70  * @return true if the given PortInfo object is equal to the current one, false - otherwise
71  */
72  bool operator==(const PortInfo& portInfo) const {
73  return layer == portInfo.layerId() && port == portInfo.portId();
74  }
75 
76  /**
77  * @brief Checks if the given PortInfo object is not equal to the current one
78  * @param portInfo PortInfo object to compare with
79  * @return true if the given PortInfo object is not equal to the current one, false - otherwise
80  */
81  bool operator!=(const PortInfo& portInfo) const {
82  return !(*this == portInfo);
83  }
84 
85  IE_SUPPRESS_DEPRECATED_END
86 
87 private:
88  idx_t layer;
89  idx_t port;
90 };
91 
92 /**
93  * @deprecated Use ngraph API instead.
94  * @brief This class is the main object to describe the Inference Engine connection.
95  */
96 class INFERENCE_ENGINE_NN_BUILDER_DEPRECATED Connection {
97 public:
98  IE_SUPPRESS_DEPRECATED_START
99 
100  /**
101  * @brief Constructor of a connection object.
102  * @param input pair of the index of input layer and the index of output port
103  * @param output pair of the index of output layer and the index of input port
104  */
105  Connection(const PortInfo& input, const PortInfo& output): input(input), output(output) {}
106 
107  /**
108  * @brief Compares the given Connection with the current one
109  * @param connection Connection to compare with
110  * @return true if the given Connection is equal to the current one, false - otherwise
111  */
112  bool operator==(const Connection& connection) const {
113  return input == connection.from() && output == connection.to();
114  }
115 
116  /**
117  * @brief Checks if the given Connection is not equal to the current one
118  * @param connection Connection to compare with
119  * @return true if the given Connection is not equal to the current one, false - otherwise
120  */
121  bool operator!=(const Connection& connection) const {
122  return !(*this == connection);
123  }
124 
125  /**
126  * Returns a constant reference to a pair of input layer index and output port index.
127  * @return pair of the index of input layer and the index of output port
128  */
129  const PortInfo& from() const {
130  return input;
131  }
132 
133  /**
134  * Returns a constant reference to a pair of output layer index and input port index.
135  * @return pair of the index of output layer and the index of input port
136  */
137  const PortInfo& to() const {
138  return output;
139  }
140 
141 private:
142  PortInfo input;
143  PortInfo output;
144 
145  IE_SUPPRESS_DEPRECATED_END
146 };
147 
148 /**
149  * @deprecated Use ngraph API instead.
150  * This class describes port data
151  */
152 class INFERENCE_ENGINE_NN_BUILDER_API_CLASS(PortData) {
153 public:
154  IE_SUPPRESS_DEPRECATED_START
155 
156  /**
157  * @brief A shared pointer to the PortData object.
158  */
159  using Ptr = std::shared_ptr<PortData>;
160 
161  IE_SUPPRESS_DEPRECATED_END
162 
163  /**
164  * @brief Default constructor
165  */
166  PortData();
167 
168  /**
169  * Creates port data with precision and shape
170  * @param shape Dimensions
171  * @param precision Precision
172  */
173  PortData(const SizeVector& shape, const Precision& precision);
174 
175  /**
176  * @brief virtual destructor
177  */
178  virtual ~PortData() = default;
179 
180  /**
181  * @brief Returns data
182  * @return Blob with data
183  */
184  const Blob::Ptr& getData() const;
185 
186  /**
187  * @brief Sets data
188  * @param data Blob with data
189  */
190  void setData(const Blob::Ptr& data);
191 
192  /**
193  * @brief Returns data parameters
194  * @return Map of parameters
195  */
196  const std::map<std::string, Parameter>& getParameters() const noexcept;
197 
198  /**
199  * @brief Sets new shapes for data
200  * @param shape New shapes
201  */
202  void setShape(const SizeVector& shape);
203 
204 private:
205  Blob::Ptr data;
206  std::map<std::string, Parameter> parameters;
207 
208  void createData(const TensorDesc& desc);
209 };
210 
211 /**
212  * @deprecated Use ngraph API instead.
213  * @brief This class is the main object to describe the Inference Engine port.
214  */
215 class INFERENCE_ENGINE_NN_BUILDER_API_CLASS(Port) {
216 public:
217  /**
218  * @brief Default constructor of a port object.
219  */
220  Port();
221 
222  /**
223  * @brief Constructor of a port object with shapes.
224  * @param shapes port shapes
225  * @param precision Port precision
226  */
227  Port(const SizeVector& shapes, const Precision& precision = Precision::UNSPECIFIED);
228 
229  /**
230  * @brief Virtual destructor
231  */
232  virtual ~Port() = default;
233 
234  IE_SUPPRESS_DEPRECATED_START
235 
236  /**
237  * @brief Copy constructor.
238  * @param port object to copy
239  */
240  Port(const Port& port);
241 
242  /**
243  * @brief Compares the given Port with the current one
244  * @param rhs Port to compare with
245  * @return true if the given Port is equal to the current one, false - otherwise
246  */
247  bool operator==(const Port& rhs) const;
248 
249  /**
250  * @brief Compares the given Port with the current one
251  * @param rhs Port to compare with
252  * @return true if the given Port is NOT equal to the current one, false - otherwise
253  */
254  bool operator!=(const Port& rhs) const;
255 
256  IE_SUPPRESS_DEPRECATED_END
257 
258  /**
259  * @brief Returns a constant reference to a vector with shapes.
260  * Shapes should be initialized if shape is empty.
261  * @return constant reference to shapes
262  */
263  const SizeVector& shape() const noexcept;
264 
265  /**
266  * @brief Sets new shapes for current port
267  * @param shape New shapes
268  */
269  void setShape(const SizeVector& shape);
270 
271  /**
272  * @brief Returns a constant reference to parameters
273  * @return Map with parameters
274  */
275  const std::map<std::string, Parameter>& getParameters() const noexcept;
276 
277  /**
278  * @brief Sets new parameters for current port
279  * @param params New parameters
280  */
281  void setParameters(const std::map<std::string, Parameter>& params) noexcept;
282 
283  /**
284  * @brief Sets the new parameter for current port
285  * @param name Name of parameter
286  * @param param New value
287  */
288  void setParameter(const std::string& name, const Parameter& param);
289 
290  IE_SUPPRESS_DEPRECATED_START
291 
292  /**
293  * @brief Returns port data
294  * @return Port data
295  */
296  const PortData::Ptr& getData() const noexcept;
297 
298  /**
299  * @brief Sets new port data for current port
300  * @param data Port data
301  */
302  void setData(const PortData::Ptr& data);
303 
304 private:
305  std::map<std::string, Parameter> parameters;
306  PortData::Ptr data;
307 
308  IE_SUPPRESS_DEPRECATED_END
309 };
310 
311 class INetwork;
312 template <class T>
314 
315 /**
316  * @deprecated Use ngraph API instead.
317  * @brief This class is the main interface to describe the Inference Engine layer.
318  * All methods here are constant and do not throw exceptions.
319  */
320 class INFERENCE_ENGINE_NN_BUILDER_DEPRECATED ILayer {
321 public:
322  IE_SUPPRESS_DEPRECATED_START
323 
324  /**
325  * @brief A shared pointer to the const ILayer object
326  */
327  using CPtr = std::shared_ptr<const ILayer>;
328 
329  IE_SUPPRESS_DEPRECATED_END
330 
331  /**
332  * @brief Virtual destructor for the layer interface
333  */
334  virtual ~ILayer() = default;
335 
336  /**
337  * @brief Returns a id of the layer.
338  * @return Layer id
339  */
340  virtual idx_t getId() const noexcept = 0;
341 
342  /**
343  * @brief Returns a layer name.
344  * @return Layer name
345  */
346  virtual const std::string& getName() const noexcept = 0;
347 
348  /**
349  * @brief Returns a layer type.
350  * @return Layer type
351  */
352  virtual const std::string& getType() const noexcept = 0;
353 
354  /**
355  * @brief Returns a constant smart pointer reference to a Parameters interface.
356  * @return Parameters interface smart pointer
357  */
358  virtual const std::map<std::string, Parameter>& getParameters() const noexcept = 0;
359 
360  IE_SUPPRESS_DEPRECATED_START
361 
362  /**
363  * @brief Returns a constant reference to a vector with input ports.
364  * @return Vector of input ports
365  */
366  virtual const std::vector<Port>& getInputPorts() const noexcept = 0;
367 
368  /**
369  * @brief Returns a constant reference to a vector with output ports.
370  * @return Vector of output ports
371  */
372  virtual const std::vector<Port>& getOutputPorts() const noexcept = 0;
373 
374  IE_SUPPRESS_DEPRECATED_END
375 };
376 
377 namespace details {
378 
379 template <class NT, class LT>
380 class INFERENCE_ENGINE_NN_BUILDER_DEPRECATED INetworkIterator;
381 
382 } // namespace details
383 
384 /**
385  * @deprecated Use ngraph API instead.
386  * @brief This class is the main interface to describe the Inference Engine network.
387  *
388  * All methods here are constant and do not throw exceptions.
389  */
390 class INFERENCE_ENGINE_NN_BUILDER_DEPRECATED INetwork {
391 public:
392  IE_SUPPRESS_DEPRECATED_START
393 
394  /**
395  * @brief A shared pointer to the constant INetwork object.
396  */
397  using CPtr = std::shared_ptr<const INetwork>;
398  /**
399  * @brief A constant iterator for INetwork definition
400  */
401  using const_iterator = details::INetworkIterator<const INetwork, const ILayer>;
402 
403  IE_SUPPRESS_DEPRECATED_END
404 
405  /**
406  * @brief Virtual destructor for the network interface
407  */
408  virtual ~INetwork() = default;
409 
410  /**
411  * @brief Begin network iterator
412  * @return const INetwork iterator
413  */
414  virtual const_iterator begin() const noexcept = 0;
415 
416  /**
417  * @brief End network iterator
418  * @return const INetwork iterator
419  */
420  virtual const_iterator end() const noexcept = 0;
421 
422  /**
423  * @brief Returns a number of layers in the network.
424  * @return Layers count
425  */
426  virtual size_t size() const noexcept = 0;
427 
428  IE_SUPPRESS_DEPRECATED_START
429 
430  /**
431  * @brief Returns a constant smart pointer to a Layer interface.
432  * If the layer is missing, returns nullptr.
433  * @param id Id of the Layer
434  * @return Layer interface smart pointer
435  */
436  virtual const ILayer::CPtr getLayer(idx_t id) const noexcept = 0;
437 
438  /**
439  * @brief Returns a constant vector of input layers.
440  * @return Vector of input layers
441  */
442  virtual const std::vector<ILayer::CPtr> getInputs() const noexcept = 0;
443 
444  /**
445  * @brief Returns a constant vector of output layers.
446  * @return Vector of output layers
447  */
448  virtual const std::vector<ILayer::CPtr> getOutputs() const noexcept = 0;
449 
450  /**
451  * @brief Returns a constant vector of connections for specific layer.
452  * If the layer is missing, returns empty vector.
453  * @param layerId layer index
454  * @return Vector of connections
455  */
456  virtual const std::vector<Connection> getLayerConnections(idx_t layerId) const noexcept = 0;
457 
458  /**
459  * @brief Returns a network context
460  * @return const reference to Context
461  */
462  virtual const Context& getContext() const noexcept = 0;
463 
464  IE_SUPPRESS_DEPRECATED_END
465 
466  /**
467  * @brief Returns a network name.
468  * @return Network name
469  */
470  virtual const std::string& getName() const noexcept = 0;
471 };
472 
473 } // namespace InferenceEngine
474 
475 #include <details/ie_inetwork_iterator.hpp>
bool operator==(const Connection &connection) const
Compares the given Connection with the current one.
Definition: ie_network.hpp:112
Inference Engine API.
Definition: ie_argmax_layer.hpp:11
const PortInfo & from() const
Definition: ie_network.hpp:129
std::shared_ptr< const ILayer > CPtr
A shared pointer to the const ILayer object.
Definition: ie_network.hpp:327
A header file for Blob and generic TBlob<>
Connection(const PortInfo &input, const PortInfo &output)
Constructor of a connection object.
Definition: ie_network.hpp:105
std::shared_ptr< const INetwork > CPtr
A shared pointer to the constant INetwork object.
Definition: ie_network.hpp:397
This class defines Tensor description.
Definition: ie_layouts.h:140
const PortInfo & to() const
Definition: ie_network.hpp:137
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:40
A header file for data layouts and conversion between them.
Definition: ie_network.hpp:313
size_t idx_t
A type of network objects indexes.
Definition: ie_network.hpp:28
This class is the main interface to describe the Inference Engine network.
Definition: ie_network.hpp:390
details::INetworkIterator< const INetwork, const ILayer > const_iterator
A constant iterator for INetwork definition.
Definition: ie_network.hpp:401
This class is the main interface to describe the Inference Engine layer. All methods here are constan...
Definition: ie_network.hpp:320
This class represents an object to work with different parameters.
Definition: ie_parameter.hpp:36
bool operator!=(const Connection &connection) const
Checks if the given Connection is not equal to the current one.
Definition: ie_network.hpp:121
std::vector< size_t > SizeVector
Represents tensor size. The order is opposite to the order in Caffe*: (w,h,n,b) where the most freque...
Definition: ie_common.h:27
This is a header file for the IE Context class.
This class is the main object to describe the Inference Engine connection.
Definition: ie_network.hpp:96
This class holds precision value and provides precision related operations.
Definition: ie_precision.hpp:21
Definition: ie_precision.hpp:25