-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathops.py
More file actions
150 lines (107 loc) · 5.09 KB
/
ops.py
File metadata and controls
150 lines (107 loc) · 5.09 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
from __future__ import division, print_function
import tensorflow as tf
import numpy as np
from keras.layers import Conv2D, Lambda
### Layers ###
def pad_reflect(x, padding=1):
return tf.pad(
x, [[0, 0], [padding, padding], [padding, padding], [0, 0]],
mode='REFLECT')
def Conv2DReflect(lambda_name, *args, **kwargs):
'''Wrap Keras Conv2D with reflect padding'''
return Lambda(lambda x: Conv2D(*args, **kwargs)(pad_reflect(x)), name=lambda_name)
### Whiten-Color Transform ops ###
def wct_tf(content, style, alpha, eps=1e-8):
'''TensorFlow version of Whiten-Color Transform
Assume that content/style encodings have shape 1xHxWxC
See p.4 of the Universal Style Transfer paper for corresponding equations:
https://arxiv.org/pdf/1705.08086.pdf
'''
# Remove batch dim and reorder to CxHxW
content_t = tf.transpose(tf.squeeze(content), (2, 0, 1))
style_t = tf.transpose(tf.squeeze(style), (2, 0, 1))
Cc, Hc, Wc = tf.unstack(tf.shape(content_t))
Cs, Hs, Ws = tf.unstack(tf.shape(style_t))
# CxHxW -> CxH*W
content_flat = tf.reshape(content_t, (Cc, Hc*Wc))
style_flat = tf.reshape(style_t, (Cs, Hs*Ws))
# Content covariance
mc = tf.reduce_mean(content_flat, axis=1, keep_dims=True)
fc = content_flat - mc
fcfc = tf.matmul(fc, fc, transpose_b=True) / (tf.cast(Hc*Wc, tf.float32) - 1.) + tf.eye(Cc)*eps
# Style covariance
ms = tf.reduce_mean(style_flat, axis=1, keep_dims=True)
fs = style_flat - ms
fsfs = tf.matmul(fs, fs, transpose_b=True) / (tf.cast(Hs*Ws, tf.float32) - 1.) + tf.eye(Cs)*eps
# tf.svd is slower on GPU, see https://github.com/tensorflow/tensorflow/issues/13603
with tf.device('/cpu:0'):
Sc, Uc, _ = tf.svd(fcfc)
Ss, Us, _ = tf.svd(fsfs)
## Uncomment to perform SVD for content/style with np in one call
## This is slower than CPU tf.svd but won't segfault for ill-conditioned matrices
# def np_svd(content, style):
# '''tf.py_func helper to run SVD with NumPy for content/style cov tensors'''
# Uc, Sc, _ = np.linalg.svd(content)
# Us, Ss, _ = np.linalg.svd(style)
# return Uc, Sc, Us, Ss
# Uc, Sc, Us, Ss = tf.py_func(np_svd, [fcfc, fsfs], [tf.float32, tf.float32, tf.float32, tf.float32])
# Filter small singular values
k_c = tf.reduce_sum(tf.cast(tf.greater(Sc, 1e-5), tf.int32))
k_s = tf.reduce_sum(tf.cast(tf.greater(Ss, 1e-5), tf.int32))
# Whiten content feature
Dc = tf.diag(tf.pow(Sc[:k_c] + eps, -0.5))
fc_hat = tf.matmul(tf.matmul(tf.matmul(Uc[:,:k_c], Dc), Uc[:,:k_c], transpose_b=True), fc)
# Color content with style
Ds = tf.diag(tf.pow(Ss[:k_s] + eps, 0.5))
fcs_hat = tf.matmul(tf.matmul(tf.matmul(Us[:,:k_s], Ds), Us[:,:k_s], transpose_b=True), fc_hat)
# Re-center with mean of style
fcs_hat = fcs_hat + ms
# Blend whiten-colored feature with original content feature
blended = alpha * fcs_hat + (1 - alpha) * (fc + mc)
# CxH*W -> CxHxW
blended = tf.reshape(blended, (Cc,Hc,Wc))
# CxHxW -> 1xHxWxC
blended = tf.expand_dims(tf.transpose(blended, (1,2,0)), 0)
return blended
def wct_np(content, style, alpha=0.6, eps=1e-5):
'''Perform Whiten-Color Transform on feature maps using numpy
See p.4 of the Universal Style Transfer paper for equations:
https://arxiv.org/pdf/1705.08086.pdf
'''
# HxWxC -> CxHxW
content_t = np.transpose(content, (2, 0, 1))
style_t = np.transpose(style, (2, 0, 1))
# CxHxW -> CxH*W
content_flat = content_t.reshape(-1, content_t.shape[1]*content_t.shape[2])
style_flat = style_t.reshape(-1, style_t.shape[1]*style_t.shape[2])
mc = content_flat.mean(axis=1, keepdims=True)
fc = content_flat - mc
fcfc = np.dot(fc, fc.T) / (content_t.shape[1]*content_t.shape[2] - 1)
Ec, wc, _ = np.linalg.svd(fcfc)
Dc_sq_inv = np.linalg.inv(np.sqrt(np.diag(wc+eps)))
fc_hat = Ec.dot(Dc_sq_inv).dot(Ec.T).dot(fc)
ms = style_flat.mean(axis=1, keepdims=True)
fs = style_flat - ms
fsfs = np.dot(fs, fs.T) / (style_t.shape[1]*style_t.shape[2] - 1)
Es, ws, _ = np.linalg.svd(fsfs)
Ds_sq = np.sqrt(np.diag(ws+eps))
fcs_hat = Es.dot(Ds_sq).dot(Es.T).dot(fc_hat)
fcs_hat = fcs_hat + ms
blended = alpha*fcs_hat + (1 - alpha)*(fc)
# CxH*W -> CxHxW
blended = blended.reshape(content_t.shape)
# CxHxW -> HxWxC
blended = np.transpose(blended, (1,2,0))
return blended
### Misc ###
def torch_decay(learning_rate, global_step, decay_rate, name=None):
'''Adapted from https://github.com/torch/optim/blob/master/adam.lua'''
if global_step is None:
raise ValueError("global_step is required for exponential_decay.")
with tf.name_scope(name, "ExponentialDecay", [learning_rate, global_step, decay_rate]) as name:
learning_rate = tf.convert_to_tensor(learning_rate, name="learning_rate")
dtype = learning_rate.dtype
global_step = tf.cast(global_step, dtype)
decay_rate = tf.cast(decay_rate, dtype)
# local clr = lr / (1 + state.t*lrd)
return learning_rate / (1 + global_step*decay_rate)