-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle transfer.py
177 lines (158 loc) · 6.87 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
from IPython.display import Image, display
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import PIL.Image
import vgg16
#vgg16.maybe_download()
#use above to download
def load_image(filename, max_size=None):
image = PIL.Image.open(filename)
if max_size is not None:
factor = max_size / np.max(image.size)
size = np.array(image.size) * factor
size = size.astype(int)
image = image.resize(size, PIL.Image.LANCZOS)
return np.float32(image)
def save_image(image, filename):
image = np.clip(image, 0.0, 255.0)
image = image.astype(np.uint8)
with open(filename, 'wb') as file:
PIL.Image.fromarray(image).save(file, 'jpeg')
def plot_image_big(image):
image = np.clip(image, 0.0, 255.0)
image = image.astype(np.uint8)
display(PIL.Image.fromarray(image))
def plot_images(content_image, style_image, mixed_image):#This function plots the content-, mixed- and style-images
fig, axes = plt.subplots(1, 3, figsize=(10, 10))
fig.subplots_adjust(hspace=0.1, wspace=0.1)
smooth = True
if smooth:
interpolation = 'sinc'
else:
interpolation = 'nearest'
ax = axes.flat[0]
ax.imshow(content_image / 255.0, interpolation=interpolation)
ax.set_xlabel("Content")
ax = axes.flat[1]
ax.imshow(mixed_image / 255.0, interpolation=interpolation)
ax.set_xlabel("Mixed")
ax = axes.flat[2]
ax.imshow(style_image / 255.0, interpolation=interpolation)
ax.set_xlabel("Style")
for ax in axes.flat:
ax.set_xticks([])
ax.set_yticks([])
plt.show()
def mean_squared_error(a, b):
return tf.reduce_mean(tf.square(a - b))
def create_content_loss(session, model, content_image, layer_ids):
feed_dict = model.create_feed_dict(image=content_image)
layers = model.get_layer_tensors(layer_ids)
values = session.run(layers, feed_dict=feed_dict)
with model.graph.as_default():
layer_losses = []
for value, layer in zip(values, layers):
value_const = tf.constant(value)
loss = mean_squared_error(layer, value_const)
layer_losses.append(loss)
total_loss = tf.reduce_mean(layer_losses)
return total_loss
def gram_matrix(tensor):
shape = tensor.get_shape()
num_channels = int(shape[3])
matrix = tf.reshape(tensor, shape=[-1, num_channels])
gram = tf.matmul(tf.transpose(matrix), matrix)
return gram
def create_style_loss(session, model, style_image, layer_ids):
feed_dict = model.create_feed_dict(image=style_image)
layers = model.get_layer_tensors(layer_ids)
with model.graph.as_default():
gram_layers = [gram_matrix(layer) for layer in layers]
values = session.run(gram_layers, feed_dict=feed_dict)
layer_losses = []
for value, gram_layer in zip(values, gram_layers):
value_const = tf.constant(value)
loss = mean_squared_error(gram_layer, value_const)
layer_losses.append(loss)
total_loss = tf.reduce_mean(layer_losses)
return total_loss
# shifts the image one pixel in the x- and y-axis, calculates the difference from the original image
def create_denoise_loss(model):
loss = tf.reduce_sum(tf.abs(model.input[:,1:,:,:] - model.input[:,:-1,:,:])) + \
tf.reduce_sum(tf.abs(model.input[:,:,1:,:] - model.input[:,:,:-1,:]))
return loss
def style_transfer(content_image, style_image,
content_layer_ids, style_layer_ids,
weight_content=1.5, weight_style=10.0,
weight_denoise=0.3,
num_iterations=120, step_size=10.0):
model = vgg16.VGG16()
session = tf.InteractiveSession(graph=model.graph)
print("Content layers:")
print(model.get_layer_names(content_layer_ids))
print()
print("Style layers:")
print(model.get_layer_names(style_layer_ids))
print()
loss_content = create_content_loss(session=session,
model=model,
content_image=content_image,
layer_ids=content_layer_ids)
loss_style = create_style_loss(session=session,
model=model,
style_image=style_image,
layer_ids=style_layer_ids)
loss_denoise = create_denoise_loss(model)
adj_content = tf.Variable(1e-10, name='adj_content')
adj_style = tf.Variable(1e-10, name='adj_style')
adj_denoise = tf.Variable(1e-10, name='adj_denoise')
session.run([adj_content.initializer,
adj_style.initializer,
adj_denoise.initializer])
update_adj_content = adj_content.assign(1.0 / (loss_content + 1e-10))
update_adj_style = adj_style.assign(1.0 / (loss_style + 1e-10))
update_adj_denoise = adj_denoise.assign(1.0 / (loss_denoise + 1e-10))
loss_combined = weight_content * adj_content * loss_content + \
weight_style * adj_style * loss_style + \
weight_denoise * adj_denoise * loss_denoise
gradient = tf.gradients(loss_combined, model.input)
run_list = [gradient, update_adj_content, update_adj_style, \
update_adj_denoise]
mixed_image = np.random.rand(*content_image.shape) + 128
for i in range(num_iterations):
feed_dict = model.create_feed_dict(image=mixed_image)
grad, adj_content_val, adj_style_val, adj_denoise_val \
= session.run(run_list, feed_dict=feed_dict)
grad = np.squeeze(grad)
step_size_scaled = step_size / (np.std(grad) + 1e-8)
mixed_image -= grad * step_size_scaled
mixed_image = np.clip(mixed_image, 0.0, 255.0)
print(". ", end="")
if (i % 10 == 0) or (i == num_iterations - 1):
print()
print("Iteration:", i)
# Print adjustment weights for loss-functions.
msg = "Weight Adj. for Content: {0:.2e}, Style: {1:.2e}, Denoise: {2:.2e}"
print(msg.format(adj_content_val, adj_style_val, adj_denoise_val))
plot_images(content_image=content_image,
style_image=style_image,
mixed_image=mixed_image)
print()
print("Final image:")
plot_image_big(mixed_image)
session.close()
return mixed_image
content_filename = 'cont1.jpg'
content_image = load_image(content_filename, max_size=None)
style_filename = 'style1.jpg'
style_image = load_image(style_filename, max_size=300)
img = style_transfer(content_image=content_image,
style_image=style_image,
content_layer_ids=[4],
style_layer_ids=list(range(13)),
weight_content=1,
weight_style=5.0,
weight_denoise=0.3,
num_iterations=60,
step_size=10.0)