-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural_style_transfer.py
127 lines (101 loc) · 4.54 KB
/
neural_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
import argparse
import torch
import time
from pathlib import Path
from models.definitions import Vgg19
from utils.losses import get_loss_func
from utils.images import load_img, save_img
from utils import time_execution
def neural_style_transfer(target_img, style_img, content_img, config):
device = config['device']
if device is None:
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f"Using {device}")
model = Vgg19()
model.to(device)
model.eval() # model is not trained, the target image is
target_img = target_img.to(device).requires_grad_()
style_img = style_img.to(device)
content_img = content_img.to(device)
content_feature_maps = model(content_img)
style_feature_maps = model(style_img)
loss = get_loss_func(content_feature_maps, style_feature_maps,
model.content_feature_maps_idx, model.style_feature_maps_indices,
config['alpha'], config['beta'])
# the paper used stochastic gradient descent
optimizer = torch.optim.Adam((target_img,), config['lr'])
for i in range(config['epochs_num']):
optimizer.zero_grad()
target_feature_maps = model(target_img)
total_loss, content_loss, style_loss = loss(target_feature_maps)
total_loss.backward()
optimizer.step()
with torch.no_grad():
if i % 25 == 0:
print(
f"epoch {i} total_loss: {total_loss.item()} content_loss: {content_loss.item()} style_loss: {style_loss.item()}")
if config['save_freq'] is not None and i % config['save_freq'] == 0:
save_img(
target_img, f"{config['save_dir']}/{config['base_filename']}_{i}.jpg")
with torch.no_grad():
save_img(
target_img, f"{config['save_dir']}/{config['base_filename']}_final.jpg")
@time_execution
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--style-img-path', required=True, type=str)
parser.add_argument('--content-img-path', required=True, type=str)
parser.add_argument('--height', type=int,
help="Height of the generated image")
parser.add_argument('--width', type=int,
help="Width of the generated image")
parser.add_argument('--start-img',
choices=['random', 'style', 'content'],
default='random')
parser.add_argument('--alpha', default=1e1, type=float)
parser.add_argument('--beta', default=1e10, type=float)
parser.add_argument('--lr', default=1e-1, type=float)
parser.add_argument('--epochs-num', default=3000, type=int)
parser.add_argument('--device', choices=['cpu', 'cuda'])
parser.add_argument('--save-freq', type=int)
parser.add_argument('--save-dir', type=str)
# according to my experience the generated image is better when the below option is passed
parser.add_argument('--resize-style-img', action='store_true')
args = parser.parse_args()
style_img_path = args.style_img_path
content_img_path = args.content_img_path
height = args.height
width = args.width
start_img = args.start_img
resize_style_img = args.resize_style_img
default_save_dir = f"nst/{start_img}_h_{height}_w_{width}_lr_{args.lr}_a_{args.alpha}_b_{args.beta}/{int(time.time())}"
config = {
'alpha': args.alpha,
'beta': args.beta,
'lr': args.lr,
'epochs_num': args.epochs_num,
'device': args.device,
'save_freq': args.save_freq,
'save_dir': args.save_dir if args.save_dir is not None else default_save_dir,
'base_filename': f"{Path(content_img_path).stem}_{Path(style_img_path).stem}"
}
content_img = load_img(content_img_path, height, width)
if resize_style_img:
_, content_img_height, content_img_width = content_img.shape
style_img = load_img(
style_img_path, content_img_height, content_img_width)
else:
style_img = load_img(style_img_path)
# the paper used random initialization
if start_img == 'random':
target_img = torch.randn_like(content_img)
elif start_img == 'content':
target_img = content_img.clone().detach()
elif start_img == 'style':
_, content_img_height, content_img_width = content_img.shape
target_img = load_img(
style_img_path, content_img_height, content_img_width)
target_img = target_img.clone().detach()
neural_style_transfer(target_img, style_img, content_img, config)
if __name__ == '__main__':
main()