JapanKanji

Форк
0
/
f1Subs.py 
144 строки · 4.4 Кб
1
import datetime
2
import sys, os
3

4
import cv2 as cv2
5
import numpy as np
6
import pandas as pd
7

8
###########---------------------------------------
9

10
def score_page(preds, truth, classesOK=True, lenPreds=5, ext=False):
11
    """
12
    Scores a single page.
13
    Args:
14
        preds: prediction string of labels and center points.
15
        truth: ground truth string of labels and bounding boxes.
16
    Returns:
17
        True/false positive and false negative counts for the page
18
    """
19
    tp = 0
20
    fp = 0
21
    fn = 0
22

23
    truth_indices = {'label': 0, 'X': 1, 'Y': 2, 'Width': 3, 'Height': 4 }
24
    preds_indices = {'label': 0, 'X': 1, 'Y': 2, 'Width': 3, 'Height': 4 }
25

26
    if pd.isna(truth) and pd.isna(preds):
27
        return {'tp': tp, 'fp': fp, 'fn': fn}
28

29
    if pd.isna(truth):
30
        fp += len(preds.split(' ')) // len(preds_indices)
31
        return {'tp': tp, 'fp': fp, 'fn': fn}
32

33
    if pd.isna(preds):
34
        fn += len(truth.split(' ')) // len(truth_indices)
35
        return {'tp': tp, 'fp': fp, 'fn': fn}
36

37
    if truth=='' and preds=='':
38
        return {'tp': tp, 'fp': fp, 'fn': fn}
39

40
    if truth=='':
41
        fp += len(preds.split(' ')) // len(preds_indices)
42
        return {'tp': tp, 'fp': fp, 'fn': fn}
43

44
    if preds=='':
45
        fn += len(truth.split(' ')) // len(truth_indices)
46
        return {'tp': tp, 'fp': fp, 'fn': fn}
47

48
    aa = np.array(truth.split()).reshape((-1,len(truth_indices)))
49
        
50
    truth_label = aa[:,0]
51
    truth_xmin  = aa[:,1].astype(float)
52
    truth_ymin  = aa[:,2].astype(float)
53
    truth_xmax  = truth_xmin + aa[:,3].astype(float)
54
    truth_ymax  = truth_ymin + aa[:,4].astype(float)
55
    
56
    truth_x     = (truth_xmin+truth_xmax)/2
57
    truth_y     = (truth_ymin+truth_ymax)/2
58
    truth_w     = (truth_xmax-truth_xmin)
59
    truth_h     = (truth_ymax-truth_ymin)
60

61
    aa = np.array(preds.split()).reshape((-1,len(truth_indices)))
62
    
63
    preds_label = aa[:,0]
64
    preds_x     = aa[:,1].astype(float)
65
    preds_y     = aa[:,2].astype(float)
66
    preds_w     = aa[:,3].astype(float)
67
    preds_h     = aa[:,4].astype(float)
68
    
69
    preds_xmin  = preds_x-preds_w/2
70
    preds_ymin  = preds_y-preds_h/2
71
    preds_xmax  = preds_xmin + preds_w
72
    preds_ymax  = preds_ymin + preds_h
73
    
74
    preds_unused = np.ones(len(preds_label)).astype(bool)
75
    preds_used   = np.ones(len(preds_label)).astype(int)-2
76
    
77
    labelDiff    = []
78

79
    for ii, (xmin, xmax, ymin, ymax, label) in enumerate(zip(truth_xmin, truth_xmax, 
80
                                                             truth_ymin, truth_ymax, 
81
                                                             truth_label)):
82
        
83
        matchingOK   = ((xmin < preds_x) & (xmax > preds_x) & 
84
                        (ymin < preds_y) & (ymax > preds_y) & 
85
                        (preds_label == label) & preds_unused)
86
        
87
        matchingNot  = ((xmin < preds_x) & (xmax > preds_x) & 
88
                        (ymin < preds_y) & (ymax > preds_y) & preds_unused)
89
            
90
        matching = matchingOK if classesOK else matchingNot
91
            
92
        if matching.sum() == 0:
93
            fn += 1
94
        else:
95
            tp += 1
96
            preds_unused[np.argmax(matching)] = False
97
            preds_used  [np.argmax(matching)] = ii
98
        
99
        if classesOK and matchingNot.sum()>0 and matchingOK.sum()==0 : 
100
            labelDiff.append([label,preds_label[np.argmax(matchingNot)]])
101
            
102
    fp += preds_unused.sum()
103
    
104
    if ext : return {'tp': tp, 'fp': fp, 'fn': fn}, np.array(labelDiff)
105

106
    return {'tp': tp, 'fp': fp, 'fn': fn,
107
            'pAll':len(preds_label), 
108
            'tAll':len(truth_label)}
109

110
def f1 (trues, preds, **kwargs) :
111
    
112
    results = [score_page(p,t,**kwargs) for t,p in zip(trues,preds)]
113

114
    tp = sum([x['tp'] for x in results])
115
    fp = sum([x['fp'] for x in results])
116
    fn = sum([x['fn'] for x in results])
117

118
    return f1_1(tp,fp,fn)
119

120
def f1All (trues, preds, **kwargs) :
121
    
122
    results = [score_page(p,t,**kwargs) for t,p in zip(trues,preds)]
123
    f1A   = [f1_1(x['tp'],x['fp'],x['fn']) for x in results]
124

125
    return f1A
126

127
def f1A3 (trues, preds, **kwargs) :
128
    
129
    results = [score_page(p,t,**kwargs) for t,p in zip(trues,preds)]
130
    f1A   = [f1_1(x['tp'],x['fp'],x['fn']) for x in results]
131

132
    return f1A, results
133

134
def f1_1 (tp,fp,fn) :
135
    
136
    if (tp + fp) == 0 or (tp + fn) == 0:
137
        return 0
138
    precision = tp / (tp + fp)
139
    recall = tp / (tp + fn)
140
    if precision > 0 and recall > 0:
141
        f1 = (2 * precision * recall) / (precision + recall)
142
    else:
143
        f1 = 0
144
    return f1

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.