metrics.py 6.1 KB
Newer Older
W
weishengyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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 numpy as np
import paddle
import paddle.nn as nn

D
dongshuilong 已提交
19

W
weishengyu 已提交
20
class TopkAcc(nn.Layer):
W
weishengyu 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    def __init__(self, topk=(1, 5)):
        super().__init__()
        assert isinstance(topk, (int, list, tuple))
        if isinstance(topk, int):
            topk = [topk]
        self.topk = topk

    def forward(self, x, label):
        if isinstance(x, dict):
            x = x["logits"]

        metric_dict = dict()
        for k in self.topk:
            metric_dict["top{}".format(k)] = paddle.metric.accuracy(
                x, label, k=k)
        return metric_dict

D
dongshuilong 已提交
38

W
weishengyu 已提交
39
class mAP(nn.Layer):
D
dongshuilong 已提交
40
    def __init__(self):
W
weishengyu 已提交
41 42
        super().__init__()

D
dongshuilong 已提交
43 44
    def forward(self, similarities_matrix, query_img_id, gallery_img_id,
                *args):
W
weishengyu 已提交
45
        metric_dict = dict()
D
dongshuilong 已提交
46 47 48 49 50 51 52 53 54 55 56

        choosen_indices = paddle.argsort(
            similarities_matrix, axis=1, descending=True)
        gallery_labels_transpose = paddle.transpose(gallery_img_id, [1, 0])
        gallery_labels_transpose = paddle.broadcast_to(
            gallery_labels_transpose,
            shape=[
                choosen_indices.shape[0], gallery_labels_transpose.shape[1]
            ])
        choosen_label = paddle.index_sample(gallery_labels_transpose,
                                            choosen_indices)
B
Bin Lu 已提交
57
        equal_flag = paddle.equal(choosen_label, query_img_id)
B
Bin Lu 已提交
58 59 60 61
        equal_flag = paddle.cast(equal_flag, 'float32')

        acc_sum = paddle.cumsum(equal_flag, axis=1)
        div = paddle.arange(acc_sum.shape[1]).astype("float32") + 1
D
dongshuilong 已提交
62
        precision = paddle.divide(acc_sum, div)
B
Bin Lu 已提交
63 64 65

        #calc map
        precision_mask = paddle.multiply(equal_flag, precision)
D
dongshuilong 已提交
66 67
        ap = paddle.sum(precision_mask, axis=1) / paddle.sum(equal_flag,
                                                             axis=1)
B
Bin Lu 已提交
68
        metric_dict["mAP"] = paddle.mean(ap).numpy()[0]
W
weishengyu 已提交
69 70
        return metric_dict

D
dongshuilong 已提交
71

W
weishengyu 已提交
72
class mINP(nn.Layer):
D
dongshuilong 已提交
73
    def __init__(self):
W
weishengyu 已提交
74 75
        super().__init__()

D
dongshuilong 已提交
76 77
    def forward(self, similarities_matrix, query_img_id, gallery_img_id,
                *args):
W
weishengyu 已提交
78
        metric_dict = dict()
D
dongshuilong 已提交
79 80 81 82 83 84 85 86 87 88 89

        choosen_indices = paddle.argsort(
            similarities_matrix, axis=1, descending=True)
        gallery_labels_transpose = paddle.transpose(gallery_img_id, [1, 0])
        gallery_labels_transpose = paddle.broadcast_to(
            gallery_labels_transpose,
            shape=[
                choosen_indices.shape[0], gallery_labels_transpose.shape[1]
            ])
        choosen_label = paddle.index_sample(gallery_labels_transpose,
                                            choosen_indices)
B
Bin Lu 已提交
90
        tmp = paddle.equal(choosen_label, query_img_id)
B
Bin Lu 已提交
91 92 93 94
        tmp = paddle.cast(tmp, 'float64')

        #do accumulative sum
        div = paddle.arange(tmp.shape[1]).astype("float64") + 2
