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