forked from Yuanyuan-Yuan/NeuraL-Coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyle_operator.py
More file actions
225 lines (203 loc) · 7.88 KB
/
Copy pathstyle_operator.py
File metadata and controls
225 lines (203 loc) · 7.88 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
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
#!/usr/bin/env python
import os
import argparse
from pathlib import Path
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import random
import numpy as np
import torch
import torch.nn as nn
import torchvision.transforms
import constants
decoder = nn.Sequential(
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 256, (3, 3)),
nn.ReLU(),
nn.Upsample(scale_factor=2),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 128, (3, 3)),
nn.ReLU(),
nn.Upsample(scale_factor=2),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 128, (3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 64, (3, 3)),
nn.ReLU(),
nn.Upsample(scale_factor=2),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 64, (3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 3, (3, 3)),
)
vgg = nn.Sequential(
nn.Conv2d(3, 3, (1, 1)),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(3, 64, (3, 3)),
nn.ReLU(), # relu1-1
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 64, (3, 3)),
nn.ReLU(), # relu1-2
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 128, (3, 3)),
nn.ReLU(), # relu2-1
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 128, (3, 3)),
nn.ReLU(), # relu2-2
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 256, (3, 3)),
nn.ReLU(), # relu3-1
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(), # relu3-2
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(), # relu3-3
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(), # relu3-4
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 512, (3, 3)),
nn.ReLU(), # relu4-1, this is the last layer used
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu4-2
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu4-3
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu4-4
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu5-1
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu5-2
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu5-3
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU() # relu5-4
)
def calc_mean_std(feat, eps=1e-5):
# eps is a small value added to the variance to avoid divide-by-zero.
size = feat.data.size()
assert (len(size) == 4)
N, C = size[:2]
feat_var = feat.view(N, C, -1).var(dim=2) + eps
feat_std = feat_var.sqrt().view(N, C, 1, 1)
feat_mean = feat.view(N, C, -1).mean(dim=2).view(N, C, 1, 1)
return feat_mean, feat_std
def adaptive_instance_normalization(content_feat, style_feat):
assert (content_feat.data.size()[:2] == style_feat.data.size()[:2])
size = content_feat.data.size()
style_mean, style_std = calc_mean_std(style_feat)
content_mean, content_std = calc_mean_std(content_feat)
normalized_feat = (content_feat - content_mean.expand(
size)) / content_std.expand(size)
return normalized_feat * style_std.expand(size) + style_mean.expand(size)
# random.seed(131213)
def input_transform(size, crop):
transform_list = []
if size != 0:
transform_list.append(torchvision.transforms.Resize(size))
if crop != 0:
transform_list.append(torchvision.transforms.CenterCrop(crop))
transform_list.append(torchvision.transforms.ToTensor())
transform = torchvision.transforms.Compose(transform_list)
return transform
def style_transfer(vgg, decoder, content, style, alpha=1.0):
with torch.no_grad():
assert (0.0 <= alpha <= 1.0)
content_f = vgg(content)
style_f = vgg(style)
feat = adaptive_instance_normalization(content_f, style_f)
feat = feat * alpha + content_f * (1 - alpha)
return decoder(feat)
class Stylized(object):
def __init__(self, style_size):
self.init_params(style_size)
self.init_models()
self.prepare_style()
self.content_tf = input_transform(self.content_size, self.crop)
self.style_tf = input_transform(self.style_size, 0)
def init_params(self, style_size):
self.style_dir = constants.STYLE_IMAGE_DIR
self.num_styles = 1
# self.alpha = 1.0
self.extensions = ['png', 'jpeg', 'jpg']
self.content_size = 0
self.style_size = style_size
self.crop = 0
self.decoder_path = os.path.join(constants.STYLE_MODEL_DIR, 'decoder.pth')
self.vgg_path = os.path.join(constants.STYLE_MODEL_DIR, 'vgg_normalised.pth')
def prepare_style(self):
style_dir = Path(self.style_dir)
style_dir = style_dir.resolve()
assert style_dir.is_dir(), 'Style directory not found'
styles = []
for ext in self.extensions:
styles += list(style_dir.rglob('*.' + ext))
assert len(styles) > 0, 'No images with specified extensions found in style directory' + self.style_dir
self.styles = sorted(styles)
print('Found %d style images in %s' % (len(self.styles), self.style_dir))
def init_models(self):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.decoder = decoder
self.vgg = vgg
self.decoder.eval()
self.vgg.eval()
self.decoder.load_state_dict(torch.load(self.decoder_path))
self.vgg.load_state_dict(torch.load(self.vgg_path))
self.vgg = nn.Sequential(*list(self.vgg.children())[:31])
self.vgg.to(self.device)
self.decoder.to(self.device)
def transform(self, image, alpha):
if type(image) is str:
content_image = Image.open(image).convert('RGB')
content_image = self.content_tf(content_image).to(self.device)
elif type(image) is np.ndarray:
content_image = torch.from_numpy(image / 255).to(self.device)
if len(content_image.size()) == 3:
content_image = content_image.transpose(0, 2).unsqueeze(0)
elif len(content_image.size()) == 4:
content_image = content_image.transpose(1, 3)
else:
raise NotImplementedError
else:
content_image = image
style_path = random.sample(self.styles, self.num_styles)[0]
style_image = Image.open(style_path).convert('RGB')
style_image = self.style_tf(style_image)
style_image = style_image.to(self.device).unsqueeze(0)
output = style_transfer(self.vgg, self.decoder,
content_image, style_image, alpha)
if type(image) is np.ndarray:
output = output * 255
if len(content_image.size()) == 3:
output = output.transpose(0, 2).detach().cpu().numpy()
elif len(content_image.size()) == 4:
output = output.transpose(1, 3).detach().cpu().numpy()
else:
raise NotImplementedError
else:
output = output.detach().cpu()
return output