-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
190 lines (138 loc) · 7.56 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Edited from Akanimax code https://github.com/akanimax/T2F
"""
This file also has some unused methods that I tried and may again use later.
"""
import torch as th
import numpy as np
from torch.nn.functional import upsample, softplus
from torch.nn import ModuleList, Upsample, Conv2d, AvgPool2d
class ConditionalGANLoss:
""" Base class for all losses """
def __init__(self, device, dis):
self.device = device
self.dis = dis
def dis_loss(self, real_samps, fake_samps, latent_vector, height, alpha):
raise NotImplementedError("dis_loss method has not been implemented")
def gen_loss(self, real_samps, fake_samps, latent_vector, height, alpha):
raise NotImplementedError("gen_loss method has not been implemented")
class CondWGAN_GP(ConditionalGANLoss):
def __init__(self, device, dis, drift=0.001, use_gp=False):
super().__init__(device, dis)
self.drift = drift
self.use_gp = use_gp
def __gradient_penalty(self, real_samps, fake_samps, latent_vector,
height, alpha, reg_lambda=10):
"""
private helper for calculating the gradient penalty
:param real_samps: real samples
:param fake_samps: fake samples
:param latent_vector: used for conditional loss calculation
:param height: current depth in the optimization
:param alpha: current alpha for fade-in
:param reg_lambda: regularisation lambda
:return: tensor (gradient penalty)
"""
from torch.autograd import grad
batch_size = real_samps.shape[0]
# generate random epsilon
epsilon = th.rand((batch_size, 1, 1, 1)).to(self.device)
# create the merge of both real and fake samples
merged = (epsilon * real_samps) + ((1 - epsilon) * fake_samps)
# forward pass
op, logits = self.dis.forward(merged, latent_vector, height, alpha)
# obtain gradient of op wrt. merged
gradient = grad(outputs=op, inputs=merged, create_graph=True, grad_outputs=th.ones_like(op), retain_graph=True, only_inputs=True)[0]
# calculate the penalty using these gradients
penalty = reg_lambda * ((gradient.norm(p=2, dim=1) - 1) ** 2).mean()
# return the calculated penalty:
#print("losses.py penalty:", penalty)
return penalty
def dis_loss(self, real_samps, fake_samps, latent_vector, height, alpha):
# define the (Wasserstein) loss
fake_out, _ = self.dis(fake_samps, latent_vector, height, alpha)
real_out, _ = self.dis(real_samps, latent_vector, height, alpha)
#print(fake_out)
loss = (th.mean(fake_out) - th.mean(real_out) + (self.drift * th.mean(real_out ** 2)))
if self.use_gp:
# calculate the WGAN-GP (gradient penalty)
fake_samps.requires_grad = True # turn on gradients for penalty calculation
gp = self.__gradient_penalty(real_samps, fake_samps, latent_vector, height, alpha)
loss += gp
return loss
def dis_loss_uluc_no_mad(self, real_samps, fake_samps, latent_vector, height, alpha):
real_out, _ = self.dis(real_samps, latent_vector, height, alpha)
real_out = softplus(-real_out).mean()
real_out.backward(retain_graph=True)
grad_real = th.autograd.grad(outputs=real_out.sum(), inputs=real_samps, create_graph=True)[0] # outputs takes loss, inputs takes images
grad_penalty_real = (grad_real.view(grad_real.size(0), -1).norm(2, dim=1) ** 2).mean()
grad_penalty_real = 10 / 2 * grad_penalty_real
grad_penalty_real.backward()
fake_out, _ = self.dis(fake_samps, latent_vector, height, alpha)
fake_out = softplus(fake_out).mean()
fake_out.backward()
loss = real_out + fake_out
return loss
def dis_loss_uluc(self, real_samps, fake_samps, latent_vector, mis_match_embed, height, alpha):
# define the (Wasserstein) loss
real_out, _ = self.dis(real_samps, latent_vector, height, alpha)
real_out = softplus(-real_out).mean()
real_out.backward(retain_graph=True)
grad_real = th.autograd.grad(outputs=real_out.sum(), inputs=real_samps, create_graph=True)[0] # outputs takes loss, inputs takes images
grad_penalty_real = (grad_real.view(grad_real.size(0), -1).norm(2, dim=1) ** 2).mean()
grad_penalty_real = 10 / 2 * grad_penalty_real
grad_penalty_real.backward()
fake_out, _ = self.dis(fake_samps, latent_vector, height, alpha)
fake_out = softplus(fake_out).mean()
fake_out.backward()
mis_match_out, _ = self.dis(real_samps, mis_match_embed, height, alpha)
mis_match_out = softplus(mis_match_out).mean()
mis_match_out.backward()
#loss = (th.mean(mis_match_out) + th.mean(fake_out) - th.mean(real_out) + (self.drift * th.mean(real_out ** 2)))
# if self.use_gp:
# # calculate the WGAN-GP (gradient penalty)
# fake_samps.requires_grad = True # turn on gradients for penalty calculation
# gp = self.__gradient_penalty(real_samps, fake_samps, latent_vector, height, alpha)
# loss += gp
loss = real_out + fake_out + mis_match_out
return loss
def dis_loss_uluc_2(self, real_samps, fake_samps, latent_vector, mis_match_embed, height, alpha):
# define the (Wasserstein) loss
real_out, _ = self.dis(real_samps, latent_vector, height, alpha)
real_out = softplus(-real_out).mean()
real_out.backward(retain_graph=True)
grad_real = th.autograd.grad(outputs=real_out.sum(), inputs=real_samps, create_graph=True)[0] # outputs takes loss, inputs takes images
grad_penalty_real = (grad_real.view(grad_real.size(0), -1).norm(2, dim=1) ** 2).mean()
grad_penalty_real = 10 / 2 * grad_penalty_real
grad_penalty_real.backward()
fake_out, _ = self.dis(fake_samps, latent_vector, height, alpha)
fake_out = softplus(fake_out).mean()
fake_out.backward()
mis_match_out, _ = self.dis(real_samps, mis_match_embed, height, alpha)
mis_match_out = softplus(mis_match_out).mean()
mis_match_out.backward()
loss = real_out + fake_out + mis_match_out + grad_real
return loss
def gen_loss_uluc(self, _, fake_samps, latent_vector, height, alpha, retain_graph=False):
# calculate the WGAN loss for generator
dis_output, _ = self.dis(fake_samps, latent_vector, height, alpha)
dis_output = softplus(-dis_output).mean()
dis_output.backward(retain_graph=retain_graph)
# print("dis_output, loss", dis_output, loss)
return dis_output
def gen_loss_uluc_2(self, _, fake_samps, latent_vector, mismatched_latent_vector, height, alpha, retain_graph=False):
# calculate the WGAN loss for generator
dis_output, _ = self.dis(fake_samps, latent_vector, height, alpha)
dis_output = softplus(-dis_output).mean()
dis_output.backward(retain_graph=True)
mismatch_dis_output, _ = self.dis(fake_samps, mismatched_latent_vector, height, alpha)
mismatch_dis_output = softplus(-mismatch_dis_output).mean()
mismatch_dis_output.backward(retain_graph=False)
g_loss = (dis_output + mismatch_dis_output)
# g_loss.backward(retain_graph=retain_graph)
return g_loss
def gen_loss(self, _, fake_samps, latent_vector, height, alpha):
# calculate the WGAN loss for generator
dis_output, _ = self.dis(fake_samps, latent_vector, height, alpha)
loss = -th.mean(dis_output)
#print("dis_output, loss", dis_output, loss)
return loss