Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132930 views
License: OTHER
1
""" An implementation of the paper "A Neural Algorithm of Artistic Style"
2
by Gatys et al. in TensorFlow.
3
4
Author: Chip Huyen ([email protected])
5
Prepared for the class CS 20SI: "TensorFlow for Deep Learning Research"
6
For more details, please read the assignment handout:
7
http://web.stanford.edu/class/cs20si/assignments/a2.pdf
8
"""
9
from __future__ import print_function
10
11
import os
12
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
13
import time
14
15
import numpy as np
16
import tensorflow as tf
17
18
import vgg_model
19
import utils
20
21
# parameters to manage experiments
22
STYLE = 'guernica'
23
CONTENT = 'deadpool'
24
STYLE_IMAGE = 'styles/' + STYLE + '.jpg'
25
CONTENT_IMAGE = 'content/' + CONTENT + '.jpg'
26
IMAGE_HEIGHT = 250
27
IMAGE_WIDTH = 333
28
NOISE_RATIO = 0.6 # percentage of weight of the noise for intermixing with the content image
29
30
CONTENT_WEIGHT = 0.01
31
STYLE_WEIGHT = 1
32
33
# Layers used for style features. You can change this.
34
STYLE_LAYERS = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1']
35
W = [0.5, 1.0, 1.5, 3.0, 4.0] # give more weights to deeper layers.
36
37
# Layer used for content features. You can change this.
38
CONTENT_LAYER = 'conv4_2'
39
40
ITERS = 300
41
LR = 2.0
42
43
MEAN_PIXELS = np.array([123.68, 116.779, 103.939]).reshape((1,1,1,3))
44
""" MEAN_PIXELS is defined according to description on their github:
45
https://gist.github.com/ksimonyan/211839e770f7b538e2d8
46
'In the paper, the model is denoted as the configuration D trained with scale jittering.
47
The input images should be zero-centered by mean pixel (rather than mean image) subtraction.
48
Namely, the following BGR values should be subtracted: [103.939, 116.779, 123.68].'
49
"""
50
51
# VGG-19 parameters file
52
VGG_DOWNLOAD_LINK = 'http://www.vlfeat.org/matconvnet/models/imagenet-vgg-verydeep-19.mat'
53
VGG_MODEL = 'imagenet-vgg-verydeep-19.mat'
54
EXPECTED_BYTES = 534904783
55
56
def _create_content_loss(p, f):
57
""" Calculate the loss between the feature representation of the
58
content image and the generated image.
59
60
Inputs:
61
p, f are just P, F in the paper
62
(read the assignment handout if you're confused)
63
Note: we won't use the coefficient 0.5 as defined in the paper
64
but the coefficient as defined in the assignment handout.
65
Output:
66
the content loss
67
68
"""
69
return tf.reduce_sum((f - p) ** 2) / (4.0 * p.size)
70
71
def _gram_matrix(F, N, M):
72
""" Create and return the gram matrix for tensor F
73
Hint: you'll first have to reshape F
74
"""
75
F = tf.reshape(F, (M, N))
76
return tf.matmul(tf.transpose(F), F)
77
78
def _single_style_loss(a, g):
79
""" Calculate the style loss at a certain layer
80
Inputs:
81
a is the feature representation of the real image
82
g is the feature representation of the generated image
83
Output:
84
the style loss at a certain layer (which is E_l in the paper)
85
86
Hint: 1. you'll have to use the function _gram_matrix()
87
2. we'll use the same coefficient for style loss as in the paper
88
3. a and g are feature representation, not gram matrices
89
"""
90
N = a.shape[3] # number of filters
91
M = a.shape[1] * a.shape[2] # height times width of the feature map
92
A = _gram_matrix(a, N, M)
93
G = _gram_matrix(g, N, M)
94
return tf.reduce_sum((G - A) ** 2 / ((2 * N * M) ** 2))
95
96
def _create_style_loss(A, model):
97
""" Return the total style loss
98
"""
99
n_layers = len(STYLE_LAYERS)
100
E = [_single_style_loss(A[i], model[STYLE_LAYERS[i]]) for i in range(n_layers)]
101
102
###############################
103
## TO DO: return total style loss
104
return sum([W[i] * E[i] for i in range(n_layers)])
105
###############################
106
107
def _create_losses(model, input_image, content_image, style_image):
108
with tf.variable_scope('loss') as scope:
109
with tf.Session() as sess:
110
sess.run(input_image.assign(content_image)) # assign content image to the input variable
111
p = sess.run(model[CONTENT_LAYER])
112
content_loss = _create_content_loss(p, model[CONTENT_LAYER])
113
114
with tf.Session() as sess:
115
sess.run(input_image.assign(style_image))
116
A = sess.run([model[layer_name] for layer_name in STYLE_LAYERS])
117
style_loss = _create_style_loss(A, model)
118
119
##########################################
120
## TO DO: create total loss.
121
## Hint: don't forget the content loss and style loss weights
122
total_loss = CONTENT_WEIGHT * content_loss + STYLE_WEIGHT * style_loss
123
##########################################
124
125
return content_loss, style_loss, total_loss
126
127
def _create_summary(model):
128
""" Create summary ops necessary
129
Hint: don't forget to merge them
130
"""
131
with tf.name_scope('summaries'):
132
tf.summary.scalar('content loss', model['content_loss'])
133
tf.summary.scalar('style loss', model['style_loss'])
134
tf.summary.scalar('total loss', model['total_loss'])
135
tf.summary.histogram('histogram content loss', model['content_loss'])
136
tf.summary.histogram('histogram style loss', model['style_loss'])
137
tf.summary.histogram('histogram total loss', model['total_loss'])
138
return tf.summary.merge_all()
139
140
def train(model, generated_image, initial_image):
141
""" Train your model.
142
Don't forget to create folders for checkpoints and outputs.
143
"""
144
skip_step = 1
145
with tf.Session() as sess:
146
saver = tf.train.Saver()
147
###############################
148
## TO DO:
149
## 1. initialize your variables
150
## 2. create writer to write your graph
151
saver = tf.train.Saver()
152
sess.run(tf.global_variables_initializer())
153
writer = tf.summary.FileWriter('graphs', sess.graph)
154
###############################
155
sess.run(generated_image.assign(initial_image))
156
ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoints/checkpoint'))
157
if ckpt and ckpt.model_checkpoint_path:
158
saver.restore(sess, ckpt.model_checkpoint_path)
159
initial_step = model['global_step'].eval()
160
161
start_time = time.time()
162
for index in range(initial_step, ITERS):
163
if index >= 5 and index < 20:
164
skip_step = 10
165
elif index >= 20:
166
skip_step = 20
167
168
sess.run(model['optimizer'])
169
if (index + 1) % skip_step == 0:
170
###############################
171
## TO DO: obtain generated image and loss
172
gen_image, total_loss, summary = sess.run([generated_image, model['total_loss'],
173
model['summary_op']])
174
175
###############################
176
gen_image = gen_image + MEAN_PIXELS
177
writer.add_summary(summary, global_step=index)
178
print('Step {}\n Sum: {:5.1f}'.format(index + 1, np.sum(gen_image)))
179
print(' Loss: {:5.1f}'.format(total_loss))
180
print(' Time: {}'.format(time.time() - start_time))
181
start_time = time.time()
182
183
filename = 'outputs/%d.png' % (index)
184
utils.save_image(filename, gen_image)
185
186
if (index + 1) % 20 == 0:
187
saver.save(sess, 'checkpoints/style_transfer', index)
188
189
def main():
190
with tf.variable_scope('input') as scope:
191
# use variable instead of placeholder because we're training the intial image to make it
192
# look like both the content image and the style image
193
input_image = tf.Variable(np.zeros([1, IMAGE_HEIGHT, IMAGE_WIDTH, 3]), dtype=tf.float32)
194
195
utils.download(VGG_DOWNLOAD_LINK, VGG_MODEL, EXPECTED_BYTES)
196
utils.make_dir('checkpoints')
197
utils.make_dir('outputs')
198
model = vgg_model.load_vgg(VGG_MODEL, input_image)
199
model['global_step'] = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step')
200
201
content_image = utils.get_resized_image(CONTENT_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH)
202
content_image = content_image - MEAN_PIXELS
203
style_image = utils.get_resized_image(STYLE_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH)
204
style_image = style_image - MEAN_PIXELS
205
206
model['content_loss'], model['style_loss'], model['total_loss'] = _create_losses(model,
207
input_image, content_image, style_image)
208
###############################
209
## TO DO: create optimizer
210
model['optimizer'] = tf.train.AdamOptimizer(LR).minimize(model['total_loss'],
211
global_step=model['global_step'])
212
###############################
213
model['summary_op'] = _create_summary(model)
214
215
initial_image = utils.generate_noise_image(content_image, IMAGE_HEIGHT, IMAGE_WIDTH, NOISE_RATIO)
216
train(model, input_image, initial_image)
217
218
if __name__ == '__main__':
219
main()
220
221