ie_network_builder.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include <cpp/ie_cnn_network.h>
8 #include <ie_blob.h>
9 #include <ie_common.h>
10 
11 #include <builders/ie_layer_builder.hpp>
12 #include <ie_context.hpp>
13 #include <ie_icnn_network.hpp>
14 #include <ie_network.hpp>
15 #include <map>
16 #include <memory>
17 #include <string>
18 #include <utility>
19 #include <vector>
20 
21 namespace InferenceEngine {
22 namespace Builder {
23 
24 /**
25  * @deprecated Use ngraph API instead.
26  * @brief This class implements a builder for IE Network
27  */
28 IE_SUPPRESS_DEPRECATED_START
29 class INFERENCE_ENGINE_NN_BUILDER_API_CLASS(Network): public INetwork {
30 public:
31  /**
32  * @brief A shared pointer to the Network builder
33  */
34  using Ptr = std::shared_ptr<Network>;
35  /**
36  * @brief An iterator for Network builder definition
37  */
38  using iterator = details::INetworkIterator<Network, Layer>;
39 
40  /**
41  * @brief Begin network iterator
42  * @return Network iterator
43  */
44  iterator begin();
45  /**
46  * @brief Begin network iterator
47  * @return const INetwork iterator
48  */
49  const_iterator begin() const noexcept override;
50 
51  /**
52  * @brief End network iterator
53  * @return Network iterator
54  */
55  iterator end();
56  /**
57  * @brief End network iterator
58  * @return const INetwork iterator
59  */
60  const_iterator end() const noexcept override;
61 
62  /**
63  * @brief Returns a number of layers in the network.
64  * @return Layers count
65  */
66  size_t size() const noexcept override;
67 
68  /**
69  * @brief The constructor creates a builder based on ICNNNetwork
70  *
71  * @param network constant reference to ICNNNetwork object
72  */
73  explicit Network(const ICNNNetwork& network);
74  /**
75  * @brief The constructor creates a empty builder with network name
76  *
77  * @param name Network name
78  */
79  explicit Network(const std::string& name);
80  /**
81  * @brief The constructor creates a builder based on INetwork
82  *
83  * @param network constant reference to INetwork object
84  */
85  explicit Network(const INetwork& network);
86 
87  /**
88  * @brief The constructor creates a builder based on ICNNNetwork with custom Context
89  *
90  * @param network constant reference to ICNNNetwork object
91  */
92  Network(const Context& ieContext, const ICNNNetwork& network);
93  /**
94  * @brief The constructor creates a empty builder with network name and custom Context
95  *
96  * @param name Network name
97  */
98  Network(const Context& ieContext, const std::string& name);
99  /**
100  * @brief The constructor creates a builder based on INetwork with custom Context
101  *
102  * @param network constant reference to INetwork object
103  */
104  Network(const Context& ieContext, const INetwork& network);
105 
106  /**
107  * @brief Adds new layer and connects it with previous layers
108  *
109  * @param inputs Vector with PortInfo objects from previous layers
110  * @param layer Layer builder for new layer
111  *
112  * @return Id of new builder for the current network
113  */
114  idx_t addLayer(const std::vector<PortInfo>& inputs, const Layer& layer);
115  /**
116  * @brief Adds new layer
117  *
118  * @param layer Layer builder for new layer
119  *
120  * @return Id of new builder for the current network
121  */
122  idx_t addLayer(const Layer& layer);
123  /**
124  * @brief Removes a layer by ID
125  *
126  * @param layerId Layer ID
127  */
128  void removeLayer(idx_t layerId);
129 
130  /**
131  * @brief Connects two layers
132  *
133  * @param input PortInfo object from previous layer
134  * @param output PortInfo object from next layer
135  */
136  void connect(const PortInfo& input, const PortInfo& output);
137  /**
138  * @brief Removes connection from the network
139  *
140  * @param connection Connection
141  */
142  void disconnect(const Connection& connection);
143 
144  /**
145  * @brief Returns vector of layer builders
146  *
147  * @return Vector of layer builders
148  */
149  std::vector<Layer::Ptr>& getLayers();
150  /**
151  * @brief Returns constant vector of layer builders
152  *
153  * @return constant vector of layer builders
154  */
155  const std::vector<Layer::Ptr>& getLayers() const;
156 
157  /**
158  * @brief Returns a constant smart pointer to a Layer interface.
159  * If the layer is missing, returns nullptr.
160  * @param id Id of the Layer
161  * @return Layer interface smart pointer
162  */
163  const ILayer::CPtr getLayer(idx_t id) const noexcept override;
164  Layer::Ptr getLayer(idx_t layerId);
165 
166  /**
167  * @brief Returns a constant vector of input layers.
168  * @return Vector of input layers
169  */
170  const std::vector<ILayer::CPtr> getInputs() const noexcept override;
171  /**
172  * @brief Returns a vector of input layers.
173  * @return Vector of input layers
174  */
175  std::vector<Layer::Ptr> getInputs();
176 
177  /**
178  * @brief Returns a constant vector of output layers.
179  * @return Vector of output layers
180  */
181  const std::vector<ILayer::CPtr> getOutputs() const noexcept override;
182  /**
183  * @brief Returns a vector of input layers.
184  * @return Vector of input layers
185  */
186  std::vector<Layer::Ptr> getOutputs();
187 
188  /**
189  * @brief Returns a constant vector of connections for specific layer.
190  * If the layer is missing, returns empty vector.
191  * @param layerId layer index
192  * @return Vector of connections
193  */
194  const std::vector<Connection> getLayerConnections(idx_t layerId) const noexcept override;
195 
196  /**
197  * @brief Returns a constant vector of all connections.
198  * @return Vector of connections
199  */
200  const std::vector<Connection>& getConnections() const;
201 
202  /**
203  * @brief Returns a network name.
204  * @return Network name
205  */
206  const std::string& getName() const noexcept override;
207 
208  /**
209  * @brief Returns a network context
210  * @return const reference to Context
211  */
212  const Context& getContext() const noexcept override;
213  /**
214  * @brief Returns a network context
215  * @return reference to Context
216  */
217  Context& getContext() noexcept;
218 
219  /**
220  * @brief Builds and validate network
221  *
222  * @return const shared pointer to INetwork
223  */
224  const INetwork::CPtr build();
225 
226  /**
227  * @brief Validates network
228  *
229  */
230  void validate();
231 
232  /**
233  * @brief The operator builds network
234  *
235  * @return const shared pointer to INetwork
236  */
237  explicit operator const INetwork::CPtr();
238 
239 private:
240  std::map<std::string, Parameter> parameters;
241 };
242 
243 /**
244  * @deprecated Use ngraph API instead.
245  * @brief This function converts INetwork to ICNNNetwork
246  *
247  * @param network constant shared pointer to INetwork object
248  * @return constant shared pointer to ICNNNetwork
249  */
250 INFERENCE_ENGINE_NN_BUILDER_DEPRECATED
251 INFERENCE_ENGINE_API_CPP(const std::shared_ptr<ICNNNetwork>) convertToICNNNetwork(const INetwork::CPtr& network);
252 
253 IE_SUPPRESS_DEPRECATED_END
254 
255 } // namespace Builder
256 
257 } // namespace InferenceEngine
A header file that provides wrapper for ICNNNetwork object.
Inference Engine API.
Definition: ie_argmax_layer.hpp:11
A header file for Blob and generic TBlob<>
This is a header file for the ICNNNetwork class.
size_t idx_t
A type of network objects indexes.
Definition: ie_network.hpp:28
This is a header file for the IE Context class.
This is a header file with common inference engine definitions.