-
Notifications
You must be signed in to change notification settings - Fork 1
/
losses.py
executable file
·90 lines (65 loc) · 2.71 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
import torch
import torch.nn.functional as F
AVAILABLE_LOSSES = ["hinge", "dcgan"]
def dis_hinge(dis_fake, dis_real):
loss = torch.mean(torch.relu(1. - dis_real)) +\
torch.mean(torch.relu(1. + dis_fake))
return loss
def gen_hinge(dis_fake, dis_real=None):
return -torch.mean(dis_fake)
def dis_dcgan(dis_fake, dis_real):
loss = torch.mean(F.softplus(-dis_real)) + torch.mean(F.softplus(dis_fake))
return loss
def gen_dcgan(dis_fake, dis_real=None):
return torch.mean(F.softplus(-dis_fake))
class _Loss(object):
"""GAN Loss base class.
Args:
loss_type (str)
is_relativistic (bool)
"""
def __init__(self, loss_type, is_relativistic=False):
assert loss_type in AVAILABLE_LOSSES, "Invalid loss. Choose from {}".format(AVAILABLE_LOSSES)
self.loss_type = loss_type
self.is_relativistic = is_relativistic
def _preprocess(self, dis_fake, dis_real):
C_xf_tilde = torch.mean(dis_fake, dim=0, keepdim=True).expand_as(dis_fake)
C_xr_tilde = torch.mean(dis_real, dim=0, keepdim=True).expand_as(dis_real)
return dis_fake - C_xr_tilde, dis_real - C_xf_tilde
class DisLoss(_Loss):
"""Discriminator Loss."""
def __call__(self, dis_fake, dis_real, **kwargs):
if not self.is_relativistic:
if self.loss_type == "hinge":
return dis_hinge(dis_fake, dis_real)
elif self.loss_type == "dcgan":
return dis_dcgan(dis_fake, dis_real)
else:
d_xf, d_xr = self._preprocess(dis_fake, dis_real)
if self.loss_type == "hinge":
return dis_hinge(d_xf, d_xr)
elif self.loss_type == "dcgan":
D_xf = torch.sigmoid(d_xf)
D_xr = torch.sigmoid(d_xr)
return -torch.log(D_xr) - torch.log(1.0 - D_xf)
else:
raise NotImplementedError
class GenLoss(_Loss):
"""Generator Loss."""
def __call__(self, dis_fake, dis_real=None, **kwargs):
if not self.is_relativistic:
if self.loss_type == "hinge":
return gen_hinge(dis_fake, dis_real)
elif self.loss_type == "dcgan":
return gen_dcgan(dis_fake, dis_real)
else:
assert dis_real is not None, "Relativistic Generator loss requires `dis_real`."
d_xf, d_xr = self._preprocess(dis_fake, dis_real)
if self.loss_type == "hinge":
return dis_hinge(d_xr, d_xf)
elif self.loss_type == "dcgan":
D_xf = torch.sigmoid(d_xf)
D_xr = torch.sigmoid(d_xr)
return -torch.log(D_xf) - torch.log(1.0 - D_xr)
else:
raise NotImplementedError