-
Notifications
You must be signed in to change notification settings - Fork 261
/
loss.py
178 lines (144 loc) · 6.31 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
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
import torch
import torch.nn as nn
import torchvision
import utils
import matplotlib.pyplot as plt
import numpy as np
import random
import pytorch_batch_sinkhorn as spc
# Decide which device we want to run on
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class PixelLoss(nn.Module):
def __init__(self, p=1):
super(PixelLoss, self).__init__()
self.p = p
def forward(self, canvas, gt, ignore_color=False):
if ignore_color:
canvas = torch.mean(canvas, dim=1)
gt = torch.mean(gt, dim=1)
loss = torch.mean(torch.abs(canvas-gt)**self.p)
return loss
class VGGPerceptualLoss(torch.nn.Module):
def __init__(self, resize=True):
super(VGGPerceptualLoss, self).__init__()
vgg = torchvision.models.vgg16(pretrained=True).to(device)
blocks = []
blocks.append(vgg.features[:4].eval())
blocks.append(vgg.features[4:9].eval())
blocks.append(vgg.features[9:16].eval())
blocks.append(vgg.features[16:23].eval())
for bl in blocks:
for p in bl:
p.requires_grad = False
self.blocks = torch.nn.ModuleList(blocks)
self.transform = torch.nn.functional.interpolate
self.mean = torch.tensor([0.485, 0.456, 0.406]).view(1,3,1,1)
self.std = torch.tensor([0.229, 0.224, 0.225]).view(1,3,1,1)
self.resize = resize
def forward(self, input, target, ignore_color=False):
self.mean = self.mean.type_as(input)
self.std = self.std.type_as(input)
if ignore_color:
input = torch.mean(input, dim=1, keepdim=True)
target = torch.mean(target, dim=1, keepdim=True)
if input.shape[1] != 3:
input = input.repeat(1, 3, 1, 1)
target = target.repeat(1, 3, 1, 1)
input = (input-self.mean) / self.std
target = (target-self.mean) / self.std
if self.resize:
input = self.transform(input, mode='bilinear', size=(224, 224), align_corners=False)
target = self.transform(target, mode='bilinear', size=(224, 224), align_corners=False)
loss = 0.0
x = input
y = target
for block in self.blocks:
x = block(x)
y = block(y)
loss += torch.nn.functional.l1_loss(x, y)
return loss
class VGGStyleLoss(torch.nn.Module):
def __init__(self, transfer_mode, resize=True):
super(VGGStyleLoss, self).__init__()
vgg = torchvision.models.vgg16(pretrained=True).to(device)
for i, layer in enumerate(vgg.features):
if isinstance(layer, torch.nn.MaxPool2d):
vgg.features[i] = torch.nn.AvgPool2d(kernel_size=2, stride=2, padding=0)
blocks = []
if transfer_mode == 0: # transfer color only
blocks.append(vgg.features[:4].eval())
blocks.append(vgg.features[4:9].eval())
else: # transfer both color and texture
blocks.append(vgg.features[:4].eval())
blocks.append(vgg.features[4:9].eval())
blocks.append(vgg.features[9:16].eval())
blocks.append(vgg.features[16:23].eval())
for bl in blocks:
for p in bl:
p.requires_grad = False
self.blocks = torch.nn.ModuleList(blocks)
self.transform = torch.nn.functional.interpolate
self.mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1).to(device)
self.std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1).to(device)
self.resize = resize
def gram_matrix(self, y):
(b, ch, h, w) = y.size()
features = y.view(b, ch, w * h)
features_t = features.transpose(1, 2)
gram = features.bmm(features_t) / (ch * w * h)
return gram
def forward(self, input, target):
if input.shape[1] != 3:
input = input.repeat(1, 3, 1, 1)
target = target.repeat(1, 3, 1, 1)
input = (input - self.mean) / self.std
target = (target - self.mean) / self.std
if self.resize:
input = self.transform(input, mode='bilinear', size=(224, 224), align_corners=False)
target = self.transform(target, mode='bilinear', size=(224, 224), align_corners=False)
loss = 0.0
x = input
y = target
for block in self.blocks:
x = block(x)
y = block(y)
gm_x = self.gram_matrix(x)
gm_y = self.gram_matrix(y)
loss += torch.sum((gm_x-gm_y)**2)
return loss
class SinkhornLoss(nn.Module):
def __init__(self, epsilon=0.01, niter=5, normalize=False):
super(SinkhornLoss, self).__init__()
self.epsilon = epsilon
self.niter = niter
self.normalize = normalize
def _mesh_grids(self, batch_size, h, w):
a = torch.linspace(0.0, h - 1.0, h).to(device)
b = torch.linspace(0.0, w - 1.0, w).to(device)
y_grid = a.view(-1, 1).repeat(batch_size, 1, w) / h
x_grid = b.view(1, -1).repeat(batch_size, h, 1) / w
grids = torch.cat([y_grid.view(batch_size, -1, 1), x_grid.view(batch_size, -1, 1)], dim=-1)
return grids
def forward(self, canvas, gt):
batch_size, c, h, w = gt.shape
if h > 24:
canvas = nn.functional.interpolate(canvas, [24, 24], mode='area')
gt = nn.functional.interpolate(gt, [24, 24], mode='area')
batch_size, c, h, w = gt.shape
canvas_grids = self._mesh_grids(batch_size, h, w)
gt_grids = torch.clone(canvas_grids)
# randomly select a color channel, to speedup and consume memory
i = random.randint(0, 2)
img_1 = canvas[:, [i], :, :]
img_2 = gt[:, [i], :, :]
mass_x = img_1.reshape(batch_size, -1)
mass_y = img_2.reshape(batch_size, -1)
if self.normalize:
loss = spc.sinkhorn_normalized(
canvas_grids, gt_grids, epsilon=self.epsilon, niter=self.niter,
mass_x=mass_x, mass_y=mass_y)
else:
loss = spc.sinkhorn_loss(
canvas_grids, gt_grids, epsilon=self.epsilon, niter=self.niter,
mass_x=mass_x, mass_y=mass_y)
return loss