-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathae_models.py
More file actions
398 lines (347 loc) · 15.5 KB
/
ae_models.py
File metadata and controls
398 lines (347 loc) · 15.5 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import keras
from keras.layers import Dense, Flatten, Reshape, Input, Conv2D, MaxPooling2D, UpSampling2D
import tensorflow as tf
import numpy as np
import sys
import math
import matplotlib.pyplot as plt
import seaborn as sns
import random
import time
import cleverhans.model
from utils import *
from tf_utils import *
from attacks import CLIP_MIN, CLIP_MAX
from attacks import *
from train import *
from resnet import resnet_v2, lr_schedule
def my_ae_conv_layer(inputs, f, k, p, batch_norm=True, up=False):
"""
f: num of filters
k: kernel size
p: pooling size
example config:
32, (3,3), (2,2)
"""
x = keras.layers.Conv2D(f, k, padding='same')(inputs)
if batch_norm:
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Activation('relu')(x)
if up:
x = keras.layers.UpSampling2D(p)(x)
else:
x = keras.layers.MaxPooling2D(p, padding='same')(x)
return x
class AEModel():
"""This is the default model for mnist."""
@staticmethod
def NAME():
return "ae"
def __init__(self, cnn_model):
"""I have cnn model here because I might use CNN model to train this AE."""
# FIXME I need to make sure these setups are only called
# once. Otherwise I need to set the AE_vars variables accordingly
self.cnn_model = cnn_model
self.shape = self.cnn_model.xshape()
with tf.variable_scope('my_AE'):
self.x = keras.layers.Input(shape=self.shape, dtype='float32')
# this function is supposed to use self.x as input, and set self.decoded
self.decoded = self.setup_AE(self.x)
self.rec = keras.layers.Activation('sigmoid')(self.decoded)
self.AE1 = keras.models.Model(self.x, self.decoded)
self.AE = keras.models.Model(self.x, self.rec)
self.AE_vars = tf.trainable_variables('my_AE')
def evaluate_np(self, sess, npx):
return sess.run(self.rec, feed_dict={self.x: npx})
def setup_AE(self, x):
raise NotImplementedError()
def train_AE(self, sess, clean_x):
noise_factor = 0.1
noisy_x = clean_x + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=clean_x.shape)
noisy_x = np.clip(noisy_x, CLIP_MIN, CLIP_MAX)
# self.AE.compile('adam', 'binary_crossentropy')
# with sess.as_default():
# self.AE.fit(noisy_x, clean_x, verbose=2)
inputs = keras.layers.Input(shape=self.shape, dtype='float32')
rec = self.AE(inputs)
model = keras.models.Model(inputs, rec)
optimizer = keras.optimizers.RMSprop(0.001)
# 'adadelta'
model.compile(optimizer=optimizer,
# loss='binary_crossentropy'
loss=keras.losses.mean_squared_error
)
with sess.as_default():
callbacks = [get_lr_reducer(), get_es(min_delta=0.0001)]
model.fit(noisy_x, clean_x, epochs=100,
validation_split=0.1,
# verbose=2,
callbacks=callbacks)
def save_weights(self, sess, path):
"""Save weights using keras API."""
with sess.as_default():
self.AE.save_weights(path)
def load_weights(self, sess, path):
with sess.as_default():
self.AE.load_weights(path)
class FCAE(AEModel):
@staticmethod
def NAME():
return "fcAE"
def setup_AE(self, x):
x = Flatten()(x)
x = Dense(32, activation='relu',
# FIXME effect of this regularizer?
activity_regularizer=keras.regularizers.l1(10e-5))(x)
# "decoded" is the lossy reconstruction of the input
x = Dense(784)(x)
decoded = Reshape(self.shape)(x)
return decoded
class deepFCAE(AEModel):
@staticmethod
def NAME():
return "deepfcAE"
def setup_AE(self, x):
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(32, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(128, activation='relu')(x)
# "decoded" is the lossy reconstruction of the input
x = Dense(784)(x)
decoded = Reshape(self.shape)(x)
return decoded
class CNN1AE(AEModel):
@staticmethod
def NAME():
return "cnn1AE"
def setup_AE(self, x):
encoded = my_ae_conv_layer(x, 16, (3,3), (2,2), batch_norm=False)
x = my_ae_conv_layer(encoded, 16, (3,3), (2,2), batch_norm=False, up=True)
# channel = 1 or 3 depending on dataset
channel = self.shape[2]
decoded = keras.layers.Conv2D(channel, (3, 3), padding='same')(x)
return decoded
class CNN2AE(AEModel):
@staticmethod
def NAME():
return "cnn2AE"
def setup_AE(self, x):
x = my_ae_conv_layer(x, 16, (3,3), (2,2), batch_norm=False)
encoded = my_ae_conv_layer(x, 8, (3,3), (2,2), batch_norm=False)
x = my_ae_conv_layer(encoded, 8, (3,3), (2,2), batch_norm=False, up=True)
x = my_ae_conv_layer(x, 16, (3,3), (2,2), batch_norm=False, up=True)
# channel = 1 or 3 depending on dataset
channel = self.shape[2]
decoded = keras.layers.Conv2D(channel, (3, 3), padding='same')(x)
return decoded
def get_wideae_model(filter_sizes):
# [32,32,32,32]
assert (len(filter_sizes) == 4)
class WideAE(AEModel):
"""TODO NOW"""
@staticmethod
def NAME():
return "wide_{}_AE".format('_'.join(map(str, filter_sizes)))
def setup_AE(self, x):
x = my_ae_conv_layer(x, filter_sizes[0], (3,3), (2,2), batch_norm=False)
encoded = my_ae_conv_layer(x, filter_sizes[1], (3,3), (2,2), batch_norm=False)
x = my_ae_conv_layer(encoded, filter_sizes[2], (3,3), (2,2), batch_norm=False, up=True)
x = my_ae_conv_layer(x, filter_sizes[3], (3,3), (2,2), batch_norm=False, up=True)
channel = self.shape[2]
decoded = keras.layers.Conv2D(channel, (3, 3), padding='same')(x)
return decoded
return WideAE
class CNN3AE(AEModel):
@staticmethod
def NAME():
return "cnn3AE"
def setup_AE(self, x):
# assert False
x = my_ae_conv_layer(x, 16, (3,3), (2,2), batch_norm=False)
x = my_ae_conv_layer(x, 8, (3,3), (2,2), batch_norm=False)
encoded = my_ae_conv_layer(x, 8, (3,3), (2,2), batch_norm=False)
x = my_ae_conv_layer(encoded, 8, (3,3), (2,2), batch_norm=False, up=True)
x = my_ae_conv_layer(x, 8, (3,3), (2,2), batch_norm=False, up=True)
# x = my_ae_conv_layer(x, 16, (3,3), (2,2), batch_norm=False, up=True)
# This should not have same padding, so that the dimension is 14*2 instead of 16*2
# But this will cause trouble in cifar dataset
x = keras.layers.Conv2D(16, (3,3), activation='relu')(x)
x = UpSampling2D((2,2))(x)
# channel = 1 or 3 depending on dataset
channel = self.shape[2]
decoded = keras.layers.Conv2D(channel, (3, 3), padding='same')(x)
return decoded
class MNISTAE(AEModel):
@staticmethod
def NAME():
return "mnistAE"
def setup_AE(self, x):
"""From noise_x to x.
Different numbers for AE:
My Default:
32, (3,3), (2,2)
32, (3,3), (2,2)
---
32, (3,3), (2,2)
32, (3,3), (2,2)
---
3, (3,3)
"""
# inputs = keras.layers.Input(shape=self.shape, dtype='float32')
# inputs = keras.layers.Input(shape=(28,28,1), dtype='float32')
# denoising
x = my_ae_conv_layer(x, 32, (3,3), (2,2))
encoded = my_ae_conv_layer(x, 32, (3,3), (2,2))
# at this point the representation is (7, 7, 32)
x = my_ae_conv_layer(encoded, 32, (3,3), (2,2), batch_norm=False, up=True)
x = my_ae_conv_layer(x, 32, (3,3), (2,2), batch_norm=False, up=True)
# channel = 1 or 3 depending on dataset
channel = self.shape[2]
decoded = keras.layers.Conv2D(channel, (3, 3), padding='same')(x)
return decoded
class CifarAEModel(AEModel):
"""This autoencoder has more capacity"""
@staticmethod
def NAME():
return "cifarAE"
# def __init__(self, cnn_model):
# super().__init__(cnn_model)
def setup_AE(self, x):
"""
64, (3,3), (2,2)
32, (3,3), (2,2)
16, (3,3), (2,2)
---
16, (3,3), (2,2)
32, (3,3), (2,2)
64, (3,3), (2,2)
---
3, (3,3)
"""
# denoising
x = my_ae_conv_layer(x, 64, (3,3), (2,2))
x = my_ae_conv_layer(x, 32, (3,3), (2,2))
encoded = my_ae_conv_layer(x, 16, (3,3), (2,2))
x = my_ae_conv_layer(encoded, 16, (3,3), (2,2), batch_norm=False, up=True)
x = my_ae_conv_layer(x, 32, (3,3), (2,2), batch_norm=False, up=True)
x = my_ae_conv_layer(x, 64, (3,3), (2,2), batch_norm=False, up=True)
# channel = 1 or 3 depending on dataset
channel = self.shape[2]
decoded = keras.layers.Conv2D(channel, (3, 3), padding='same')(x)
return decoded
class TestCifarAEModel(AEModel):
"""This autoencoder has more capacity"""
@staticmethod
def NAME():
return "testcifarae"
# def __init__(self, cnn_model):
# super().__init__(cnn_model)
def setup_AE(self):
inputs = keras.layers.Input(shape=self.shape, dtype='float32')
# inputs = keras.layers.Input(shape=(32,32,3), dtype='float32')
# denoising
x = my_ae_conv_layer(inputs, 32, (3,3), (2,2))
x = my_ae_conv_layer(x, 64, (3,3), (2,2))
# x = my_ae_conv_layer(x, 128, (3,3), (2,2))
encoded = my_ae_conv_layer(x, 128, (3,3), (2,2))
x = my_ae_conv_layer(encoded, 128, (3,3), (2,2), batch_norm=False, up=True)
# x = my_ae_conv_layer(x, 128, (3,3), (2,2), batch_norm=False, up=True)
x = my_ae_conv_layer(x, 64, (3,3), (2,2), batch_norm=False, up=True)
x = my_ae_conv_layer(x, 32, (3,3), (2,2), batch_norm=False, up=True)
# channel = 1 or 3 depending on dataset
channel = self.shape[2]
decoded = keras.layers.Conv2D(channel, (3, 3), padding='same')(x)
self.AE1 = keras.models.Model(inputs, decoded)
self.AE = keras.models.Model(inputs, keras.layers.Activation('sigmoid')(decoded))
class DunetModel(AEModel):
"""Unet TODO"""
@staticmethod
def NAME():
# FIXME change NAME to CLSNAME to avoid bugs
return "dunet"
# def __init__(self, cnn_model):
# super().__init__(cnn_model)
def setup_AE(self, x):
"""
inputs = Input((32, 32, 3))
"""
conv1 = keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
conv1 = keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
pool1 = keras.layers.MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = keras.layers.MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conv3 = keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
pool3 = keras.layers.MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conv4 = keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
pool4 = keras.layers.MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = keras.layers.Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
conv5 = keras.layers.Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
up6 = keras.layers.concatenate(
[keras.layers.Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4], axis=3)
conv6 = keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
up7 = keras.layers.concatenate(
[keras.layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3], axis=3)
conv7 = keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)
up8 = keras.layers.concatenate(
[keras.layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2], axis=3)
conv8 = keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)
up9 = keras.layers.concatenate(
[keras.layers.Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1], axis=3)
conv9 = keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conv9 = keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)
# conv10 = keras.layers.Conv2D(3, (3, 3), activation='sigmoid', padding='same')(conv9)
conv10 = keras.layers.Conv2D(3, (3, 3), padding='same')(conv9)
# denoising
# x = my_ae_conv_layer(inputs, 32, (3,3), (2,2))
# encoded = my_ae_conv_layer(x, 32, (3,3), (2,2))
# # at this point the representation is (7, 7, 32)
# x = my_ae_conv_layer(encoded, 32, (3,3), (2,2), batch_norm=False, up=True)
# x = my_ae_conv_layer(x, 32, (3,3), (2,2), batch_norm=False, up=True)
# channel = 1 or 3 depending on dataset
# channel = self.shape[2]
# decoded = keras.layers.Conv2D(channel, (3, 3), padding='same')(x)
# DEBUG XXX TODO NOW use add?
decoded = keras.layers.Add()([x, conv10])
# decoded = keras.layers.Subtract()([inputs, conv10])
# DEBUG what if I just use this as output?
# decoded = conv10
return decoded
class IdentityAEModel(AEModel):
"""This model implementation is a HACK. The AE model does not have any
parameters. It is merely an identity map. However, it still supports
the load and save function, in which the underline CNN model
parameters are saved and loaded. The AE_vars are set to the CNN/FC
vars, so that adv training would change those instead.
"""
def setup_AE(self, x):
# This class is not using a standard setup_AE routine
assert False
@staticmethod
def NAME():
return "identityAE"
def __init__(self, cnn_model):
"""I have cnn model here because I might use CNN model to train this AE."""
# FIXME I need to make sure these setups are only called
# once. Otherwise I need to set the AE_vars variables accordingly
self.cnn_model = cnn_model
self.shape = self.cnn_model.xshape()
with tf.variable_scope('my_AE'):
self.x = keras.layers.Input(shape=self.shape, dtype='float32')
outputs = keras.layers.Lambda(lambda x: x, output_shape=self.shape)(self.x)
# FIMXE this is dummy
self.AE1 = keras.models.Model(self.x, outputs)
self.AE = keras.models.Model(self.x, outputs)
self.AE_vars = tf.trainable_variables('my_AE')
assert not self.AE_vars
# FIXME I removed the save_weights function. The previous one
# should be the wrong one, so need to
# 1. verify if this is working as expected
# 2. rerun previous experiments