-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
51 lines (38 loc) · 1.66 KB
/
utils.py
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
import numpy as np
from scipy.optimize import curve_fit
from scipy import stats
import torch
def logistic_func(X, bayta1, bayta2, bayta3, bayta4):
logisticPart = 1 + np.exp(np.negative(np.divide(X - bayta3, np.abs(bayta4))))
yhat = bayta2 + np.divide(bayta1 - bayta2, logisticPart)
return yhat
def fit_function(y_label, y_output):
beta = [np.max(y_label), np.min(y_label), np.mean(y_output), 0.5]
popt, _ = curve_fit(logistic_func, y_output, \
y_label, p0=beta, maxfev=100000000)
y_output_logistic = logistic_func(y_output, *popt)
return y_output_logistic
def fit_function_regression_values(y_label, y_output):
beta = [np.max(y_label), np.min(y_label), np.mean(y_output), 0.5]
popt, _ = curve_fit(logistic_func, y_output, \
y_label, p0=beta, maxfev=100000000)
y_output_logistic = logistic_func(y_output, *popt)
return y_output_logistic, popt
def performance_fit(y_label, y_output):
y_output_logistic, popt = fit_function_regression_values(y_label, y_output)
PLCC = stats.pearsonr(y_output_logistic, y_label)[0]
SRCC = stats.spearmanr(y_output, y_label)[0]
KRCC = stats.stats.kendalltau(y_output, y_label)[0]
RMSE = np.sqrt(((y_output_logistic-y_label) ** 2).mean())
MAE = np.absolute((y_output_logistic-y_label)).mean()
return PLCC, SRCC, KRCC, RMSE, MAE, popt
EPS = 1e-2
esp = 1e-8
class Fidelity_Loss(torch.nn.Module):
def __init__(self):
super(Fidelity_Loss, self).__init__()
def forward(self, p, g):
g = g.view(-1, 1)
p = p.view(-1, 1)
loss = 1 - (torch.sqrt(p * g + esp) + torch.sqrt((1 - p) * (1 - g) + esp))
return torch.mean(loss)