test_detection_map_op.py 5.4 KB
Newer Older
W
wanghaox 已提交
1 2 3 4 5 6 7 8 9 10 11 12
import unittest
import numpy as np
import sys
import collections
import math
from op_test import OpTest


class TestDetectionMAPOp(OpTest):
    def set_data(self):
        self.init_test_case()

W
wanghaox 已提交
13
        self.mAP = [self.calc_map(self.tf_pos, self.tf_pos_lod)]
W
wanghaox 已提交
14 15 16 17 18 19
        self.label = np.array(self.label).astype('float32')
        self.detect = np.array(self.detect).astype('float32')
        self.mAP = np.array(self.mAP).astype('float32')

        self.inputs = {
            'Label': (self.label, self.label_lod),
W
wanghaox 已提交
20
            'Detection': (self.detect, self.detect_lod)
W
wanghaox 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33
        }

        self.attrs = {
            'overlap_threshold': self.overlap_threshold,
            'evaluate_difficult': self.evaluate_difficult,
            'ap_type': self.ap_type
        }

        self.outputs = {'MAP': self.mAP}

    def init_test_case(self):
        self.overlap_threshold = 0.3
        self.evaluate_difficult = True
W
wanghaox 已提交
34
        self.ap_type = "integral"
W
wanghaox 已提交
35 36

        self.label_lod = [[0, 2, 4]]
W
wanghaox 已提交
37 38 39
        # label difficult xmin ymin xmax ymax
        self.label = [[1, 0, 0.1, 0.1, 0.3, 0.3], [1, 1, 0.6, 0.6, 0.8, 0.8],
                      [2, 0, 0.3, 0.3, 0.6, 0.5], [1, 0, 0.7, 0.1, 0.9, 0.3]]
W
wanghaox 已提交
40

W
wanghaox 已提交
41 42
        # label score xmin ymin xmax ymax difficult
        self.detect_lod = [[0, 3, 7]]
W
wanghaox 已提交
43
        self.detect = [
W
wanghaox 已提交
44 45 46 47
            [1, 0.3, 0.1, 0.0, 0.4, 0.3], [1, 0.7, 0.0, 0.1, 0.2, 0.3],
            [1, 0.9, 0.7, 0.6, 0.8, 0.8], [2, 0.8, 0.2, 0.1, 0.4, 0.4],
            [2, 0.1, 0.4, 0.3, 0.7, 0.5], [1, 0.2, 0.8, 0.1, 1.0, 0.3],
            [3, 0.2, 0.8, 0.1, 1.0, 0.3]
W
wanghaox 已提交
48 49
        ]

W
wanghaox 已提交
50 51 52 53 54
        # label score true_pos false_pos
        self.tf_pos_lod = [[0, 3, 7]]
        self.tf_pos = [[1, 0.9, 1, 0], [1, 0.7, 1, 0], [1, 0.3, 0, 1],
                       [1, 0.2, 1, 0], [2, 0.8, 0, 1], [2, 0.1, 1, 0],
                       [3, 0.2, 0, 1]]
W
wanghaox 已提交
55

W
wanghaox 已提交
56
    def calc_map(self, tf_pos, tf_pos_lod):
W
wanghaox 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        mAP = 0.0
        count = 0

        class_pos_count = {}
        true_pos = {}
        false_pos = {}

        def get_accumulation(pos_list):
            sorted_list = sorted(pos_list, key=lambda pos: pos[0], reverse=True)
            sum = 0
            accu_list = []
            for (score, count) in sorted_list:
                sum += count
                accu_list.append(sum)
            return accu_list

        label_count = collections.Counter()
W
wanghaox 已提交
74
        for (label, difficult, xmin, ymin, xmax, ymax) in self.label:
W
wanghaox 已提交
75 76 77 78 79 80 81
            if self.evaluate_difficult:
                label_count[label] += 1
            elif not difficult:
                label_count[label] += 1

        true_pos = collections.defaultdict(list)
        false_pos = collections.defaultdict(list)
W
wanghaox 已提交
82
        for (label, score, tp, fp) in tf_pos:
W
wanghaox 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
            true_pos[label].append([score, tp])
            false_pos[label].append([score, fp])

        for (label, label_pos_num) in label_count.items():
            if label_pos_num == 0 or label not in true_pos:
                continue

            label_true_pos = true_pos[label]
            label_false_pos = false_pos[label]

            accu_tp_sum = get_accumulation(label_true_pos)
            accu_fp_sum = get_accumulation(label_false_pos)

            precision = []
            recall = []

            for i in range(len(accu_tp_sum)):
                precision.append(
                    float(accu_tp_sum[i]) /
                    float(accu_tp_sum[i] + accu_fp_sum[i]))
                recall.append(float(accu_tp_sum[i]) / label_pos_num)

            if self.ap_type == "11point":
W
wanghaox 已提交
106
                max_precisions = [0.0] * 11
W
wanghaox 已提交
107
                start_idx = len(accu_tp_sum) - 1
W
wanghaox 已提交
108 109 110
                for j in range(10, -1, -1):
                    for i in range(start_idx, -1, -1):
                        if recall[i] < float(j) / 10.0:
W
wanghaox 已提交
111 112 113 114
                            start_idx = i
                            if j > 0:
                                max_precisions[j - 1] = max_precisions[j]
                                break
W
wanghaox 已提交
115 116 117 118
                        else:
                            if max_precisions[j] < precision[i]:
                                max_precisions[j] = precision[i]
                for j in range(10, -1, -1):
W
wanghaox 已提交
119 120
                    mAP += max_precisions[j] / 11
                count += 1
W
wanghaox 已提交
121
            elif self.ap_type == "integral":
W
wanghaox 已提交
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
                average_precisions = 0.0
                prev_recall = 0.0
                for i in range(len(accu_tp_sum)):
                    if math.fabs(recall[i] - prev_recall) > 1e-6:
                        average_precisions += precision[i] * \
                            math.fabs(recall[i] - prev_recall)
                        prev_recall = recall[i]

                mAP += average_precisions
                count += 1

        if count != 0: mAP /= count
        return mAP * 100.0

    def setUp(self):
        self.op_type = "detection_map"
        self.set_data()

    def test_check_output(self):
        self.check_output()


class TestDetectionMAPOpSkipDiff(TestDetectionMAPOp):
    def init_test_case(self):
        super(TestDetectionMAPOpSkipDiff, self).init_test_case()

        self.evaluate_difficult = False

W
wanghaox 已提交
150 151 152 153 154 155 156 157 158 159 160
        self.tf_pos_lod = [[0, 2, 6]]
        # label score true_pos false_pos
        self.tf_pos = [[1, 0.7, 1, 0], [1, 0.3, 0, 1], [1, 0.2, 1, 0],
                       [2, 0.8, 0, 1], [2, 0.1, 1, 0], [3, 0.2, 0, 1]]


class TestDetectionMAPOp11Point(TestDetectionMAPOp):
    def init_test_case(self):
        super(TestDetectionMAPOp11Point, self).init_test_case()

        self.ap_type = "11point"
W
wanghaox 已提交
161 162 163 164


if __name__ == '__main__':
    unittest.main()