|
| 1 | +/* |
| 2 | + * Copyright (C) 2020-2025 Intel Corporation |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#pragma once |
| 7 | + |
| 8 | +#include <opencv2/opencv.hpp> |
| 9 | +#include <openvino/openvino.hpp> |
| 10 | + |
| 11 | +#include "adapters/inference_adapter.h" |
| 12 | +#include "tasks/classification/resolvers.h" |
| 13 | +#include "tasks/results.h" |
| 14 | +#include "utils/config.h" |
| 15 | +#include "utils/vision_pipeline.h" |
| 16 | + |
| 17 | +class Classification { |
| 18 | +public: |
| 19 | + std::shared_ptr<InferenceAdapter> adapter; |
| 20 | + VisionPipeline<ClassificationResult> pipeline; |
| 21 | + |
| 22 | + Classification(std::shared_ptr<InferenceAdapter> adapter) : adapter(adapter) { |
| 23 | + pipeline = VisionPipeline<ClassificationResult>( |
| 24 | + adapter, |
| 25 | + [&](cv::Mat image) { |
| 26 | + return preprocess(image); |
| 27 | + }, |
| 28 | + [&](InferenceResult result) { |
| 29 | + return postprocess(result); |
| 30 | + }); |
| 31 | + |
| 32 | + auto config = adapter->getModelConfig(); |
| 33 | + labels = utils::get_from_any_maps("labels", config, {}, labels); |
| 34 | + |
| 35 | + topk = utils::get_from_any_maps("topk", config, {}, topk); |
| 36 | + multilabel = utils::get_from_any_maps("multilabel", config, {}, multilabel); |
| 37 | + output_raw_scores = utils::get_from_any_maps("output_raw_scores", config, {}, output_raw_scores); |
| 38 | + confidence_threshold = utils::get_from_any_maps("confidence_threshold", config, {}, confidence_threshold); |
| 39 | + hierarchical = utils::get_from_any_maps("hierarchical", config, {}, hierarchical); |
| 40 | + hierarchical_config = utils::get_from_any_maps("hierarchical_config", config, {}, hierarchical_config); |
| 41 | + hierarchical_postproc = utils::get_from_any_maps("hierarchical_postproc", config, {}, hierarchical_postproc); |
| 42 | + if (hierarchical) { |
| 43 | + if (hierarchical_config.empty()) { |
| 44 | + throw std::runtime_error("Error: empty hierarchical classification config"); |
| 45 | + } |
| 46 | + hierarchical_info = HierarchicalConfig(hierarchical_config); |
| 47 | + if (hierarchical_postproc == "probabilistic") { |
| 48 | + resolver = std::make_unique<ProbabilisticLabelsResolver>(hierarchical_info); |
| 49 | + } else if (hierarchical_postproc == "greedy") { |
| 50 | + resolver = std::make_unique<GreedyLabelsResolver>(hierarchical_info); |
| 51 | + } else { |
| 52 | + throw std::runtime_error("Wrong hierarchical labels postprocessing type"); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + static void serialize(std::shared_ptr<ov::Model>& ov_model); |
| 58 | + static Classification load(const std::string& model_path); |
| 59 | + |
| 60 | + ClassificationResult infer(cv::Mat image); |
| 61 | + std::vector<ClassificationResult> inferBatch(std::vector<cv::Mat> image); |
| 62 | + |
| 63 | + std::map<std::string, ov::Tensor> preprocess(cv::Mat); |
| 64 | + ClassificationResult postprocess(InferenceResult& infResult); |
| 65 | + |
| 66 | + bool postprocess_semantic_masks = true; |
| 67 | + |
| 68 | +private: |
| 69 | + ClassificationResult get_multilabel_predictions(InferenceResult& infResult, bool add_raw_scores); |
| 70 | + ClassificationResult get_multiclass_predictions(InferenceResult& infResult, bool add_raw_scores); |
| 71 | + ClassificationResult get_hierarchical_predictions(InferenceResult& infResult, bool add_raw_scores); |
| 72 | + |
| 73 | + ov::Tensor reorder_saliency_maps(const ov::Tensor& source_maps); |
| 74 | + |
| 75 | + // multiclass serialization step |
| 76 | + static void addOrFindSoftmaxAndTopkOutputs(std::shared_ptr<ov::Model>& model, size_t topk, bool add_raw_scores); |
| 77 | + |
| 78 | +private: |
| 79 | + cv::Size input_shape; |
| 80 | + std::vector<std::string> labels; |
| 81 | + float confidence_threshold = 0.5f; |
| 82 | + |
| 83 | + bool multilabel = false; |
| 84 | + bool hierarchical = false; |
| 85 | + bool output_raw_scores = false; |
| 86 | + |
| 87 | + // hierarchical |
| 88 | + size_t topk = 1; |
| 89 | + std::string hierarchical_config; |
| 90 | + std::string hierarchical_postproc = "greedy"; |
| 91 | + HierarchicalConfig hierarchical_info; |
| 92 | + std::unique_ptr<GreedyLabelsResolver> resolver; |
| 93 | +}; |
0 commit comments