topk.py 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# copyright (c) 2021 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 os
import numpy as np
import paddle
import paddle.nn.functional as F


class Topk(object):
W
weishengyu 已提交
22
    def __init__(self, topk=1, class_id_map_file=None, delimiter=None):
23 24
        assert isinstance(topk, (int, ))
        self.topk = topk
W
weishengyu 已提交
25
        self.delimiter = delimiter if delimiter is not None else " "
G
gaotingquan 已提交
26
        self.class_id_map = self.parse_class_id_map(class_id_map_file)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

    def parse_class_id_map(self, class_id_map_file):
        if class_id_map_file is None:
            return None
        if not os.path.exists(class_id_map_file):
            print(
                "Warning: If want to use your own label_dict, please input legal path!\nOtherwise label_names will be empty!"
            )
            return None

        try:
            class_id_map = {}
            with open(class_id_map_file, "r") as fin:
                lines = fin.readlines()
                for line in lines:
W
weishengyu 已提交
42
                    partition = line.split("\n")[0].partition(self.delimiter)
43 44 45 46 47 48
                    class_id_map[int(partition[0])] = str(partition[-1])
        except Exception as ex:
            print(ex)
            class_id_map = None
        return class_id_map

C
cuicheng01 已提交
49
    def __call__(self, x, file_names=None):
wc晨曦's avatar
wc晨曦 已提交
50 51
        if isinstance(x, dict):
            x = x['logits']
52 53 54
        assert isinstance(x, paddle.Tensor)
        if file_names is not None:
            assert x.shape[0] == len(file_names)
C
cuicheng01 已提交
55
        x = F.softmax(x, axis=-1)
56 57 58
        x = x.numpy()
        y = []
        for idx, probs in enumerate(x):
C
cuicheng01 已提交
59
            index = probs.argsort(axis=0)[-self.topk:][::-1].astype(
C
cuicheng01 已提交
60
                "int32")
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
            clas_id_list = []
            score_list = []
            label_name_list = []
            for i in index:
                clas_id_list.append(i.item())
                score_list.append(probs[i].item())
                if self.class_id_map is not None:
                    label_name_list.append(self.class_id_map[i.item()])
            result = {
                "class_ids": clas_id_list,
                "scores": np.around(
                    score_list, decimals=5).tolist(),
            }
            if file_names is not None:
                result["file_name"] = file_names[idx]
            if label_name_list is not None:
                result["label_names"] = label_name_list
            y.append(result)
        return y
C
cuicheng01 已提交
80