-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
81 lines (64 loc) · 2.79 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
import torch
import torch.nn as nn
import torch.nn.functional as F
def tv_loss(x, beta = 0.5, reg_coeff = 5):
'''Calculates TV loss for an image `x`.
Args:
x: image, torch.Variable of torch.Tensor
beta: See https://arxiv.org/abs/1412.0035 (fig. 2) to see effect of `beta`
'''
dh = torch.pow(x[:,:,:,1:] - x[:,:,:,:-1], 2)
dw = torch.pow(x[:,:,1:,:] - x[:,:,:-1,:], 2)
a,b,c,d=x.shape
return reg_coeff*(torch.sum(torch.pow(dh[:, :, :-1] + dw[:, :, :, :-1], beta))/(a*b*c*d))
class TVLoss(nn.Module):
def __init__(self, tv_loss_weight=1):
super(TVLoss, self).__init__()
self.tv_loss_weight = tv_loss_weight
def forward(self, x):
batch_size = x.size()[0]
h_x = x.size()[2]
w_x = x.size()[3]
count_h = self.tensor_size(x[:, :, 1:, :])
count_w = self.tensor_size(x[:, :, :, 1:])
h_tv = torch.pow((x[:, :, 1:, :] - x[:, :, :h_x - 1, :]), 2).sum()
w_tv = torch.pow((x[:, :, :, 1:] - x[:, :, :, :w_x - 1]), 2).sum()
return self.tv_loss_weight * 2 * (h_tv / count_h + w_tv / count_w) / batch_size
@staticmethod
def tensor_size(t):
return t.size()[1] * t.size()[2] * t.size()[3]
class CharbonnierLoss(nn.Module):
"""Charbonnier Loss (L1)"""
def __init__(self, eps=1e-3):
super(CharbonnierLoss, self).__init__()
self.eps = eps
def forward(self, x, y):
diff = x - y
# loss = torch.sum(torch.sqrt(diff * diff + self.eps))
loss = torch.mean(torch.sqrt((diff * diff) + (self.eps*self.eps)))
return loss
class GWLoss(nn.Module):
def __init__(self, rgb_range=1.):
super(GWLoss, self).__init__()
self.rgb_range=rgb_range
def forward(self, x1, x2):
x1=torch.clamp(x1,min=0.0,max=1.0)
x2=torch.clamp(x2,min=0.0,max=1.0)
sobel_x = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]
sobel_y = [[-1, -2, -1], [0, 0, 0], [1, 2, 1]]
b, c, w, h = x1.shape
sobel_x = torch.FloatTensor(sobel_x).expand(c, 1, 3, 3)
sobel_y = torch.FloatTensor(sobel_y).expand(c, 1, 3, 3)
sobel_x = sobel_x.type_as(x1)
sobel_y = sobel_y.type_as(x1)
weight_x = nn.Parameter(data=sobel_x, requires_grad=False)
weight_y = nn.Parameter(data=sobel_y, requires_grad=False)
Ix1 = F.conv2d(x1, weight_x, stride=1, padding=1, groups=c)
Ix2 = F.conv2d(x2, weight_x, stride=1, padding=1, groups=c)
Iy1 = F.conv2d(x1, weight_y, stride=1, padding=1, groups=c)
Iy2 = F.conv2d(x2, weight_y, stride=1, padding=1, groups=c)
dx = torch.abs(Ix1 - Ix2)
dy = torch.abs(Iy1 - Iy2)
# loss = torch.exp(2*(dx + dy)) * torch.abs(x1 - x2)
loss = (1 + 4 * dx) * (1 + 4 * dy) * torch.abs(x1 - x2)
return torch.mean(loss)