-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdfloss.py
49 lines (37 loc) · 1.38 KB
/
mdfloss.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
import torch
import torch.nn as nn
class MDFLoss(nn.Module):
def __init__(self, saved_ds_path, cuda_available = True):
super(MDFLoss, self).__init__()
if cuda_available:
self.Ds = torch.load(saved_ds_path)
else:
self.Ds = torch.load(saved_ds_path, map_location=torch.device('cpu'))
self.num_discs = len(self.Ds)
def forward(self, x, y, num_scales=8, is_ascending=1):
# Get batch_size
batch_size = x.shape[0]
# Initialize loss vector
loss = torch.zeros([batch_size]).to(x.device)
# For every scale
for scale_idx in range(num_scales):
# Reverse if required
if is_ascending:
scale = scale_idx
else:
scale = self.num_discs - 1 - scale_idx
# Choose discriminator
D = self.Ds[scale]
# Get discriminator activations
pxs = D(x, is_loss=True)
pys = D(y, is_loss=True)
# For every layer in the output
for idx in range(len(pxs)):
# Compute L2 between representations
l2 = (pxs[idx] - pys[idx])**2
l2 = torch.mean(l2, dim=(1, 2, 3))
# Add current difference to the loss
loss += l2
# Mean loss
loss = torch.mean(loss)
return loss