From eb8883160d8705fba59f884d9ebcb9842c8552d7 Mon Sep 17 00:00:00 2001 From: TolyaTalamanov Date: Thu, 15 Sep 2022 10:47:13 +0000 Subject: [PATCH 1/3] Support config as part of Infer node in yml --- .../gapi/samples/pipeline_modeling_tool.cpp | 22 ++++++++++++++----- .../samples/pipeline_modeling_tool/utils.hpp | 22 +++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/modules/gapi/samples/pipeline_modeling_tool.cpp b/modules/gapi/samples/pipeline_modeling_tool.cpp index 7a0f94655c..a10cb5f052 100644 --- a/modules/gapi/samples/pipeline_modeling_tool.cpp +++ b/modules/gapi/samples/pipeline_modeling_tool.cpp @@ -190,6 +190,15 @@ CallParams read(const cv::FileNode& fn) { return CallParams{std::move(name), static_cast(call_every_nth)}; } +template +std::map readMap(const cv::FileNode& fn) { + std::map map; + for (auto item : fn) { + map.emplace(item.name(), read(item)); + } + return map; +} + template <> InferParams read(const cv::FileNode& fn) { auto name = @@ -200,7 +209,7 @@ InferParams read(const cv::FileNode& fn) { params.device = check_and_read(fn, "device", name); params.input_layers = readList(fn, "input_layers", name); params.output_layers = readList(fn, "output_layers", name); - + params.config = readMap(fn["config"]); return params; } @@ -309,13 +318,13 @@ int main(int argc, char* argv[]) { cv::FileStorage::MEMORY); } - std::map config; + std::map gconfig; if (!load_config.empty()) { - loadConfig(load_config, config); + loadConfig(load_config, gconfig); } // NB: Takes priority over config from file if (!cached_dir.empty()) { - config = + gconfig = std::map{{"CACHE_DIR", cached_dir}}; } @@ -371,7 +380,10 @@ int main(int argc, char* argv[]) { builder.addDummy(call_params, read(node_fn)); } else if (node_type == "Infer") { auto infer_params = read(node_fn); - infer_params.config = config; + RETHROW_WITH_MSG_IF_FAILED( + utils::intersectMapWith(infer_params.config, gconfig), + "Failed to combine global and local configs for Infer node: " + + call_params.name); builder.addInfer(call_params, infer_params); } else { throw std::logic_error("Unsupported node type: " + node_type); diff --git a/modules/gapi/samples/pipeline_modeling_tool/utils.hpp b/modules/gapi/samples/pipeline_modeling_tool/utils.hpp index c110bf3b47..c8f0101fe1 100644 --- a/modules/gapi/samples/pipeline_modeling_tool/utils.hpp +++ b/modules/gapi/samples/pipeline_modeling_tool/utils.hpp @@ -1,6 +1,8 @@ #ifndef OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP #define OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP +#include + #include #if defined(_WIN32) @@ -91,6 +93,26 @@ typename duration_t::rep timestamp() { return duration_cast(now.time_since_epoch()).count(); } +#define RETHROW_WITH_MSG_IF_FAILED(expr, msg) \ + try { \ + expr; \ + } catch (const std::exception& e) { \ + std::stringstream ss; \ + ss << msg << "\n caused by: " << e.what(); \ + throw std::logic_error(ss.str()); \ + } \ + +template +void intersectMapWith(std::map& target, const std::map& second) { + for (auto&& item : second) { + auto it = target.find(item.first); + if (it != target.end()) { + throw std::logic_error("Met already existing key: " + item.first); + } + target.insert(item); + } +} + } // namespace utils #endif // OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP From ec92f3fefa8421a65f64ab11a942252057483683 Mon Sep 17 00:00:00 2001 From: TolyaTalamanov Date: Fri, 16 Sep 2022 15:24:13 +0000 Subject: [PATCH 2/3] Apply comments * Rename intersectMapWith -> mergeMapWith * Remove macro * Add r-value ref --- modules/gapi/samples/pipeline_modeling_tool.cpp | 14 +++++++++----- .../gapi/samples/pipeline_modeling_tool/utils.hpp | 13 ++----------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/modules/gapi/samples/pipeline_modeling_tool.cpp b/modules/gapi/samples/pipeline_modeling_tool.cpp index a10cb5f052..bacd1742ea 100644 --- a/modules/gapi/samples/pipeline_modeling_tool.cpp +++ b/modules/gapi/samples/pipeline_modeling_tool.cpp @@ -193,7 +193,7 @@ CallParams read(const cv::FileNode& fn) { template std::map readMap(const cv::FileNode& fn) { std::map map; - for (auto item : fn) { + for (auto&& item : fn) { map.emplace(item.name(), read(item)); } return map; @@ -380,10 +380,14 @@ int main(int argc, char* argv[]) { builder.addDummy(call_params, read(node_fn)); } else if (node_type == "Infer") { auto infer_params = read(node_fn); - RETHROW_WITH_MSG_IF_FAILED( - utils::intersectMapWith(infer_params.config, gconfig), - "Failed to combine global and local configs for Infer node: " - + call_params.name); + try { + utils::mergeMapWith(infer_params.config, gconfig); + } catch (std::exception& e) { + std::stringstream ss; + ss << "Failed to merge global and local config for Infer node: " + << call_params.name << std::endl << e.what(); + throw std::logic_error(ss.str()); + } builder.addInfer(call_params, infer_params); } else { throw std::logic_error("Unsupported node type: " + node_type); diff --git a/modules/gapi/samples/pipeline_modeling_tool/utils.hpp b/modules/gapi/samples/pipeline_modeling_tool/utils.hpp index c8f0101fe1..a5be323747 100644 --- a/modules/gapi/samples/pipeline_modeling_tool/utils.hpp +++ b/modules/gapi/samples/pipeline_modeling_tool/utils.hpp @@ -93,21 +93,12 @@ typename duration_t::rep timestamp() { return duration_cast(now.time_since_epoch()).count(); } -#define RETHROW_WITH_MSG_IF_FAILED(expr, msg) \ - try { \ - expr; \ - } catch (const std::exception& e) { \ - std::stringstream ss; \ - ss << msg << "\n caused by: " << e.what(); \ - throw std::logic_error(ss.str()); \ - } \ - template -void intersectMapWith(std::map& target, const std::map& second) { +void mergeMapWith(std::map& target, const std::map& second) { for (auto&& item : second) { auto it = target.find(item.first); if (it != target.end()) { - throw std::logic_error("Met already existing key: " + item.first); + throw std::logic_error("Error: key: " + it->first + " is already in target map"); } target.insert(item); } From 4521d66103cc016ade98777c63c2e37ef42d91be Mon Sep 17 00:00:00 2001 From: TolyaTalamanov Date: Mon, 26 Sep 2022 08:50:23 +0000 Subject: [PATCH 3/3] Remove r-value ref --- modules/gapi/samples/pipeline_modeling_tool.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/gapi/samples/pipeline_modeling_tool.cpp b/modules/gapi/samples/pipeline_modeling_tool.cpp index bacd1742ea..60547a9c9b 100644 --- a/modules/gapi/samples/pipeline_modeling_tool.cpp +++ b/modules/gapi/samples/pipeline_modeling_tool.cpp @@ -193,7 +193,7 @@ CallParams read(const cv::FileNode& fn) { template std::map readMap(const cv::FileNode& fn) { std::map map; - for (auto&& item : fn) { + for (auto item : fn) { map.emplace(item.name(), read(item)); } return map;