misc.py 5.1 KB
Newer Older
1
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
W
WuHaobo 已提交
2
#
3 4 5
# 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
W
WuHaobo 已提交
6 7 8
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
9 10 11 12 13
# 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.
W
WuHaobo 已提交
14

C
cuicheng01 已提交
15 16
import paddle

W
WuHaobo 已提交
17 18 19 20 21 22
__all__ = ['AverageMeter']


class AverageMeter(object):
    """
    Computes and stores the average and current value
littletomatodonkey's avatar
littletomatodonkey 已提交
23
    Code was based on https://github.com/pytorch/examples/blob/master/imagenet/main.py
W
WuHaobo 已提交
24 25
    """

L
littletomatodonkey 已提交
26
    def __init__(self, name='', fmt='f', postfix="", need_avg=True):
W
WuHaobo 已提交
27 28
        self.name = name
        self.fmt = fmt
L
littletomatodonkey 已提交
29
        self.postfix = postfix
30
        self.need_avg = need_avg
W
WuHaobo 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        self.reset()

    def reset(self):
        """ reset """
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        """ update """
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

C
cuicheng01 已提交
47 48
    @property
    def avg_info(self):
C
cuicheng01 已提交
49
        if isinstance(self.avg, paddle.Tensor):
50
            self.avg = float(self.avg)
C
cuicheng01 已提交
51 52
        return "{}: {:.5f}".format(self.name, self.avg)

53 54
    @property
    def total(self):
L
littletomatodonkey 已提交
55 56
        return '{self.name}_sum: {self.sum:{self.fmt}}{self.postfix}'.format(
            self=self)
57

58 59
    @property
    def total_minute(self):
L
littletomatodonkey 已提交
60
        return '{self.name} {s:{self.fmt}}{self.postfix} min'.format(
61 62
            s=self.sum / 60, self=self)

63 64
    @property
    def mean(self):
L
littletomatodonkey 已提交
65
        return '{self.name}: {self.avg:{self.fmt}}{self.postfix}'.format(
66 67 68 69
            self=self) if self.need_avg else ''

    @property
    def value(self):
L
littletomatodonkey 已提交
70 71
        return '{self.name}: {self.val:{self.fmt}}{self.postfix}'.format(
            self=self)
Z
zhiboniu 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155


class AttrMeter(object):
    """
    Computes and stores the average and current value
    Code was based on https://github.com/pytorch/examples/blob/master/imagenet/main.py
    """

    def __init__(self, threshold=0.5):
        self.threshold = threshold
        self.reset()

    def reset(self):
        self.gt_pos = 0
        self.gt_neg = 0
        self.true_pos = 0
        self.true_neg = 0
        self.false_pos = 0
        self.false_neg = 0

        self.gt_pos_ins = []
        self.true_pos_ins = []
        self.intersect_pos = []
        self.union_pos = []

    def update(self, metric_dict):
        self.gt_pos += metric_dict['gt_pos']
        self.gt_neg += metric_dict['gt_neg']
        self.true_pos += metric_dict['true_pos']
        self.true_neg += metric_dict['true_neg']
        self.false_pos += metric_dict['false_pos']
        self.false_neg += metric_dict['false_neg']

        self.gt_pos_ins += metric_dict['gt_pos_ins'].tolist()
        self.true_pos_ins += metric_dict['true_pos_ins'].tolist()
        self.intersect_pos += metric_dict['intersect_pos'].tolist()
        self.union_pos += metric_dict['union_pos'].tolist()

    def res(self):
        import numpy as np
        eps = 1e-20
        label_pos_recall = 1.0 * self.true_pos / (
            self.gt_pos + eps)  # true positive
        label_neg_recall = 1.0 * self.true_neg / (
            self.gt_neg + eps)  # true negative
        # mean accuracy
        label_ma = (label_pos_recall + label_neg_recall) / 2

        label_pos_recall = np.mean(label_pos_recall)
        label_neg_recall = np.mean(label_neg_recall)
        label_prec = (self.true_pos / (self.true_pos + self.false_pos + eps))
        label_acc = (self.true_pos /
                     (self.true_pos + self.false_pos + self.false_neg + eps))
        label_f1 = np.mean(2 * label_prec * label_pos_recall /
                           (label_prec + label_pos_recall + eps))

        ma = (np.mean(label_ma))

        self.gt_pos_ins = np.array(self.gt_pos_ins)
        self.true_pos_ins = np.array(self.true_pos_ins)
        self.intersect_pos = np.array(self.intersect_pos)
        self.union_pos = np.array(self.union_pos)
        instance_acc = self.intersect_pos / (self.union_pos + eps)
        instance_prec = self.intersect_pos / (self.true_pos_ins + eps)
        instance_recall = self.intersect_pos / (self.gt_pos_ins + eps)
        instance_f1 = 2 * instance_prec * instance_recall / (
            instance_prec + instance_recall + eps)

        instance_acc = np.mean(instance_acc)
        instance_prec = np.mean(instance_prec)
        instance_recall = np.mean(instance_recall)
        instance_f1 = 2 * instance_prec * instance_recall / (
            instance_prec + instance_recall + eps)

        instance_acc = np.mean(instance_acc)
        instance_prec = np.mean(instance_prec)
        instance_recall = np.mean(instance_recall)
        instance_f1 = np.mean(instance_f1)

        res = [
            ma, label_f1, label_pos_recall, label_neg_recall, instance_f1,
            instance_acc, instance_prec, instance_recall
        ]
        return res