-
Notifications
You must be signed in to change notification settings - Fork 1
/
loss.py
142 lines (110 loc) · 4.28 KB
/
loss.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
import torch
import torch.nn as nn
import torch.nn.functional
import numpy as np
class Flow_Loss(nn.Module):
def __init__(self):
super().__init__()
def forward(self, gen_flows, gt_flows):
return torch.mean(torch.abs(gen_flows - gt_flows))
class Intensity_Loss(nn.Module):
def __init__(self):
super().__init__()
def forward(self, gen_frames, gt_frames):
return torch.mean(torch.abs((gen_frames - gt_frames) ** 2))
class Intensity_5frame_Loss(nn.Module):
def __init__(self):
super().__init__()
self.loss = Intensity_Loss().cuda()
def forward(self, gen_frames, gt_frames):
intels = []
for i in range(5):
gt = gt_frames[:, i*3:(i+1)*3, :, :]
gen = gen_frames[:, i*3:(i+1)*3, :, :]
intel = self.loss(gen,gt)
intels.append(intel)
return sum(intels)/len(intels)
class Gradient_Loss(nn.Module):
def __init__(self, channels):
super().__init__()
pos = torch.from_numpy(np.identity(channels, dtype=np.float32))
neg = -1 * pos
# Note: when doing conv2d, the channel order is different from tensorflow, so do permutation.
self.filter_x = torch.stack((neg, pos)).unsqueeze(0).permute(3, 2, 0, 1).cuda()
self.filter_y = torch.stack((pos.unsqueeze(0), neg.unsqueeze(0))).permute(3, 2, 0, 1).cuda()
def forward(self, gen_frames, gt_frames):
# Do padding to match the result of the original tensorflow implementation
gen_frames_x = nn.functional.pad(gen_frames, [0, 1, 0, 0])
gen_frames_y = nn.functional.pad(gen_frames, [0, 0, 0, 1])
gt_frames_x = nn.functional.pad(gt_frames, [0, 1, 0, 0])
gt_frames_y = nn.functional.pad(gt_frames, [0, 0, 0, 1])
gen_dx = torch.abs(nn.functional.conv2d(gen_frames_x, self.filter_x))
gen_dy = torch.abs(nn.functional.conv2d(gen_frames_y, self.filter_y))
gt_dx = torch.abs(nn.functional.conv2d(gt_frames_x, self.filter_x))
gt_dy = torch.abs(nn.functional.conv2d(gt_frames_y, self.filter_y))
grad_diff_x = torch.abs(gt_dx - gen_dx)
grad_diff_y = torch.abs(gt_dy - gen_dy)
return torch.mean(grad_diff_x + grad_diff_y)
# ??????
class Adversarial_Loss(nn.Module):
def __init__(self):
super().__init__()
def forward(self, fake_outputs):
# TODO: compare with torch.nn.MSELoss ?
return torch.mean((fake_outputs - 1) ** 2 / 2)
# ??????
class Discriminate_Loss(nn.Module):
def __init__(self):
super().__init__()
def forward(self, real_outputs, fake_outputs):
return torch.mean((real_outputs - 1) ** 2 / 2) + torch.mean(fake_outputs ** 2 / 2)
# from __future__ import absolute_import, print_function
import torch
from torch import nn
def feature_map_permute(input):
s = input.data.shape
l = len(s)
# permute feature channel to the last:
# NxCxDxHxW --> NxDxHxW x C
if l == 2:
x = input # NxC
elif l == 3:
x = input.permute(0, 2, 1)
elif l == 4:
x = input.permute(0, 2, 3, 1)
elif l == 5:
x = input.permute(0, 2, 3, 4, 1)
else:
x = []
print('wrong feature map size')
x = x.contiguous()
# NxDxHxW x C --> (NxDxHxW) x C
x = x.view(-1, s[1])
return x
class EntropyLoss(nn.Module):
def __init__(self, eps = 1e-12):
super(EntropyLoss, self).__init__()
self.eps = eps
def forward(self, x):
b = x * torch.log(x + self.eps)
b = -1.0 * b.sum(dim=1)
b = b.mean()
return b
class EntropyLossEncap(nn.Module):
def __init__(self, eps = 1e-12):
super(EntropyLossEncap, self).__init__()
self.eps = eps
self.entropy_loss = EntropyLoss(eps)
def forward(self, input):
score = feature_map_permute(input)
ent_loss_val = self.entropy_loss(score)
return ent_loss_val
def cosineSimilarity(tensor_1, tensor_2):
normalized_tensor_1 = tensor_1 / tensor_1.norm(dim=-1, keepdim=True)
normalized_tensor_2 = tensor_2 / tensor_2.norm(dim=-1, keepdim=True)
return torch.mean(1-(normalized_tensor_1 * normalized_tensor_2).sum(dim=-1))
if __name__ == '__main__':
a = torch.rand(1,512,32,32).cuda()
b = torch.rand(1,512,32,32).cuda()
net = cosineSimilarity(a,b)
print(net)