-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathloss.py
More file actions
74 lines (55 loc) · 1.91 KB
/
Copy pathloss.py
File metadata and controls
74 lines (55 loc) · 1.91 KB
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
import enum
from abc import ABC, abstractmethod
import torch
import torch.nn.functional as F
from einops import rearrange
from einops.layers.torch import Rearrange
from modules.functional import STFT, ToMagnitude, FFT2d, ToSTFT
from modules.seq import Seq
import torch.nn as nn
class LossInterface(ABC):
@abstractmethod
def calculate(self, prediction, target):
pass
class LossType(enum.Enum):
MASKED_MSE = "masked_mse"
STFT_RMSE = "stft_rmse"
class StftRmseLoss(LossInterface):
def __init__(self):
self.stft = Seq(
Rearrange("b n c t -> (b n) c t"),
ToSTFT(),
)
def calculate(self, prediction, target):
prediction = self.stft(prediction)
target = self.stft(target)
loss = F.mse_loss(prediction, target)
return loss.sqrt() * 1000
class EasyMiningMSE(LossInterface):
def __init__(self, q=0.5):
self.q = q
def apply_mask(self, loss):
batch_size = loss.shape[0]
loss_flat = loss.view(batch_size, -1)
quantiles = torch.quantile(loss_flat.detach(), self.q, dim=1, keepdim=True)
mask = loss_flat < quantiles
masked_losses = []
for i in range(batch_size):
if mask[i].sum() == 0:
masked_losses.append(loss_flat[i].mean())
else:
masked_losses.append(loss_flat[i][mask[i]].mean())
return torch.stack(masked_losses).mean()
def calculate(self, prediction, target):
loss = torch.nn.MSELoss(reduction='none')(prediction, target)
masked_loss = self.apply_mask(loss)
return masked_loss
class LossFactory:
@staticmethod
def create(loss_type):
if loss_type == LossType.MASKED_MSE:
return EasyMiningMSE()
elif loss_type == LossType.STFT_RMSE:
return StftRmseLoss()
else:
raise ValueError(f"Unsupported loss type: {loss_type}")