Skip to content

Commit 9e0b089

Browse files
Reorder saliency maps for h-cls according to labels in .xml meta
1 parent 0045fb3 commit 9e0b089

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

model_api/python/openvino/model_api/models/classification.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2021-2023 Intel Corporation
2+
Copyright (c) 2021-2024 Intel Corporation
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -15,8 +15,10 @@
1515
"""
1616

1717
import json
18+
from typing import Dict
1819

1920
import numpy as np
21+
2022
from openvino.preprocess import PrePostProcessor
2123
from openvino.runtime import Model, Type
2224
from openvino.runtime import opset10 as opset
@@ -170,11 +172,23 @@ def postprocess(self, outputs, meta):
170172

171173
return ClassificationResult(
172174
result,
173-
outputs.get(_saliency_map_name, np.ndarray(0)),
175+
self.get_saliency_maps(outputs),
174176
outputs.get(_feature_vector_name, np.ndarray(0)),
175177
raw_scores,
176178
)
177179

180+
def get_saliency_maps(self, outputs: Dict) -> np.ndarray:
181+
saliency_maps = outputs.get(_saliency_map_name, np.ndarray(0))
182+
if not self.hierarchical:
183+
return saliency_maps
184+
# In hierarchical case reorder saliency maps to match the order of labels in .XML meta
185+
reordered_saliency_maps = [[] for _ in range(len(saliency_maps))]
186+
for batch in range(len(saliency_maps)):
187+
for label in self.labels:
188+
idx = self.hierarchical_info['cls_heads_info']['label_to_idx'][label]
189+
reordered_saliency_maps[batch].append(saliency_maps[batch][idx])
190+
return np.array(reordered_saliency_maps)
191+
178192
def get_all_probs(self, logits: np.ndarray):
179193
if self.multilabel:
180194
probs = sigmoid_numpy(logits.reshape(-1))

0 commit comments

Comments
 (0)