|
| 1 | +/* |
| 2 | +// Copyright (C) 2024 Intel Corporation |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | + |
| 18 | +#include <vector> |
| 19 | +#include <opencv2/core.hpp> |
| 20 | + |
| 21 | +#include <tilers/semantic_segmentation.h> |
| 22 | +#include <models/segmentation_model.h> |
| 23 | +#include <models/results.h> |
| 24 | +#include "utils/common.hpp" |
| 25 | + |
| 26 | +namespace { |
| 27 | +void normalize_soft_prediction(cv::Mat& soft_prediction, const cv::Mat& normalize_factor) { |
| 28 | + float* data = soft_prediction.ptr<float>(0); |
| 29 | + const int num_classes = soft_prediction.channels(); |
| 30 | + const size_t step_rows = soft_prediction.step[0] / sizeof(float); |
| 31 | + const size_t step_cols = soft_prediction.step[1] / sizeof(float); |
| 32 | + |
| 33 | + for (int y = 0; y < soft_prediction.rows; ++y) { |
| 34 | + for (int x = 0; x < soft_prediction.cols; ++x) { |
| 35 | + int weight = normalize_factor.at<int>(y, x); |
| 36 | + if (weight > 0) { |
| 37 | + for (int c = 0; c < num_classes; ++c) { |
| 38 | + data[y * step_rows + x * step_cols + c] /= weight; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | +} |
| 45 | + |
| 46 | +SemanticSegmentationTiler::SemanticSegmentationTiler(std::shared_ptr<ImageModel> _model, const ov::AnyMap& configuration) : |
| 47 | + TilerBase(_model, configuration) { |
| 48 | + ov::AnyMap extra_config; |
| 49 | + try { |
| 50 | + auto ov_model = model->getModel(); |
| 51 | + extra_config = ov_model->get_rt_info<ov::AnyMap>("model_info"); |
| 52 | + } |
| 53 | + catch (const std::runtime_error&) { |
| 54 | + extra_config = model->getInferenceAdapter()->getModelConfig(); |
| 55 | + } |
| 56 | + |
| 57 | + blur_strength = get_from_any_maps("blur_strength", configuration, extra_config, blur_strength); |
| 58 | + soft_threshold = get_from_any_maps("soft_threshold", configuration, extra_config, soft_threshold); |
| 59 | + return_soft_prediction = get_from_any_maps("return_soft_prediction", configuration, extra_config, return_soft_prediction); |
| 60 | +} |
| 61 | + |
| 62 | +std::unique_ptr<ImageResultWithSoftPrediction> SemanticSegmentationTiler::run(const ImageInputData& inputData) { |
| 63 | + auto result = this->run_impl(inputData); |
| 64 | + return std::unique_ptr<ImageResultWithSoftPrediction>(static_cast<ImageResultWithSoftPrediction*>(result.release())); |
| 65 | +} |
| 66 | + |
| 67 | +std::unique_ptr<ResultBase> SemanticSegmentationTiler::postprocess_tile(std::unique_ptr<ResultBase> tile_result, const cv::Rect&) { |
| 68 | + ImageResultWithSoftPrediction* soft = dynamic_cast<ImageResultWithSoftPrediction*>(tile_result.get()); |
| 69 | + if (!soft) { |
| 70 | + throw std::runtime_error("SemanticSegmentationTiler requires the underlying model to return ImageResultWithSoftPrediction"); |
| 71 | + } |
| 72 | + return tile_result; |
| 73 | +} |
| 74 | + |
| 75 | +std::unique_ptr<ResultBase> SemanticSegmentationTiler::merge_results(const std::vector<std::unique_ptr<ResultBase>>& tiles_results, |
| 76 | + const cv::Size& image_size, const std::vector<cv::Rect>& tile_coords) { |
| 77 | + if (tiles_results.empty()) { |
| 78 | + return std::unique_ptr<ResultBase>(new ImageResultWithSoftPrediction()); |
| 79 | + } |
| 80 | + |
| 81 | + cv::Mat voting_mask(cv::Size(image_size.width, image_size.height), CV_32SC1, cv::Scalar(0)); |
| 82 | + auto* sseg_res = static_cast<ImageResultWithSoftPrediction*>(tiles_results[0].get()); |
| 83 | + cv::Mat merged_soft_prediction(cv::Size(image_size.width, image_size.height), CV_32FC(sseg_res->soft_prediction.channels()), cv::Scalar(0)); |
| 84 | + |
| 85 | + for (size_t i = 0; i < tiles_results.size(); ++i) { |
| 86 | + auto* sseg_res = static_cast<ImageResultWithSoftPrediction*>(tiles_results[i].get()); |
| 87 | + voting_mask(tile_coords[i]) += 1; |
| 88 | + merged_soft_prediction(tile_coords[i]) += sseg_res->soft_prediction; |
| 89 | + } |
| 90 | + |
| 91 | + normalize_soft_prediction(merged_soft_prediction, voting_mask); |
| 92 | + |
| 93 | + cv::Mat hard_prediction = create_hard_prediction_from_soft_prediction(merged_soft_prediction, soft_threshold, blur_strength); |
| 94 | + |
| 95 | + std::unique_ptr<ResultBase> retVal; |
| 96 | + if (return_soft_prediction) { |
| 97 | + auto* result = new ImageResultWithSoftPrediction(); |
| 98 | + retVal = std::unique_ptr<ResultBase>(result); |
| 99 | + result->soft_prediction = merged_soft_prediction; |
| 100 | + result->resultImage = hard_prediction; |
| 101 | + } |
| 102 | + else { |
| 103 | + auto* result = new ImageResult(); |
| 104 | + retVal = std::unique_ptr<ResultBase>(result); |
| 105 | + result->resultImage = hard_prediction; |
| 106 | + } |
| 107 | + return retVal; |
| 108 | +} |
0 commit comments