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 <map>
13 #include <memory>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 #include "ie_icnn_network.hpp"
19 #include "ie_blob.h"
20 #include "ie_common.h"
21 #include "ie_data.h"
23 
24 namespace ngraph {
25 
26 class Function;
27 
28 } // namespace ngraph
29 
30 namespace InferenceEngine {
31 
32 /**
33  * @brief This class contains all the information about the Neural Network and the related binary information
34  */
35 class INFERENCE_ENGINE_API_CLASS(CNNNetwork) {
36 public:
37  /**
38  * @brief A default constructor
39  */
40  CNNNetwork() = default;
41 
42  /**
43  * @brief Allows helper class to manage lifetime of network object
44  *
45  * @param network Pointer to the network object
46  */
47  explicit CNNNetwork(std::shared_ptr<ICNNNetwork> network): network(network) {
48  actual = network.get();
49  if (actual == nullptr) THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
50  }
51 
52  /**
53  * @brief A constructor from ngraph::Function object
54  * This constructor wraps existing ngraph::Function
55  * If you want to avoid modification of original Function, please create a copy
56  * @param network Pointer to the ngraph::Function object
57  */
58  explicit CNNNetwork(const std::shared_ptr<ngraph::Function>& network);
59 
60  /**
61  * @brief A destructor
62  */
63  virtual ~CNNNetwork() {}
64 
65  /**
66  * @copybrief ICNNNetwork::getOutputsInfo
67  *
68  * Wraps ICNNNetwork::getOutputsInfo
69  *
70  * @return outputs Reference to the OutputsDataMap object
71  */
72  virtual OutputsDataMap getOutputsInfo() const {
73  if (actual == nullptr) THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
74  OutputsDataMap outputs;
75  actual->getOutputsInfo(outputs);
76  return outputs;
77  }
78 
79  /**
80  * @copybrief ICNNNetwork::getInputsInfo
81  *
82  * Wraps ICNNNetwork::getInputsInfo
83  *
84  * @return inputs Reference to InputsDataMap object
85  */
86  virtual InputsDataMap getInputsInfo() const {
87  if (actual == nullptr) THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
88  InputsDataMap inputs;
89  actual->getInputsInfo(inputs);
90  return inputs;
91  }
92 
93  /**
94  * @copybrief ICNNNetwork::layerCount
95  *
96  * Wraps ICNNNetwork::layerCount
97  *
98  * @return The number of layers as an integer value
99  */
100  size_t layerCount() const {
101  if (actual == nullptr) THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
102  return actual->layerCount();
103  }
104 
105  /**
106  * @copybrief ICNNNetwork::getName
107  *
108  * Wraps ICNNNetwork::getName
109  *
110  * @return Network name
111  */
112  const std::string& getName() const {
113  if (actual == nullptr) THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
114  return actual->getName();
115  }
116 
117  /**
118  * @copybrief ICNNNetwork::setBatchSize
119  *
120  * Wraps ICNNNetwork::setBatchSize
121  *
122  * @param size Size of batch to set
123  * @return Status code of the operation
124  */
125  virtual void setBatchSize(const size_t size) {
126  CALL_STATUS_FNC(setBatchSize, size);
127  }
128 
129  /**
130  * @copybrief ICNNNetwork::getBatchSize
131  *
132  * Wraps ICNNNetwork::getBatchSize
133  *
134  * @return The size of batch as a size_t value
135  */
136  virtual size_t getBatchSize() const {
137  if (actual == nullptr) THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
138  return actual->getBatchSize();
139  }
140 
141  /**
142  * @brief An overloaded operator cast to get pointer on current network
143  *
144  * @return A shared pointer of the current network
145  */
146  operator ICNNNetwork::Ptr() {
147  return network;
148  }
149 
150  /**
151  * @brief An overloaded operator & to get current network
152  *
153  * @return An instance of the current network
154  */
155  operator ICNNNetwork&() {
156  if (actual == nullptr) THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
157  return *actual;
158  }
159 
160  /**
161  * @brief An overloaded operator & to get current network
162  *
163  * @return A const reference of the current network
164  */
165  operator const ICNNNetwork&() const {
166  if (actual == nullptr) THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
167  return *actual;
168  }
169 
170  /**
171  * @brief Returns constant nGraph function
172  *
173  * @return constant nGraph function
174  */
175  std::shared_ptr<ngraph::Function> getFunction() {
176  if (actual == nullptr) THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
177  return actual->getFunction();
178  }
179 
180  /**
181  * @brief Returns constant nGraph function
182  *
183  * @return constant nGraph function
184  */
185  std::shared_ptr<const ngraph::Function> getFunction() const {
186  if (actual == nullptr) THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
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  * @brief Helper method to get collect all input shapes with names of corresponding Data objects
204  *
205  * @return Map of pairs: input name and its dimension.
206  */
208  if (actual == nullptr) THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
210  InputsDataMap inputs;
211  actual->getInputsInfo(inputs);
212  for (const auto& pair : inputs) {
213  auto info = pair.second;
214  if (info) {
215  auto data = info->getInputData();
216  if (data) {
217  shapes[data->getName()] = data->getTensorDesc().getDims();
218  }
219  }
220  }
221  return shapes;
222  }
223 
224  /**
225  * @brief Run shape inference with new input shapes for the network
226  *
227  * @param inputShapes - map of pairs: name of corresponding data and its dimension.
228  */
229  virtual void reshape(const ICNNNetwork::InputShapes& inputShapes) {
230  CALL_STATUS_FNC(reshape, inputShapes);
231  }
232 
233  /**
234  * @brief Serialize network to IR and weights files.
235  *
236  * @param xmlPath Path to output IR file.
237  * @param binPath Path to output weights file. The parameter is skipped in case
238  * of executable graph info serialization.
239  */
240  void serialize(const std::string& xmlPath, const std::string& binPath = "") const {
241  CALL_STATUS_FNC(serialize, xmlPath, binPath);
242  }
243 
244 protected:
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
ie_exception_conversion.hpp
A header file that provides macros to handle no exception methods.
InferenceEngine::ICNNNetwork::InputShapes
std::map< std::string, SizeVector > InputShapes
Map of pairs: name of corresponding data and its dimension.
Definition: ie_icnn_network.hpp:147
ie_icnn_network.hpp
This is a header file for the ICNNNetwork class.
InferenceEngine::CNNNetwork::layerCount
size_t layerCount() const
Returns the number of layers in the network as an integer value.
Definition: ie_cnn_network.h:100
InferenceEngine::CNNNetwork::~CNNNetwork
virtual ~CNNNetwork()
A destructor.
Definition: ie_cnn_network.h:63
InferenceEngine::CNNNetwork::getInputsInfo
virtual InputsDataMap getInputsInfo() const
Gets the network input Data node information. The received info is stored in the given InputsDataMap ...
Definition: ie_cnn_network.h:86
InferenceEngine::CNNNetwork::reshape
virtual void reshape(const ICNNNetwork::InputShapes &inputShapes)
Run shape inference with new input shapes for the network.
Definition: ie_cnn_network.h:229
ie_blob.h
A header file for Blob and generic TBlob<>
InferenceEngine::CNNNetwork::getInputShapes
virtual ICNNNetwork::InputShapes getInputShapes() const
Helper method to get collect all input shapes with names of corresponding Data objects.
Definition: ie_cnn_network.h:207
ie_data.h
This header file defines the main Data representation node.
InferenceEngine::ICNNNetwork
This is the main interface to describe the NN topology.
Definition: ie_icnn_network.hpp:39
InferenceEngine::CNNNetwork::addOutput
void addOutput(const std::string &layerName, size_t outputIndex=0)
Adds output to the layer.
Definition: ie_cnn_network.h:198
InferenceEngine::CNNNetwork::getFunction
std::shared_ptr< ngraph::Function > getFunction()
Returns constant nGraph function.
Definition: ie_cnn_network.h:175
InferenceEngine::CNNNetwork::network
std::shared_ptr< ICNNNetwork > network
Network extra interface, might be nullptr.
Definition: ie_cnn_network.h:248
InferenceEngine::CNNNetwork::getBatchSize
virtual size_t getBatchSize() const
Gets the inference batch size.
Definition: ie_cnn_network.h:136
InferenceEngine::CNNNetwork::getOutputsInfo
virtual OutputsDataMap getOutputsInfo() const
Gets the network output Data node information. The received info is stored in the given Data node.
Definition: ie_cnn_network.h:72
InferenceEngine::CNNNetwork::output
DataPtr output
A pointer to output data.
Definition: ie_cnn_network.h:257
ie_common.h
This is a header file with common inference engine definitions.
InferenceEngine::CNNNetwork::CNNNetwork
CNNNetwork(const std::shared_ptr< ngraph::Function > &network)
A constructor from ngraph::Function object This constructor wraps existing ngraph::Function If you wa...
InferenceEngine::CNNNetwork::serialize
void serialize(const std::string &xmlPath, const std::string &binPath="") const
Serialize network to IR and weights files.
Definition: ie_cnn_network.h:240
InferenceEngine::CNNNetwork::getName
const std::string & getName() const
Returns the network name.
Definition: ie_cnn_network.h:112
THROW_IE_EXCEPTION
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
InferenceEngine::CNNNetwork::setBatchSize
virtual void setBatchSize(const size_t size)
Changes the inference batch size.
Definition: ie_cnn_network.h:125
InferenceEngine::DataPtr
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:37
InferenceEngine::InputsDataMap
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
InferenceEngine::ICNNNetwork::Ptr
std::shared_ptr< ICNNNetwork > Ptr
A shared pointer to a ICNNNetwork interface.
Definition: ie_icnn_network.hpp:44
InferenceEngine::CNNNetwork::CNNNetwork
CNNNetwork(std::shared_ptr< ICNNNetwork > network)
Allows helper class to manage lifetime of network object.
Definition: ie_cnn_network.h:47
InferenceEngine::CNNNetwork::getFunction
std::shared_ptr< const ngraph::Function > getFunction() const
Returns constant nGraph function.
Definition: ie_cnn_network.h:185
InferenceEngine::OutputsDataMap
std::map< std::string, DataPtr > OutputsDataMap
A collection that contains string as key, and Data smart pointer as value.
Definition: ie_icnn_network.hpp:33
InferenceEngine::CNNNetwork::CNNNetwork
CNNNetwork()=default
A default constructor.