-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
144 lines (128 loc) · 5.8 KB
/
models.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
from skimage.util import random_noise
import random
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import math
import matplotlib.pyplot as plt
from scipy.stats import norm
import tensorflow as tf
import cv2
import os
import glob
from keras.layers import Input, Dense, Lambda
regularizer = keras.regularizers.l1_l2(0.01)
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), mean=0,stddev=0)
return z_mean + tf.exp(0.5 * z_log_var) * epsilon
def build_encoder(latent_dim, shape):
encoder_inputs = keras.Input(shape=shape)
regularizer = keras.regularizers.l1_l2(0.01)
x = layers.Conv2D(16, 3, activation="relu", strides=1, padding="same",
kernel_regularizer=regularizer)(encoder_inputs)
x = layers.Conv2D(32, 3, activation="relu", strides=2, padding="same",
kernel_regularizer=regularizer)(x)
x = layers.Conv2D(64, 3, activation="relu", strides=2, padding="same",
kernel_regularizer=regularizer)(x)
x = layers.Conv2D(72, 3, activation="relu", strides=1, padding="same",
kernel_regularizer=regularizer)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = layers.Flatten()(x)
x = layers.Dense(128, activation="relu")(x)
z = layers.Dense(latent_dim, name="z")(x)
z_sig = layers.Dense(latent_dim, activation='softplus')(x)
encoder = keras.Model(encoder_inputs, [z, z_sig], name="encoder")
return encoder
def build_decoder(latent_dim, shape):
latent_inputs = keras.Input(shape=(latent_dim,))
regularizer = keras.regularizers.l1_l2(0.01)
x = layers.Dense(shape[0] * shape[1] * 16, activation="relu",
kernel_regularizer=regularizer)(latent_inputs)
x = layers.Reshape((shape[0]//4, shape[1]//4, 256))(x)
x = layers.Conv2DTranspose(72, 3, activation="relu", strides=1,
kernel_regularizer=regularizer, padding="same")(x)
x = layers.Conv2DTranspose(48, 3, activation="relu", strides=2,
kernel_regularizer=regularizer, padding="same")(x)
x = layers.Conv2DTranspose(32, 3, activation="relu", strides=1,
kernel_regularizer=regularizer, padding="same")(x)
x = layers.Conv2DTranspose(16, 3, activation="relu", strides=2,
kernel_regularizer=regularizer, padding="same")(x)
output = layers.Conv2DTranspose(3, 3,
activation="sigmoid",
kernel_regularizer=regularizer,
padding="same")(x)
decoder = keras.Model(latent_inputs, output)
return decoder
def build_transformation(latent_dim):
model = Sequential()
model.add(Dense(latent_dim, activation='relu', input_shape=(latent_dim,)))
model.add(Dense(latent_dim, activation='relu'))
model.add(Dense(latent_dim, activation='relu'))
return model
class VAE(tf.keras.Model):
def __init__(self, latent_dim, shape):
super(VAE, self).__init__()
self.latent_dim = latent_dim
self.encoder = build_encoder(latent_dim, shape)
self.transform = build_transformation(latent_dim)
self.decoder = build_decoder(latent_dim, shape)
self.sampling = Sampling()
self.shape = shape
def call(self, x):
mean, logvar = self.encode(x)
z = self.reparameterize(mean, logvar)
z = self.transform(z)
return self.decode(z)
@tf.function
def encode(self, x):
mean, logvar = self.encoder(x)
return mean, logvar
def reparameterize(self, mean, logvar):
return self.sampling([mean, logvar])
def decode(self, z, apply_sigmoid=False):
logits = self.decoder(z)
if apply_sigmoid:
probs = tf.sigmoid(logits)
return probs
return logits
def elbo_loss(self, z, mu, logvar, target):
recons = self.decode(z)
mse = tf.reduce_mean(tf.keras.losses.MSE(target, recons))
mse *= self.shape[0] * self.shape[1]
kld = -0.5 * tf.reduce_mean(1 + logvar - tf.math.pow(mu, 2) - tf.math.exp(logvar))
return mse + kld
def train_step(self, inputs):
noise = inputs[0][0]
clean = inputs[0][1]
# data: [batch * height * width * channel], noise image
# label: [batch * height * width * channel], clean image
with tf.GradientTape(persistent=True) as tape:
m_n, var_n = self.encode(noise)
z_n = self.transform(self.reparameterize(m_n, var_n))
m_c, var_c = self.encode(clean)
z_c = self.reparameterize(m_c, var_c)
latent_loss = tf.reduce_mean(tf.keras.losses.MSE(z_c, z_n))
elbo_loss = self.elbo_loss(z_n, m_n, var_n, clean)
tran_grads = tape.gradient(latent_loss, self.transform.trainable_weights)
self.optimizer.apply_gradients(zip(tran_grads, self.transform.trainable_weights))
vae_grads = tape.gradient(elbo_loss, self.trainable_weights)
self.optimizer.apply_gradients(zip(vae_grads, self.trainable_weights))
return {
"elbo_loss": elbo_loss,
"latent_loss": latent_loss,
}
def train_model(model, clear_images, noise_images, epoch=1, batch_size=128):
lr = keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=0.001,
decay_steps=1000,
decay_rate=0.95
)
model.compile(optimizer=keras.optimizers.Adam(learning_rate=lr))
model.fit((noise_images,clear_images), epochs=epoch, batch_size=batch_size)
return model