Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix saliency maps order for h-cls in cpp #159

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions model_api/cpp/models/include/models/classification_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct HierarchicalConfig {
std::vector<std::pair<std::string, std::string>> label_tree_edges;
std::vector<std::vector<std::string>> all_groups;
std::map<size_t, std::pair<size_t,size_t>> head_idx_to_logits_range;
std::map<size_t, std::string> logit_idx_to_label;
size_t num_multiclass_heads;
size_t num_multilabel_heads;
size_t num_single_label_classes;
Expand Down Expand Up @@ -90,4 +91,6 @@ class ClassificationModel : public ImageModel {
std::unique_ptr<ResultBase> get_multilabel_predictions(InferenceResult& infResult, bool add_raw_scores);
std::unique_ptr<ResultBase> get_multiclass_predictions(InferenceResult& infResult, bool add_raw_scores);
std::unique_ptr<ResultBase> get_hierarchical_predictions(InferenceResult& infResult, bool add_raw_scores);
ov::Tensor reorder_saliency_maps(const ov::Tensor&);

};
33 changes: 33 additions & 0 deletions model_api/cpp/models/src/classification_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ std::unique_ptr<ResultBase> ClassificationModel::postprocess(InferenceResult& in
auto saliency_map_iter = infResult.outputsData.find(saliency_map_name);
if (saliency_map_iter != infResult.outputsData.end()) {
cls_res->saliency_map = std::move(saliency_map_iter->second);
cls_res->saliency_map = reorder_saliency_maps(cls_res->saliency_map);
}
auto feature_vector_iter = infResult.outputsData.find(feature_vector_name);
if (feature_vector_iter != infResult.outputsData.end()) {
Expand Down Expand Up @@ -358,6 +359,27 @@ std::unique_ptr<ResultBase> ClassificationModel::get_hierarchical_predictions(In
return retVal;
}

ov::Tensor ClassificationModel::reorder_saliency_maps(const ov::Tensor& source_maps) {
if (!hierarchical || source_maps.get_shape().size() == 1) {
return source_maps;
}

auto reordered_maps = ov::Tensor(source_maps.get_element_type(), source_maps.get_shape());
const std::uint8_t* source_maps_ptr = static_cast<std::uint8_t*>(source_maps.data());
std::uint8_t* reordered_maps_ptr = static_cast<std::uint8_t*>(reordered_maps.data());

size_t shape_offset = (source_maps.get_shape().size() == 4) ? 1 : 0;
size_t map_byte_size = source_maps.get_element_type().size() *
source_maps.get_shape()[shape_offset + 1] * source_maps.get_shape()[shape_offset + 2];

for (size_t i = 0; i < source_maps.get_shape()[shape_offset]; ++i) {
size_t new_index = hierarchical_info.label_to_idx[hierarchical_info.logit_idx_to_label[i]];
std::copy_n(source_maps_ptr + i*map_byte_size, map_byte_size, reordered_maps_ptr + new_index * map_byte_size);
}

return reordered_maps;
}

std::unique_ptr<ResultBase> ClassificationModel::get_multiclass_predictions(InferenceResult& infResult, bool add_raw_scores) {
const ov::Tensor& indicesTensor = infResult.outputsData.find(indices_name)->second;
const int* indicesPtr = indicesTensor.data<int>();
Expand Down Expand Up @@ -490,6 +512,17 @@ HierarchicalConfig::HierarchicalConfig(const std::string& json_repr) {
for (const auto& range_descr : tmp_head_idx_to_logits_range) {
head_idx_to_logits_range[stoi(range_descr.first)] = range_descr.second;
}

size_t logits_processed = 0;
for (size_t i = 0; i < num_multiclass_heads; ++i) {
const auto& logits_range = head_idx_to_logits_range[i];
for (size_t k = logits_range.first; k < logits_range.second; ++k) {
logit_idx_to_label[logits_processed++] = all_groups[i][k - logits_range.first];
}
}
for (size_t i = 0; i < num_multilabel_heads; ++i) {
logit_idx_to_label[logits_processed++] = all_groups[num_multiclass_heads + i][0];
}
}

GreedyLabelsResolver::GreedyLabelsResolver(const HierarchicalConfig& config) :
Expand Down
Loading