From 6ef53edbbf6d4b83ff5513272931d34b7d69b26e Mon Sep 17 00:00:00 2001 From: Ronald Hecker Date: Wed, 2 Apr 2025 15:20:31 +0200 Subject: [PATCH 1/3] Merge ModelBase into ImageModel Example works, but probably some missing parts. --- src/cpp/models/include/models/image_model.h | 42 +++- src/cpp/models/include/models/model_base.h | 73 ------ src/cpp/models/src/image_model.cpp | 196 +++++++++++++++- src/cpp/models/src/model_base.cpp | 221 ------------------- src/cpp/tilers/src/instance_segmentation.cpp | 4 +- src/cpp/tilers/src/tiler_base.cpp | 2 +- 6 files changed, 224 insertions(+), 314 deletions(-) delete mode 100644 src/cpp/models/include/models/model_base.h delete mode 100644 src/cpp/models/src/model_base.cpp diff --git a/src/cpp/models/include/models/image_model.h b/src/cpp/models/include/models/image_model.h index dfb274bb..74b0087a 100644 --- a/src/cpp/models/include/models/image_model.h +++ b/src/cpp/models/include/models/image_model.h @@ -9,9 +9,12 @@ #include #include +#include "adapters/inference_adapter.h" #include "models/input_data.h" -#include "models/model_base.h" +#include "models/results.h" #include "utils/image_utils.h" +#include "utils/ocv_common.hpp" +#include "utils/args_helper.hpp" namespace ov { class InferRequest; @@ -20,7 +23,7 @@ struct InputData; struct InternalModelData; // ImageModel implements preprocess(), ImageModel's direct or indirect children are expected to implement prostprocess() -class ImageModel : public ModelBase { +class ImageModel { public: /// Constructor /// @param modelFile name of model to load @@ -33,9 +36,24 @@ class ImageModel : public ModelBase { ImageModel(std::shared_ptr& model, const ov::AnyMap& configuration); ImageModel(std::shared_ptr& adapter, const ov::AnyMap& configuration = {}); - using ModelBase::ModelBase; - std::shared_ptr preprocess(const InputData& inputData, InferenceInput& input) override; + virtual std::shared_ptr preprocess(const InputData& inputData, InferenceInput& input); + virtual std::unique_ptr postprocess(InferenceResult& infResult) = 0; + + void load(ov::Core& core, const std::string& device, size_t num_infer_requests = 1); + + std::shared_ptr prepare(); + + virtual size_t getNumAsyncExecutors() const; + virtual bool isReady(); + virtual void awaitAll(); + virtual void awaitAny(); + virtual void setCallback( + std::function, const ov::AnyMap& callback_args)> callback); + + std::shared_ptr getModel(); + std::shared_ptr getInferenceAdapter(); + static std::vector loadLabels(const std::string& labelFilename); std::shared_ptr embedProcessing(std::shared_ptr& model, const std::string& inputName, @@ -54,7 +72,7 @@ class ImageModel : public ModelBase { protected: RESIZE_MODE selectResizeMode(const std::string& resize_type); - void updateModelInfo() override; + virtual void updateModelInfo(); void init_from_config(const ov::AnyMap& top_priority, const ov::AnyMap& mid_priority); std::string getLabelName(size_t labelID) { @@ -73,4 +91,18 @@ class ImageModel : public ModelBase { bool reverse_input_channels = false; std::vector scale_values; std::vector mean_values; + +protected: + virtual void prepareInputsOutputs(std::shared_ptr& model) = 0; + + InputTransform inputTransform = InputTransform(); + + std::shared_ptr model; + std::vector inputNames; + std::vector outputNames; + std::string modelFile; + std::shared_ptr inferenceAdapter; + std::map inputsLayouts; + ov::Layout getInputLayout(const ov::Output& input); + std::function, const ov::AnyMap&)> lastCallback; }; diff --git a/src/cpp/models/include/models/model_base.h b/src/cpp/models/include/models/model_base.h deleted file mode 100644 index 6fe3525d..00000000 --- a/src/cpp/models/include/models/model_base.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2020-2024 Intel Corporation - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -struct InferenceResult; -struct InputData; -struct InternalModelData; -struct ResultBase; - -class ModelBase { -public: - ModelBase(const std::string& modelFile, const std::string& layout = ""); - ModelBase(std::shared_ptr& adapter, const ov::AnyMap& configuration = {}); - ModelBase(std::shared_ptr& model, const ov::AnyMap& configuration); - - virtual ~ModelBase() = default; - - std::shared_ptr prepare(); - void load(ov::Core& core, const std::string& device, size_t num_infer_requests = 1); - // Modifying ov::Model doesn't affect the model wrapper - std::shared_ptr getModel(); - std::shared_ptr getInferenceAdapter(); - - virtual std::shared_ptr preprocess(const InputData& inputData, InferenceInput& input) = 0; - virtual std::unique_ptr postprocess(InferenceResult& infResult) = 0; - virtual std::unique_ptr infer(const InputData& inputData); - virtual void inferAsync(const InputData& inputData, const ov::AnyMap& callback_args = {}); - virtual std::vector> inferBatch( - const std::vector>& inputData); - virtual std::vector> inferBatch(const std::vector& inputData); - - virtual bool isReady(); - virtual void awaitAll(); - virtual void awaitAny(); - virtual void setCallback( - std::function, const ov::AnyMap& callback_args)> callback); - virtual size_t getNumAsyncExecutors() const; - - const std::vector& getoutputNames() const { - return outputNames; - } - const std::vector& getinputNames() const { - return inputNames; - } - -protected: - virtual void prepareInputsOutputs(std::shared_ptr& model) = 0; - virtual void updateModelInfo(); - - InputTransform inputTransform = InputTransform(); - - std::shared_ptr model; - std::vector inputNames; - std::vector outputNames; - std::string modelFile; - std::shared_ptr inferenceAdapter; - std::map inputsLayouts; - ov::Layout getInputLayout(const ov::Output& input); - std::function, const ov::AnyMap&)> lastCallback; -}; diff --git a/src/cpp/models/src/image_model.cpp b/src/cpp/models/src/image_model.cpp index f87d8029..9ec3fd42 100644 --- a/src/cpp/models/src/image_model.cpp +++ b/src/cpp/models/src/image_model.cpp @@ -15,17 +15,137 @@ #include #include +#include "adapters/openvino_adapter.h" #include "models/input_data.h" #include "models/internal_model_data.h" #include "models/results.h" +#include "utils/common.hpp" + +namespace { +class TmpCallbackSetter { +public: + ImageModel* model; + std::function, const ov::AnyMap&)> last_callback; + TmpCallbackSetter(ImageModel* model_, + std::function, const ov::AnyMap&)> tmp_callback, + std::function, const ov::AnyMap&)> last_callback_) + : model(model_), + last_callback(last_callback_) { + model->setCallback(tmp_callback); + } + ~TmpCallbackSetter() { + if (last_callback) { + model->setCallback(last_callback); + } else { + model->setCallback([](std::unique_ptr, const ov::AnyMap&) {}); + } + } +}; +} // namespace ImageModel::ImageModel(const std::string& modelFile, const std::string& resize_type, bool useAutoResize, const std::string& layout) - : ModelBase(modelFile, layout), - useAutoResize(useAutoResize), - resizeMode(selectResizeMode(resize_type)) {} + : useAutoResize(useAutoResize), + resizeMode(selectResizeMode(resize_type)), + modelFile(modelFile), + inputsLayouts(parseLayoutString(layout)) { + auto core = ov::Core(); + model = core.read_model(modelFile); +} + + +void ImageModel::load(ov::Core& core, const std::string& device, size_t num_infer_requests) { + if (!inferenceAdapter) { + inferenceAdapter = std::make_shared(); + } + + // Update model_info erased by pre/postprocessing + updateModelInfo(); + + inferenceAdapter->loadModel(model, core, device, {}, num_infer_requests); +} + +std::shared_ptr ImageModel::prepare() { + prepareInputsOutputs(model); + logBasicModelInfo(model); + ov::set_batch(model, 1); + + return model; +} + +ov::Layout ImageModel::getInputLayout(const ov::Output& input) { + ov::Layout layout = ov::layout::get_layout(input); + if (layout.empty()) { + if (inputsLayouts.empty()) { + layout = getLayoutFromShape(input.get_partial_shape()); + slog::warn << "Automatically detected layout '" << layout.to_string() << "' for input '" + << input.get_any_name() << "' will be used." << slog::endl; + } else if (inputsLayouts.size() == 1) { + layout = inputsLayouts.begin()->second; + } else { + layout = inputsLayouts[input.get_any_name()]; + } + } + + return layout; +} + +size_t ImageModel::getNumAsyncExecutors() const { + return inferenceAdapter->getNumAsyncExecutors(); +} + +bool ImageModel::isReady() { + return inferenceAdapter->isReady(); +} +void ImageModel::awaitAll() { + inferenceAdapter->awaitAll(); +} +void ImageModel::awaitAny() { + inferenceAdapter->awaitAny(); +} + +void ImageModel::setCallback( + std::function, const ov::AnyMap& callback_args)> callback) { + lastCallback = callback; + inferenceAdapter->setCallback([this, callback](ov::InferRequest request, CallbackData args) { + InferenceResult result; + + InferenceOutput output; + for (const auto& item : this->getInferenceAdapter()->getOutputNames()) { + output.emplace(item, request.get_tensor(item)); + } + + result.outputsData = output; + auto model_data_iter = args->find("internalModelData"); + if (model_data_iter != args->end()) { + result.internalModelData = std::move(model_data_iter->second.as>()); + } + auto retVal = this->postprocess(result); + *retVal = static_cast(result); + callback(std::move(retVal), args ? *args : ov::AnyMap()); + }); +} + +std::shared_ptr ImageModel::getModel() { + if (!model) { + throw std::runtime_error(std::string("ov::Model is not accessible for the current model adapter: ") + + typeid(inferenceAdapter).name()); + } + + updateModelInfo(); + return model; +} + +std::shared_ptr ImageModel::getInferenceAdapter() { + if (!inferenceAdapter) { + throw std::runtime_error(std::string("Model wasn't loaded")); + } + + return inferenceAdapter; +} + RESIZE_MODE ImageModel::selectResizeMode(const std::string& resize_type) { RESIZE_MODE resize = RESIZE_FILL; @@ -68,36 +188,88 @@ void ImageModel::init_from_config(const ov::AnyMap& top_priority, const ov::AnyM } ImageModel::ImageModel(std::shared_ptr& model, const ov::AnyMap& configuration) - : ModelBase(model, configuration) { + : model(model) { + auto layout_iter = configuration.find("layout"); + std::string layout = ""; + + if (layout_iter != configuration.end()) { + layout = layout_iter->second.as(); + } else { + if (model->has_rt_info("model_info", "layout")) { + layout = model->get_rt_info("model_info", "layout"); + } + } + inputsLayouts = parseLayoutString(layout); init_from_config(configuration, model->has_rt_info("model_info") ? model->get_rt_info("model_info") : ov::AnyMap{}); } ImageModel::ImageModel(std::shared_ptr& adapter, const ov::AnyMap& configuration) - : ModelBase(adapter, configuration) { + : inferenceAdapter(adapter) { + const ov::AnyMap& adapter_configuration = adapter->getModelConfig(); + + std::string layout = ""; + layout = get_from_any_maps("layout", configuration, adapter_configuration, layout); + inputsLayouts = parseLayoutString(layout); + + inputNames = adapter->getInputNames(); + outputNames = adapter->getOutputNames(); + init_from_config(configuration, adapter->getModelConfig()); } std::unique_ptr ImageModel::inferImage(const ImageInputData& inputData) { - return ModelBase::infer(static_cast(inputData)); - ; + InferenceInput inputs; + InferenceResult result; + auto internalModelData = this->preprocess(inputData, inputs); + + result.outputsData = inferenceAdapter->infer(inputs); + result.internalModelData = std::move(internalModelData); + + auto retVal = this->postprocess(result); + *retVal = static_cast(result); + return retVal; } std::vector> ImageModel::inferBatchImage(const std::vector& inputImgs) { - std::vector> inputData; + std::vector> inputData; inputData.reserve(inputImgs.size()); for (const auto& img : inputImgs) { - inputData.push_back(static_cast(img)); + inputData.push_back(img); + } + auto results = std::vector>(inputData.size()); + auto setter = TmpCallbackSetter( + this, + [&](std::unique_ptr result, const ov::AnyMap& callback_args) { + size_t id = callback_args.find("id")->second.as(); + results[id] = std::move(result); + }, + lastCallback); + size_t req_id = 0; + for (const auto& data : inputData) { + inferAsync(data, {{"id", req_id++}}); } - return ModelBase::inferBatch(inputData); + awaitAll(); + return results; } void ImageModel::inferAsync(const ImageInputData& inputData, const ov::AnyMap& callback_args) { - ModelBase::inferAsync(static_cast(inputData), callback_args); + InferenceInput inputs; + auto internalModelData = this->preprocess(inputData, inputs); + auto callback_args_ptr = std::make_shared(callback_args); + (*callback_args_ptr)["internalModelData"] = std::move(internalModelData); + inferenceAdapter->inferAsync(inputs, callback_args_ptr); } void ImageModel::updateModelInfo() { - ModelBase::updateModelInfo(); + if (!model) { + throw std::runtime_error("The ov::Model object is not accessible"); + } + + if (!inputsLayouts.empty()) { + auto layouts = formatLayouts(inputsLayouts); + model->set_rt_info(layouts, "model_info", "layout"); + } model->set_rt_info(useAutoResize, "model_info", "auto_resize"); model->set_rt_info(formatResizeMode(resizeMode), "model_info", "resize_type"); diff --git a/src/cpp/models/src/model_base.cpp b/src/cpp/models/src/model_base.cpp deleted file mode 100644 index 3e2f817f..00000000 --- a/src/cpp/models/src/model_base.cpp +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Copyright (C) 2020-2024 Intel Corporation - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "models/model_base.h" - -#include -#include - -#include -#include -#include -#include -#include - -#include "models/input_data.h" -#include "utils/args_helper.hpp" - -namespace { -class TmpCallbackSetter { -public: - ModelBase* model; - std::function, const ov::AnyMap&)> last_callback; - TmpCallbackSetter(ModelBase* model_, - std::function, const ov::AnyMap&)> tmp_callback, - std::function, const ov::AnyMap&)> last_callback_) - : model(model_), - last_callback(last_callback_) { - model->setCallback(tmp_callback); - } - ~TmpCallbackSetter() { - if (last_callback) { - model->setCallback(last_callback); - } else { - model->setCallback([](std::unique_ptr, const ov::AnyMap&) {}); - } - } -}; -} // namespace - -ModelBase::ModelBase(const std::string& modelFile, const std::string& layout) - : modelFile(modelFile), - inputsLayouts(parseLayoutString(layout)) { - auto core = ov::Core(); - model = core.read_model(modelFile); -} - -ModelBase::ModelBase(std::shared_ptr& adapter, const ov::AnyMap& configuration) - : inferenceAdapter(adapter) { - const ov::AnyMap& adapter_configuration = adapter->getModelConfig(); - - std::string layout = ""; - layout = get_from_any_maps("layout", configuration, adapter_configuration, layout); - inputsLayouts = parseLayoutString(layout); - - inputNames = adapter->getInputNames(); - outputNames = adapter->getOutputNames(); -} - -ModelBase::ModelBase(std::shared_ptr& model, const ov::AnyMap& configuration) : model(model) { - auto layout_iter = configuration.find("layout"); - std::string layout = ""; - - if (layout_iter != configuration.end()) { - layout = layout_iter->second.as(); - } else { - if (model->has_rt_info("model_info", "layout")) { - layout = model->get_rt_info("model_info", "layout"); - } - } - inputsLayouts = parseLayoutString(layout); -} - -void ModelBase::updateModelInfo() { - if (!model) { - throw std::runtime_error("The ov::Model object is not accessible"); - } - - if (!inputsLayouts.empty()) { - auto layouts = formatLayouts(inputsLayouts); - model->set_rt_info(layouts, "model_info", "layout"); - } -} - -void ModelBase::load(ov::Core& core, const std::string& device, size_t num_infer_requests) { - if (!inferenceAdapter) { - inferenceAdapter = std::make_shared(); - } - - // Update model_info erased by pre/postprocessing - updateModelInfo(); - - inferenceAdapter->loadModel(model, core, device, {}, num_infer_requests); -} - -std::shared_ptr ModelBase::prepare() { - prepareInputsOutputs(model); - logBasicModelInfo(model); - ov::set_batch(model, 1); - - return model; -} - -ov::Layout ModelBase::getInputLayout(const ov::Output& input) { - ov::Layout layout = ov::layout::get_layout(input); - if (layout.empty()) { - if (inputsLayouts.empty()) { - layout = getLayoutFromShape(input.get_partial_shape()); - slog::warn << "Automatically detected layout '" << layout.to_string() << "' for input '" - << input.get_any_name() << "' will be used." << slog::endl; - } else if (inputsLayouts.size() == 1) { - layout = inputsLayouts.begin()->second; - } else { - layout = inputsLayouts[input.get_any_name()]; - } - } - - return layout; -} - -std::unique_ptr ModelBase::infer(const InputData& inputData) { - InferenceInput inputs; - InferenceResult result; - auto internalModelData = this->preprocess(inputData, inputs); - - result.outputsData = inferenceAdapter->infer(inputs); - result.internalModelData = std::move(internalModelData); - - auto retVal = this->postprocess(result); - *retVal = static_cast(result); - return retVal; -} - -std::vector> ModelBase::inferBatch( - const std::vector>& inputData) { - auto results = std::vector>(inputData.size()); - auto setter = TmpCallbackSetter( - this, - [&](std::unique_ptr result, const ov::AnyMap& callback_args) { - size_t id = callback_args.find("id")->second.as(); - results[id] = std::move(result); - }, - lastCallback); - size_t req_id = 0; - for (const auto& data : inputData) { - inferAsync(data, {{"id", req_id++}}); - } - awaitAll(); - return results; -} - -std::vector> ModelBase::inferBatch(const std::vector& inputData) { - std::vector> inputRefData; - inputRefData.reserve(inputData.size()); - for (const auto& item : inputData) { - inputRefData.push_back(item); - } - return inferBatch(inputRefData); -} - -void ModelBase::inferAsync(const InputData& inputData, const ov::AnyMap& callback_args) { - InferenceInput inputs; - auto internalModelData = this->preprocess(inputData, inputs); - auto callback_args_ptr = std::make_shared(callback_args); - (*callback_args_ptr)["internalModelData"] = std::move(internalModelData); - inferenceAdapter->inferAsync(inputs, callback_args_ptr); -} - -bool ModelBase::isReady() { - return inferenceAdapter->isReady(); -} -void ModelBase::awaitAll() { - inferenceAdapter->awaitAll(); -} -void ModelBase::awaitAny() { - inferenceAdapter->awaitAny(); -} -void ModelBase::setCallback( - std::function, const ov::AnyMap& callback_args)> callback) { - lastCallback = callback; - inferenceAdapter->setCallback([this, callback](ov::InferRequest request, CallbackData args) { - InferenceResult result; - - InferenceOutput output; - for (const auto& item : this->getInferenceAdapter()->getOutputNames()) { - output.emplace(item, request.get_tensor(item)); - } - - result.outputsData = output; - auto model_data_iter = args->find("internalModelData"); - if (model_data_iter != args->end()) { - result.internalModelData = std::move(model_data_iter->second.as>()); - } - auto retVal = this->postprocess(result); - *retVal = static_cast(result); - callback(std::move(retVal), args ? *args : ov::AnyMap()); - }); -} - -size_t ModelBase::getNumAsyncExecutors() const { - return inferenceAdapter->getNumAsyncExecutors(); -} - -std::shared_ptr ModelBase::getModel() { - if (!model) { - throw std::runtime_error(std::string("ov::Model is not accessible for the current model adapter: ") + - typeid(inferenceAdapter).name()); - } - - updateModelInfo(); - return model; -} - -std::shared_ptr ModelBase::getInferenceAdapter() { - if (!inferenceAdapter) { - throw std::runtime_error(std::string("Model wasn't loaded")); - } - - return inferenceAdapter; -} diff --git a/src/cpp/tilers/src/instance_segmentation.cpp b/src/cpp/tilers/src/instance_segmentation.cpp index 7632f229..f53d6c3c 100644 --- a/src/cpp/tilers/src/instance_segmentation.cpp +++ b/src/cpp/tilers/src/instance_segmentation.cpp @@ -18,10 +18,10 @@ namespace { class MaskRCNNModelParamsSetter { public: - std::shared_ptr model; + std::shared_ptr model; bool state; MaskRCNNModel* model_ptr; - MaskRCNNModelParamsSetter(std::shared_ptr model_) : model(model_) { + MaskRCNNModelParamsSetter(std::shared_ptr model_) : model(model_) { model_ptr = static_cast(model.get()); state = model_ptr->postprocess_semantic_masks; model_ptr->postprocess_semantic_masks = false; diff --git a/src/cpp/tilers/src/tiler_base.cpp b/src/cpp/tilers/src/tiler_base.cpp index ecdd26eb..0473d705 100644 --- a/src/cpp/tilers/src/tiler_base.cpp +++ b/src/cpp/tilers/src/tiler_base.cpp @@ -75,7 +75,7 @@ std::unique_ptr TilerBase::predict_sync(const cv::Mat& image, const for (const auto& coord : tile_coords) { auto tile_img = crop_tile(image, coord); - auto tile_prediction = model->infer(ImageInputData(tile_img.clone())); + auto tile_prediction = model->inferImage(ImageInputData(tile_img.clone())); auto tile_result = postprocess_tile(std::move(tile_prediction), coord); tile_results.push_back(std::move(tile_result)); } From 7bc1bbedc8e6d3ace4b6ef12f427188078828444 Mon Sep 17 00:00:00 2001 From: "Hecker, Ronald" Date: Fri, 4 Apr 2025 13:40:18 +0200 Subject: [PATCH 2/3] Fix clang linter --- src/cpp/models/include/models/image_model.h | 2 +- src/cpp/models/src/image_model.cpp | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/cpp/models/include/models/image_model.h b/src/cpp/models/include/models/image_model.h index 74b0087a..acec8e26 100644 --- a/src/cpp/models/include/models/image_model.h +++ b/src/cpp/models/include/models/image_model.h @@ -12,9 +12,9 @@ #include "adapters/inference_adapter.h" #include "models/input_data.h" #include "models/results.h" +#include "utils/args_helper.hpp" #include "utils/image_utils.h" #include "utils/ocv_common.hpp" -#include "utils/args_helper.hpp" namespace ov { class InferRequest; diff --git a/src/cpp/models/src/image_model.cpp b/src/cpp/models/src/image_model.cpp index 9ec3fd42..e5a9651b 100644 --- a/src/cpp/models/src/image_model.cpp +++ b/src/cpp/models/src/image_model.cpp @@ -55,7 +55,6 @@ ImageModel::ImageModel(const std::string& modelFile, model = core.read_model(modelFile); } - void ImageModel::load(ov::Core& core, const std::string& device, size_t num_infer_requests) { if (!inferenceAdapter) { inferenceAdapter = std::make_shared(); @@ -146,7 +145,6 @@ std::shared_ptr ImageModel::getInferenceAdapter() { return inferenceAdapter; } - RESIZE_MODE ImageModel::selectResizeMode(const std::string& resize_type) { RESIZE_MODE resize = RESIZE_FILL; if ("crop" == resize_type) { @@ -187,8 +185,7 @@ void ImageModel::init_from_config(const ov::AnyMap& top_priority, const ov::AnyM mean_values = get_from_any_maps("mean_values", top_priority, mid_priority, mean_values); } -ImageModel::ImageModel(std::shared_ptr& model, const ov::AnyMap& configuration) - : model(model) { +ImageModel::ImageModel(std::shared_ptr& model, const ov::AnyMap& configuration) : model(model) { auto layout_iter = configuration.find("layout"); std::string layout = ""; From ccbfcbbd60d3fcca1b83117fd20ff61a3d8c0b45 Mon Sep 17 00:00:00 2001 From: "Hecker, Ronald" Date: Mon, 7 Apr 2025 10:23:03 +0200 Subject: [PATCH 3/3] Rename ImageModel to BaseModel as opposed to ModelBase as it was. So first it was ImageModel: ModelBase. Then ModelBase merged into ImageModel now rename ImageModel to BaseModel --- src/cpp/models/include/models/anomaly_model.h | 4 +- .../models/{image_model.h => base_model.h} | 14 ++-- .../include/models/classification_model.h | 4 +- .../models/include/models/detection_model.h | 4 +- .../include/models/instance_segmentation.h | 4 +- .../include/models/keypoint_detection.h | 4 +- .../include/models/segmentation_model.h | 4 +- src/cpp/models/src/anomaly_model.cpp | 14 ++-- .../src/{image_model.cpp => base_model.cpp} | 76 +++++++++---------- src/cpp/models/src/classification_model.cpp | 12 +-- src/cpp/models/src/detection_model.cpp | 12 +-- src/cpp/models/src/detection_model_ext.cpp | 2 +- src/cpp/models/src/detection_model_ssd.cpp | 2 +- src/cpp/models/src/detection_model_yolo.cpp | 2 +- .../src/detection_model_yolov3_onnx.cpp | 2 +- src/cpp/models/src/instance_segmentation.cpp | 12 +-- src/cpp/models/src/keypoint_detection.cpp | 12 +-- src/cpp/models/src/segmentation_model.cpp | 12 +-- src/cpp/py_bindings/py_base.cpp | 8 +- ...assificaiton.cpp => py_classification.cpp} | 2 +- src/cpp/tilers/include/tilers/detection.h | 2 +- .../include/tilers/instance_segmentation.h | 2 +- .../include/tilers/semantic_segmentation.h | 2 +- src/cpp/tilers/include/tilers/tiler_base.h | 6 +- src/cpp/tilers/src/detection.cpp | 2 +- src/cpp/tilers/src/instance_segmentation.cpp | 6 +- src/cpp/tilers/src/semantic_segmentation.cpp | 2 +- src/cpp/tilers/src/tiler_base.cpp | 6 +- 28 files changed, 115 insertions(+), 119 deletions(-) rename src/cpp/models/include/models/{image_model.h => base_model.h} (91%) rename src/cpp/models/src/{image_model.cpp => base_model.cpp} (81%) rename src/cpp/py_bindings/{py_classificaiton.cpp => py_classification.cpp} (97%) diff --git a/src/cpp/models/include/models/anomaly_model.h b/src/cpp/models/include/models/anomaly_model.h index 9efa0ede..1cc5be22 100644 --- a/src/cpp/models/include/models/anomaly_model.h +++ b/src/cpp/models/include/models/anomaly_model.h @@ -4,7 +4,7 @@ */ #pragma once -#include "models/image_model.h" +#include "models/base_model.h" namespace ov { class Model; @@ -12,7 +12,7 @@ class Model; struct AnomalyResult; struct ImageInputData; -class AnomalyModel : public ImageModel { +class AnomalyModel : public BaseModel { public: AnomalyModel(std::shared_ptr& model, const ov::AnyMap& configuration); AnomalyModel(std::shared_ptr& adapter, const ov::AnyMap& configuration = {}); diff --git a/src/cpp/models/include/models/image_model.h b/src/cpp/models/include/models/base_model.h similarity index 91% rename from src/cpp/models/include/models/image_model.h rename to src/cpp/models/include/models/base_model.h index acec8e26..85131805 100644 --- a/src/cpp/models/include/models/image_model.h +++ b/src/cpp/models/include/models/base_model.h @@ -23,19 +23,19 @@ struct InputData; struct InternalModelData; // ImageModel implements preprocess(), ImageModel's direct or indirect children are expected to implement prostprocess() -class ImageModel { +class BaseModel { public: /// Constructor /// @param modelFile name of model to load /// @param useAutoResize - if true, image is resized by openvino /// @param layout - model input layout - ImageModel(const std::string& modelFile, - const std::string& resize_type, - bool useAutoResize, - const std::string& layout = ""); + BaseModel(const std::string& modelFile, + const std::string& resize_type, + bool useAutoResize, + const std::string& layout = ""); - ImageModel(std::shared_ptr& model, const ov::AnyMap& configuration); - ImageModel(std::shared_ptr& adapter, const ov::AnyMap& configuration = {}); + BaseModel(std::shared_ptr& model, const ov::AnyMap& configuration); + BaseModel(std::shared_ptr& adapter, const ov::AnyMap& configuration = {}); virtual std::shared_ptr preprocess(const InputData& inputData, InferenceInput& input); virtual std::unique_ptr postprocess(InferenceResult& infResult) = 0; diff --git a/src/cpp/models/include/models/classification_model.h b/src/cpp/models/include/models/classification_model.h index 5b1a1be2..88ac03bc 100644 --- a/src/cpp/models/include/models/classification_model.h +++ b/src/cpp/models/include/models/classification_model.h @@ -12,7 +12,7 @@ #include #include -#include "models/image_model.h" +#include "models/base_model.h" namespace ov { class Model; @@ -88,7 +88,7 @@ class ProbabilisticLabelsResolver : public GreedyLabelsResolver { SimpleLabelsGraph label_tree; }; -class ClassificationModel : public ImageModel { +class ClassificationModel : public BaseModel { public: ClassificationModel(std::shared_ptr& model, const ov::AnyMap& configuration); ClassificationModel(std::shared_ptr& adapter, const ov::AnyMap& configuration = {}); diff --git a/src/cpp/models/include/models/detection_model.h b/src/cpp/models/include/models/detection_model.h index 36bd1c8f..16ba8cf8 100644 --- a/src/cpp/models/include/models/detection_model.h +++ b/src/cpp/models/include/models/detection_model.h @@ -7,13 +7,13 @@ #include -#include "models/image_model.h" +#include "models/base_model.h" struct DetectionResult; struct ImageInputData; struct InferenceAdatper; -class DetectionModel : public ImageModel { +class DetectionModel : public BaseModel { public: DetectionModel(std::shared_ptr& model, const ov::AnyMap& configuration); DetectionModel(std::shared_ptr& adapter, const ov::AnyMap& configuration = {}); diff --git a/src/cpp/models/include/models/instance_segmentation.h b/src/cpp/models/include/models/instance_segmentation.h index f88b9f53..c6cadce7 100644 --- a/src/cpp/models/include/models/instance_segmentation.h +++ b/src/cpp/models/include/models/instance_segmentation.h @@ -8,7 +8,7 @@ #include #include -#include "models/image_model.h" +#include "models/base_model.h" namespace ov { class Model; @@ -19,7 +19,7 @@ struct InstanceSegmentationResult; struct ImageInputData; struct SegmentedObject; -class MaskRCNNModel : public ImageModel { +class MaskRCNNModel : public BaseModel { public: MaskRCNNModel(std::shared_ptr& model, const ov::AnyMap& configuration); MaskRCNNModel(std::shared_ptr& adapter, const ov::AnyMap& configuration = {}); diff --git a/src/cpp/models/include/models/keypoint_detection.h b/src/cpp/models/include/models/keypoint_detection.h index 63e4e50f..15d21cba 100644 --- a/src/cpp/models/include/models/keypoint_detection.h +++ b/src/cpp/models/include/models/keypoint_detection.h @@ -8,7 +8,7 @@ #include #include -#include "models/image_model.h" +#include "models/base_model.h" namespace ov { class Model; @@ -18,7 +18,7 @@ struct ResultBase; struct KeypointDetectionResult; struct ImageInputData; -class KeypointDetectionModel : public ImageModel { +class KeypointDetectionModel : public BaseModel { public: KeypointDetectionModel(std::shared_ptr& model, const ov::AnyMap& configuration); KeypointDetectionModel(std::shared_ptr& adapter, const ov::AnyMap& configuration = {}); diff --git a/src/cpp/models/include/models/segmentation_model.h b/src/cpp/models/include/models/segmentation_model.h index ed296e92..922828f7 100644 --- a/src/cpp/models/include/models/segmentation_model.h +++ b/src/cpp/models/include/models/segmentation_model.h @@ -8,7 +8,7 @@ #include #include -#include "models/image_model.h" +#include "models/base_model.h" namespace ov { class Model; @@ -20,7 +20,7 @@ struct ImageResultWithSoftPrediction; struct ImageInputData; struct Contour; -class SegmentationModel : public ImageModel { +class SegmentationModel : public BaseModel { public: SegmentationModel(std::shared_ptr& model, const ov::AnyMap& configuration); SegmentationModel(std::shared_ptr& adapter, const ov::AnyMap& configuration = {}); diff --git a/src/cpp/models/src/anomaly_model.cpp b/src/cpp/models/src/anomaly_model.cpp index 4feadb9f..eeccf08a 100644 --- a/src/cpp/models/src/anomaly_model.cpp +++ b/src/cpp/models/src/anomaly_model.cpp @@ -10,7 +10,7 @@ #include #include -#include "models/image_model.h" +#include "models/base_model.h" #include "models/input_data.h" #include "models/internal_model_data.h" #include "models/results.h" @@ -29,23 +29,23 @@ void AnomalyModel::init_from_config(const ov::AnyMap& top_priority, const ov::An } AnomalyModel::AnomalyModel(std::shared_ptr& model, const ov::AnyMap& configuration) - : ImageModel(model, configuration) { + : BaseModel(model, configuration) { init_from_config(configuration, model->get_rt_info("model_info")); } AnomalyModel::AnomalyModel(std::shared_ptr& adapter, const ov::AnyMap& configuration) - : ImageModel(adapter, configuration) { + : BaseModel(adapter, configuration) { init_from_config(configuration, adapter->getModelConfig()); } std::unique_ptr AnomalyModel::infer(const ImageInputData& inputData) { - auto result = ImageModel::inferImage(inputData); + auto result = BaseModel::inferImage(inputData); return std::unique_ptr(static_cast(result.release())); } std::vector> AnomalyModel::inferBatch(const std::vector& inputImgs) { - auto results = ImageModel::inferBatchImage(inputImgs); + auto results = BaseModel::inferBatchImage(inputImgs); std::vector> anoResults; anoResults.reserve(results.size()); for (auto& result : results) { @@ -177,7 +177,7 @@ void AnomalyModel::prepareInputsOutputs(std::shared_ptr& model) { const ov::Layout& inputLayout = getInputLayout(input); if (!embedded_processing) { - model = ImageModel::embedProcessing( + model = BaseModel::embedProcessing( model, inputNames[0], inputLayout, @@ -194,7 +194,7 @@ void AnomalyModel::prepareInputsOutputs(std::shared_ptr& model) { } void AnomalyModel::updateModelInfo() { - ImageModel::updateModelInfo(); + BaseModel::updateModelInfo(); model->set_rt_info(AnomalyModel::ModelType, "model_info", "model_type"); model->set_rt_info(task, "model_info", "task"); diff --git a/src/cpp/models/src/image_model.cpp b/src/cpp/models/src/base_model.cpp similarity index 81% rename from src/cpp/models/src/image_model.cpp rename to src/cpp/models/src/base_model.cpp index e5a9651b..6fd83d73 100644 --- a/src/cpp/models/src/image_model.cpp +++ b/src/cpp/models/src/base_model.cpp @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "models/image_model.h" +#include "models/base_model.h" #include #include @@ -24,9 +24,9 @@ namespace { class TmpCallbackSetter { public: - ImageModel* model; + BaseModel* model; std::function, const ov::AnyMap&)> last_callback; - TmpCallbackSetter(ImageModel* model_, + TmpCallbackSetter(BaseModel* model_, std::function, const ov::AnyMap&)> tmp_callback, std::function, const ov::AnyMap&)> last_callback_) : model(model_), @@ -43,10 +43,10 @@ class TmpCallbackSetter { }; } // namespace -ImageModel::ImageModel(const std::string& modelFile, - const std::string& resize_type, - bool useAutoResize, - const std::string& layout) +BaseModel::BaseModel(const std::string& modelFile, + const std::string& resize_type, + bool useAutoResize, + const std::string& layout) : useAutoResize(useAutoResize), resizeMode(selectResizeMode(resize_type)), modelFile(modelFile), @@ -55,7 +55,7 @@ ImageModel::ImageModel(const std::string& modelFile, model = core.read_model(modelFile); } -void ImageModel::load(ov::Core& core, const std::string& device, size_t num_infer_requests) { +void BaseModel::load(ov::Core& core, const std::string& device, size_t num_infer_requests) { if (!inferenceAdapter) { inferenceAdapter = std::make_shared(); } @@ -66,7 +66,7 @@ void ImageModel::load(ov::Core& core, const std::string& device, size_t num_infe inferenceAdapter->loadModel(model, core, device, {}, num_infer_requests); } -std::shared_ptr ImageModel::prepare() { +std::shared_ptr BaseModel::prepare() { prepareInputsOutputs(model); logBasicModelInfo(model); ov::set_batch(model, 1); @@ -74,7 +74,7 @@ std::shared_ptr ImageModel::prepare() { return model; } -ov::Layout ImageModel::getInputLayout(const ov::Output& input) { +ov::Layout BaseModel::getInputLayout(const ov::Output& input) { ov::Layout layout = ov::layout::get_layout(input); if (layout.empty()) { if (inputsLayouts.empty()) { @@ -91,21 +91,21 @@ ov::Layout ImageModel::getInputLayout(const ov::Output& input) { return layout; } -size_t ImageModel::getNumAsyncExecutors() const { +size_t BaseModel::getNumAsyncExecutors() const { return inferenceAdapter->getNumAsyncExecutors(); } -bool ImageModel::isReady() { +bool BaseModel::isReady() { return inferenceAdapter->isReady(); } -void ImageModel::awaitAll() { +void BaseModel::awaitAll() { inferenceAdapter->awaitAll(); } -void ImageModel::awaitAny() { +void BaseModel::awaitAny() { inferenceAdapter->awaitAny(); } -void ImageModel::setCallback( +void BaseModel::setCallback( std::function, const ov::AnyMap& callback_args)> callback) { lastCallback = callback; inferenceAdapter->setCallback([this, callback](ov::InferRequest request, CallbackData args) { @@ -127,7 +127,7 @@ void ImageModel::setCallback( }); } -std::shared_ptr ImageModel::getModel() { +std::shared_ptr BaseModel::getModel() { if (!model) { throw std::runtime_error(std::string("ov::Model is not accessible for the current model adapter: ") + typeid(inferenceAdapter).name()); @@ -137,7 +137,7 @@ std::shared_ptr ImageModel::getModel() { return model; } -std::shared_ptr ImageModel::getInferenceAdapter() { +std::shared_ptr BaseModel::getInferenceAdapter() { if (!inferenceAdapter) { throw std::runtime_error(std::string("Model wasn't loaded")); } @@ -145,7 +145,7 @@ std::shared_ptr ImageModel::getInferenceAdapter() { return inferenceAdapter; } -RESIZE_MODE ImageModel::selectResizeMode(const std::string& resize_type) { +RESIZE_MODE BaseModel::selectResizeMode(const std::string& resize_type) { RESIZE_MODE resize = RESIZE_FILL; if ("crop" == resize_type) { resize = RESIZE_CROP; @@ -162,7 +162,7 @@ RESIZE_MODE ImageModel::selectResizeMode(const std::string& resize_type) { return resize; } -void ImageModel::init_from_config(const ov::AnyMap& top_priority, const ov::AnyMap& mid_priority) { +void BaseModel::init_from_config(const ov::AnyMap& top_priority, const ov::AnyMap& mid_priority) { useAutoResize = get_from_any_maps("auto_resize", top_priority, mid_priority, useAutoResize); std::string resize_type = "standard"; @@ -185,7 +185,7 @@ void ImageModel::init_from_config(const ov::AnyMap& top_priority, const ov::AnyM mean_values = get_from_any_maps("mean_values", top_priority, mid_priority, mean_values); } -ImageModel::ImageModel(std::shared_ptr& model, const ov::AnyMap& configuration) : model(model) { +BaseModel::BaseModel(std::shared_ptr& model, const ov::AnyMap& configuration) : model(model) { auto layout_iter = configuration.find("layout"); std::string layout = ""; @@ -201,7 +201,7 @@ ImageModel::ImageModel(std::shared_ptr& model, const ov::AnyMap& conf model->has_rt_info("model_info") ? model->get_rt_info("model_info") : ov::AnyMap{}); } -ImageModel::ImageModel(std::shared_ptr& adapter, const ov::AnyMap& configuration) +BaseModel::BaseModel(std::shared_ptr& adapter, const ov::AnyMap& configuration) : inferenceAdapter(adapter) { const ov::AnyMap& adapter_configuration = adapter->getModelConfig(); @@ -215,7 +215,7 @@ ImageModel::ImageModel(std::shared_ptr& adapter, const ov::Any init_from_config(configuration, adapter->getModelConfig()); } -std::unique_ptr ImageModel::inferImage(const ImageInputData& inputData) { +std::unique_ptr BaseModel::inferImage(const ImageInputData& inputData) { InferenceInput inputs; InferenceResult result; auto internalModelData = this->preprocess(inputData, inputs); @@ -228,7 +228,7 @@ std::unique_ptr ImageModel::inferImage(const ImageInputData& inputDa return retVal; } -std::vector> ImageModel::inferBatchImage(const std::vector& inputImgs) { +std::vector> BaseModel::inferBatchImage(const std::vector& inputImgs) { std::vector> inputData; inputData.reserve(inputImgs.size()); for (const auto& img : inputImgs) { @@ -250,7 +250,7 @@ std::vector> ImageModel::inferBatchImage(const std:: return results; } -void ImageModel::inferAsync(const ImageInputData& inputData, const ov::AnyMap& callback_args) { +void BaseModel::inferAsync(const ImageInputData& inputData, const ov::AnyMap& callback_args) { InferenceInput inputs; auto internalModelData = this->preprocess(inputData, inputs); auto callback_args_ptr = std::make_shared(callback_args); @@ -258,7 +258,7 @@ void ImageModel::inferAsync(const ImageInputData& inputData, const ov::AnyMap& c inferenceAdapter->inferAsync(inputs, callback_args_ptr); } -void ImageModel::updateModelInfo() { +void BaseModel::updateModelInfo() { if (!model) { throw std::runtime_error("The ov::Model object is not accessible"); } @@ -280,17 +280,17 @@ void ImageModel::updateModelInfo() { model->set_rt_info(netInputHeight, "model_info", "orig_height"); } -std::shared_ptr ImageModel::embedProcessing(std::shared_ptr& model, - const std::string& inputName, - const ov::Layout& layout, - const RESIZE_MODE resize_mode, - const cv::InterpolationFlags interpolationMode, - const ov::Shape& targetShape, - uint8_t pad_value, - bool brg2rgb, - const std::vector& mean, - const std::vector& scale, - const std::type_info& dtype) { +std::shared_ptr BaseModel::embedProcessing(std::shared_ptr& model, + const std::string& inputName, + const ov::Layout& layout, + const RESIZE_MODE resize_mode, + const cv::InterpolationFlags interpolationMode, + const ov::Shape& targetShape, + uint8_t pad_value, + bool brg2rgb, + const std::vector& mean, + const std::vector& scale, + const std::type_info& dtype) { ov::preprocess::PrePostProcessor ppp(model); // Change the input type to the 8-bit image @@ -326,7 +326,7 @@ std::shared_ptr ImageModel::embedProcessing(std::shared_ptr ImageModel::preprocess(const InputData& inputData, InferenceInput& input) { +std::shared_ptr BaseModel::preprocess(const InputData& inputData, InferenceInput& input) { const auto& origImg = inputData.asRef().inputImage; auto img = inputTransform(origImg); @@ -351,7 +351,7 @@ std::shared_ptr ImageModel::preprocess(const InputData& input return std::make_shared(origImg.cols, origImg.rows); } -std::vector ImageModel::loadLabels(const std::string& labelFilename) { +std::vector BaseModel::loadLabels(const std::string& labelFilename) { std::vector labelsList; /* Read labels (if any) */ diff --git a/src/cpp/models/src/classification_model.cpp b/src/cpp/models/src/classification_model.cpp index 207c7a62..a9d281e1 100644 --- a/src/cpp/models/src/classification_model.cpp +++ b/src/cpp/models/src/classification_model.cpp @@ -221,20 +221,20 @@ void ClassificationModel::init_from_config(const ov::AnyMap& top_priority, const } ClassificationModel::ClassificationModel(std::shared_ptr& model, const ov::AnyMap& configuration) - : ImageModel(model, configuration) { + : BaseModel(model, configuration) { init_from_config(configuration, model->has_rt_info("model_info") ? model->get_rt_info("model_info") : ov::AnyMap{}); } ClassificationModel::ClassificationModel(std::shared_ptr& adapter, const ov::AnyMap& configuration) - : ImageModel(adapter, configuration) { + : BaseModel(adapter, configuration) { outputNames = get_non_xai_names(adapter->getOutputNames()); append_xai_names(adapter->getOutputNames(), outputNames); init_from_config(configuration, adapter->getModelConfig()); } void ClassificationModel::updateModelInfo() { - ImageModel::updateModelInfo(); + BaseModel::updateModelInfo(); model->set_rt_info(ClassificationModel::ModelType, "model_info", "model_type"); model->set_rt_info(topk, "model_info", "topk"); @@ -468,7 +468,7 @@ void ClassificationModel::prepareInputsOutputs(std::shared_ptr& model const ov::Layout& inputLayout = getInputLayout(input); if (!embedded_processing) { - model = ImageModel::embedProcessing( + model = BaseModel::embedProcessing( model, inputNames[0], inputLayout, @@ -536,13 +536,13 @@ void ClassificationModel::prepareInputsOutputs(std::shared_ptr& model } std::unique_ptr ClassificationModel::infer(const ImageInputData& inputData) { - auto result = ImageModel::inferImage(inputData); + auto result = BaseModel::inferImage(inputData); return std::unique_ptr(static_cast(result.release())); } std::vector> ClassificationModel::inferBatch( const std::vector& inputImgs) { - auto results = ImageModel::inferBatchImage(inputImgs); + auto results = BaseModel::inferBatchImage(inputImgs); std::vector> clsResults; clsResults.reserve(results.size()); for (auto& result : results) { diff --git a/src/cpp/models/src/detection_model.cpp b/src/cpp/models/src/detection_model.cpp index 963760d6..6b55eeba 100644 --- a/src/cpp/models/src/detection_model.cpp +++ b/src/cpp/models/src/detection_model.cpp @@ -10,17 +10,17 @@ #include #include +#include "models/base_model.h" #include "models/detection_model_ssd.h" #include "models/detection_model_yolo.h" #include "models/detection_model_yolov3_onnx.h" #include "models/detection_model_yolox.h" -#include "models/image_model.h" #include "models/input_data.h" #include "models/results.h" #include "utils/slog.hpp" DetectionModel::DetectionModel(std::shared_ptr& model, const ov::AnyMap& configuration) - : ImageModel(model, configuration) { + : BaseModel(model, configuration) { auto confidence_threshold_iter = configuration.find("confidence_threshold"); if (confidence_threshold_iter == configuration.end()) { if (model->has_rt_info("model_info", "confidence_threshold")) { @@ -32,13 +32,13 @@ DetectionModel::DetectionModel(std::shared_ptr& model, const ov::AnyM } DetectionModel::DetectionModel(std::shared_ptr& adapter, const ov::AnyMap& configuration) - : ImageModel(adapter, configuration) { + : BaseModel(adapter, configuration) { confidence_threshold = get_from_any_maps("confidence_threshold", configuration, adapter->getModelConfig(), confidence_threshold); } void DetectionModel::updateModelInfo() { - ImageModel::updateModelInfo(); + BaseModel::updateModelInfo(); model->set_rt_info(confidence_threshold, "model_info", "confidence_threshold"); } @@ -103,12 +103,12 @@ std::unique_ptr DetectionModel::create_model(std::shared_ptr DetectionModel::infer(const ImageInputData& inputData) { - auto result = ImageModel::inferImage(inputData); + auto result = BaseModel::inferImage(inputData); return std::unique_ptr(static_cast(result.release())); } std::vector> DetectionModel::inferBatch(const std::vector& inputImgs) { - auto results = ImageModel::inferBatchImage(inputImgs); + auto results = BaseModel::inferBatchImage(inputImgs); std::vector> detResults; detResults.reserve(results.size()); for (auto& result : results) { diff --git a/src/cpp/models/src/detection_model_ext.cpp b/src/cpp/models/src/detection_model_ext.cpp index 134d2a9c..d1084820 100644 --- a/src/cpp/models/src/detection_model_ext.cpp +++ b/src/cpp/models/src/detection_model_ext.cpp @@ -9,7 +9,7 @@ #include #include -#include "models/image_model.h" +#include "models/base_model.h" #include "models/input_data.h" #include "models/results.h" diff --git a/src/cpp/models/src/detection_model_ssd.cpp b/src/cpp/models/src/detection_model_ssd.cpp index 20ed396a..f6f7c818 100644 --- a/src/cpp/models/src/detection_model_ssd.cpp +++ b/src/cpp/models/src/detection_model_ssd.cpp @@ -251,7 +251,7 @@ void ModelSSD::prepareInputsOutputs(std::shared_ptr& model) { } if (!embedded_processing) { - model = ImageModel::embedProcessing( + model = BaseModel::embedProcessing( model, inputNames[0], inputLayout, diff --git a/src/cpp/models/src/detection_model_yolo.cpp b/src/cpp/models/src/detection_model_yolo.cpp index d43e903c..1698b8e6 100644 --- a/src/cpp/models/src/detection_model_yolo.cpp +++ b/src/cpp/models/src/detection_model_yolo.cpp @@ -505,7 +505,7 @@ void YOLOv5::prepareInputsOutputs(std::shared_ptr& model) { inputNames.push_back(input.get_any_name()); const ov::Layout& inputLayout = getInputLayout(input); if (!embedded_processing) { - model = ImageModel::embedProcessing( + model = BaseModel::embedProcessing( model, inputNames[0], inputLayout, diff --git a/src/cpp/models/src/detection_model_yolov3_onnx.cpp b/src/cpp/models/src/detection_model_yolov3_onnx.cpp index d7974f41..68830220 100644 --- a/src/cpp/models/src/detection_model_yolov3_onnx.cpp +++ b/src/cpp/models/src/detection_model_yolov3_onnx.cpp @@ -103,7 +103,7 @@ std::shared_ptr ModelYoloV3ONNX::preprocess(const InputData& data[0] = origImg.rows; data[1] = origImg.cols; input.emplace(inputNames[1], std::move(info)); - return ImageModel::preprocess(inputData, input); + return BaseModel::preprocess(inputData, input); } namespace { diff --git a/src/cpp/models/src/instance_segmentation.cpp b/src/cpp/models/src/instance_segmentation.cpp index 2b20e806..384fb057 100644 --- a/src/cpp/models/src/instance_segmentation.cpp +++ b/src/cpp/models/src/instance_segmentation.cpp @@ -143,13 +143,13 @@ void MaskRCNNModel::init_from_config(const ov::AnyMap& top_priority, const ov::A } MaskRCNNModel::MaskRCNNModel(std::shared_ptr& model, const ov::AnyMap& configuration) - : ImageModel(model, configuration) { + : BaseModel(model, configuration) { init_from_config(configuration, model->has_rt_info("model_info") ? model->get_rt_info("model_info") : ov::AnyMap{}); } MaskRCNNModel::MaskRCNNModel(std::shared_ptr& adapter, const ov::AnyMap& configuration) - : ImageModel(adapter, configuration) { + : BaseModel(adapter, configuration) { init_from_config(configuration, adapter->getModelConfig()); } @@ -201,7 +201,7 @@ std::unique_ptr MaskRCNNModel::create_model(std::shared_ptrset_rt_info(MaskRCNNModel::ModelType, "model_info", "model_type"); model->set_rt_info(confidence_threshold, "model_info", "confidence_threshold"); @@ -224,7 +224,7 @@ void MaskRCNNModel::prepareInputsOutputs(std::shared_ptr& model) { } if (!embedded_processing) { - model = ImageModel::embedProcessing( + model = BaseModel::embedProcessing( model, inputNames[0], inputLayout, @@ -359,13 +359,13 @@ std::unique_ptr MaskRCNNModel::postprocess(InferenceResult& infResul } std::unique_ptr MaskRCNNModel::infer(const ImageInputData& inputData) { - auto result = ImageModel::inferImage(inputData); + auto result = BaseModel::inferImage(inputData); return std::unique_ptr(static_cast(result.release())); } std::vector> MaskRCNNModel::inferBatch( const std::vector& inputImgs) { - auto results = ImageModel::inferBatchImage(inputImgs); + auto results = BaseModel::inferBatchImage(inputImgs); std::vector> isegResults; isegResults.reserve(results.size()); for (auto& result : results) { diff --git a/src/cpp/models/src/keypoint_detection.cpp b/src/cpp/models/src/keypoint_detection.cpp index af438bad..4fbe778c 100644 --- a/src/cpp/models/src/keypoint_detection.cpp +++ b/src/cpp/models/src/keypoint_detection.cpp @@ -96,14 +96,14 @@ void KeypointDetectionModel::init_from_config(const ov::AnyMap& top_priority, co } KeypointDetectionModel::KeypointDetectionModel(std::shared_ptr& model, const ov::AnyMap& configuration) - : ImageModel(model, configuration) { + : BaseModel(model, configuration) { init_from_config(configuration, model->has_rt_info("model_info") ? model->get_rt_info("model_info") : ov::AnyMap{}); } KeypointDetectionModel::KeypointDetectionModel(std::shared_ptr& adapter, const ov::AnyMap& configuration) - : ImageModel(adapter, configuration) { + : BaseModel(adapter, configuration) { init_from_config(configuration, adapter->getModelConfig()); } @@ -156,7 +156,7 @@ std::unique_ptr KeypointDetectionModel::create_model( } void KeypointDetectionModel::updateModelInfo() { - ImageModel::updateModelInfo(); + BaseModel::updateModelInfo(); model->set_rt_info(KeypointDetectionModel::ModelType, "model_info", "model_type"); model->set_rt_info(labels, "model_info", "labels"); @@ -182,7 +182,7 @@ void KeypointDetectionModel::prepareInputsOutputs(std::shared_ptr& mo } if (!embedded_processing) { - model = ImageModel::embedProcessing( + model = BaseModel::embedProcessing( model, inputNames[0], inputLayout, @@ -250,13 +250,13 @@ std::unique_ptr KeypointDetectionModel::postprocess(InferenceResult& } std::unique_ptr KeypointDetectionModel::infer(const ImageInputData& inputData) { - auto result = ImageModel::inferImage(inputData); + auto result = BaseModel::inferImage(inputData); return std::unique_ptr(static_cast(result.release())); } std::vector> KeypointDetectionModel::inferBatch( const std::vector& inputImgs) { - auto results = ImageModel::inferBatchImage(inputImgs); + auto results = BaseModel::inferBatchImage(inputImgs); std::vector> kpDetResults; kpDetResults.reserve(results.size()); for (auto& result : results) { diff --git a/src/cpp/models/src/segmentation_model.cpp b/src/cpp/models/src/segmentation_model.cpp index 25fae273..deea1c87 100644 --- a/src/cpp/models/src/segmentation_model.cpp +++ b/src/cpp/models/src/segmentation_model.cpp @@ -81,13 +81,13 @@ void SegmentationModel::init_from_config(const ov::AnyMap& top_priority, const o } SegmentationModel::SegmentationModel(std::shared_ptr& model, const ov::AnyMap& configuration) - : ImageModel(model, configuration) { + : BaseModel(model, configuration) { init_from_config(configuration, model->has_rt_info("model_info") ? model->get_rt_info("model_info") : ov::AnyMap{}); } SegmentationModel::SegmentationModel(std::shared_ptr& adapter, const ov::AnyMap& configuration) - : ImageModel(adapter, configuration) { + : BaseModel(adapter, configuration) { init_from_config(configuration, adapter->getModelConfig()); } @@ -139,7 +139,7 @@ std::unique_ptr SegmentationModel::create_model(std::shared_p } void SegmentationModel::updateModelInfo() { - ImageModel::updateModelInfo(); + BaseModel::updateModelInfo(); model->set_rt_info(SegmentationModel::ModelType, "model_info", "model_type"); model->set_rt_info(blur_strength, "model_info", "blur_strength"); @@ -182,7 +182,7 @@ void SegmentationModel::prepareInputsOutputs(std::shared_ptr& model) } if (!embedded_processing) { - model = ImageModel::embedProcessing( + model = BaseModel::embedProcessing( model, inputNames[0], inputLayout, @@ -316,12 +316,12 @@ std::vector SegmentationModel::getContours(const ImageResultWithSoftPre } std::unique_ptr SegmentationModel::infer(const ImageInputData& inputData) { - auto result = ImageModel::inferImage(inputData); + auto result = BaseModel::inferImage(inputData); return std::unique_ptr(static_cast(result.release())); } std::vector> SegmentationModel::inferBatch(const std::vector& inputImgs) { - auto results = ImageModel::inferBatchImage(inputImgs); + auto results = BaseModel::inferBatchImage(inputImgs); std::vector> segResults; segResults.reserve(results.size()); for (auto& result : results) { diff --git a/src/cpp/py_bindings/py_base.cpp b/src/cpp/py_bindings/py_base.cpp index e257991d..ecc5e4c7 100644 --- a/src/cpp/py_bindings/py_base.cpp +++ b/src/cpp/py_bindings/py_base.cpp @@ -8,7 +8,7 @@ #include -#include "models/image_model.h" +#include "models/base_model.h" #include "models/results.h" namespace nb = nanobind; @@ -16,11 +16,9 @@ namespace nb = nanobind; void init_base_modules(nb::module_& m) { nb::class_(m, "ResultBase").def(nb::init<>()); - nb::class_(m, "ModelBase") - .def("load", [](ModelBase& self, const std::string& device, size_t num_infer_requests) { + nb::class_(m, "BaseModel") + .def("load", [](BaseModel& self, const std::string& device, size_t num_infer_requests) { auto core = ov::Core(); self.load(core, device, num_infer_requests); }); - - nb::class_(m, "ImageModel"); } diff --git a/src/cpp/py_bindings/py_classificaiton.cpp b/src/cpp/py_bindings/py_classification.cpp similarity index 97% rename from src/cpp/py_bindings/py_classificaiton.cpp rename to src/cpp/py_bindings/py_classification.cpp index 19e2b4eb..d01d84f0 100644 --- a/src/cpp/py_bindings/py_classificaiton.cpp +++ b/src/cpp/py_bindings/py_classification.cpp @@ -52,7 +52,7 @@ void init_classification(nb::module_& m) { }, nb::rv_policy::reference_internal); - nb::class_(m, "ClassificationModel") + nb::class_(m, "ClassificationModel") .def_static( "create_model", [](const std::string& model_path, diff --git a/src/cpp/tilers/include/tilers/detection.h b/src/cpp/tilers/include/tilers/detection.h index 28fc14aa..8fde112b 100644 --- a/src/cpp/tilers/include/tilers/detection.h +++ b/src/cpp/tilers/include/tilers/detection.h @@ -10,7 +10,7 @@ struct DetectionResult; class DetectionTiler : public TilerBase { public: - DetectionTiler(const std::shared_ptr& model, + DetectionTiler(const std::shared_ptr& model, const ov::AnyMap& configuration, ExecutionMode exec_mode = ExecutionMode::sync); virtual ~DetectionTiler() = default; diff --git a/src/cpp/tilers/include/tilers/instance_segmentation.h b/src/cpp/tilers/include/tilers/instance_segmentation.h index 75c00f2c..3ca20dcb 100644 --- a/src/cpp/tilers/include/tilers/instance_segmentation.h +++ b/src/cpp/tilers/include/tilers/instance_segmentation.h @@ -11,7 +11,7 @@ struct InstanceSegmentationResult; class InstanceSegmentationTiler : public TilerBase { /*InstanceSegmentationTiler tiler works with MaskRCNNModel model only*/ public: - InstanceSegmentationTiler(std::shared_ptr model, + InstanceSegmentationTiler(std::shared_ptr model, const ov::AnyMap& configuration, ExecutionMode exec_mode = ExecutionMode::sync); virtual std::unique_ptr run(const ImageInputData& inputData); diff --git a/src/cpp/tilers/include/tilers/semantic_segmentation.h b/src/cpp/tilers/include/tilers/semantic_segmentation.h index 5e0966d9..4c9b9d1d 100644 --- a/src/cpp/tilers/include/tilers/semantic_segmentation.h +++ b/src/cpp/tilers/include/tilers/semantic_segmentation.h @@ -11,7 +11,7 @@ struct ImageResultWithSoftPrediction; class SemanticSegmentationTiler : public TilerBase { public: - SemanticSegmentationTiler(std::shared_ptr model, + SemanticSegmentationTiler(std::shared_ptr model, const ov::AnyMap& configuration, ExecutionMode exec_mode = ExecutionMode::sync); virtual std::unique_ptr run(const ImageInputData& inputData); diff --git a/src/cpp/tilers/include/tilers/tiler_base.h b/src/cpp/tilers/include/tilers/tiler_base.h index fcebaf7b..3fb45d1e 100644 --- a/src/cpp/tilers/include/tilers/tiler_base.h +++ b/src/cpp/tilers/include/tilers/tiler_base.h @@ -4,7 +4,7 @@ */ #pragma once -#include +#include #include #include @@ -20,7 +20,7 @@ enum class ExecutionMode { sync, async }; class TilerBase { public: - TilerBase(const std::shared_ptr& model, + TilerBase(const std::shared_ptr& model, const ov::AnyMap& configuration, ExecutionMode exec_mode = ExecutionMode::sync); @@ -38,7 +38,7 @@ class TilerBase { const cv::Size&, const std::vector&) = 0; - std::shared_ptr model; + std::shared_ptr model; size_t tile_size = 400; float tiles_overlap = 0.5f; float iou_threshold = 0.45f; diff --git a/src/cpp/tilers/src/detection.cpp b/src/cpp/tilers/src/detection.cpp index 71f65ab8..ec248664 100644 --- a/src/cpp/tilers/src/detection.cpp +++ b/src/cpp/tilers/src/detection.cpp @@ -27,7 +27,7 @@ cv::Mat non_linear_normalization(cv::Mat& class_map) { } // namespace -DetectionTiler::DetectionTiler(const std::shared_ptr& _model, +DetectionTiler::DetectionTiler(const std::shared_ptr& _model, const ov::AnyMap& configuration, ExecutionMode exec_mode) : TilerBase(_model, configuration, exec_mode) { diff --git a/src/cpp/tilers/src/instance_segmentation.cpp b/src/cpp/tilers/src/instance_segmentation.cpp index f53d6c3c..211a4761 100644 --- a/src/cpp/tilers/src/instance_segmentation.cpp +++ b/src/cpp/tilers/src/instance_segmentation.cpp @@ -18,10 +18,10 @@ namespace { class MaskRCNNModelParamsSetter { public: - std::shared_ptr model; + std::shared_ptr model; bool state; MaskRCNNModel* model_ptr; - MaskRCNNModelParamsSetter(std::shared_ptr model_) : model(model_) { + MaskRCNNModelParamsSetter(std::shared_ptr model_) : model(model_) { model_ptr = static_cast(model.get()); state = model_ptr->postprocess_semantic_masks; model_ptr->postprocess_semantic_masks = false; @@ -32,7 +32,7 @@ class MaskRCNNModelParamsSetter { }; } // namespace -InstanceSegmentationTiler::InstanceSegmentationTiler(std::shared_ptr _model, +InstanceSegmentationTiler::InstanceSegmentationTiler(std::shared_ptr _model, const ov::AnyMap& configuration, ExecutionMode exec_mode) : TilerBase(_model, configuration, exec_mode) { diff --git a/src/cpp/tilers/src/semantic_segmentation.cpp b/src/cpp/tilers/src/semantic_segmentation.cpp index b98a7d6f..6a8efc89 100644 --- a/src/cpp/tilers/src/semantic_segmentation.cpp +++ b/src/cpp/tilers/src/semantic_segmentation.cpp @@ -32,7 +32,7 @@ void normalize_soft_prediction(cv::Mat& soft_prediction, const cv::Mat& normaliz } } // namespace -SemanticSegmentationTiler::SemanticSegmentationTiler(std::shared_ptr _model, +SemanticSegmentationTiler::SemanticSegmentationTiler(std::shared_ptr _model, const ov::AnyMap& configuration, ExecutionMode exec_mode) : TilerBase(_model, configuration, exec_mode) { diff --git a/src/cpp/tilers/src/tiler_base.cpp b/src/cpp/tilers/src/tiler_base.cpp index 0473d705..6d979dea 100644 --- a/src/cpp/tilers/src/tiler_base.cpp +++ b/src/cpp/tilers/src/tiler_base.cpp @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include #include #include #include @@ -11,9 +11,7 @@ #include #include -TilerBase::TilerBase(const std::shared_ptr& _model, - const ov::AnyMap& configuration, - ExecutionMode exec_mode) +TilerBase::TilerBase(const std::shared_ptr& _model, const ov::AnyMap& configuration, ExecutionMode exec_mode) : model(_model), run_mode(exec_mode) { ov::AnyMap extra_config;