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