This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathladder_model.py
252 lines (195 loc) · 11.7 KB
/
ladder_model.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import numpy as np
import tensorflow as tf
import layers
from tftools import calculate_class_weights, trainer
from tensorflow.contrib.tensorboard.plugins import projector
def encoder(inputs, mode, layer_params, scope="encoder", reuse=None):
with tf.variable_scope(scope, reuse=reuse):
nb_layers = len(layer_params)
L = nb_layers - 1
z = {}
z_pre = {}
h = {}
outputs = {'y': None, 'z_pre': list(), 'z': list()}
with tf.variable_scope('input_layer'):
z_pre[0] = inputs['x']
h[0] = z[0] = layers.noise(tf.layers.batch_normalization(z_pre[0],
training=(mode == tf.estimator.ModeKeys.TRAIN),
center=False,
scale=False,
name='batch_norm'),
noise_pwr=layer_params[0]['noise'] * inputs['noise_mod'],
name="noise")
for it in range(L):
i = it + 1 # first layer is input layer
print(h[i-1])
# Reshape
if not np.asarray(layer_params[i]['input_shape'] == layer_params[i - 1]['output_shape']).all():
with tf.variable_scope('reshape_layer_' + str(i - 1) + str(i)):
h[i - 1] = tf.reshape(h[i - 1], layer_params[i]['input_shape'])
# Transformation
with tf.variable_scope('enc_layer_' + str(i)):
if layer_params[i]['type'] == 'conv':
k_size = layer_params[i]['kernel']
z_pre[i] = tf.layers.conv2d(tf.pad(h[i - 1],
[[0, 0],
[k_size - 1, k_size - 1],
[k_size - 1, k_size - 1],
[0, 0]]),
filters=layer_params[i]['output_shape'][-1],
kernel_size=[k_size, k_size],
use_bias=False,
name='conv_layer')
elif layer_params[i]['type'] == 'fc':
z_pre[i] = tf.layers.dense(h[i - 1],
units=layer_params[i]['output_shape'][-1],
use_bias=False,
name='dense_layer')
elif layer_params[i]['type'] == 'maxpool':
k_size = layer_params[i]['kernel']
z_pre[i] = tf.layers.max_pooling2d(h[i - 1], k_size, k_size, padding='SAME')
elif layer_params[i]['type'] == 'meanpool':
k_size = layer_params[i]['kernel']
z_pre[i] = tf.layers.average_pooling2d(h[i - 1], k_size, k_size, padding='SAME')
else:
raise ValueError('Invalid layer type: \n - layer: '+str(i)+" \n - type: "+layer_params[i]['type'])
# Normalization & Noise
z[i] = layers.noise(tf.layers.batch_normalization(z_pre[i],
training=(mode == tf.estimator.ModeKeys.TRAIN),
center=False,
scale=False,
name="batch_norm"),
noise_pwr=layer_params[i]['noise'],
name="noise")
# Rescale
with tf.variable_scope('activation'):
activation_shape = layer_params[i]['output_shape'][-1]
beta = tf.get_variable('beta',
shape=[activation_shape],
initializer=tf.zeros_initializer(),
trainable=layer_params[i]['center'])
gamma = tf.get_variable('gamma',
shape=[activation_shape],
initializer=tf.ones_initializer(),
trainable=layer_params[i]['scale'])
# Activation
h[i] = layer_params[i]['activation'](gamma * (z[i] + beta))
outputs['z_pre'] = z_pre
outputs['z'] = z
outputs['y'] = h[L]
return outputs
def decoder(inputs, mode, layer_params, scope="decoder", reuse=None):
with tf.variable_scope(scope, reuse=reuse):
z_est = {}
u_pre = {}
u = {}
outputs = {}
nb_layers = len(layer_params)
L = nb_layers - 1
u[L] = inputs['y']
z_corr = inputs['z']
for it in range(L):
i = L - it # first layer is input layer
with tf.variable_scope('dec_layer_' + str(i)):
# Denoise layer
z_est[i] = layers.denoise(u[i], z_corr[i], "denoise")
# Transformation
if layer_params[i]['type'] == 'conv':
k_size = layer_params[i]['kernel']
u_pre[i - 1] = tf.layers.conv2d_transpose(z_est[i],
filters=layer_params[i]['input_shape'][-1],
kernel_size=[k_size, k_size],
use_bias=False,
name="deconv_layer")[:, k_size - 1:-k_size + 1, k_size - 1:-k_size + 1, :]
elif layer_params[i]['type'] == 'maxpool' or layer_params[i]['type'] == 'meanpool':
k_size = layer_params[i]['kernel']
u_pre[i - 1] = tf.layers.conv2d_transpose(z_est[i],
filters=layer_params[i]['input_shape'][-1],
kernel_size=k_size,
strides=[k_size, k_size],
use_bias=False,
kernel_initializer=tf.ones_initializer(),
trainable=False
)
elif layer_params[i]['type'] == 'fc':
u_pre[i - 1] = tf.layers.dense(z_est[i],
units=layer_params[i]['input_shape'][-1],
use_bias=False,
name='dense_layer')
else:
raise ValueError('Invalid layer type: \n - layer: '+str(i)+" \n - type: "+layer_params[i]['type'])
# Normalization
u[i - 1] = tf.layers.batch_normalization(u_pre[i - 1],
training=(mode == tf.estimator.ModeKeys.TRAIN),
center=False,
scale=False,
name="batch_norm_layer")
print(u[i-1])
if not np.asarray(layer_params[i]['input_shape'] == layer_params[i - 1]['output_shape']).all():
with tf.variable_scope('reshape_layer_' + str(i) + str(i - 1)):
u[i - 1] = tf.reshape(u[i - 1], layer_params[i - 1]['output_shape'])
with tf.variable_scope('output_layer'):
z_est[0] = layers.denoise(u[0], z_corr[0], name="denoise")
outputs['z_est'] = z_est
return outputs
def ladder_fn(features, labels, mode, params):
labels = tf.one_hot(labels, depth=10)
x_mean = params['x_mean']
x_std = params['x_std']
layer_params = params['layer_params']
with tf.variable_scope('ladder'):
x = features['inputs']
x_norm = (x - x_mean) / x_std
inputs = {'x': x_norm}
with tf.variable_scope('encoder') as enc_scope:
enc_inputs = {'x': inputs['x'],
'noise_mod': tf.zeros([], dtype=tf.float32)}
enc_clean = encoder(enc_inputs, mode, layer_params=layer_params, scope=enc_scope)
enc_inputs['noise_mod'] = tf.ones([], dtype=tf.float32)
enc_noisy = encoder(enc_inputs, mode, layer_params=layer_params, scope=enc_scope, reuse=True)
embedding = tf.Variable(tf.zeros_like(enc_clean['z'][len(layer_params)-2]), trainable=False, name="emb_code")
embed_update = embedding.assign(enc_clean['z'][len(layer_params)-2])
config = projector.ProjectorConfig()
proj_embedding = config.embeddings.add()
proj_embedding.tensor_name = embedding.name
with tf.variable_scope('decoder') as dec_scope:
dec_inputs = {}
dec_inputs['z'] = enc_noisy['z']
dec_inputs['y'] = enc_noisy['y']
dec = decoder(dec_inputs, mode, layer_params=layer_params, scope=dec_scope)
nb_layers = len(layer_params)
# layerwise loss
for i in range(nb_layers):
with tf.variable_scope('loss_' + str(i)):
z_pre_norm = tf.layers.batch_normalization(enc_clean['z'][i],
training=(mode == tf.estimator.ModeKeys.TRAIN),
scale=False,
center=False,
name="batch_norm")
z_est_norm = tf.layers.batch_normalization(dec['z_est'][i],
training=(mode == tf.estimator.ModeKeys.TRAIN),
trainable=False,
scale=False,
center=False,
reuse=True,
name="batch_norm")
tf.losses.mean_squared_error(z_pre_norm, z_est_norm)
# supervised loss
with tf.variable_scope('loss_sup'):
tf.losses.softmax_cross_entropy(labels, enc_noisy['y'])
predictions = tf.one_hot(tf.argmax(enc_clean['y'], axis=1),
depth=layer_params[-1]['output_shape'][-1])
x_est = dec["z_est"][0] * x_std + x_mean
tf.summary.image("reconstruction", tf.reshape(x_est, [-1, 28, 28, 1]))
loss_op = tf.losses.get_total_loss()
train_op = trainer(loss_op, tf.train.get_global_step(), learning_rate=params['learning_rate'])
eval_metric_ops = {
"accuracy": tf.metrics.accuracy(labels, predictions),
"precision": tf.metrics.precision(labels, predictions),
"mse": tf.metrics.mean_squared_error(x, x_est)
}
return tf.estimator.EstimatorSpec(mode,
predictions={'predictions': predictions, 'embedding': enc_clean['z'][len(layer_params)-2]},
loss=loss_op,
train_op=train_op,
eval_metric_ops=eval_metric_ops)