-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlosses.py
61 lines (40 loc) · 1.26 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from piq import LPIPS, DISTS
class L1Loss(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x_pred, x):
return (x_pred - x).abs().mean()
class L2Loss(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x_pred, x):
return ((x_pred - x) ** 2).mean()
class LPIPSLoss(nn.Module):
def __init__(self):
super().__init__()
self.loss = LPIPS()
def forward(self, x_pred, x):
x_pred = F.interpolate(x_pred, size=224, mode="bilinear")
x = F.interpolate(x.float(), size=224, mode="bilinear")
x_pred = (x_pred + 1) / 2
x = (x + 1) / 2
return self.loss(x_pred, x)
class DISTSLoss(nn.Module):
def __init__(self):
super().__init__()
self.loss = DISTS()
def forward(self, x_pred, x):
x_pred = F.interpolate(x_pred, size=224, mode="bilinear")
x = F.interpolate(x.float(), size=224, mode="bilinear")
x_pred = (x_pred + 1) / 2
x = (x + 1) / 2
return self.loss(x_pred, x)
loss_dict = {
'l1': L1Loss,
'l2': L2Loss,
'lpips': LPIPSLoss,
'dists': DISTSLoss
}