ie_cnn_network.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief A header file that provides wrapper for ICNNNetwork object
7  *
8  * @file ie_cnn_network.h
9  */
10 #pragma once
11 
12 #include <ie_icnn_net_reader.h>
13 
16 #include <ie_icnn_network.hpp>
17 #include <map>
18 #include <memory>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "ie_blob.h"
24 #include "ie_common.h"
25 #include "ie_data.h"
26 
27 namespace ngraph {
28 
29 class Function;
30 
31 } // namespace ngraph
32 
33 namespace InferenceEngine {
34 
35 /**
36  * @brief This class contains all the information about the Neural Network and the related binary information
37  */
38 class INFERENCE_ENGINE_API_CLASS(CNNNetwork) {
39 public:
40  /**
41  * @brief A default constructor
42  */
43  CNNNetwork() = default;
44 
45  /**
46  * @brief Allows helper class to manage lifetime of network object
47  *
48  * @param network Pointer to the network object
49  */
50  explicit CNNNetwork(std::shared_ptr<ICNNNetwork> network): network(network) {
51  actual = network.get();
52  if (actual == nullptr) {
53  THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
54  }
55  }
56 
57  /**
58  * @brief A constructor from ngraph::Function object
59  * @param network Pointer to the ngraph::Function object
60  */
61  explicit CNNNetwork(const std::shared_ptr<const ngraph::Function>& network);
62 
63  /**
64  * @brief A constructor from ICNNNetReader object
65  *
66  * @param reader Pointer to the ICNNNetReader object
67  */
68  IE_SUPPRESS_DEPRECATED_START
69  explicit CNNNetwork(std::shared_ptr<ICNNNetReader> reader): reader(reader), actual(reader->getNetwork(nullptr)) {
70  if (actual == nullptr) {
71  THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
72  }
73  }
74  IE_SUPPRESS_DEPRECATED_END
75 
76  /**
77  * @brief A destructor
78  */
79  virtual ~CNNNetwork() {}
80 
81  /**
82  * @deprecated Network precision does not make sence, use precision on egdes. The method will be removed in 2021.1
83  * @copybrief ICNNNetwork::getPrecision
84  *
85  * Wraps ICNNNetwork::getPrecision
86  *
87  * @return A precision type
88  */
89  INFERENCE_ENGINE_DEPRECATED("Network precision does not make sence, use precision on egdes. The method will be removed in 2021.1")
90  virtual Precision getPrecision() const;
91 
92  /**
93  * @copybrief ICNNNetwork::getOutputsInfo
94  *
95  * Wraps ICNNNetwork::getOutputsInfo
96  *
97  * @return outputs Reference to the OutputsDataMap object
98  */
99  virtual OutputsDataMap getOutputsInfo() const {
100  OutputsDataMap outputs;
101  actual->getOutputsInfo(outputs);
102  return outputs;
103  }
104 
105  /**
106  * @copybrief ICNNNetwork::getInputsInfo
107  *
108  * Wraps ICNNNetwork::getInputsInfo
109  *
110  * @return inputs Reference to InputsDataMap object
111  */
112  virtual InputsDataMap getInputsInfo() const {
113  InputsDataMap inputs;
114  actual->getInputsInfo(inputs);
115  return inputs;
116  }
117 
118  /**
119  * @copybrief ICNNNetwork::layerCount
120  *
121  * Wraps ICNNNetwork::layerCount
122  *
123  * @return The number of layers as an integer value
124  */
125  size_t layerCount() const {
126  return actual->layerCount();
127  }
128 
129  /**
130  * @copybrief ICNNNetwork::getName
131  *
132  * Wraps ICNNNetwork::getName
133  *
134  * @return Network name
135  */
136  const std::string& getName() const noexcept {
137  return actual->getName();
138  }
139 
140  /**
141  * @copybrief ICNNNetwork::setBatchSize
142  *
143  * Wraps ICNNNetwork::setBatchSize
144  *
145  * @param size Size of batch to set
146  * @return Status code of the operation
147  */
148  virtual void setBatchSize(const size_t size) {
149  CALL_STATUS_FNC(setBatchSize, size);
150  }
151 
152  /**
153  * @copybrief ICNNNetwork::getBatchSize
154  *
155  * Wraps ICNNNetwork::getBatchSize
156  *
157  * @return The size of batch as a size_t value
158  */
159  virtual size_t getBatchSize() const {
160  return actual->getBatchSize();
161  }
162 
163  /**
164  * @brief An overloaded operator & to get current network
165  *
166  * @return An instance of the current network
167  */
168  operator ICNNNetwork&() {
169  return *actual;
170  }
171 
172  /**
173  * @brief An overloaded operator & to get current network
174  *
175  * @return A const reference of the current network
176  */
177  operator const ICNNNetwork&() const {
178  return *actual;
179  }
180 
181  /**
182  * @brief Returns constant nGraph function
183  *
184  * @return constant nGraph function
185  */
186  std::shared_ptr<const ngraph::Function> getFunction() const noexcept {
187  return actual->getFunction();
188  }
189 
190  /**
191  * @copybrief ICNNNetwork::addOutput
192  *
193  * Wraps ICNNNetwork::addOutput
194  *
195  * @param layerName Name of the layer
196  * @param outputIndex Index of the output
197  */
198  void addOutput(const std::string& layerName, size_t outputIndex = 0) {
199  CALL_STATUS_FNC(addOutput, layerName, outputIndex);
200  }
201 
202  /**
203  * @deprecated Migrate to IR v10 and work with ngraph::Function directly. The method will be removed in 2021.1
204  * @copybrief ICNNNetwork::getLayerByName
205  *
206  * Wraps ICNNNetwork::getLayerByName
207  *
208  * @param layerName Given name of the layer
209  * @return Status code of the operation. InferenceEngine::OK if succeeded
210  */
211  INFERENCE_ENGINE_DEPRECATED("Migrate to IR v10 and work with ngraph::Function directly. The method will be removed in 2021.1")
212  CNNLayerPtr getLayerByName(const char* layerName) const;
213 
214  /**
215  * @deprecated Use CNNNetwork::getFunction() and work with ngraph::Function directly. The method will be removed in 2021.1
216  * @brief Begin layer iterator
217  *
218  * Order of layers is implementation specific,
219  * and can be changed in future
220  *
221  * @return Iterator pointing to a layer
222  */
223  IE_SUPPRESS_DEPRECATED_START
224  INFERENCE_ENGINE_DEPRECATED("Use CNNNetwork::getFunction() and work with ngraph::Function directly. The method will be removed in 2021.1")
225  details::CNNNetworkIterator begin() const;
226 
227  /**
228  * @deprecated Use CNNNetwork::getFunction() and work with ngraph::Function directly. The method will be removed in 2021.1
229  * @brief End layer iterator
230  * @return Iterator pointing to a layer
231  */
232  INFERENCE_ENGINE_DEPRECATED("Use CNNNetwork::getFunction() and work with ngraph::Function directly. The method will be removed in 2021.1")
233  details::CNNNetworkIterator end() const;
234  IE_SUPPRESS_DEPRECATED_END
235 
236  /**
237  * @deprecated Use CNNNetwork::layerCount() instead. The method will be removed in 2021.1
238  * @brief Number of layers in network object
239  *
240  * @return Number of layers.
241  */
242  INFERENCE_ENGINE_DEPRECATED("Use CNNNetwork::layerCount() instead. The method will be removed in 2021.1")
243  size_t size() const;
244 
245  /**
246  * @deprecated Use Core::AddExtension to add an extension to the library
247  * @brief Registers extension within the plugin
248  *
249  * @param extension Pointer to already loaded reader extension with shape propagation implementations
250  */
251  INFERENCE_ENGINE_DEPRECATED("Use Core::AddExtension to add an extension to the library")
252  void AddExtension(InferenceEngine::IShapeInferExtensionPtr extension);
253 
254  /**
255  * @brief Helper method to get collect all input shapes with names of corresponding Data objects
256  *
257  * @return Map of pairs: input name and its dimension.
258  */
261  InputsDataMap inputs;
262  actual->getInputsInfo(inputs);
263  for (const auto& pair : inputs) {
264  auto info = pair.second;
265  if (info) {
266  auto data = info->getInputData();
267  if (data) {
268  shapes[data->getName()] = data->getTensorDesc().getDims();
269  }
270  }
271  }
272  return shapes;
273  }
274 
275  /**
276  * @brief Run shape inference with new input shapes for the network
277  *
278  * @param inputShapes - map of pairs: name of corresponding data and its dimension.
279  */
280  virtual void reshape(const ICNNNetwork::InputShapes& inputShapes) {
281  CALL_STATUS_FNC(reshape, inputShapes);
282  }
283 
284  /**
285  * @brief Serialize network to IR and weights files.
286  *
287  * @param xmlPath Path to output IR file.
288  * @param binPath Path to output weights file. The parameter is skipped in case
289  * of executable graph info serialization.
290  */
291  void serialize(const std::string& xmlPath, const std::string& binPath = "") const {
292  CALL_STATUS_FNC(serialize, xmlPath, binPath);
293  }
294 
295 protected:
296  /**
297  * @brief Reader extra reference, might be nullptr
298  */
299  IE_SUPPRESS_DEPRECATED_START
300  std::shared_ptr<ICNNNetReader> reader;
301  IE_SUPPRESS_DEPRECATED_END
302  /**
303  * @brief Network extra interface, might be nullptr
304  */
305  std::shared_ptr<ICNNNetwork> network;
306 
307  /**
308  * @brief A pointer to the current network
309  */
310  ICNNNetwork* actual = nullptr;
311  /**
312  * @brief A pointer to output data
313  */
315 };
316 
317 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
virtual ~CNNNetwork()
A destructor.
Definition: ie_cnn_network.h:79
CNNNetwork(std::shared_ptr< ICNNNetwork > network)
Allows helper class to manage lifetime of network object.
Definition: ie_cnn_network.h:50
Inference Engine API.
Definition: ie_argmax_layer.hpp:15
A header file that provides macros to handle no exception methods.
virtual ICNNNetwork::InputShapes getInputShapes() const
Helper method to get collect all input shapes with names of corresponding Data objects.
Definition: ie_cnn_network.h:259
const std::string & getName() const noexcept
Definition: ie_cnn_network.h:136
std::shared_ptr< IShapeInferExtension > IShapeInferExtensionPtr
A shared pointer to a IShapeInferExtension interface.
Definition: ie_iextension.h:366
virtual InputsDataMap getInputsInfo() const
Definition: ie_cnn_network.h:112
void addOutput(const std::string &layerName, size_t outputIndex=0)
Definition: ie_cnn_network.h:198
A header file for Blob and generic TBlob<>
A header file for the CNNNetworkIterator class.
This is a header file for the ICNNNetwork class.
virtual void reshape(const ICNNNetwork::InputShapes &inputShapes)
Run shape inference with new input shapes for the network.
Definition: ie_cnn_network.h:280
size_t layerCount() const
Definition: ie_cnn_network.h:125
virtual size_t getBatchSize() const
Definition: ie_cnn_network.h:159
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:53
std::shared_ptr< ICNNNetReader > reader
Reader extra reference, might be nullptr.
Definition: ie_cnn_network.h:300
This is the main interface to describe the NN topology.
Definition: ie_icnn_network.hpp:43
Definition: ie_cnn_network.h:27
void serialize(const std::string &xmlPath, const std::string &binPath="") const
Serialize network to IR and weights files.
Definition: ie_cnn_network.h:291
CNNNetwork(std::shared_ptr< ICNNNetReader > reader)
A constructor from ICNNNetReader object.
Definition: ie_cnn_network.h:69
virtual OutputsDataMap getOutputsInfo() const
Definition: ie_cnn_network.h:99
This class contains all the information about the Neural Network and the related binary information...
Definition: ie_cnn_network.h:38
This header file defines the main Data representation node.
std::shared_ptr< ICNNNetwork > network
Network extra interface, might be nullptr.
Definition: ie_cnn_network.h:305
std::shared_ptr< CNNLayer > CNNLayerPtr
A smart pointer to the CNNLayer.
Definition: ie_common.h:39
std::map< std::string, DataPtr > OutputsDataMap
A collection that contains string as key, and Data smart pointer as value.
Definition: ie_icnn_network.hpp:37
virtual void setBatchSize(const size_t size)
Definition: ie_cnn_network.h:148
DataPtr output
A pointer to output data.
Definition: ie_cnn_network.h:314
A header file that provides interface for network reader that is used to build networks from a given ...
std::shared_ptr< const ngraph::Function > getFunction() const noexcept
Returns constant nGraph function.
Definition: ie_cnn_network.h:186
std::map< std::string, SizeVector > InputShapes
Map of pairs: name of corresponding data and its dimension.
Definition: ie_icnn_network.hpp:196
std::map< std::string, InputInfo::Ptr > InputsDataMap
A collection that contains string as key, and InputInfo smart pointer as value.
Definition: ie_input_info.hpp:160
This is a header file with common inference engine definitions.
This class holds precision value and provides precision related operations.
Definition: ie_precision.hpp:22