Support config as part of Infer node in yml

This commit is contained in:
TolyaTalamanov 2022-09-15 10:47:13 +00:00
parent 46d988e2cb
commit eb8883160d
2 changed files with 39 additions and 5 deletions

View File

@ -190,6 +190,15 @@ CallParams read<CallParams>(const cv::FileNode& fn) {
return CallParams{std::move(name), static_cast<size_t>(call_every_nth)}; return CallParams{std::move(name), static_cast<size_t>(call_every_nth)};
} }
template <typename V>
std::map<std::string, V> readMap(const cv::FileNode& fn) {
std::map<std::string, V> map;
for (auto item : fn) {
map.emplace(item.name(), read<V>(item));
}
return map;
}
template <> template <>
InferParams read<InferParams>(const cv::FileNode& fn) { InferParams read<InferParams>(const cv::FileNode& fn) {
auto name = auto name =
@ -200,7 +209,7 @@ InferParams read<InferParams>(const cv::FileNode& fn) {
params.device = check_and_read<std::string>(fn, "device", name); params.device = check_and_read<std::string>(fn, "device", name);
params.input_layers = readList<std::string>(fn, "input_layers", name); params.input_layers = readList<std::string>(fn, "input_layers", name);
params.output_layers = readList<std::string>(fn, "output_layers", name); params.output_layers = readList<std::string>(fn, "output_layers", name);
params.config = readMap<std::string>(fn["config"]);
return params; return params;
} }
@ -309,13 +318,13 @@ int main(int argc, char* argv[]) {
cv::FileStorage::MEMORY); cv::FileStorage::MEMORY);
} }
std::map<std::string, std::string> config; std::map<std::string, std::string> gconfig;
if (!load_config.empty()) { if (!load_config.empty()) {
loadConfig(load_config, config); loadConfig(load_config, gconfig);
} }
// NB: Takes priority over config from file // NB: Takes priority over config from file
if (!cached_dir.empty()) { if (!cached_dir.empty()) {
config = gconfig =
std::map<std::string, std::string>{{"CACHE_DIR", cached_dir}}; std::map<std::string, std::string>{{"CACHE_DIR", cached_dir}};
} }
@ -371,7 +380,10 @@ int main(int argc, char* argv[]) {
builder.addDummy(call_params, read<DummyParams>(node_fn)); builder.addDummy(call_params, read<DummyParams>(node_fn));
} else if (node_type == "Infer") { } else if (node_type == "Infer") {
auto infer_params = read<InferParams>(node_fn); auto infer_params = read<InferParams>(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); builder.addInfer(call_params, infer_params);
} else { } else {
throw std::logic_error("Unsupported node type: " + node_type); throw std::logic_error("Unsupported node type: " + node_type);

View File

@ -1,6 +1,8 @@
#ifndef OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP #ifndef OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP
#define OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP #define OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP
#include <map>
#include <opencv2/core.hpp> #include <opencv2/core.hpp>
#if defined(_WIN32) #if defined(_WIN32)
@ -91,6 +93,26 @@ typename duration_t::rep timestamp() {
return duration_cast<duration_t>(now.time_since_epoch()).count(); return duration_cast<duration_t>(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 <typename K, typename V>
void intersectMapWith(std::map<K, V>& target, const std::map<K, V>& 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 } // namespace utils
#endif // OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP #endif // OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP