forked from facebookresearch/hyperreel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
165 lines (116 loc) · 4.22 KB
/
losses.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import torch
from torch import nn
class HuberLoss(nn.Module):
def __init__(self, cfg):
super().__init__()
self.loss = nn.HuberLoss(reduction='mean', delta=cfg.delta if 'delta' in cfg else 1.0)
def forward(self, inputs, targets, **kwargs):
loss = self.loss(inputs, targets)
return loss
class MSELoss(nn.Module):
def __init__(self, *args):
super().__init__()
self.loss = nn.MSELoss(reduction='mean')
def forward(self, inputs, targets, **kwargs):
loss = self.loss(inputs, targets)
return loss
class HuberLoss(nn.Module):
def __init__(self, cfg):
super().__init__()
self.loss = nn.HuberLoss(reduction='mean', delta=cfg.delta)
def forward(self, inputs, targets, **kwargs):
loss = self.loss(inputs, targets)
return loss
class WeightedMSELoss(nn.Module):
def __init__(self, *args):
super().__init__()
def forward(self, inputs, targets, **kwargs):
if 'weight' in kwargs:
weight = kwargs['weight']
else:
weight = 1.0
return torch.mean(weight * torch.square(inputs - targets))
class MAELoss(nn.Module):
def __init__(self, *args):
super().__init__()
self.loss = nn.L1Loss(reduction='mean')
def forward(self, inputs, targets, **kwargs):
loss = self.loss(inputs, targets)
return loss
class WeightedMAELoss(nn.Module):
def __init__(self, *args):
super().__init__()
def forward(self, inputs, targets, **kwargs):
if 'weight' in kwargs:
weight = kwargs['weight']
else:
weight = 1.0
return torch.mean(weight * torch.abs(inputs - targets))
class TVLoss(nn.Module):
def __init__(self, *args):
super().__init__()
def forward(self, inputs, targets):
return torch.sqrt(torch.square(inputs - targets).sum(-1) + 1e-8).mean()
class ComplexMSELoss(nn.Module):
def __init__(self, *args):
super().__init__()
self.loss = nn.MSELoss(reduction='mean')
def forward(self, inputs, targets):
loss = self.loss(torch.real(inputs), torch.real(targets))
loss += self.loss(torch.imag(inputs), torch.imag(targets))
return loss
class ComplexMAELoss(nn.Module):
def __init__(self, *args):
super().__init__()
self.loss = nn.L1Loss(reduction='mean')
def forward(self, inputs, targets):
loss = self.loss(torch.real(inputs), torch.real(targets))
loss += self.loss(torch.imag(inputs), torch.imag(targets))
return loss
class MSETopN(nn.Module):
def __init__(self, cfg):
super().__init__()
self.frac = cfg.frac
self.loss = nn.MSELoss(reduction='mean')
def forward(self, inputs, targets):
diff = torch.abs(inputs - targets)
n = int(self.frac * targets.shape[0])
idx = torch.argsort(diff, dim=0)
targets_sorted = torch.gather(targets, 0, idx)
targets_sorted = targets_sorted[:n]
inputs_sorted = torch.gather(inputs, 0, idx)
inputs_sorted = inputs_sorted[:n]
loss = self.loss(inputs_sorted, targets_sorted)
return loss
class MAETopN(nn.Module):
def __init__(self, cfg):
super().__init__()
self.frac = cfg.frac
self.loss = nn.L1Loss(reduction='mean')
def forward(self, inputs, targets):
diff = torch.abs(inputs - targets)
n = int(self.frac * targets.shape[0])
idx = torch.argsort(diff, dim=0)
targets_sorted = torch.gather(targets, 0, idx)
targets_sorted = targets_sorted[:n]
inputs_sorted = torch.gather(inputs, 0, idx)
inputs_sorted = inputs_sorted[:n]
loss = self.loss(inputs_sorted, targets_sorted)
return loss
loss_dict = {
'huber': HuberLoss,
'mse': MSELoss,
'weighted_mse': WeightedMSELoss,
'mae': MAELoss,
'weighted_mae': WeightedMAELoss,
'tv': TVLoss,
'complex_mse': ComplexMSELoss,
'complex_mae': ComplexMAELoss,
'mse_top_n': MSETopN,
'mae_top_n': MAETopN,
}