D
dongshuilong 已提交
95 96
        minus = paddle.divide(tmp, div)
        auxilary = paddle.subtract(tmp, minus)
B
Bin Lu 已提交
97 98 99 100
        hard_index = paddle.argmax(auxilary, axis=1).astype("float64")
        all_INP = paddle.divide(paddle.sum(tmp, axis=1), hard_index)
        mINP = paddle.mean(all_INP)
        metric_dict["mINP"] = mINP.numpy()[0]
W
weishengyu 已提交
101 102
        return metric_dict

D
dongshuilong 已提交
103

W
weishengyu 已提交
104
class Recallk(nn.Layer):
D
dongshuilong 已提交
105
    def __init__(self, topk=(1, 5)):
W
weishengyu 已提交
106
        super().__init__()
B
Bin Lu 已提交
107
        assert isinstance(topk, (int, list, tuple))
W
weishengyu 已提交
108 109 110 111
        if isinstance(topk, int):
            topk = [topk]
        self.topk = topk

D
dongshuilong 已提交
112 113
    def forward(self, similarities_matrix, query_img_id, gallery_img_id,
                keep_mask):
W
weishengyu 已提交
114
        metric_dict = dict()
B
Bin Lu 已提交
115 116

        #get cmc
D
dongshuilong 已提交
117 118 119 120 121 122 123 124 125 126
        choosen_indices = paddle.argsort(
            similarities_matrix, axis=1, descending=True)
        gallery_labels_transpose = paddle.transpose(gallery_img_id, [1, 0])
        gallery_labels_transpose = paddle.broadcast_to(
            gallery_labels_transpose,
            shape=[
                choosen_indices.shape[0], gallery_labels_transpose.shape[1]
            ])
        choosen_label = paddle.index_sample(gallery_labels_transpose,
                                            choosen_indices)
B
Bin Lu 已提交
127
        equal_flag = paddle.equal(choosen_label, query_img_id)
D
dongshuilong 已提交
128 129 130 131 132
        if keep_mask is not None:
            keep_mask = paddle.index_sample(
                keep_mask.astype('float32'), choosen_indices)
            equal_flag = paddle.logical_and(equal_flag,
                                            keep_mask.astype('bool'))
B
Bin Lu 已提交
133
        equal_flag = paddle.cast(equal_flag, 'float32')
D
dongshuilong 已提交
134 135 136 137 138
        real_query_num = paddle.sum(equal_flag, axis=1)
        real_query_num = paddle.sum(
            paddle.greater_than(real_query_num, paddle.to_tensor(0.)).astype(
                "float32"))

B
Bin Lu 已提交
139
        acc_sum = paddle.cumsum(equal_flag, axis=1)
D
dongshuilong 已提交
140 141 142
        mask = paddle.greater_than(acc_sum,
                                   paddle.to_tensor(0.)).astype("float32")
        all_cmc = (paddle.sum(mask, axis=0) / real_query_num).numpy()
W
weishengyu 已提交
143 144 145 146 147

        for k in self.topk:
            metric_dict["recall{}".format(k)] = all_cmc[k - 1]
        return metric_dict

D
dongshuilong 已提交
148

149 150 151 152 153 154 155 156 157 158 159
class DistillationTopkAcc(TopkAcc):
    def __init__(self, model_key, feature_key=None, topk=(1, 5)):
        super().__init__(topk=topk)
        self.model_key = model_key
        self.feature_key = feature_key

    def forward(self, x, label):
        x = x[self.model_key]
        if self.feature_key is not None:
            x = x[self.feature_key]
        return super().forward(x, label)
C
cuicheng01 已提交
160 161 162 163 164 165 166 167 168 169 170 171


class GoogLeNetTopkAcc(TopkAcc):
    def __init__(self, topk=(1, 5)):
        super().__init__()
        assert isinstance(topk, (int, list, tuple))
        if isinstance(topk, int):
            topk = [topk]
        self.topk = topk

    def forward(self, x, label):
        return super().forward(x[0], label)