-
Notifications
You must be signed in to change notification settings - Fork 2
/
losses.py
87 lines (70 loc) · 3.32 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models.vgg import vgg16
class Per_loss(torch.nn.Module):
def __init__(self, requires_grad=False):
super(Per_loss, self).__init__()
self.maeloss = torch.nn.L1Loss()
vgg = vgg16(pretrained=True).cuda()
vgg_pretrained_features = vgg.features
self.slice1 = torch.nn.Sequential()
self.slice2 = torch.nn.Sequential()
self.slice3 = torch.nn.Sequential()
for x in range(2):
self.slice1.add_module(str(x), vgg_pretrained_features[x])
for x in range(2, 4):
self.slice2.add_module(str(x), vgg_pretrained_features[x])
for x in range(4, 6):
self.slice3.add_module(str(x), vgg_pretrained_features[x])
if not requires_grad:
for param in self.parameters():
param.requires_grad = True
def forward(self, X, Y):
X = X.expand(-1, 3, -1, -1)
Y = Y.expand(-1, 3, -1, -1)
xx = self.slice1(X)
fx2 = xx
xx = self.slice2(xx)
fx4 = xx
xx = self.slice3(xx)
fx6 = xx
yy = self.slice1(Y)
fy2 = yy
yy = self.slice2(yy)
fy4 = yy
yy = self.slice3(yy)
fy6 = yy
loss_p = self.maeloss(fx2, fy2) + self.maeloss(fx4, fy4) + self.maeloss(fx6, fy6)
return loss_p
class Spa_loss(nn.Module):
def __init__(self):
super(Spa_loss, self).__init__()
kernel_left = torch.FloatTensor([[0, 0, 0], [-1, 1, 0], [0, 0, 0]]).cuda().unsqueeze(0).unsqueeze(0)
kernel_right = torch.FloatTensor([[0, 0, 0], [0, 1, -1], [0, 0, 0]]).cuda().unsqueeze(0).unsqueeze(0)
kernel_up = torch.FloatTensor([[0, -1, 0], [0, 1, 0], [0, 0, 0]]).cuda().unsqueeze(0).unsqueeze(0)
kernel_down = torch.FloatTensor([[0, 0, 0], [0, 1, 0], [0, -1, 0]]).cuda().unsqueeze(0).unsqueeze(0)
self.weight_left = nn.Parameter(data=kernel_left, requires_grad=False)
self.weight_right = nn.Parameter(data=kernel_right, requires_grad=False)
self.weight_up = nn.Parameter(data=kernel_up, requires_grad=False)
self.weight_down = nn.Parameter(data=kernel_down, requires_grad=False)
self.pool = nn.AvgPool2d(4)
def forward(self, org, enhance):
org_mean = torch.mean(org, 1, keepdim=True)
enhance_mean = torch.mean(enhance, 1, keepdim=True)
org_pool = self.pool(org_mean)
enhance_pool = self.pool(enhance_mean)
D_org_left = F.conv2d(org_pool, self.weight_left, padding=1)
D_org_right = F.conv2d(org_pool, self.weight_right, padding=1)
D_org_up = F.conv2d(org_pool, self.weight_up, padding=1)
D_org_down = F.conv2d(org_pool, self.weight_down, padding=1)
D_enhance_left = F.conv2d(enhance_pool, self.weight_left, padding=1)
D_enhance_right = F.conv2d(enhance_pool, self.weight_right, padding=1)
D_enhance_up = F.conv2d(enhance_pool, self.weight_up, padding=1)
D_enhance_down = F.conv2d(enhance_pool, self.weight_down, padding=1)
D_left = torch.pow(D_org_left - D_enhance_left, 2)
D_right = torch.pow(D_org_right - D_enhance_right, 2)
D_up = torch.pow(D_org_up - D_enhance_up, 2)
D_down = torch.pow(D_org_down - D_enhance_down, 2)
E = (D_left + D_right + D_up + D_down)
return E