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<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  * @copybrief ICNNNetwork::getPrecision
83  *
84  * Wraps ICNNNetwork::getPrecision
85  *
86  * @return A precision type
87  */
88  virtual Precision getPrecision() const {
89  return actual->getPrecision();
90  }
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&() const {
169  return *actual;
170  }
171 
172  /**
173  * @brief Returns constant nGraph function
174  *
175  * @return constant nGraph function
176  */
177  const std::shared_ptr<const ngraph::Function> getFunction() const noexcept {
178  return actual->getFunction();
179  }
180 
181  /**
182  * @copybrief ICNNNetwork::addOutput
183  *
184  * Wraps ICNNNetwork::addOutput
185  *
186  * @param layerName Name of the layer
187  * @param outputIndex Index of the output
188  */
189  void addOutput(const std::string& layerName, size_t outputIndex = 0) {
190  CALL_STATUS_FNC(addOutput, layerName, outputIndex);
191  }
192 
193  /**
194  * @copybrief ICNNNetwork::getLayerByName
195  *
196  * Wraps ICNNNetwork::getLayerByName
197  *
198  * @param layerName Given name of the layer
199  * @return Status code of the operation. InferenceEngine::OK if succeeded
200  */
201  CNNLayerPtr getLayerByName(const char* layerName) const {
202  CNNLayerPtr layer;
203  CALL_STATUS_FNC(getLayerByName, layerName, layer);
204  return layer;
205  }
206 
207  /**
208  * @brief Begin layer iterator
209  *
210  * Order of layers is implementation specific,
211  * and can be changed in future
212  *
213  * @return Iterator pointing to a layer
214  */
215  details::CNNNetworkIterator begin() const {
216  return details::CNNNetworkIterator(actual);
217  }
218 
219  /**
220  * @brief End layer iterator
221  */
222  details::CNNNetworkIterator end() const {
223  return details::CNNNetworkIterator();
224  }
225 
226  /**
227  * @brief Number of layers in network object
228  *
229  * @return
230  */
231  size_t size() const {
232  return std::distance(std::begin(*this), std::end(*this));
233  }
234 
235  /**
236  * @brief Registers extension within the plugin
237  *
238  * @param extension Pointer to already loaded reader extension with shape propagation implementations
239  */
240  void AddExtension(InferenceEngine::IShapeInferExtensionPtr extension) {
241  CALL_STATUS_FNC(AddExtension, extension);
242  }
243 
244  /**
245  * @brief Helper method to get collect all input shapes with names of corresponding Data objects
246  *
247  * @return Map of pairs: input name and its dimension.
248  */
251  InputsDataMap inputs;
252  actual->getInputsInfo(inputs);
253  for (const auto& pair : inputs) {
254  auto info = pair.second;
255  if (info) {
256  auto data = info->getInputData();
257  if (data) {
258  shapes[data->getName()] = data->getTensorDesc().getDims();
259  }
260  }
261  }
262  return shapes;
263  }
264 
265  /**
266  * @brief Run shape inference with new input shapes for the network
267  *
268  * @param inputShapes - map of pairs: name of corresponding data and its dimension.
269  */
270  virtual void reshape(const ICNNNetwork::InputShapes& inputShapes) {
271  CALL_STATUS_FNC(reshape, inputShapes);
272  }
273 
274  /**
275  * @brief Serialize network to IR and weights files.
276  *
277  * @param xmlPath Path to output IR file.
278  * @param binPath Path to output weights file. The parameter is skipped in case
279  * of executable graph info serialization.
280  */
281  void serialize(const std::string& xmlPath, const std::string& binPath = "") const {
282  CALL_STATUS_FNC(serialize, xmlPath, binPath);
283  }
284 
285 protected:
286  /**
287  * @brief Reader extra reference, might be nullptr
288  */
289  IE_SUPPRESS_DEPRECATED_START
290  std::shared_ptr<ICNNNetReader> reader;
291  IE_SUPPRESS_DEPRECATED_END
292  /**
293  * @brief Network extra interface, might be nullptr
294  */
295  std::shared_ptr<ICNNNetwork> network;
296 
297  /**
298  * @brief A pointer to the current network
299  */
300  ICNNNetwork* actual = nullptr;
301  /**
302  * @brief A pointer to output data
303  */
305 };
306 
307 } // namespace InferenceEngine
CNNLayerPtr getLayerByName(const char *layerName) const
Definition: ie_cnn_network.h:201
#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:249
const std::string & getName() const noexcept
Definition: ie_cnn_network.h:136
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:189
A header file for Blob and generic TBlob<>
A header file for the CNNNetworkIterator class.
details::CNNNetworkIterator begin() const
Begin layer iterator.
Definition: ie_cnn_network.h:215
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:270
size_t layerCount() const
Definition: ie_cnn_network.h:125
virtual Precision getPrecision() const
Definition: ie_cnn_network.h:88
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:290
This is the main interface to describe the NN topology.
Definition: ie_icnn_network.hpp:42
Definition: ie_cnn_network.h:27
details::CNNNetworkIterator end() const
End layer iterator.
Definition: ie_cnn_network.h:222
void serialize(const std::string &xmlPath, const std::string &binPath="") const
Serialize network to IR and weights files.
Definition: ie_cnn_network.h:281
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:295
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
size_t size() const
Number of layers in network object.
Definition: ie_cnn_network.h:231
DataPtr output
A pointer to output data.
Definition: ie_cnn_network.h:304
A header file that provides interface for network reader that is used to build networks from a given ...
void AddExtension(InferenceEngine::IShapeInferExtensionPtr extension)
Registers extension within the plugin.
Definition: ie_cnn_network.h:240
std::map< std::string, SizeVector > InputShapes
Map of pairs: name of corresponding data and its dimension.
Definition: ie_icnn_network.hpp:181
const std::shared_ptr< const ngraph::Function > getFunction() const noexcept
Returns constant nGraph function.
Definition: ie_cnn_network.h:177
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:164
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