OpenVINO.cpp
Go to the documentation of this file.
1 
3 
4 #include <algorithm>
5 #include <exception>
6 #include <fstream>
7 #include <string>
8 #include <utility>
9 #include <vector>
10 
11 #include "BlobReader.hpp"
12 #include "spdlog/spdlog.h"
13 #include "utility/Logging.hpp"
14 #include "utility/spdlog-fmt.hpp"
15 
16 namespace dai {
17 
18 // Definition
20 
21 // static member init
22 // {{major, minor}, 'guessed openvino version to support it'}
23 // major and minor represent openvino NN blob version information
24 const std::map<std::pair<std::uint32_t, std::uint32_t>, OpenVINO::Version> OpenVINO::blobVersionToOpenvinoGuessMapping = {
25  {{5, 0}, OpenVINO::VERSION_2020_3},
26  {{6, 0}, OpenVINO::VERSION_2022_1},
27  {{2020, 3}, OpenVINO::VERSION_2020_3},
28  {{2020, 4}, OpenVINO::VERSION_2020_4},
29  {{2021, 1}, OpenVINO::VERSION_2021_1},
30  {{2021, 2}, OpenVINO::VERSION_2021_2},
31  {{2021, 3}, OpenVINO::VERSION_2021_3},
32  {{2021, 4}, OpenVINO::VERSION_2021_4},
33  {{2022, 1}, OpenVINO::VERSION_2022_1},
34 
35 };
36 
37 const std::map<std::pair<std::uint32_t, std::uint32_t>, std::vector<OpenVINO::Version>> OpenVINO::blobVersionToOpenvinoMapping = {
39  {{6, 0},
54 
55 };
56 
57 std::vector<OpenVINO::Version> OpenVINO::getVersions() {
65 }
66 
68  switch(version) {
70  return "2020.3";
72  return "2020.4";
74  return "2021.1";
76  return "2021.2";
78  return "2021.3";
80  return "2021.4";
82  return "2022.1";
84  return "universal";
85  }
86  throw std::logic_error("OpenVINO - Unknown version enum specified");
87 }
88 
89 OpenVINO::Version OpenVINO::parseVersionName(const std::string& versionString) {
90  auto versions = getVersions();
91  for(const auto& v : versions) {
92  if(versionString == getVersionName(v)) {
93  return v;
94  }
95  }
96  throw std::logic_error("OpenVINO - Cannot parse version name: " + versionString);
97 }
98 
99 std::vector<OpenVINO::Version> OpenVINO::getBlobSupportedVersions(std::uint32_t majorVersion, std::uint32_t minorVersion) {
100  std::pair<std::uint32_t, std::uint32_t> blobVersion;
101  blobVersion.first = majorVersion;
102  blobVersion.second = minorVersion;
103 
104  if(blobVersionToOpenvinoMapping.count(blobVersion) > 0) {
105  return blobVersionToOpenvinoMapping.at(blobVersion);
106  }
107  return {};
108 }
109 
110 OpenVINO::Version OpenVINO::getBlobVersion(std::uint32_t majorVersion, std::uint32_t minorVersion) {
111  std::pair<std::uint32_t, std::uint32_t> blobVersion;
112  blobVersion.first = majorVersion;
113  blobVersion.second = minorVersion;
114 
115  return blobVersionToOpenvinoGuessMapping.at(blobVersion);
116 }
117 
118 OpenVINO::Version OpenVINO::getBlobLatestSupportedVersion(std::uint32_t majorVersion, std::uint32_t minorVersion) {
119  (void)majorVersion;
120  (void)minorVersion;
122 }
123 
125  // Universal check
126  if(v1 == VERSION_UNIVERSAL || v2 == VERSION_UNIVERSAL) {
127  return true;
128  }
129 
130  // Classic check
131  // Check all blob versions
132  for(const auto& kv : blobVersionToOpenvinoMapping) {
133  bool v1Found = false;
134  bool v2Found = false;
135 
136  // Check if both openvino versions are in same blob version
137  for(const auto& el : blobVersionToOpenvinoMapping.at(kv.first)) {
138  if(el == v1) v1Found = true;
139  if(el == v2) v2Found = true;
140  }
141 
142  if(v1Found && v2Found) {
143  // if both were found, return true
144  return true;
145  } else if(!v1Found && !v2Found) {
146  // If both weren't found, continue
147  continue;
148  } else {
149  // If one was found but other wasn't, return false
150  return false;
151  }
152  }
153 
154  // If versions weren't matched up in any of the above cases, log an error and return false
155  logger::error("OpenVINO - version compatibility check with invalid values or unknown blob version");
156  return false;
157 }
158 
159 static void blobInit(OpenVINO::Blob& blob, std::vector<uint8_t> data) {
160  blob.data = std::move(data);
161  BlobReader reader;
162  reader.parse(blob.data);
163  blob.networkInputs = reader.getNetworkInputs();
164  blob.networkOutputs = reader.getNetworkOutputs();
165  blob.stageCount = reader.getStageCount();
166  blob.numShaves = reader.getNumberOfShaves();
167  blob.numSlices = reader.getNumberOfSlices();
169 }
170 
171 OpenVINO::Blob::Blob(std::vector<uint8_t> data) {
172  blobInit(*this, std::move(data));
173 }
174 
176  // Load binary file at path
177  std::ifstream stream(path, std::ios::in | std::ios::binary);
178  if(!stream.is_open()) {
179  // Throw an error
180  // TODO(themarpe) - Unify exceptions into meaningful groups
181  throw std::runtime_error(fmt::format("Cannot load blob, file at path {} doesn't exist.", path));
182  }
183  blobInit(*this, std::vector<std::uint8_t>(std::istreambuf_iterator<char>(stream), {}));
184 }
185 
186 } // namespace dai
dai::OpenVINO::Blob::version
Version version
OpenVINO version.
Definition: OpenVINO.hpp:38
dai::BlobReader::getNumberOfSlices
uint32_t getNumberOfSlices() const
Definition: BlobReader.hpp:35
dai::OpenVINO::Blob
OpenVINO Blob.
Definition: OpenVINO.hpp:23
dai::OpenVINO::Blob::stageCount
uint32_t stageCount
Number of network stages.
Definition: OpenVINO.hpp:44
dai::OpenVINO::Version
Version
OpenVINO Version supported version information.
Definition: OpenVINO.hpp:20
dai::OpenVINO::VERSION_2020_4
@ VERSION_2020_4
Definition: OpenVINO.hpp:20
dai::OpenVINO::DEFAULT_VERSION
constexpr static const Version DEFAULT_VERSION
Main OpenVINO version.
Definition: OpenVINO.hpp:54
dai::BlobReader::parse
void parse(const std::vector< std::uint8_t > &blob)
Definition: BlobReader.cpp:45
dai::OpenVINO::getBlobVersion
static Version getBlobVersion(std::uint32_t majorVersion, std::uint32_t minorVersion)
Definition: OpenVINO.cpp:110
dai::BlobReader::getStageCount
uint32_t getStageCount() const
Definition: BlobReader.hpp:27
DAI_SPAN_NAMESPACE_NAME::detail::data
constexpr auto data(C &c) -> decltype(c.data())
Definition: span.hpp:177
dai::OpenVINO::areVersionsBlobCompatible
static bool areVersionsBlobCompatible(Version v1, Version v2)
Definition: OpenVINO.cpp:124
dai::OpenVINO::Blob::data
std::vector< uint8_t > data
Blob data.
Definition: OpenVINO.hpp:50
dai::BlobReader::getNumberOfShaves
uint32_t getNumberOfShaves() const
Definition: BlobReader.hpp:34
dai::OpenVINO::Blob::numShaves
uint32_t numShaves
Number of shaves the blob was compiled for.
Definition: OpenVINO.hpp:46
dai::OpenVINO::Blob::numSlices
uint32_t numSlices
Number of CMX slices the blob was compiled for.
Definition: OpenVINO.hpp:48
dai::OpenVINO::getBlobLatestSupportedVersion
static Version getBlobLatestSupportedVersion(std::uint32_t majorVersion, std::uint32_t minorVersion)
Definition: OpenVINO.cpp:118
dai::OpenVINO::blobVersionToOpenvinoGuessMapping
static const std::map< std::pair< std::uint32_t, std::uint32_t >, Version > blobVersionToOpenvinoGuessMapping
Definition: OpenVINO.hpp:106
dai::BlobReader::getVersionMajor
uint32_t getVersionMajor() const
Definition: BlobReader.hpp:31
dai::BlobReader::getNetworkOutputs
const std::unordered_map< std::string, TensorInfo > & getNetworkOutputs() const
Definition: BlobReader.hpp:25
dai::logger::error
void error(const FormatString &fmt, Args &&...args)
Definition: Logging.hpp:90
BlobReader.hpp
dai::OpenVINO::VERSION_2020_3
@ VERSION_2020_3
Definition: OpenVINO.hpp:20
dai::OpenVINO::VERSION_2021_4
@ VERSION_2021_4
Definition: OpenVINO.hpp:20
dai::OpenVINO::VERSION_2022_1
@ VERSION_2022_1
Definition: OpenVINO.hpp:20
dai::OpenVINO::VERSION_UNIVERSAL
@ VERSION_UNIVERSAL
Definition: OpenVINO.hpp:20
dai::OpenVINO::VERSION_2021_1
@ VERSION_2021_1
Definition: OpenVINO.hpp:20
dai::OpenVINO::Blob::networkOutputs
std::unordered_map< std::string, TensorInfo > networkOutputs
Map of output names to additional information.
Definition: OpenVINO.hpp:42
OpenVINO.hpp
dai::OpenVINO::getVersions
static std::vector< Version > getVersions()
Definition: OpenVINO.cpp:57
dai::OpenVINO::getVersionName
static std::string getVersionName(Version version)
Definition: OpenVINO.cpp:67
dai::OpenVINO::getBlobSupportedVersions
static std::vector< Version > getBlobSupportedVersions(std::uint32_t majorVersion, std::uint32_t minorVersion)
Definition: OpenVINO.cpp:99
dai::BlobReader::getVersionMinor
uint32_t getVersionMinor() const
Definition: BlobReader.hpp:32
dai::blobInit
static void blobInit(OpenVINO::Blob &blob, std::vector< uint8_t > data)
Definition: OpenVINO.cpp:159
spdlog-fmt.hpp
dai::BlobReader
Definition: BlobReader.hpp:18
dai::OpenVINO::Blob::Blob
Blob(std::vector< uint8_t > data)
Construct a new Blob from data in memory.
Definition: OpenVINO.cpp:171
dai::BlobReader::getNetworkInputs
const std::unordered_map< std::string, TensorInfo > & getNetworkInputs() const
Definition: BlobReader.hpp:24
dai::Path
Represents paths on a filesystem; accepts utf-8, Windows utf-16 wchar_t, or std::filesystem::path.
Definition: Path.hpp:27
Logging.hpp
dai
Definition: CameraExposureOffset.hpp:6
dai::OpenVINO::blobVersionToOpenvinoMapping
static const std::map< std::pair< std::uint32_t, std::uint32_t >, std::vector< Version > > blobVersionToOpenvinoMapping
Definition: OpenVINO.hpp:107
dai::OpenVINO::VERSION_2021_2
@ VERSION_2021_2
Definition: OpenVINO.hpp:20
dai::OpenVINO::Blob::networkInputs
std::unordered_map< std::string, TensorInfo > networkInputs
Map of input names to additional information.
Definition: OpenVINO.hpp:40
dai::OpenVINO::VERSION_2021_3
@ VERSION_2021_3
Definition: OpenVINO.hpp:20
dai::OpenVINO::parseVersionName
static Version parseVersionName(const std::string &versionString)
Definition: OpenVINO.cpp:89


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19