File size: 2,564 Bytes
46fdf2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import json
import numpy as np



def json_metric(score_json, target_json, num_classes, types):
    assert len(score_json) == len(target_json)
    scores = np.zeros((len(score_json), num_classes))
    targets = np.zeros((len(target_json), num_classes))
    for index in range(len(score_json)):
        scores[index] = score_json[index]["scores"]
        targets[index] = target_json[index]["target"]


    return metric(scores, targets, types)

def json_metric_top3(score_json, target_json, num_classes, types):
    assert len(score_json) == len(target_json)
    scores = np.zeros((len(score_json), num_classes))
    targets = np.zeros((len(target_json), num_classes))
    for index in range(len(score_json)):
        tmp = np.array(score_json[index]['scores'])
        idx = np.argsort(-tmp)
        idx_after_3 = idx[3:]
        tmp[idx_after_3] = 0.

        scores[index] = tmp
        # scores[index] = score_json[index]["scores"]
        targets[index] = target_json[index]["target"]

    return metric(scores, targets, types)


def metric(scores, targets, types):
    """

    :param scores: the output the model predict

    :param targets: the gt label

    :return: OP, OR, OF1, CP, CR, CF1

    calculate the Precision of every class by: TP/TP+FP i.e. TP/total predict

    calculate the Recall by: TP/total GT

    """
    num, num_class = scores.shape
    gt_num = np.zeros(num_class)
    tp_num = np.zeros(num_class)
    predict_num = np.zeros(num_class)


    for index in range(num_class):
        score = scores[:, index]
        target = targets[:, index]
        if types == 'wider':
            tmp = np.where(target == 99)[0]
            # score[tmp] = 0
            target[tmp] = 0
        
        if types == 'voc07':
            tmp = np.where(target != 0)[0]
            score = score[tmp]
            target = target[tmp]
            neg_id = np.where(target == -1)[0]
            target[neg_id] = 0


        gt_num[index] = np.sum(target == 1)
        predict_num[index] = np.sum(score >= 0.5)
        tp_num[index] = np.sum(target * (score >= 0.5))

    predict_num[predict_num == 0] = 1  # avoid dividing 0
    OP = np.sum(tp_num) / np.sum(predict_num)
    OR = np.sum(tp_num) / np.sum(gt_num)
    OF1 = (2 * OP * OR) / (OP + OR)

    #print(tp_num / predict_num)
    #print(tp_num / gt_num)
    CP = np.sum(tp_num / predict_num) / num_class
    CR = np.sum(tp_num / gt_num) / num_class
    CF1 = (2 * CP * CR) / (CP + CR)

    return OP, OR, OF1, CP, CR, CF1