File size: 1,858 Bytes
0eb3766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 math
import random

def _sigmoid(x):
    return 1 / (1 + math.exp(-x))


def _2_sigmoid_minus_1(x):
    return 2 * _sigmoid(x) - 1

def _tanh(x):
    return math.tanh(x)


# mapping param for special metrix
special_metric_dict = {
    # with T
    'MAE': 50,
    'RMS': 50,
    'MSE': 5,
    'RMSE': 5,
    'ABSREL': 0.1,
    'EPE': 1,
    'FID': 25,
    'FVD': 100,
    'FAD': 10,
    'PSNR': 1 / 20,  # higher is better
    'SAD': 10, 
    'RTE': 0.5,
    'CD': 1,
    'MCD': 5,
    # without T
    'WER': None,
    'MS-SSIM': None,
    'MOS': None,
}

HIGHER_IS_BETTER = [
    'PSNR',
]

def map_function_for_special(metrix: str, score: float) -> float:
    """

    Score mapping function for special metrics.

    >>> metrix: metrix name, str, e.g., 'MAE'.

    >>> score: task score, float, e.g., 5.3.

    return: mapped scores, float.

    """
    metrix = metrix.upper()
    T = special_metric_dict[metrix]

    assert score > 0, f'score should be > 0, but found: {score}'
    
    if metrix in HIGHER_IS_BETTER:
        y = _tanh(T * score)
    elif metrix == 'WER':
        y = 1 - score
    elif metrix == 'MS-SSIM':
        y = (score + 1) / 2
    elif metrix == 'MOS':
        y = (score - 1) / 4
    else:  # lower is better
        y = _2_sigmoid_minus_1(T / score)

    return y * 100  # Convert to percentage scale

# β€’ Normalizing WER:
#   y = 1 βˆ’ x, where x ∈ [0, 1], y ∈ [0, 1].
# β€’ Normalizing MS-SSIM:
#   y = (x + 1) / 2 , where x ∈ [βˆ’1, 1], y ∈ [0, 1].
# β€’ Normalizing MOS:
#   y = x βˆ’ 1 / 4 , where x ∈ [1, 5], y ∈ [0, 1].

if __name__ == '__main__':
    r = random.random()
    print(f"{r = }")
    print(f"{_sigmoid(r) = }")
    print(f"{_2_sigmoid_minus_1(r) = }")
    print(f"{_tanh(r) = }")
    print(f"{_tanh(r / 2) = }")