Files
openvino/samples/cpp/benchmark_app/utils.hpp
Piotr Krzemiński d0a97af629 [BENCHMARK_APP] Introduce Numpy array loading for C++ benchmark app/Fix a bug that would cause Python Numpy array loading to fail (#14021)
* [C++/BENCHMARK_APP] Introduce Numpy array loading for C++ benchmark app

* [DOCS/BENCHMARK_APP] Update docs to reflect changes, update list of available extensions from OpenCV, align help messages

* Update inputs_filling.cpp

* Update tools/benchmark_tool/openvino/tools/benchmark/utils/inputs_filling.py

Co-authored-by: Zlobin Vladimir <vladimir.zlobin@intel.com>

* Update samples/cpp/benchmark_app/inputs_filling.cpp

Co-authored-by: Zlobin Vladimir <vladimir.zlobin@intel.com>

* [C++/Python] Implement quality-of-life improvements from PR comments

* [C++] Fix compilation errors, fix linter output

* [C++/PYTHON] Apply requested changes

* Update samples/cpp/benchmark_app/main.cpp

Co-authored-by: Zlobin Vladimir <vladimir.zlobin@intel.com>

* Update samples/cpp/benchmark_app/utils.cpp

Co-authored-by: Zlobin Vladimir <vladimir.zlobin@intel.com>

* [PYTHON] Separate loading of numpy arrays similar to images

* [PYTHON] Remove unnecessary 'Prepare xxx file' print

* Update README again because IF OPENCV.. dissapeared for some reason

* Update second README with missing IF OPENCV..

* [C++] Remove unnecessary vector print function

* [C++ Add Numpy processing function - TODO link it to the tensor filling

* Reverse OneDnn plugin modification

* [C++] Numpy array loading for C++

* [C++] Add (almost) all missing types of data

* Reverse submodule modifications

* [C++/PYTHON] Fix compilation errors, clean code

* [C++] Modify supported extensions, add numpy checking to utils, add numpy to get_image_info method

* Update samples/cpp/benchmark_app/inputs_filling.cpp

Co-authored-by: Zlobin Vladimir <vladimir.zlobin@intel.com>

* [C++] Fix utils header file to reflect unordered set change

* [PYTHON/C++] Fix compilation errors in C++ code, fix Python dynamic shapes numpy loading

* [C++] Fix explicit instantiation of NumpyArray reader

* [C++] Clang format, minor syntax fixes

* [PYTHON/C++] Remove unnecessary data types, introduce a new approach to cast data of different types from format_rt_reader, remove uppercase types from Python precision parameters

* [PYTHON] Update README to reflect new precision settings

* [PYTHON] Fix README, fix clang format

* [C++] Clean headers

* [C++] Fix uninitialized variable error

* [C++/PYTHON] Fixed choices in Python benchmark, fixed types in C++ benchmark

* [C++] Fixed ov::float16conversion, fixed Python types map - removed redundancies

* [C++] Add back boolean support

* [C++] Fix compilation errors

---------

Co-authored-by: Zlobin Vladimir <vladimir.zlobin@intel.com>
2023-02-13 12:04:23 +04:00

155 lines
7.9 KiB
C++

// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <chrono>
#include <iomanip>
#include <map>
#include <openvino/openvino.hpp>
#include <samples/slog.hpp>
#include <string>
#include <unordered_set>
#include <vector>
#ifdef USE_OPENCV
const std::unordered_set<std::string> supported_image_extensions =
{"bmp", "dib", "jpeg", "jpg", "jpe", "jp2", "png", "pbm", "pgm", "ppm", "sr", "ras", "tiff", "tif"};
#else
const std::unordered_set<std::string> supported_image_extensions = {"bmp"};
#endif
const std::unordered_set<std::string> supported_numpy_extensions = {"npy"};
const std::unordered_set<std::string> supported_binary_extensions = {"bin"};
typedef std::chrono::high_resolution_clock Time;
typedef std::chrono::nanoseconds ns;
inline uint64_t get_duration_in_milliseconds(uint64_t duration) {
return duration * 1000LL;
}
inline uint64_t get_duration_in_nanoseconds(uint64_t duration) {
return duration * 1000000000LL;
}
inline double get_duration_ms_till_now(Time::time_point& startTime) {
return std::chrono::duration_cast<ns>(Time::now() - startTime).count() * 0.000001;
};
namespace benchmark_app {
struct InputInfo {
ov::element::Type type;
ov::PartialShape partialShape;
ov::Shape dataShape;
ov::Layout layout;
std::vector<float> scale;
std::vector<float> mean;
bool is_image() const;
bool is_image_info() const;
size_t width() const;
size_t height() const;
size_t channels() const;
size_t batch() const;
size_t depth() const;
std::vector<std::string> fileNames;
};
using InputsInfo = std::map<std::string, InputInfo>;
using PartialShapes = std::map<std::string, ngraph::PartialShape>;
} // namespace benchmark_app
std::vector<std::string> parse_devices(const std::string& device_string);
uint32_t device_default_device_duration_in_seconds(const std::string& device);
std::map<std::string, std::string> parse_value_per_device(const std::vector<std::string>& devices,
const std::string& values_string);
void parse_value_for_virtual_device(const std::string& device, std::map<std::string, std::string>& values_string);
std::string get_shapes_string(const benchmark_app::PartialShapes& shapes);
size_t get_batch_size(const benchmark_app::InputsInfo& inputs_info);
std::vector<std::string> split(const std::string& s, char delim);
std::map<std::string, std::vector<float>> parse_scale_or_mean(const std::string& scale_mean,
const benchmark_app::InputsInfo& inputs_info);
std::pair<std::string, std::vector<std::string>> parse_input_files(const std::string& file_paths_string);
std::map<std::string, std::vector<std::string>> parse_input_arguments(const std::vector<std::string>& args);
std::map<std::string, std::vector<std::string>> parse_input_parameters(const std::string& parameter_string,
const ov::ParameterVector& input_info);
/// <summary>
/// Parses command line data and data obtained from the function and returns configuration of each input
/// </summary>
/// <param name="shape_string">command-line shape string</param>
/// <param name="layout_string">command-line layout string</param>
/// <param name="batch_size">command-line batch string</param>
/// <param name="tensors_shape_string">command-line data_shape string</param>
/// <param name="scale_string">command-line iscale string</param>
/// <param name="mean_string">command-line imean string</param>
/// <param name="input_info">inputs vector obtained from ov::Model</param>
/// <param name="reshape_required">returns true to this parameter if reshape is required</param>
/// <returns>vector of benchmark_app::InputsInfo elements.
/// Each element is a configuration item for every test configuration case
/// (number of cases is calculated basing on data_shape and other parameters).
/// Each element is a map (input_name, configuration) containing data for each input</returns>
std::vector<benchmark_app::InputsInfo> get_inputs_info(const std::string& shape_string,
const std::string& layout_string,
const size_t batch_size,
const std::string& data_shapes_string,
const std::map<std::string, std::vector<std::string>>& fileNames,
const std::string& scale_string,
const std::string& mean_string,
const std::vector<ov::Output<const ov::Node>>& input_info,
bool& reshape_required);
/// <summary>
/// Parses command line data and data obtained from the function and returns configuration of each input
/// </summary>
/// <param name="shape_string">command-line shape string</param>
/// <param name="layout_string">command-line layout string</param>
/// <param name="batch_size">command-line batch string</param>
/// <param name="tensors_shape_string">command-line data_shape string</param>
/// <param name="scale_string">command-line iscale string</param>
/// <param name="mean_string">command-line imean string</param>
/// <param name="input_info">inputs vector obtained from ov::Model</param>
/// <param name="reshape_required">returns true to this parameter if reshape is required</param>
/// <returns>vector of benchmark_app::InputsInfo elements.
/// Each element is a configuration item for every test configuration case
/// (number of cases is calculated basing on data_shape and other parameters).
/// Each element is a map (input_name, configuration) containing data for each
/// input</returns>
std::vector<benchmark_app::InputsInfo> get_inputs_info(const std::string& shape_string,
const std::string& layout_string,
const size_t batch_size,
const std::string& data_shapes_string,
const std::map<std::string, std::vector<std::string>>& fileNames,
const std::string& scale_string,
const std::string& mean_string,
const std::vector<ov::Output<const ov::Node>>& input_info);
void dump_config(const std::string& filename, const std::map<std::string, ov::AnyMap>& config);
void load_config(const std::string& filename, std::map<std::string, ov::AnyMap>& config);
std::string get_extension(const std::string& name);
bool is_binary_file(const std::string& filePath);
bool is_numpy_file(const std::string& filePath);
bool is_image_file(const std::string& filePath);
bool contains_binaries(const std::vector<std::string>& filePaths);
std::vector<std::string> filter_files_by_extensions(const std::vector<std::string>& filePaths,
const std::unordered_set<std::string>& extensions);
std::string parameter_name_to_tensor_name(
const std::string& name,
const std::vector<ov::Output<const ov::Node>>& inputs_info,
const std::vector<ov::Output<const ov::Node>>& outputs_info = std::vector<ov::Output<const ov::Node>>());
template <class T>
void convert_io_names_in_map(
T& map,
const std::vector<ov::Output<const ov::Node>>& inputs_info,
const std::vector<ov::Output<const ov::Node>>& outputs_info = std::vector<ov::Output<const ov::Node>>()) {
T new_map;
for (auto& item : map) {
new_map[item.first == "" ? "" : parameter_name_to_tensor_name(item.first, inputs_info, outputs_info)] =
std::move(item.second);
}
map = new_map;
}