ie_cnn_network.h
Go to the documentation of this file.
1 // Copyright (C) 2018 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 
13 #include <ie_icnn_network.hpp>
14 #include <ie_icnn_net_reader.h>
15 #include "ie_common.h"
16 #include "ie_data.h"
17 #include "ie_blob.h"
18 #include <vector>
19 #include <string>
20 #include <map>
21 #include <utility>
22 #include <memory>
23 
24 namespace InferenceEngine {
25 
26 /**
27  * @brief This class contains all the information about the Neural Network and the related binary information
28  */
29 class CNNNetwork {
30 public:
31  /**
32  * @brief A default constructor
33  */
34  CNNNetwork() = default;
35 
36  /**
37  * @brief Initialises helper class from externally managed pointer
38  * @deprecated use shared_pointers based version of CNNNetworks constructor
39  * @param actual Pointer to the network object
40  */
41  explicit CNNNetwork(ICNNNetwork* actual) : actual(actual) {
42  if (actual == nullptr) {
43  THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
44  }
45  }
46 
47  /**
48  * @brief Allows helper class to manage lifetime of network object
49  * @param network Pointer to the network object
50  */
51  explicit CNNNetwork(std::shared_ptr<ICNNNetwork> network)
52  : network(network) {
53  actual = network.get();
54  if (actual == nullptr) {
55  THROW_IE_EXCEPTION << "CNNNetwork was not initialized.";
56  }
57  }
58 
59  /**
60  * @brief A constructor from ICNNNetReader object
61  * @param reader Pointer to the ICNNNetReader object
62  */
63  explicit CNNNetwork(std::shared_ptr<ICNNNetReader> reader)
64  : reader(reader)
65  , 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 std::move(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 std::move(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::setBatchSize
115  */
116  virtual void setBatchSize(const size_t size) {
117  CALL_STATUS_FNC(setBatchSize, size);
118  }
119 
120  /**
121  * @brief Wraps original method
122  * ICNNNetwork::getBatchSize
123  */
124  virtual size_t getBatchSize() const {
125  return actual->getBatchSize();
126  }
127 
128  /**
129  * @brief An overloaded operator & to get current network
130  * @return An instance of the current network
131  */
132  operator ICNNNetwork &() const {
133  return *actual;
134  }
135 
136  /**
137  * @brief Sets tha target device
138  * @param device Device instance to set
139  */
141  actual->setTargetDevice(device);
142  }
143 
144  /**
145  * @brief Wraps original method
146  * ICNNNetwork::addOutput
147  */
148  void addOutput(const std::string &layerName, size_t outputIndex = 0) {
149  CALL_STATUS_FNC(addOutput, layerName, outputIndex);
150  }
151 
152  /**
153  * @brief Wraps original method
154  * ICNNNetwork::getLayerByName
155  */
156  CNNLayerPtr getLayerByName(const char *layerName) const {
157  CNNLayerPtr layer;
158  CALL_STATUS_FNC(getLayerByName, layerName, layer);
159  return layer;
160  }
161 
162  /**
163  * @brief Begin layer iterator
164  * Order of layers is implementation specific,
165  * and can be changed in future
166  */
167  details::CNNNetworkIterator begin() const {
168  return details::CNNNetworkIterator(actual);
169  }
170 
171  /**
172  * @brief End layer iterator
173  */
174  details::CNNNetworkIterator end() const {
175  return details::CNNNetworkIterator();
176  }
177 
178  /**
179  * @brief number of layers in network object
180  * @return
181  */
182  size_t size() const {
183  return std::distance(std::begin(*this), std::end(*this));
184  }
185 
186  /**
187  * @brief Registers extension within the plugin
188  * @param extension Pointer to already loaded reader extension with shape propagation implementations
189  */
190  void AddExtension(InferenceEngine::IShapeInferExtensionPtr extension) {
191  CALL_STATUS_FNC(AddExtension, extension);
192  }
193 
194  /**
195  * @brief - Helper method to get collect all input shapes with names of corresponding Data objects
196  * @return Map of pairs: input's name and its dimension.
197  */
200  InputsDataMap inputs;
201  actual->getInputsInfo(inputs);
202  for (const auto& pair : inputs) {
203  auto info = pair.second;
204  if (info) {
205  auto data = info->getInputData();
206  if (data) {
207  shapes[data->name] = data->getTensorDesc().getDims();
208  }
209  }
210  }
211  return std::move(shapes);
212  }
213 
214  /**
215  * @brief Run shape inference with new input shapes for the network
216  * @param inputShapes - map of pairs: name of corresponding data and its dimension.
217  */
218  virtual void reshape(const ICNNNetwork::InputShapes &inputShapes) {
219  CALL_STATUS_FNC(reshape, inputShapes);
220  }
221 
222  /**
223  * @brief Serialize network to IR and weights files.
224  * @param xmlPath Path to output IR file.
225  * @param binPath Path to output weights file.
226  */
227  void serialize(const std::string &xmlPath, const std::string &binPath) const {
228  CALL_STATUS_FNC(serialize, xmlPath, binPath);
229  }
230 
231 protected:
232  /**
233  * @brief reader extra reference, might be nullptr
234  */
235  std::shared_ptr<ICNNNetReader> reader;
236  /**
237  * @brief network extra interface, might be nullptr
238  */
239  std::shared_ptr<ICNNNetwork> network;
240 
241  /**
242  * @brief A pointer to the current network
243  */
244  ICNNNetwork *actual = nullptr;
245  /**
246  * @brief A pointer to output data
247  */
249 };
250 
251 } // namespace InferenceEngine
CNNLayerPtr getLayerByName(const char *layerName) const
Wraps original method ICNNNetwork::getLayerByName.
Definition: ie_cnn_network.h:156
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:22
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:51
TargetDevice
Describes known device types.
Definition: ie_device.hpp:23
ICNNNetwork * actual
A pointer to the current network.
Definition: ie_cnn_network.h:244
Definition: ie_argmax_layer.hpp:11
std::shared_ptr< CNNLayer > CNNLayerPtr
A smart pointer to the CNNLayer.
Definition: ie_common.h:36
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:198
virtual void getInputsInfo(InputsDataMap &inputs) const noexcept=0
Gets the network input Data node information. The received info is stored in the given InputsDataMap ...
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:148
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:167
void setTargetDevice(TargetDevice device)
Sets tha target device.
Definition: ie_cnn_network.h:140
This is a header file for the ICNNNetwork class.
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:193
virtual void reshape(const ICNNNetwork::InputShapes &inputShapes)
Run shape inference with new input shapes for the network.
Definition: ie_cnn_network.h:218
size_t layerCount() const
Wraps original method ICNNNetwork::layerCount.
Definition: ie_cnn_network.h:108
virtual size_t getBatchSize() const noexcept=0
Gets the inference batch size.
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:124
std::shared_ptr< ICNNNetReader > reader
reader extra reference, might be nullptr
Definition: ie_cnn_network.h:235
This is the main interface to describe the NN topology.
Definition: ie_icnn_network.hpp:35
details::CNNNetworkIterator end() const
End layer iterator.
Definition: ie_cnn_network.h:174
CNNNetwork(std::shared_ptr< ICNNNetReader > reader)
A constructor from ICNNNetReader object.
Definition: ie_cnn_network.h:63
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:29
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:239
virtual void setTargetDevice(TargetDevice device) noexcept=0
Sets a desirable device to perform all work on. Some plug-ins might not support some target devices a...
CNNNetwork(ICNNNetwork *actual)
Initialises helper class from externally managed pointer.
Definition: ie_cnn_network.h:41
virtual size_t layerCount() const noexcept=0
Returns the number of layers in the network as an integer value.
virtual void setBatchSize(const size_t size)
Wraps original method ICNNNetwork::setBatchSize.
Definition: ie_cnn_network.h:116
size_t size() const
number of layers in network object
Definition: ie_cnn_network.h:182
DataPtr output
A pointer to output data.
Definition: ie_cnn_network.h:248
A header file that provides interface for network reader that is used to build networks from a given ...
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:50
void AddExtension(InferenceEngine::IShapeInferExtensionPtr extension)
Registers extension within the plugin.
Definition: ie_cnn_network.h:190
std::map< std::string, DataPtr > OutputsDataMap
A collection that contains string as key, and Data smart pointer as value.
Definition: ie_icnn_network.hpp:30
std::map< std::string, SizeVector > InputShapes
Map of pairs: name of corresponding data and its dimension.
Definition: ie_icnn_network.hpp:163
virtual Precision getPrecision() const noexcept=0
Returns the main network operating precision. This may be MIXED if not homogeneous.
virtual void getOutputsInfo(OutputsDataMap &out) const noexcept=0
Gets the network output Data node information. The received info is stored in the given Data node...
void serialize(const std::string &xmlPath, const std::string &binPath) const
Serialize network to IR and weights files.
Definition: ie_cnn_network.h:227
CNNNetwork()=default
A default constructor.
This is a header file with common inference engine definitions.
This class holds precision value and provides precision related operations.
Definition: ie_precision.hpp:19