summaryrefslogtreecommitdiff
path: root/inference-engine/include/builders/ie_network_builder.hpp
blob: 586a267c5e890feb459a2cc014a0af2751041c1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (C) 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <builders/ie_layer_builder.hpp>
#include <ie_icnn_network.hpp>
#include <cpp/ie_cnn_network.h>
#include <ie_inetwork.hpp>
#include <ie_context.hpp>
#include <ie_common.h>
#include <ie_blob.h>
#include <utility>
#include <memory>
#include <string>
#include <vector>
#include <map>

namespace InferenceEngine {
namespace Builder {

/**
 * @brief This class implements a builder for IE Network
 */
class INFERENCE_ENGINE_API_CLASS(Network) {
public:
    /**
     * @brief A shared pointer to the Network builder
     */
    using Ptr = std::shared_ptr<Network>;

    /**
     * @brief The constructor creates a builder based on ICNNNetwork
     *
     * @param network constant reference to ICNNNetwork object
     */
    explicit Network(const ICNNNetwork& network);
    /**
     * @brief The constructor creates a empty builder with network name
     *
     * @param name Network name
     */
    explicit Network(const std::string& name);
    /**
     * @brief The constructor creates a builder based on INetwork
     *
     * @param network constant reference to INetwork object
     */
    explicit Network(const INetwork& network);

    /**
     * @brief The constructor creates a builder based on ICNNNetwork with custom Context
     *
     * @param network constant reference to ICNNNetwork object
     */
    Network(const Context& ieContext, const ICNNNetwork& network);
    /**
     * @brief The constructor creates a empty builder with network name and custom Context
     *
     * @param name Network name
     */
    Network(const Context& ieContext, const std::string& name);
    /**
     * @brief The constructor creates a builder based on INetwork with custom Context
     *
     * @param network constant reference to INetwork object
     */
    Network(const Context& ieContext, const INetwork& network);

    /**
     * @brief Virtual destructor
     */
    virtual ~Network() = default;

    /**
     * @brief Adds new layer and connects it with previous layers
     *
     * @param inputs Vector with PortInfo objects from previous layers
     * @param layer Layer builder for new layer
     *
     * @return Id of new builder for the current network
     */
    idx_t addLayer(const std::vector<PortInfo>& inputs, const Layer& layer);
    /**
     * @brief Adds new layer
     *
     * @param layer Layer builder for new layer
     *
     * @return Id of new builder for the current network
     */
    idx_t addLayer(const Layer& layer);
    /**
     * @brief Removes a layer by ID
     *
     * @param layerId Layer ID
     */
    void removeLayer(idx_t layerId);

    /**
     * @brief Connects two layers
     *
     * @param input PortInfo object from previous layer
     * @param output PortInfo object from next layer
     */
    void connect(const PortInfo& input, const PortInfo& output);
    /**
     * @brief Removes connection from the network
     *
     * @param connection Connection
     */
    void disconnect(const Connection& connection);

    /**
     * @brief Returns layer builder by ID
     *
     * @param layerId Layer ID
     *
     * @return Layer buider
     */
    Layer& getLayer(idx_t layerId);
    /**
     * @brief Returns constant layer builder by ID
     *
     * @param layerId Layer ID
     *
     * @return constant layer builder
     */
    const Layer& getLayer(idx_t layerId) const;

    /**
     * @brief Returns vector of layer builders
     *
     * @return Vector of layer builders
     */
    std::vector<Layer>& getLayers();
    /**
     * @brief Returns constant vector of layer builders
     *
     * @return constant vector of layer builders
     */
    const std::vector<Layer>& getLayers() const;

    /**
     * @brief Returns all connections for layer
     *
     * @param layerId Layer ID
     *
     * @return Vector of connections for the current layer
     */
    const std::vector<Connection> getLayerConnections(idx_t layerId) const noexcept;

    /**
     * @brief Builds and validate networks
     *
     * @return const shared pointer to INetwork
     */
    const INetwork::Ptr build() const;

    /**
     * @brief The operator builds network
     *
     * @return const shared pointer to INetwork
     */
    explicit operator const INetwork::Ptr() const;

private:
    const Context ctx;
    const size_t version;
    std::string name;
    std::vector<Layer> layers;
    std::vector<Connection> connections;
};

/**
 * @brief This function converts INetwork to ICNNNetwork
 *
 * @param network constant shared pointer to INetwork object
 * @return constant shared pointer to ICNNNetwork
 */
INFERENCE_ENGINE_API_CPP(const std::shared_ptr<ICNNNetwork>) convertToICNNNetwork(const INetwork::Ptr& network);

}  // namespace Builder

}  // namespace InferenceEngine