plugin  0.1.0
common.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2024 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
10 #pragma once
11 
12 #include <iostream>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include <openvino/openvino.hpp>
18 #include "utils/slog.hpp"
19 #include "utils/args_helper.hpp"
20 
21 #ifndef UNUSED
22 #ifdef _WIN32
23 #define UNUSED
24 #else
25 #define UNUSED __attribute__((unused))
26 #endif
27 #endif
28 
29 template <typename T, std::size_t N>
30 constexpr std::size_t arraySize(const T(&)[N]) noexcept {
31  return N;
32 }
33 
34 static inline void catcher() noexcept {
35  if (std::current_exception()) {
36  try {
37  std::rethrow_exception(std::current_exception());
38  } catch (const std::exception& error) {
39  slog::err << error.what() << slog::endl;
40  } catch (...) {
41  slog::err << "Non-exception object thrown" << slog::endl;
42  }
43  std::exit(1);
44  }
45  std::abort();
46 }
47 
48 template <typename T>
49 T clamp(T value, T low, T high) {
50  return value < low ? low : (value > high ? high : value);
51 }
52 
53 inline slog::LogStream& operator<<(slog::LogStream& os, const ov::Version& version) {
54  return os << "OpenVINO" << slog::endl
55  << "\tversion: " << OPENVINO_VERSION_MAJOR << "." << OPENVINO_VERSION_MINOR << "." << OPENVINO_VERSION_PATCH << slog::endl
56  << "\tbuild: " << version.buildNumber;
57 }
58 
63 class Color {
64 private:
65  unsigned char _r;
66  unsigned char _g;
67  unsigned char _b;
68 
69 public:
76  Color(unsigned char r,
77  unsigned char g,
78  unsigned char b) : _r(r), _g(g), _b(b) {}
79 
80  inline unsigned char red() const {
81  return _r;
82  }
83 
84  inline unsigned char blue() const {
85  return _b;
86  }
87 
88  inline unsigned char green() const {
89  return _g;
90  }
91 };
92 
93 // Known colors for training classes from the Cityscapes dataset
94 static UNUSED const Color CITYSCAPES_COLORS[] = {
95  { 128, 64, 128 },
96  { 232, 35, 244 },
97  { 70, 70, 70 },
98  { 156, 102, 102 },
99  { 153, 153, 190 },
100  { 153, 153, 153 },
101  { 30, 170, 250 },
102  { 0, 220, 220 },
103  { 35, 142, 107 },
104  { 152, 251, 152 },
105  { 180, 130, 70 },
106  { 60, 20, 220 },
107  { 0, 0, 255 },
108  { 142, 0, 0 },
109  { 70, 0, 0 },
110  { 100, 60, 0 },
111  { 90, 0, 0 },
112  { 230, 0, 0 },
113  { 32, 11, 119 },
114  { 0, 74, 111 },
115  { 81, 0, 81 }
116 };
117 
118 inline void showAvailableDevices() {
119  ov::Core core;
120  std::vector<std::string> devices = core.get_available_devices();
121 
122  std::cout << "Available devices:";
123  for (const auto& device : devices) {
124  std::cout << ' ' << device;
125  }
126  std::cout << std::endl;
127 }
128 
129 inline std::string fileNameNoExt(const std::string& filepath) {
130  auto pos = filepath.rfind('.');
131  if (pos == std::string::npos) return filepath;
132  return filepath.substr(0, pos);
133 }
134 
135 inline void logCompiledModelInfo(
136  const ov::CompiledModel& compiledModel,
137  const std::string& modelName,
138  const std::string& deviceName,
139  const std::string& modelType = "") {
140  slog::info << "The " << modelType << (modelType.empty() ? "" : " ") << "model " << modelName << " is loaded to " << deviceName << slog::endl;
141  std::set<std::string> devices;
142  for (const std::string& device : parseDevices(deviceName)) {
143  devices.insert(device);
144  }
145 
146  if (devices.find("AUTO") == devices.end()) { // do not print info for AUTO device
147  for (const auto& device : devices) {
148  try {
149  slog::info << "\tDevice: " << device << slog::endl;
150  int32_t nstreams = compiledModel.get_property(ov::streams::num);
151  slog::info << "\t\tNumber of streams: " << nstreams << slog::endl;
152  if (device == "CPU") {
153  int32_t nthreads = compiledModel.get_property(ov::inference_num_threads);
154  slog::info << "\t\tNumber of threads: " << (nthreads == 0 ? "AUTO" : std::to_string(nthreads)) << slog::endl;
155  }
156  }
157  catch (const ov::Exception&) {}
158  }
159  }
160 }
161 
162 inline void logBasicModelInfo(const std::shared_ptr<ov::Model>& model) {
163  slog::info << "Model name: " << model->get_friendly_name() << slog::endl;
164 
165  // Dump information about model inputs/outputs
166  ov::OutputVector inputs = model->inputs();
167  ov::OutputVector outputs = model->outputs();
168 
169  slog::info << "\tInputs: " << slog::endl;
170  for (const ov::Output<ov::Node>& input : inputs) {
171  const std::string name = input.get_any_name();
172  const ov::element::Type type = input.get_element_type();
173  const ov::PartialShape shape = input.get_partial_shape();
174  const ov::Layout layout = ov::layout::get_layout(input);
175 
176  slog::info << "\t\t" << name << ", " << type << ", " << shape << ", " << layout.to_string() << slog::endl;
177  }
178 
179  slog::info << "\tOutputs: " << slog::endl;
180  for (const ov::Output<ov::Node>& output : outputs) {
181  const std::string name = output.get_any_name();
182  const ov::element::Type type = output.get_element_type();
183  const ov::PartialShape shape = output.get_partial_shape();
184  const ov::Layout layout = ov::layout::get_layout(output);
185 
186  slog::info << "\t\t" << name << ", " << type << ", " << shape << ", " << layout.to_string() << slog::endl;
187  }
188 
189  return;
190 }
191 
192 std::vector<unsigned> loadClassIndices(const std::string &groundtruth_filepath,
193  const std::vector<std::string> &imageNames);
A Color class stores channels of a given color.
Definition: common.hpp:63
Color(unsigned char r, unsigned char g, unsigned char b)
Definition: common.hpp:76
The LogStream class implements a stream for sample logging.
Definition: slog.hpp:39
a header file with common samples functionality