-
Notifications
You must be signed in to change notification settings - Fork 1
/
style_transfer_1.py
125 lines (88 loc) · 2.58 KB
/
style_transfer_1.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
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 14 22:27:57 2019
@author: tanma
"""
from keras.layers import Input, Lambda, Dense, Flatten
from keras.layers import AveragePooling2D, MaxPooling2D
from keras.layers.convolutional import Conv2D
from keras.models import Model, Sequential
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing import image
import keras.backend as K
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fmin_l_bfgs_b
def VGG16_AvgPool(shape):
vgg = VGG16(input_shape=shape, weights='imagenet', include_top=False)
new_model = Sequential()
for layer in vgg.layers:
if layer.__class__ == MaxPooling2D:
new_model.add(AveragePooling2D())
else:
new_model.add(layer)
return new_model
def VGG16_AvgPool_CutOff(shape, num_convs):
if num_convs < 1 or num_convs > 13:
print("num_convs must be in the range [1, 13]")
return None
model = VGG16_AvgPool(shape)
new_model = Sequential()
n = 0
for layer in model.layers:
if layer.__class__ == Conv2D:
n += 1
new_model.add(layer)
if n >= num_convs:
break
return new_model
def unpreprocess(img):
img[..., 0] += 103.939
img[..., 1] += 116.779
img[..., 2] += 126.68
img = img[..., ::-1]
return img
def scale_img(x):
x = x - x.min()
x = x / x.max()
return x
if __name__ == '__main__':
path = 'content/elephant.jpg'
img = image.load_img(path)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
batch_shape = x.shape
shape = x.shape[1:]
content_model = VGG16_AvgPool_CutOff(shape, 11)
target = K.variable(content_model.predict(x))
loss = K.mean(K.square(target - content_model.output))
grads = K.gradients(loss, content_model.input)
get_loss_and_grads = K.function(
inputs=[content_model.input],
outputs=[loss] + grads
)
def get_loss_and_grads_wrapper(x_vec):
l, g = get_loss_and_grads([x_vec.reshape(*batch_shape)])
return l.astype(np.float64), g.flatten().astype(np.float64)
from datetime import datetime
t0 = datetime.now()
losses = []
x = np.random.randn(np.prod(batch_shape))
for i in range(10):
x, l, _ = fmin_l_bfgs_b(
func=get_loss_and_grads_wrapper,
x0=x,
maxfun=20
)
x = np.clip(x, -127, 127)
print("iter=%s, loss=%s" % (i, l))
losses.append(l)
print("duration:", datetime.now() - t0)
plt.plot(losses)
plt.show()
newimg = x.reshape(*batch_shape)
final_img = unpreprocess(newimg)
plt.imshow(scale_img(final_img[0]))
plt.show()