ie_cnn_network.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief A header file that provides wrapper for ICNNNetwork object
7  * @file ie_cnn_network.h
8  */
9 #pragma once
10 
11 #include <ie_icnn_net_reader.h>
12 
15 #include <ie_icnn_network.hpp>
16 #include <map>
17 #include <memory>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include "ie_blob.h"
23 #include "ie_common.h"
24 #include "ie_data.h"
25 
26 namespace ngraph {
27 
28 class Function;
29 
30 } // namespace ngraph
31 
32 namespace InferenceEngine {
33 
34 /**
35  * @brief This class contains all the information about the Neural Network and the related binary information
36  */
37 class INFERENCE_ENGINE_API_CLASS(CNNNetwork) {
38 public:
39  /**
40  * @brief A default constructor
41  */
42  CNNNetwork() = default;
43 
44  /**
45  * @brief Allows helper class to manage lifetime of network object
46  * @param network Pointer to the network object
47  */
48  explicit CNNNetwork(std::shared_ptr<ICNNNetwork> network): network(network) {
49  actual = network.get();
50  if (actual == nullptr) {
51  THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
52  }
53  }
54 
55  /**
56  * @brief A constructor from ngraph::Function object
57  * @param network Pointer to the ngraph::Function object
58  */
59  explicit CNNNetwork(const std::shared_ptr<ngraph::Function>& network);
60 
61  /**
62  * @brief A constructor from ICNNNetReader object
63  * @param reader Pointer to the ICNNNetReader object
64  */
65  explicit CNNNetwork(std::shared_ptr<ICNNNetReader> reader): reader(reader), actual(reader->getNetwork(nullptr)) {
66  if (actual == nullptr) {
67  THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
68  }
69  }
70 
71  /**
72  * @brief A destructor
73  */
74  virtual ~CNNNetwork() {}
75 
76  /**
77  * @brief Wraps original method
78  * ICNNNetwork::getPrecision
79  */
80  virtual Precision getPrecision() const {
81  return actual->getPrecision();
82  }
83 
84  /**
85  * @brief Wraps original method
86  * ICNNNetwork::getOutputsInfo
87  */
88  virtual OutputsDataMap getOutputsInfo() const {
89  OutputsDataMap outputs;
90  actual->getOutputsInfo(outputs);
91  return outputs;
92  }
93 
94  /**
95  * @brief Wraps original method
96  * ICNNNetwork::getInputsInfo
97  */
98  virtual InputsDataMap getInputsInfo() const {
99  InputsDataMap inputs;
100  actual->getInputsInfo(inputs);
101  return inputs;
102  }
103 
104  /**
105  * @brief Wraps original method
106  * ICNNNetwork::layerCount
107  */
108  size_t layerCount() const {
109  return actual->layerCount();
110  }
111 
112  /**
113  * @brief Wraps original method
114  * ICNNNetwork::getName
115  */
116  const std::string& getName() const noexcept {
117  return actual->getName();
118  }
119 
120  /**
121  * @brief Wraps original method
122  * ICNNNetwork::setBatchSize
123  */
124  virtual void setBatchSize(const size_t size) {
125  CALL_STATUS_FNC(setBatchSize, size);
126  }
127 
128  /**
129  * @brief Wraps original method
130  * ICNNNetwork::getBatchSize
131  */
132  virtual size_t getBatchSize() const {
133  return actual->getBatchSize();
134  }
135 
136  /**
137  * @brief An overloaded operator & to get current network
138  * @return An instance of the current network
139  */
140  operator ICNNNetwork&() const {
141  return *actual;
142  }
143 
144  /**
145  * @brief Returns constant nGraph function
146  * @return constant nGraph function
147  */
148  const std::shared_ptr<ngraph::Function> getFunction() const noexcept {
149  return actual->getFunction();
150  }
151 
152  /**
153  * @brief Wraps original method
154  * ICNNNetwork::addOutput
155  */
156  void addOutput(const std::string& layerName, size_t outputIndex = 0) {
157  CALL_STATUS_FNC(addOutput, layerName, outputIndex);
158  }
159 
160  /**
161  * @brief Wraps original method
162  * ICNNNetwork::getLayerByName
163  */
164  CNNLayerPtr getLayerByName(const char* layerName) const {
165  CNNLayerPtr layer;
166  CALL_STATUS_FNC(getLayerByName, layerName, layer);
167  return layer;
168  }
169 
170  /**
171  * @brief Begin layer iterator
172  * Order of layers is implementation specific,
173  * and can be changed in future
174  */
175  details::CNNNetworkIterator begin() const {
176  return details::CNNNetworkIterator(actual);
177  }
178 
179  /**
180  * @brief End layer iterator
181  */
182  details::CNNNetworkIterator end() const {
183  return details::CNNNetworkIterator();
184  }
185 
186  /**
187  * @brief number of layers in network object
188  * @return
189  */
190  size_t size() const {
191  return std::distance(std::begin(*this), std::end(*this));
192  }
193 
194  /**
195  * @brief Registers extension within the plugin
196  * @param extension Pointer to already loaded reader extension with shape propagation implementations
197  */
198  void AddExtension(InferenceEngine::IShapeInferExtensionPtr extension) {
199  CALL_STATUS_FNC(AddExtension, extension);
200  }
201 
202  /**
203  * @brief - Helper method to get collect all input shapes with names of corresponding Data objects
204  * @return Map of pairs: input's name and its dimension.
205  */
208  InputsDataMap inputs;
209  actual->getInputsInfo(inputs);
210  for (const auto& pair : inputs) {
211  auto info = pair.second;
212  if (info) {
213  auto data = info->getInputData();
214  if (data) {
215  shapes[data->getName()] = data->getTensorDesc().getDims();
216  }
217  }
218  }
219  return shapes;
220  }
221 
222  /**
223  * @brief Run shape inference with new input shapes for the network
224  * @param inputShapes - map of pairs: name of corresponding data and its dimension.
225  */
226  virtual void reshape(const ICNNNetwork::InputShapes& inputShapes) {
227  CALL_STATUS_FNC(reshape, inputShapes);
228  }
229 
230  /**
231  * @brief Serialize network to IR and weights files.
232  * @param xmlPath Path to output IR file.
233  * @param binPath Path to output weights file. The parameter is skipped in case
234  * of executable graph info serialization.
235  */
236  void serialize(const std::string& xmlPath, const std::string& binPath = "") const {
237  CALL_STATUS_FNC(serialize, xmlPath, binPath);
238  }
239 
240 protected:
241  /**
242  * @brief reader extra reference, might be nullptr
243  */
244  std::shared_ptr<ICNNNetReader> reader;
245  /**
246  * @brief network extra interface, might be nullptr
247  */
248  std::shared_ptr<ICNNNetwork> network;
249 
250  /**
251  * @brief A pointer to the current network
252  */
253  ICNNNetwork* actual = nullptr;
254  /**
255  * @brief A pointer to output data
256  */
258 };
259 
260 } // namespace InferenceEngine
CNNLayerPtr getLayerByName(const char *layerName) const
Wraps original method ICNNNetwork::getLayerByName.
Definition: ie_cnn_network.h:164
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:24
virtual ~CNNNetwork()
A destructor.
Definition: ie_cnn_network.h:74
CNNNetwork(std::shared_ptr< ICNNNetwork > network)
Allows helper class to manage lifetime of network object.
Definition: ie_cnn_network.h:48
Inference Engine API.
Definition: ie_argmax_layer.hpp:11
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:206
const std::string & getName() const noexcept
Wraps original method ICNNNetwork::getName.
Definition: ie_cnn_network.h:116
virtual InputsDataMap getInputsInfo() const
Wraps original method ICNNNetwork::getInputsInfo.
Definition: ie_cnn_network.h:98
void addOutput(const std::string &layerName, size_t outputIndex=0)
Wraps original method ICNNNetwork::addOutput.
Definition: ie_cnn_network.h:156
A header file for Blob and generic TBlob<>
A header file for the CNNNetworkIterator class.
details::CNNNetworkIterator begin() const
Begin layer iterator Order of layers is implementation specific, and can be changed in future...
Definition: ie_cnn_network.h:175
This is a header file for the ICNNNetwork class.
const std::shared_ptr< ngraph::Function > getFunction() const noexcept
Returns constant nGraph function.
Definition: ie_cnn_network.h:148
virtual void reshape(const ICNNNetwork::InputShapes &inputShapes)
Run shape inference with new input shapes for the network.
Definition: ie_cnn_network.h:226
size_t layerCount() const
Wraps original method ICNNNetwork::layerCount.
Definition: ie_cnn_network.h:108
virtual Precision getPrecision() const
Wraps original method ICNNNetwork::getPrecision.
Definition: ie_cnn_network.h:80
virtual size_t getBatchSize() const
Wraps original method ICNNNetwork::getBatchSize.
Definition: ie_cnn_network.h:132
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:51
std::shared_ptr< ICNNNetReader > reader
reader extra reference, might be nullptr
Definition: ie_cnn_network.h:244
This is the main interface to describe the NN topology.
Definition: ie_icnn_network.hpp:41
Definition: ie_cnn_network.h:26
details::CNNNetworkIterator end() const
End layer iterator.
Definition: ie_cnn_network.h:182
void serialize(const std::string &xmlPath, const std::string &binPath="") const
Serialize network to IR and weights files.
Definition: ie_cnn_network.h:236
CNNNetwork(std::shared_ptr< ICNNNetReader > reader)
A constructor from ICNNNetReader object.
Definition: ie_cnn_network.h:65
virtual OutputsDataMap getOutputsInfo() const
Wraps original method ICNNNetwork::getOutputsInfo.
Definition: ie_cnn_network.h:88
This class contains all the information about the Neural Network and the related binary information...
Definition: ie_cnn_network.h:37
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:248
std::shared_ptr< CNNLayer > CNNLayerPtr
A smart pointer to the CNNLayer.
Definition: ie_common.h:37
std::map< std::string, DataPtr > OutputsDataMap
A collection that contains string as key, and Data smart pointer as value.
Definition: ie_icnn_network.hpp:36
virtual void setBatchSize(const size_t size)
Wraps original method ICNNNetwork::setBatchSize.
Definition: ie_cnn_network.h:124
size_t size() const
number of layers in network object
Definition: ie_cnn_network.h:190
DataPtr output
A pointer to output data.
Definition: ie_cnn_network.h:257
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:198
std::map< std::string, SizeVector > InputShapes
Map of pairs: name of corresponding data and its dimension.
Definition: ie_icnn_network.hpp:155
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:156
This is a header file with common inference engine definitions.
This class holds precision value and provides precision related operations.
Definition: ie_precision.hpp:21