-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.py
More file actions
185 lines (163 loc) · 6.01 KB
/
transfer.py
File metadata and controls
185 lines (163 loc) · 6.01 KB
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
179
180
181
182
183
184
185
import numpy as np
import copy
from PIL import Image
import matplotlib.pyplot as plt
from torchvision import transforms
import torchvision.models as models
from torch.autograd import Variable
import torch.nn as nn
import torch.optim as optim
import torch
use_cuda = torch.cuda.is_available()
dtype = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
def preprocess_imgs(style, content, reference):
output_size = (min(style.size[1], content.size[1]),
min(style.size[0], content.size[0]))
loader = transforms.Compose([transforms.Resize(output_size),
transforms.ToTensor()])
style_tensor = Variable(loader(style))
content_tensor = Variable(loader(content))
if reference is not None:
reference_tensor = Variable(loader(reference)).unsqueeze(0)
else:
reference_tensor = None
return style_tensor.unsqueeze(0), content_tensor.unsqueeze(0), reference_tensor
class ContentLoss(nn.Module):
def __init__(self, gt, weight):
super(ContentLoss, self).__init__()
self.gt = gt.detach() * weight
self.weight = weight
def forward(self, x):
self.loss = nn.MSELoss()(x * self.weight, self.gt)
return x
def backward(self, retain_graph=True):
self.loss.backward(retain_graph=retain_graph)
return self.loss
def get_gram_matrix(x):
N, C, W, H = x.shape
G = x.view(N * C, W * H)
return torch.mm(G, G.t()).div(N * C * W * H)
class StyleLoss(nn.Module):
def __init__(self, gt, weight):
super(StyleLoss, self).__init__()
self.gt = gt.detach() * weight
self.weight = weight
def forward(self, x):
x_copy = x.clone()
gram_matrix = get_gram_matrix(x).mul_(self.weight)
self.loss = nn.MSELoss()(gram_matrix, self.gt)
return x_copy
def backward(self, retain_graph=True):
self.loss.backward(retain_graph=retain_graph)
return self.loss
class Normalization(nn.Module):
def forward(self, img):
mean = torch.tensor([0.485, 0.456, 0.406]).view(-1, 1, 1)
std = torch.tensor([0.229, 0.224, 0.225]).view(-1, 1, 1)
if torch.cuda.is_available():
mean = mean.to("cuda")
std = std.to("cuda")
return (img - mean) / std
vgg = models.vgg19(pretrained=True).features
if torch.cuda.is_available():
vgg = vgg.to("cuda")
vgg.eval()
default_content_layers = ["conv4_2"]
default_style_layers = ["conv1_1", "conv2_1", "conv3_1", "conv4_1", "conv5_1"]
default_ref_layers = ["conv4_2"]
def get_model(content, style, reference,
content_layers, style_layers, ref_layers,
content_weight, style_weight, ref_weight):
vgg_copy = copy.deepcopy(vgg)
model = nn.Sequential()
normalization = Normalization()
if torch.cuda.is_available():
model = model.to("cuda")
normalization = normalization.to("cuda")
model.add_module("normalization", normalization)
level = 1
i = 1
count = len(content_layers) + len(style_layers) + len(ref_layers)
content_losses = []
style_losses = []
ref_losses = []
for layer in list(vgg_copy):
if isinstance(layer, nn.MaxPool2d):
name = "maxpool_" + str(level)
level += 1
i = 1
elif isinstance(layer, nn.Conv2d):
name = "conv{}_{}".format(str(level), str(i))
else:
name = "relu{}_{}".format(str(level), str(i))
layer = nn.ReLU(inplace=False)
i += 1
model.add_module(name, layer)
if name in content_layers:
count -= 1
content_target = model(content).clone()
content_loss = ContentLoss(content_target, content_weight)
model.add_module("content_loss_" + name, content_loss)
content_losses.append(content_loss)
if name in style_layers:
count -= 1
style_target = model(style).clone()
weight = style_weight / layer.out_channels
style_target_gram = get_gram_matrix(style_target)
style_loss = StyleLoss(style_target_gram, weight)
model.add_module("style_loss_" + name, style_loss)
style_losses.append(style_loss)
if reference is not None and name in ref_layers:
count -= 1
ref_target = model(reference).clone()
ref_loss = ContentLoss(ref_target, ref_weight)
model.add_module("reference_loss_" + name, ref_loss)
ref_losses.append(ref_loss)
if count == 0:
break
return model, content_losses, style_losses, ref_losses
def style_transfer(content, style, reference,
content_weight, style_weight, ref_weight,
content_layers=default_content_layers,
style_layers=default_style_layers,
ref_layers=default_ref_layers,
white_noise_input=False,
iteration=500):
model_set = get_model(content, style, reference,
content_layers, style_layers, ref_layers,
content_weight, style_weight, ref_weight)
model, content_losses, style_losses, ref_losses = model_set
input = torch.randn(content.shape) if white_noise_input else content.clone()
if torch.cuda.is_available():
input = input.to("cuda")
optimizer = optim.LBFGS([input.requires_grad_()])
i = [0, float("inf")]
while i[0] < iteration:
def closure():
input.data.clamp_(0, 1)
optimizer.zero_grad()
model(input)
style_loss_val = 0
content_loss_val = 0
ref_loss_val = 0
for style_loss in style_losses:
style_loss_val += style_loss.loss
for content_loss in content_losses:
content_loss_val += content_loss.loss
for ref_loss in ref_losses:
ref_loss_val += ref_loss.loss
i[0] += 1
if i[0] % 100 == 0:
print("Iteration: {}, Style loss: {}, "
"Content loss: {}, "
"Reference loss: {}".format(i[0],
style_loss_val,
content_loss_val,
ref_loss_val))
loss = style_loss_val + content_loss_val + ref_loss_val
i[1] = loss.item()
loss.backward()
return loss
optimizer.step(closure)
input.data.clamp_(0, 1)
return transforms.ToPILImage()(input.data[0].cpu())