ie_cnn_net_reader.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 This is a header file for the Network reader class (wrapper) used to build networks from a given IR
7  *
8  * @file ie_cnn_net_reader.h
9  */
10 #pragma once
11 
12 #include <map>
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
19 #include "ie_blob.h"
20 #include "ie_cnn_network.h"
21 #include "ie_common.h"
22 #include "ie_icnn_net_reader.h"
23 
24 namespace InferenceEngine {
25 /**
26  * @deprecated Use InferenceEngine::Core::ReadNetwork methods. This API will be removed in 2021.1
27  * @brief This is a wrapper class used to build and parse a network from the given IR.
28  *
29  * All the methods here can throw exceptions.
30  */
31 IE_SUPPRESS_DEPRECATED_START
32 class INFERENCE_ENGINE_DEPRECATED("Use InferenceEngine::Core::ReadNetwork methods. This API will be removed in 2021.1")
33  CNNNetReader {
34 public:
35  /**
36  * @brief A smart pointer to this class
37  */
38  using Ptr = std::shared_ptr<CNNNetReader>;
39 
40  /**
41  * @brief A default constructor
42  */
43  CNNNetReader(): actual(shared_from_irelease(InferenceEngine::CreateCNNNetReader())) {
44  if (actual == nullptr) {
45  THROW_IE_EXCEPTION << "CNNNetReader was not initialized.";
46  }
47  }
48 
49 #ifdef ENABLE_UNICODE_PATH_SUPPORT
50  /**
51  * @brief Resolve wstring path then call original ReadNetwork
52  *
53  * Wraps ICNNNetReader::ReadNetwork
54  *
55  * @param filepath The full path to the .xml file of the IR
56  */
57  void ReadNetwork(const std::wstring& filepath) {
58  CALL_STATUS_FNC(ReadNetwork, details::wStringtoMBCSstringChar(filepath).c_str());
59  }
60 #endif
61 
62  /**
63  * @copybrief ICNNNetReader::ReadNetwork
64  *
65  * Wraps ICNNNetReader::ReadNetwork
66  *
67  * @param filepath The full path to the .xml file of the IR
68  */
69  void ReadNetwork(const std::string& filepath) {
70  CALL_STATUS_FNC(ReadNetwork, filepath.c_str());
71  }
72 
73  /**
74  * @copybrief ICNNNetReader::ReadNetwork(const void*, size_t, ResponseDesc*)
75  *
76  * Wraps ICNNNetReader::ReadNetwork(const void*, size_t, ResponseDesc*)
77  *
78  * @param model Pointer to a char array with the IR
79  * @param size Size of the char array in bytes
80  */
81  void ReadNetwork(const void* model, size_t size) {
82  CALL_STATUS_FNC(ReadNetwork, model, size);
83  }
84 
85  /**
86  * @copybrief ICNNNetReader::SetWeights
87  *
88  * Wraps ICNNNetReader::SetWeights
89  *
90  * @param weights Blob of bytes that holds all the IR binary data
91  */
92  void SetWeights(const TBlob<uint8_t>::Ptr& weights) {
93  CALL_STATUS_FNC(SetWeights, weights);
94  }
95 
96 #ifdef ENABLE_UNICODE_PATH_SUPPORT
97  /**
98  * @brief Resolve wstring path then call original ReadWeights
99  *
100  * ICNNNetReader::ReadWeights
101  *
102  * @param filepath Full path to the .bin file
103  */
104  void ReadWeights(const std::wstring& filepath) {
105  CALL_STATUS_FNC(ReadWeights, details::wStringtoMBCSstringChar(filepath).c_str());
106  }
107 #endif
108 
109  /**
110  * @copybrief ICNNNetReader::ReadWeights
111  *
112  * Wraps ICNNNetReader::ReadWeights
113  *
114  * @param filepath Full path to the .bin file
115  */
116  void ReadWeights(const std::string& filepath) {
117  CALL_STATUS_FNC(ReadWeights, filepath.c_str());
118  }
119 
120  /**
121  * @brief Gets a copy of built network object
122  *
123  * @return A copy of the CNNNetwork object to be loaded
124  */
126  // network obj are to be updated upon this call
127  if (network.get() == nullptr) {
128  try {
129  network.reset(new CNNNetwork(actual));
130  } catch (...) {
131  THROW_IE_EXCEPTION << "Could not allocate memory";
132  }
133  }
134  return *network.get();
135  }
136 
137  /**
138  * @copybrief ICNNNetReader::isParseSuccess
139  *
140  * Wraps ICNNNetReader::isParseSuccess
141  *
142  * @return true if a parse is successful, false otherwise
143  */
144  bool isParseSuccess() const {
145  CALL_FNC_NO_ARGS(isParseSuccess);
146  }
147 
148  /**
149  * @copybrief ICNNNetReader::getDescription
150  *
151  * Wraps ICNNNetReader::getDescription
152  *
153  * @return StatusCode that indicates the network status
154  */
155  std::string getDescription() const {
156  CALL_STATUS_FNC_NO_ARGS(getDescription);
157  return resp.msg;
158  }
159 
160  /**
161  * @copybrief ICNNNetReader::getName
162  *
163  * Wraps ICNNNetReader::getName
164  *
165  * @return String
166  */
167  std::string getName() const {
168  char name[64];
169  CALL_STATUS_FNC(getName, name, sizeof(name) / sizeof(*name));
170  return name;
171  }
172 
173  /**
174  * @copybrief ICNNNetReader::getVersion
175  *
176  * Wraps ICNNNetReader::getVersion
177  *
178  * @return IR version number: 1 or 2
179  */
180  int getVersion() const {
181  CALL_FNC_NO_ARGS(getVersion);
182  }
183 
184 private:
185  std::shared_ptr<ICNNNetReader> actual;
186  std::shared_ptr<CNNNetwork> network;
187 };
188 IE_SUPPRESS_DEPRECATED_END
189 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
A header file that provides wrapper for ICNNNetwork object.
Inference Engine API.
Definition: ie_argmax_layer.hpp:15
A header file that provides macros to handle no exception methods.
CNNNetwork ReadNetwork(const std::string &modelPath, const std::string &binPath="") const
Reads IR xml and bin files.
This is a wrapper class used to build and parse a network from the given IR.
Definition: ie_cnn_net_reader.h:32
std::string name
Layer name.
Definition: ie_layers.h:42
std::shared_ptr< CNNNetReader > Ptr
A smart pointer to this class.
Definition: ie_cnn_net_reader.h:38
A header file for Blob and generic TBlob<>
std::string getName() const
Definition: ie_cnn_net_reader.h:167
ICNNNetReader * CreateCNNNetReader() noexcept
Creates a CNNNetReader instance.
std::shared_ptr< TBlob< T >> Ptr
Smart Pointer to this TBlob object.
Definition: ie_blob.h:478
CNNNetwork getNetwork()
Gets a copy of built network object.
Definition: ie_cnn_net_reader.h:125
void ReadNetwork(const void *model, size_t size)
Definition: ie_cnn_net_reader.h:81
This class contains all the information about the Neural Network and the related binary information...
Definition: ie_cnn_network.h:38
int getVersion() const
Definition: ie_cnn_net_reader.h:180
void SetWeights(const TBlob< uint8_t >::Ptr &weights)
Definition: ie_cnn_net_reader.h:92
CNNNetReader()
A default constructor.
Definition: ie_cnn_net_reader.h:43
A header file that provides interface for network reader that is used to build networks from a given ...
bool isParseSuccess() const
Definition: ie_cnn_net_reader.h:144
This is a header file with functions related to filesystem operations.
void ReadNetwork(const std::string &filepath)
Definition: ie_cnn_net_reader.h:69
std::string getDescription() const
Definition: ie_cnn_net_reader.h:155
void ReadWeights(const std::string &filepath)
Definition: ie_cnn_net_reader.h:116
This is a header file with common inference engine definitions.