-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloss.py
139 lines (102 loc) · 3.89 KB
/
loss.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import torch
#-------------------------------
#RI-MAG
#-------------------------------
class LossRIMAG:
def __init__(self, config):
self.loss_f = LossF(config)
self.loss_ri = LossRI(config)
def __call__(self, x_lr, x_sr, x_hr):
return self.loss_f(x_sr, x_hr) + self.loss_ri(x_sr, x_hr)
#-------------------------------
#TF Loss
#-------------------------------
class LossTF:
def __init__(self, config):
self.loss_t = LossT(config)
self.loss_f = LossF(config)
self.tf_alpha = config['loss']['tf_alpha']
def __call__(self, x_lr, x_sr, x_hr):
return self.tf_alpha*self.loss_t(x_sr, x_hr) + (1-self.tf_alpha)*self.loss_f(x_sr, x_hr)
#-------------------------------
#T-PCM Loss
#-------------------------------
class LossTPCM:
def __init__(self, config):
self.loss_t = LossT(config)
self.loss_pcm = LossPCM(config)
self.tpcm_beta = config['loss']['tpcm_beta']
def __call__(self, x_lr, x_sr, x_hr):
return self.tpcm_beta * self.loss_t(x_sr, x_hr) + (1-self.tpcm_beta)*self.loss_pcm(x_lr, x_sr, x_hr)
#-------------------------------
#Ingredient Loss
#-------------------------------
#T Loss
class LossT:
def __init__(self, config):
pass
def __call__(self, x_sr, x_hr):
return torch.mean(torch.abs(x_sr - x_hr))
#F Loss
class LossF:
def __init__(self, config):
self.stft_mag = stft_mag(
nfft = config['loss']['window_size'],
window_size = config['loss']['window_size'],
hop_size = config['loss']['hop_size']
)
def __call__(self, x_sr, x_hr):
total_num = x_sr.shape[0]
total_loss = 0
for idx in range(total_num):
x_noisy = x_sr[idx]
x_target = x_hr[idx]
loss = torch.mean(torch.abs(self.stft_mag(x_target) - self.stft_mag(x_noisy)))
total_loss += loss
return total_loss/total_num
class stft_mag:
def __init__(self, nfft, window_size, hop_size):
self.nfft = nfft
self.window_size = window_size
self.hop_size = hop_size
def __call__(self, x):
window = torch.hann_window(self.window_size).to(x.device)
x_stft = torch.stft(x, n_fft = self.nfft, hop_length=self.hop_size, win_length=self.window_size,
window = window, return_complex=True)
return abs(x_stft)
#RI loss
class LossRI:
def __init__(self, config):
self.stft_risum = stft_RIsum(
nfft = config['loss']['window_size'],
window_size = config['loss']['window_size'],
hop_size = config['loss']['hop_size']
)
def __call__(self, x_sr, x_hr):
total_num = x_sr.shape[0]
total_loss = 0
for idx in range(total_num):
x_noisy = x_sr[idx]
x_target = x_hr[idx]
loss = torch.mean(torch.abs(self.stft_risum(x_target) - self.stft_risum(x_noisy)))
total_loss+=loss
return total_loss/total_num
class stft_RIsum:
def __init__(self, nfft, window_size, hop_size):
self.nfft = nfft
self.window_size = window_size
self.hop_size = hop_size
def __call__(self, x):
window = torch.hann_window(self.window_size).to(x.device)
x_stft = torch.stft(x, n_fft = self.nfft, hop_length=self.hop_size, win_length=self.window_size,
window = window, return_complex=True)
real = x_stft[...,0]
imag = x_stft[...,1]
return torch.abs(real) + torch.abs(imag)
#PCM Loss
class LossPCM:
def __init__(self, config):
self.loss_f1 = LossF(config)
self.loss_f2 = LossF(config)
def __call__(self, x_lr, x_sr, x_hr):
return self.loss_f1(x_sr, x_hr) + self.loss_f2(x_sr - x_lr, x_hr - x_lr)