-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvnst.py
362 lines (302 loc) · 13.6 KB
/
vnst.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
from style_content_extractor import StyleContentExtractor
import sys
import getopt
import utils as U
import torch
import torch.optim as optim
from torchvision import utils, transforms as T
from tqdm import tqdm
from losses import *
from PIL import Image
import matplotlib.pyplot as plt
content_layers = ['block4_conv2']
style_layers = ['block1_conv1',
'block2_conv1',
'block3_conv1',
'block4_conv1',
'block5_conv1']
def apply_style_transfer(content_path, style_path, output_path, content_weight,
style_weight, resize, temporal_loss,
temporal_weight, target_start, start_from_prev,
style_from_prev, opt, iters, early_stopping):
# Load device
cuda_on = torch.cuda.is_available()
device = 'cuda' if cuda_on else 'cpu'
print('Device:',
device,
'(' + torch.cuda.get_device_name(0) +')' if cuda_on else '')
if cuda_on:
torch.cuda.empty_cache()
# Determine image or video.
nst_mode = None # video or image
extension = content_path.split('.')[1]
if extension == 'mp4' or extension == 'gif':
nst_mode = 'video'
elif extension == 'jpg' or extension == 'png':
nst_mode = 'image'
else:
print('Unknown file extension: {}'.format(extension))
sys.exit(2)
# Announce
print('-' * 80)
print('Applying style transfer...')
print('Content {}: {}'.format(nst_mode, content_path.split('/')[-1]))
print('Style image: {}'.format(style_path.split('/')[-1]))
print('Optimizer:', opt)
print('Content weight: {}'.format(content_weight))
print('Style weight: {}'.format(style_weight))
print('Target start: {}'.format(target_start))
print('Early stopping: {}'.format(early_stopping))
if nst_mode == 'video':
print('Temporal loss: {}'.format(temporal_loss))
print('Temporal weight: {}'.format(temporal_weight))
print('Optimize from warped previous frame: {}'.format(start_from_prev))
print('Style from previous frame: {}'.format(style_from_prev))
# Start style transfer
if nst_mode == 'image':
image_style_transfer(content_path, style_path, output_path,
content_weight, style_weight, resize,
target_start, iters, early_stopping, opt, device)
else:
video_style_transfer(content_path, style_path, output_path,
content_weight, style_weight, resize,
temporal_loss, temporal_weight, target_start,
start_from_prev, style_from_prev,
iters, early_stopping, opt, device)
def image_style_transfer(content_path, style_path, output_path, content_weight,
style_weight, resize, target_start,
iters, early_stopping, opt, device):
# Initialize StyleContentExtractor
sce = StyleContentExtractor(style_layers, content_layers, device)
# Load images
content_img = U.load_image(content_path, resize, device)
style_img = U.load_image(style_path, resize, device)
output_img = __transfer(sce, content_img, style_img, content_weight,
style_weight, False,
0, target_start, None, None, False, iters,
early_stopping, opt, device)
print('Image generation done. Saving to {}...'.format(output_path))
utils.save_image(output_img, output_path)
def video_style_transfer(content_path, style_path, output_path,
content_weight, style_weight, resize,
temporal_loss, temporal_weight, target_start,
start_from_prev, style_from_prev,
iters, early_stopping, opt, device):
# Initialize StyleContentExtractor
sce = StyleContentExtractor(style_layers, content_layers, device)
content_gif = Image.open(content_path)
style_img = U.load_image(style_path, resize, device)
transferred_frames = []
prev_frame = None
prev_frame_stylized = None
opti_bar = tqdm(total=iters, position=0, leave=True)
pbar = tqdm(total=int(content_gif.n_frames), position=1, leave=True)
for frame in range(0, int(content_gif.n_frames)):
content_gif.seek(frame)
content_img = U.process_image(content_gif.convert('RGB'), resize, device)
transferred_img = __transfer(sce, content_img, style_img, content_weight,
style_weight, temporal_loss,
temporal_weight, target_start,
prev_frame_stylized, prev_frame,
start_from_prev, iters, early_stopping, opt,
device, p_bar=opti_bar)
prev_frame = content_img
prev_frame_stylized = transferred_img
if style_from_prev:
style_img = transferred_img.squeeze(0).clone().detach()
transferred_frames.append(T.ToPILImage()(transferred_img.squeeze(0)))
pbar.update(1)
opti_bar.close()
pbar.close()
print('Video generation done. Saving to {}...'.format(output_path))
transferred_frames[0].save(output_path, save_all=True,
append_images=transferred_frames[1:], loop=0)
def __transfer(sce, content_img, style_img, content_weight,
style_weight, temp_loss, temporal_weight,
target_start, prev_img_stylized, prev_frame,
start_from_prev, iters, early_stopping, opt, device, p_bar=None):
style_targets = sce(style_img.detach().unsqueeze(0))['style']
content_targets = sce(content_img.detach().unsqueeze(0))['content']
if target_start == 'content':
target_img = content_img.unsqueeze(0).clone()
elif target_start == 'random':
target_img = torch.randn(content_img.unsqueeze(0).data.size(),
device=device)
if prev_frame is not None:
prev_frame = prev_frame.detach()
if prev_img_stylized is not None:
prev_img_stylized = prev_img_stylized.detach()
if start_from_prev and prev_img_stylized is not None:
target_img = U.warp_img(prev_img_stylized, prev_frame, content_img, device).contiguous()
target_img.requires_grad_(True)
sce.requires_grad_(False)
sce.vgg.requires_grad_(False)
pbar = tqdm(total=iters) if p_bar is None else p_bar
pbar.reset()
pbar.refresh()
s_losses = []
c_losses = []
temp_losses = []
losses = []
if opt == 'adam':
optimizer = optim.Adam([target_img], lr=0.02, betas=(0.99, 0.999), eps=1e-1)
while pbar.n < iters:
with torch.no_grad():
target_img.clamp_(0, 1)
optimizer.zero_grad()
outputs = sce(target_img)
# Compute total loss
style_loss, content_loss = style_content_loss(outputs, style_targets,
content_targets, style_weight,
content_weight, 5, 1)
loss = style_loss + content_loss
with torch.no_grad():
s_losses.append(style_loss.detach().cpu().numpy())
c_losses.append(content_loss.detach().cpu().numpy())
if temp_loss and prev_img_stylized is not None:
temploss = temporal_weight * temporal_loss(prev_img_stylized, target_img,
prev_frame, content_img, device)
loss += temploss
temp_losses.append(temploss.detach().cpu().numpy())
with torch.no_grad():
losses.append(loss.detach().cpu().numpy())
if pbar.n > 50 and early_stopping:
last_50_loss = losses[-50]
if last_50_loss > 0 and torch.abs(last_50_loss - loss) / loss < 1e-3:
break
loss.backward(retain_graph=True)
pbar.update(1)
optimizer.step()
elif opt == 'lbfgs':
optimizer = optim.LBFGS([target_img])
while pbar.n < iters:
def closure():
with torch.no_grad():
target_img.clamp_(0, 1)
optimizer.zero_grad()
outputs = sce(target_img)
# Compute total loss
style_loss, content_loss = style_content_loss(outputs, style_targets,
content_targets, style_weight,
content_weight, 5, 1)
loss = style_loss + content_loss
with torch.no_grad():
s_losses.append(style_loss.detach().cpu().numpy())
c_losses.append(content_loss.detach().cpu().numpy())
if temp_loss and prev_img_stylized is not None:
temploss = temporal_weight * temporal_loss(prev_img_stylized, target_img,
prev_frame, content_img, device)
loss += temploss
temp_losses.append(temploss.detach().cpu().numpy())
with torch.no_grad():
losses.append(loss.detach().cpu().numpy())
# early stopping not available for LBFGS.
loss.backward(retain_graph=True)
pbar.update(1)
return loss
optimizer.step(closure)
with torch.no_grad():
target_img.clamp_(0, 1)
# toggle loss plots
if False:
plt.plot(losses, label='Loss')
plt.legend()
plt.show()
plt.plot(s_losses, label='Style Loss')
plt.legend()
plt.show()
plt.plot(c_losses, label='Content Loss')
plt.legend()
plt.show()
if temp_loss:
plt.plot(temp_losses, label='Temporal Loss')
plt.legend()
plt.show()
return target_img
def main(argv):
arg_help = "{0} -c content -s style [-o output ".format(argv[0]) + \
"-w content_weight -y style_weight -z size -T true/false " + \
"-W temporal_weight -i iters" + \
"-I content/random -p true/false -Y true/false -a lbfgs/adam -e true/false] "
try:
opts, args = getopt.getopt(argv[1:],
"hc:s:o:w:y:z:T:W:i:I:p:Y:a:e:",
["help",
"content=",
"style=",
"output=",
"content_weight=",
"style_weight=",
"resize="
"temporal_loss=",
"temporal_weight=",
"iters=",
"target_start=",
"start_from_prev=",
"style_from_prev=",
"optimizer=",
"early_stopping="])
except:
print(arg_help)
sys.exit(2)
content = None
style = None
# Optional arguments defaults
output = None
content_weight = 1
style_weight = 1e5
temporal_loss = True
temporal_weight = 1e-3
resize = 512
target_start = 'content'
start_from_prev = True # If true, start optimizing from previous frame warped.
iters = 300
optimizer = 'lbfgs'
style_from_prev = False # If true, use previous (unwarped) stylized frame as style.
early_stopping = False
for opt, arg in opts:
if opt in ("-h", "--help"):
print(arg_help)
sys.exit(2)
elif opt in ("-c", "--content"):
content = arg
elif opt in ("-s", "--style"):
style = arg
elif opt in ("-o", "--output"):
output = arg
elif opt in ("-w", "--content_weight"):
content_weight = float(arg)
elif opt in ("-y", "--style_weight"):
style_weight = float(arg)
elif opt in ("-z", "--resize"):
resize = int(arg)
elif opt in ("-T", "--temporal_loss"):
temporal_loss = True if arg == 'true' else False
elif opt in ("-W", "--temporal_weight"):
temporal_weight = float(arg)
elif opt in ("-I", "--target_start"):
target_start = arg
elif opt in ("-p", "--start_from_prev"):
start_from_prev = True if arg == 'true' else False
elif opt in ("-Y", "--style_from_prev"):
style_from_prev = True if arg == 'true' else False
elif opt in ("-i", "--iters"):
iters = int(arg)
elif opt in ("-a", "--optimizer"):
optimizer = arg
elif opt in ("-e", "--early_stopping"):
early_stopping = True if arg == 'true' else False
if content is None or style is None:
print(arg_help)
sys.exit(2)
if output is None:
output = content.split('.')[0] + "_" + style + "." + content.split('.')[1]
content = 'content/{}'.format(content)
style = 'style/{}.jpg'.format(style)
output = 'result/{}'.format(output)
apply_style_transfer(content, style, output, content_weight,
style_weight, resize, temporal_loss, temporal_weight,
target_start, start_from_prev, style_from_prev,
optimizer, iters, early_stopping)
if __name__ == "__main__":
main(sys.argv)