| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- import numpy as np
- from ....utils import logging
- from ...results import TopkResult
- from ..base import BaseComponent
- __all__ = ["Topk", "NormalizeFeatures"]
- def _parse_class_id_map(class_ids):
- """parse class id to label map file"""
- if class_ids is None:
- return None
- class_id_map = {id: str(lb) for id, lb in enumerate(class_ids)}
- return class_id_map
- class Topk(BaseComponent):
- """Topk Transform"""
- INPUT_KEYS = ["pred", "img_path"]
- OUTPUT_KEYS = ["topk_res"]
- DEAULT_INPUTS = {"pred": "pred", "img_path": "img_path"}
- DEAULT_OUTPUTS = {"topk_res": "topk_res"}
- def __init__(self, topk, class_ids=None):
- super().__init__()
- assert isinstance(topk, (int,))
- self.topk = topk
- self.class_id_map = _parse_class_id_map(class_ids)
- def apply(self, pred, img_path):
- """apply"""
- cls_pred = pred
- class_id_map = self.class_id_map
- index = cls_pred.argsort(axis=0)[-self.topk :][::-1].astype("int32")
- clas_id_list = []
- score_list = []
- label_name_list = []
- for i in index:
- clas_id_list.append(i.item())
- score_list.append(cls_pred[i].item())
- if class_id_map is not None:
- label_name_list.append(class_id_map[i.item()])
- result = {
- "img_path": img_path,
- "class_ids": clas_id_list,
- "scores": np.around(score_list, decimals=5).tolist(),
- }
- if label_name_list is not None:
- result["label_names"] = label_name_list
- return {"topk_res": TopkResult(result)}
- class NormalizeFeatures(BaseComponent):
- """Normalize Features Transform"""
- INPUT_KEYS = ["cls_pred"]
- OUTPUT_KEYS = ["cls_res"]
- DEAULT_INPUTS = {"cls_res": "cls_res"}
- DEAULT_OUTPUTS = {"cls_pred": "cls_pred"}
- def apply(self, cls_pred):
- """apply"""
- feas_norm = np.sqrt(np.sum(np.square(cls_pred), axis=0, keepdims=True))
- cls_res = np.divide(cls_pred, feas_norm)
- return {"cls_res": cls_res}
|