diff options
Diffstat (limited to 'look2hear/losses')
| -rw-r--r-- | look2hear/losses/__init__.py | 14 | ||||
| -rw-r--r-- | look2hear/losses/gan_losses.py | 58 | ||||
| -rw-r--r-- | look2hear/losses/matrix.py | 46 |
3 files changed, 118 insertions, 0 deletions
diff --git a/look2hear/losses/__init__.py b/look2hear/losses/__init__.py new file mode 100644 index 0000000..1ee6cda --- /dev/null +++ b/look2hear/losses/__init__.py @@ -0,0 +1,14 @@ +### +# Author: Kai Li +# Date: 2021-06-09 16:34:19 +# LastEditors: Kai Li +# LastEditTime: 2021-07-12 20:55:35 +### +from .gan_losses import MultiFrequencyDisLoss, MultiFrequencyGenLoss +from .matrix import MultiSrcNegSDR + +__all__ = [ + "MultiFrequencyDisLoss", + "MultiFrequencyGenLoss", + "MultiSrcNegSDR" +] diff --git a/look2hear/losses/gan_losses.py b/look2hear/losses/gan_losses.py new file mode 100644 index 0000000..d43db78 --- /dev/null +++ b/look2hear/losses/gan_losses.py @@ -0,0 +1,58 @@ +### +# Author: Kai Li +# Date: 2021-06-09 16:43:09 +# LastEditors: Please set LastEditors +# LastEditTime: 2024-01-24 00:00:52 +### + +import torch +from torch.nn.modules.loss import _Loss + +def freq_MAE(output, target): + loss = 0. + eps = torch.finfo(torch.float32).eps + all_win = [32, 64, 128, 256, 512, 1024, 2048] + for win in all_win: + est_spec = torch.stft(output.view(-1, output.shape[-1]), n_fft=win, hop_length=win//2, + window=torch.hann_window(win).to(output.device).float(), + return_complex=True) + target_spec = torch.stft(target.view(-1, target.shape[-1]), n_fft=win, hop_length=win//2, + window=torch.hann_window(win).to(target.device).float(), + return_complex=True) + + loss = loss + (est_spec.abs() - target_spec.abs()).abs().mean() / (target_spec.abs().mean() + eps) + + return loss / len(all_win) + +class MultiFrequencyDisLoss(_Loss): + def __init__(self, eps=1e-8): + super(MultiFrequencyDisLoss, self).__init__() + + def forward(self, target_outputs, est_outputs): + D_real = 0 + D_fake = 0 + for i in range(len(target_outputs)): + D_real = D_real + (target_outputs[i] - 1).pow(2).mean() / len(target_outputs) + D_fake = D_fake + (est_outputs[i]).pow(2).mean() / len(est_outputs) + return D_real + D_fake + +class MultiFrequencyGenLoss(_Loss): + def __init__(self, eps=1e-8): + super(MultiFrequencyGenLoss, self).__init__() + self.eps = eps + + def forward(self, est_outputs, est_feature_maps, targets_feature_maps, output, ori_data): + G_fake = 0 + feature_matching = 0 + eps = self.eps + + for i in range(len(est_outputs)): + G_fake = G_fake + (est_outputs[i] - 1).pow(2).mean() / len(est_outputs) + for j in range(len(est_feature_maps[i])): + feature_matching = feature_matching + (est_feature_maps[i][j] - targets_feature_maps[i][j].detach()).abs().mean() / (targets_feature_maps[i][j].detach().abs().mean() + eps) + + feature_matching = feature_matching / (len(est_outputs) * len(est_feature_maps[0])) + freq_loss = freq_MAE(output, ori_data.unsqueeze(1)) + total_loss = freq_loss + G_fake + feature_matching + + return total_loss diff --git a/look2hear/losses/matrix.py b/look2hear/losses/matrix.py new file mode 100644 index 0000000..1ce4ee5 --- /dev/null +++ b/look2hear/losses/matrix.py @@ -0,0 +1,46 @@ +import torch +from torch.nn.modules.loss import _Loss + +class MultiSrcNegSDR(_Loss): + def __init__(self, sdr_type, zero_mean=True, take_log=True, EPS=1e-8): + super().__init__() + + assert sdr_type in ["snr", "sisdr", "sdsdr"] + self.sdr_type = sdr_type + self.zero_mean = zero_mean + self.take_log = take_log + self.EPS = 1e-8 + + def forward(self, ests, targets): + if targets.size() != ests.size() or targets.ndim != 3: + raise TypeError( + f"Inputs must be of shape [batch, n_src, time], got {targets.size()} and {ests.size()} instead" + ) + # Step 1. Zero-mean norm + if self.zero_mean: + mean_source = torch.mean(targets, dim=2, keepdim=True) + mean_est = torch.mean(ests, dim=2, keepdim=True) + targets = targets - mean_source + ests = ests - mean_est + # Step 2. Pair-wise SI-SDR. + if self.sdr_type in ["sisdr", "sdsdr"]: + # [batch, n_src] + pair_wise_dot = torch.sum(ests * targets, dim=2, keepdim=True) + # [batch, n_src] + s_target_energy = torch.sum(targets ** 2, dim=2, keepdim=True) + self.EPS + # [batch, n_src, time] + scaled_targets = pair_wise_dot * targets / s_target_energy + else: + # [batch, n_src, time] + scaled_targets = targets + if self.sdr_type in ["sdsdr", "snr"]: + e_noise = ests - targets + else: + e_noise = ests - scaled_targets + # [batch, n_src] + pair_wise_sdr = torch.sum(scaled_targets ** 2, dim=2) / ( + torch.sum(e_noise ** 2, dim=2) + self.EPS + ) + if self.take_log: + pair_wise_sdr = 10 * torch.log10(pair_wise_sdr + self.EPS) + return -torch.mean(pair_wise_sdr, dim=-1).mean(0)
\ No newline at end of file |
