-
Notifications
You must be signed in to change notification settings - Fork 0
/
style_transfer.py
387 lines (284 loc) · 13.7 KB
/
style_transfer.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
from typing import List, Tuple
import time
from IPython.display import display, Pretty
import torch
from torch import Tensor
from torch.optim import Adam
from torchvision.transforms import functional as TF
import numpy as np
from PIL import Image
from models import VGGFeatures, FacialFeatures
from utils.modules import Scale, LayerApply, EMA
from utils.modules import MergeModels, Model, Layers
from utils.modules import gen_scales, size_to_fit, interpolate
from utils.modules import scale_adam, scale_boundaries, get_model_min_size
from utils.losses import TVLoss, ContentLoss, StyleLoss, SumLoss
import warnings
warnings.filterwarnings("ignore")
class StyleTransfer:
def __init__(self, device=torch.device("cpu"), pooling: str = 'max'):
self.device = device
self.image = None
self.average = None
# The default content and style layers follow Gatys et al. (2015).
self.content_layers = Layers(tag="vgg", layers=[22])
self.style_layers = Layers(tag="vgg", layers=[1, 6, 11, 20, 29])
# The facial layers were empirically selected through debugging of
# the FaceNet Model (shallow layers are better in generalization).
# Layer 14 represents the FaceMesh Model Landmarks.
self.face_layers = Layers(tag="facenet", layers=[0, 1, 3, 4, 5], hidden_layers=[14])
# The weighting of the style layers differs from Gatys et al. (2015) and Johnson et al.
style_weights = [256, 64, 16, 4, 1]
weight_sum = sum(abs(w) for w in style_weights)
self.style_weights = [w / weight_sum for w in style_weights]
self.vgg = VGGFeatures(
layers=self.style_layers.layers + self.content_layers.layers,
pooling=pooling,
device=self.device
)
self.facenet = FacialFeatures(
layers=self.face_layers.layers,
device=self.device
)
self.model = MergeModels([
Model(model=self.vgg, tag="vgg"),
Model(model=self.facenet, tag="facenet")
])
self.min_input_size = max(
get_model_min_size(
layers=self.content_layers.layers + self.style_layers.layers,
notable_layers=self.vgg.notable_layers
),
get_model_min_size(
layers=self.face_layers.layers,
notable_layers=self.facenet.notable_layers
)
)
def get_image_tensor(self):
return self.average.get().detach()[0].clamp(0, 1)
def get_image(self, image_type='pil'):
if self.average is not None:
image = self.get_image_tensor()
if image_type.lower() == 'pil':
return TF.to_pil_image(image)
elif image_type.lower() == 'np_uint16':
arr = image.cpu().movedim(0, 2).numpy()
return np.uint16(np.round(arr * 65535))
else:
raise ValueError("image_type must be 'pil' or 'np_uint16'")
def initialize_image(self, content_image: Image,
style_images: List,
style_weights: List[int],
size: Tuple[int, int],
init: str = 'content',
device=torch.device("cpu")):
if init == 'content':
self.image = TF.to_tensor(content_image.resize(size, Image.LANCZOS))[None].to(device)
elif init == 'gray':
self.image = torch.rand([1, 3, *size]) / 255 + 0.5
self.image.to(device)
elif init == 'uniform':
self.image = torch.rand([1, 3, *size]).to(device)
elif init == 'style_mean':
means = []
for i, image in enumerate(style_images):
means.append(TF.to_tensor(image).mean(dim=(1, 2)) * style_weights[i])
self.image = torch.rand([1, 3, *size]) / 255 + sum(means)[None, :, None, None]
self.image.to(device)
else:
raise ValueError("init must be one of 'content', 'gray', 'uniform', 'style_mean'")
@staticmethod
def calculate_losses(features: dict, layers: List[str], weights: List[float]):
losses = []
for layer, weight in zip(layers, weights):
target = features[layer]
losses.append(Scale(LayerApply(ContentLoss(target), layer), weight))
return losses
def process_layers(self, content_weight: float,
face_weight: float,
mesh_weight: float) -> List[Layers]:
layers = []
if content_weight > 0:
layers.append(self.content_layers)
if face_weight > 0 or mesh_weight > 0:
face_layers = Layers(self.face_layers.tag, [])
if face_weight > 0:
face_layers.load(self.face_layers.layers)
if mesh_weight > 0:
face_layers.append(14)
layers.append(face_layers)
return layers
def process_content(self, content: Tensor,
content_weights: List[float],
face_weights: List[float],
mesh_weight: float) -> Tuple[List[Scale], List[Layers]]:
layers = self.process_layers(
content_weight=sum(content_weights),
face_weight=sum(face_weights),
mesh_weight=mesh_weight
)
content_feats = self.model(content, layers)
losses = []
if sum(content_weights) > 0:
content_losses = self.calculate_losses(
features=content_feats,
layers=list(self.content_layers),
weights=content_weights
)
losses += content_losses
if sum(face_weights) > 0:
facial_losses = self.calculate_losses(
features=content_feats,
layers=list(self.face_layers),
weights=face_weights
)
losses += facial_losses
if mesh_weight > 0:
mesh_layer = f'{self.face_layers.tag}_14'
mesh_loss = Scale(LayerApply(ContentLoss(content_feats[mesh_layer]), mesh_layer), mesh_weight)
losses.append(mesh_loss)
return losses, layers
def process_styles(self, style_images: List,
style_size: int,
scale: int,
style_scale_fac: float,
style_weights: List[float]) -> List[Scale]:
style_targets, style_losses = {}, []
for i, image in enumerate(style_images):
if style_size is None:
sw, sh = size_to_fit(image.size, round(scale * style_scale_fac))
else:
sw, sh = size_to_fit(image.size, style_size)
style = TF.to_tensor(image.resize((sw, sh), Image.LANCZOS))[None]
style = style.to(self.device)
# Take the weighted average of multiple style targets (Gram matrices).
style_feats = self.model(style, [self.style_layers])
for layer in list(self.style_layers):
target = StyleLoss.get_target(style_feats[layer]) * style_weights[i]
if layer not in style_targets:
style_targets[layer] = target
else:
style_targets[layer] += target
for layer, weight in zip(list(self.style_layers), self.style_weights):
target = style_targets[layer]
style_losses.append(Scale(LayerApply(StyleLoss(target), layer), weight))
return style_losses
def stylize(self, content_image: Image.Image, style_images: List[Image.Image], *,
style_weights: list = None,
content_weight: float = 0.015,
face_weight: float = 0.015,
mesh_weight: float = 0,
tv_weight: float = 2.,
min_scale: int = 128,
end_scale: int = 606,
iterations: int = 500,
initial_iterations: int = 1000,
step_size: float = 0.02,
avg_decay: float = 0.99,
init: str = 'content',
style_size: int = None,
style_scale_fac: float = 1.,
padding_scale_fac: float = 0.2,
crop_faces: bool = False,
square_faces: bool = False,
plot_progress: bool = False,
plot_every: int = 100,
save_path: str = "./out.png",
save_every: int = 10):
if square_faces and not crop_faces:
raise ValueError("To use 'square_faces', 'crop_faces' need to be True.")
if plot_every == 0:
raise ValueError("'plot_every' can't be zero to avoid ZeroDivisionError.")
style_sizes = [style.size for style in style_images]
if min([size for sizes in style_sizes for size in sizes]) < self.min_input_size:
raise ValueError(f'Style images need to be at least {self.min_input_size}x{self.min_input_size}.')
content_image = content_image.convert("RGB")
style_images = [style_image.convert("RGB") for style_image in style_images]
min_scale = min(min_scale, end_scale)
content_weights = [content_weight / len(self.content_layers)] * len(self.content_layers)
face_weights = [face_weight / len(self.face_layers)] * len(self.face_layers)
if style_weights is None:
style_weights = [1 / len(style_images)] * len(style_images)
else:
weight_sum = sum(abs(w) for w in style_weights)
style_weights = [weight / weight_sum for weight in style_weights]
if len(style_images) != len(style_weights):
raise ValueError('style_images and style_weights must have the same length')
tv_loss = Scale(LayerApply(TVLoss(), 'input'), tv_weight)
scales = gen_scales(min_scale, end_scale)
cw, ch = size_to_fit(content_image.size, scales[0], scale_up=True)
self.initialize_image(
content_image=content_image,
style_images=style_images,
style_weights=style_weights,
size=(cw, ch),
init=init,
device=self.device
)
self.model.get_model("facenet").padding_scale = padding_scale_fac
self.model.get_model("facenet").square_faces = square_faces
plot_size = scale_boundaries(*content_image.size, ref_size=256)
image_display = display(content_image.resize(plot_size), display_id=True) if plot_progress else None
status_display = display((), display_id=True)
optimizer = None
# Stylize the image at successively finer scales, each greater by a factor of sqrt(2).
# This differs from the scheme given in Gatys et al. (2016).
for scale in scales:
if self.device.type == 'cuda':
torch.cuda.empty_cache()
cw, ch = size_to_fit(content_image.size, scale, scale_up=True)
scaled_content = content_image.resize((cw, ch), Image.LANCZOS)
content = TF.to_tensor(scaled_content)[None]
content = content.to(self.device)
self.model.get_model("facenet").extract_boxes(scaled_content if crop_faces else None)
self.image = interpolate(self.image.detach(), (ch, cw), mode='bicubic').clamp(0, 1)
self.average = EMA(self.image, avg_decay)
self.image.requires_grad_()
content_losses, content_layers = self.process_content(
content=content,
content_weights=content_weights,
face_weights=face_weights,
mesh_weight=mesh_weight,
)
style_losses = self.process_styles(
style_images=style_images,
style_size=style_size,
scale=scale,
style_scale_fac=style_scale_fac,
style_weights=style_weights,
)
criterion = SumLoss([*content_losses, *style_losses, tv_loss])
scale_optimizer = Adam([self.image], lr=step_size)
# Warm-start the Adam optimizer if this is not the first scale.
if scale != scales[0]:
optimizer_state = scale_adam(optimizer.state_dict(), (ch, cw))
scale_optimizer.load_state_dict(optimizer_state)
optimizer = scale_optimizer
if self.device.type == 'cuda':
torch.cuda.empty_cache()
max_iterations = initial_iterations if scale == scales[0] else iterations
for i in range(1, max_iterations + 1):
start_ts = time.time()
feats = self.model(self.image, [*content_layers, self.style_layers])
loss = criterion(feats)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Enforce box constraints.
with torch.no_grad():
self.image.clamp_(0, 1)
self.average.update(self.image)
end_ts = time.time()
status = f'Size: {cw}x{ch}, iteration: {i}/{max_iterations}, '\
f'loss:{loss:g}, elapsed (ms): {(end_ts - start_ts) * 1000:.2f}'
status_display.update(Pretty(status)) if status_display else print(status)
if i % save_every == 0 and save_path:
result = self.get_image()
result.save(save_path)
if i % plot_every == 0 and image_display:
result = self.get_image()
image_display.update(result.resize(plot_size))
# Initialize each new scale with the previous scale's averaged iterate.
with torch.no_grad():
self.image.copy_(self.average.get())
return self.get_image()