forked from ellie-as/sleep-continual-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerative_model.py
More file actions
255 lines (204 loc) · 9.51 KB
/
generative_model.py
File metadata and controls
255 lines (204 loc) · 9.51 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
import tensorflow as tf
from tensorflow.keras import layers
import tensorflow.keras.backend as K
from tensorflow import keras
from utils import prepare_data
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.linear_model import Perceptron
from sklearn.svm import LinearSVC, SVC
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
from sklearn.naive_bayes import MultinomialNB
def label_classifier(latents, labels, num=200):
np.random.seed(1)
x_train, x_test, y_train, y_test = train_test_split(latents[0], labels,
test_size=0.5, random_state=1)
clf = make_pipeline(StandardScaler(), SVC())
clf.fit(x_train[0:num], y_train[0:num])
score = clf.score(x_test, y_test)
return score
class DecodingHistory(keras.callbacks.Callback):
def __init__(self, dataset):
_, self.test_data, _, _, _, self.test_labels = prepare_data(dataset, labels=True)
self.decoding_history = []
def on_epoch_begin(self, epoch, logs=None):
latents = self.model.encoder.predict(self.test_data)
score = label_classifier(latents, self.test_labels)
self.decoding_history.append(score)
class Sampling(layers.Layer):
# Uses (z_mean, z_log_var) to sample z, the vector encoding a digit.
def call(self, inputs):
z_mean, z_log_var = inputs
batch = tf.shape(z_mean)[0]
dim = tf.shape(z_mean)[1]
epsilon = tf.keras.backend.random_normal(shape=(batch, dim))
return z_mean + tf.exp(0.5 * z_log_var) * epsilon
def encoder_network_large(input_shape, latent_dim=100):
input_img = layers.Input(shape=input_shape)
x = layers.Dropout(0.2, input_shape=input_shape)(input_img)
x = layers.Conv2D(32, 4, strides=(2, 2))(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(64, 4, strides=(2, 2))(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(128, 4, strides=(2, 2))(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(256, 4, strides=(2, 2))(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.GlobalAveragePooling2D()(x)
z_mean = layers.Dense(latent_dim, name='mean')(x)
z_log_var = layers.Dense(latent_dim)(x)
z = Sampling()([z_mean, z_log_var])
encoder = keras.Model(input_img, [z_mean, z_log_var, z], name="encoder")
return encoder, z_mean, z_log_var
def decoder_network_large(latent_dim=100):
decoder_input = layers.Input(shape=(latent_dim,))
x = layers.Dense(4096)(decoder_input)
x = layers.Reshape((4, 4, 256))(x)
x = layers.UpSampling2D((2, 2), interpolation='nearest')(x)
x = layers.Conv2D(128, 3, strides=1, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.UpSampling2D((2, 2), interpolation='nearest')(x)
x = layers.Conv2D(64, 3, strides=1, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.UpSampling2D((2, 2), interpolation='nearest')(x)
x = layers.Conv2D(32, 3, strides=1, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.UpSampling2D((2, 2), interpolation='nearest')(x)
x = layers.Conv2D(1, 3, strides=1, padding='same', activation='sigmoid')(x)
decoder = keras.Model(decoder_input, x)
return decoder
def build_encoder_decoder_large(latent_dim = 5):
input_shape = (28,28,1)
encoder, z_mean, z_log_var = encoder_network_large(input_shape, latent_dim)
decoder = decoder_network_large(latent_dim)
return encoder, decoder
def build_encoder_decoder_small_v1(latent_dim=20):
# Encoder
input_layer = layers.Input(shape=(28, 28, 1))
x = layers.Dropout(0.1, input_shape=(28, 28, 1))(input_layer)
x = layers.Conv2D(32, 3, strides=2, padding="same")(x)
#x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(64, 3, strides=2, padding="same")(x)
#x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Flatten()(x)
x = layers.Dense(16, activation="relu")(x)
x = layers.Dense(latent_dim)(x)
z_mean = layers.Dense(latent_dim, name="z_mean")(x)
z_log_var = layers.Dense(latent_dim, name="z_log_var")(x)
z = Sampling()([z_mean, z_log_var])
encoder = keras.Model(input_layer, [z_mean, z_log_var, z], name="encoder")
# Decoder
latent_inputs = keras.Input(shape=(latent_dim,))
x = layers.Dense(7 * 7 * 64, activation="relu")(latent_inputs)
x = layers.Reshape((7, 7, 64))(x)
x = layers.Conv2DTranspose(64, 3, strides=2, padding="same")(x)
#x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2DTranspose(32, 3, strides=2, padding="same")(x)
#x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
decoder_outputs = layers.Conv2DTranspose(1, 3, activation="sigmoid", padding="same")(x)
decoder = keras.Model(latent_inputs, decoder_outputs, name="decoder")
return encoder, decoder
def build_encoder_decoder_small(latent_dim=20):
# Encoder
input_layer = layers.Input(shape=(28, 28, 1))
x = layers.Dropout(0.1, input_shape=(28, 28, 1))(input_layer)
x = layers.Conv2D(32, 3, activation="relu", strides=2, padding="same")(x)
x = layers.Conv2D(64, 3, activation="relu", strides=2, padding="same")(x)
x = layers.Flatten()(x)
x = layers.Dense(16, activation="relu")(x)
x = layers.Dense(latent_dim)(x)
z_mean = layers.Dense(latent_dim, name="z_mean")(x)
z_log_var = layers.Dense(latent_dim, name="z_log_var")(x)
z = Sampling()([z_mean, z_log_var])
encoder = keras.Model(input_layer, [z_mean, z_log_var, z], name="encoder")
# Decoder
latent_inputs = keras.Input(shape=(latent_dim,))
x = layers.Dense(7 * 7 * 64, activation="relu")(latent_inputs)
x = layers.Reshape((7, 7, 64))(x)
x = layers.Conv2DTranspose(64, 3, activation="relu", strides=2, padding="same")(x)
x = layers.Conv2DTranspose(32, 3, activation="relu", strides=2, padding="same")(x)
decoder_outputs = layers.Conv2DTranspose(1, 3, activation="sigmoid", padding="same")(x)
decoder = keras.Model(latent_inputs, decoder_outputs, name="decoder")
return encoder, decoder
def kl_loss_fn(z_mean, z_log_var, kl_weighting):
# take the sum across the n latent variables
# then take the mean across the batch
kl = K.mean(-0.5 * K.sum(1 + z_log_var \
- K.square(z_mean) \
- K.exp(z_log_var), axis=-1))
return kl_weighting * kl
def reconstruction_loss_fn_v1(x, t_decoded):
# binary_crossentropy() returns result of dim (n_in_batch, pixels)
# take the sum across the 784 pixels
# take the mean across the batch
data = x
reconstruction = t_decoded
# Note that mean_absolute_error loss also gives good results
reconstruction_loss = tf.reduce_mean(tf.reduce_sum(
keras.losses.binary_crossentropy(data, reconstruction), axis=(1,2)))
return reconstruction_loss
def reconstruction_loss_fn(x, t_decoded):
# mean_absolute_error() returns result of dim (n_in_batch, pixels)
# take the sum across the 64x64x3 pixels
# take the mean across the batch
data = x
reconstruction = t_decoded
# note that binary_crossentropy loss also gives good results
reconstruction_loss = tf.reduce_mean(tf.reduce_sum(
keras.losses.mean_absolute_error(data, reconstruction), axis=(1, 2)))
return reconstruction_loss
class VAE(keras.Model):
def __init__(self, encoder, decoder, kl_weighting=1, **kwargs):
super(VAE, self).__init__(**kwargs)
self.encoder = encoder
self.decoder = decoder
self.kl_weighting = kl_weighting
self.total_loss_tracker = keras.metrics.Mean(name="total_loss")
self.reconstruction_loss_tracker = keras.metrics.Mean(
name="reconstruction_loss"
)
self.kl_loss_tracker = keras.metrics.Mean(name="kl_loss")
@property
def metrics(self):
return [
self.total_loss_tracker,
self.reconstruction_loss_tracker,
self.kl_loss_tracker,
]
def train_step(self, data):
with tf.GradientTape() as tape:
z_mean, z_log_var, z = self.encoder(data)
reconstruction = self.decoder(z)
reconstruction_loss = reconstruction_loss_fn(data, reconstruction)
kl_loss = kl_loss_fn(z_mean, z_log_var, self.kl_weighting)
total_loss = reconstruction_loss + kl_loss
grads = tape.gradient(total_loss, self.trainable_weights)
self.optimizer.apply_gradients(zip(grads, self.trainable_weights))
self.total_loss_tracker.update_state(total_loss)
self.reconstruction_loss_tracker.update_state(reconstruction_loss)
self.kl_loss_tracker.update_state(kl_loss)
return {
"loss": self.total_loss_tracker.result(),
"reconstruction_loss": self.reconstruction_loss_tracker.result(),
"kl_loss": self.kl_loss_tracker.result(),
}
models_dict = {"mnist": build_encoder_decoder_small,
"fashion_mnist": build_encoder_decoder_small,
"symmetric_solids": build_encoder_decoder_large,
"shapes3d": build_encoder_decoder_large,
"kmnist": build_encoder_decoder_small,
"plant_village": build_encoder_decoder_small, }