-
Notifications
You must be signed in to change notification settings - Fork 0
/
sigma_vae.py
266 lines (241 loc) · 12.3 KB
/
sigma_vae.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
253
254
255
256
257
258
259
260
261
262
263
264
265
"""
Authors : inzapp
Github url : https://github.com/inzapp/sigma-vae
Copyright (c) 2022 Inzapp
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import os
import natsort
import numpy as np
import tensorflow as tf
import cv2
from glob import glob
from tqdm import tqdm
from time import time
from model import Model
from lr_scheduler import LRScheduler
from generator import DataGenerator
class SigmaVAE:
def __init__(self,
train_image_path=None,
input_shape=(64, 64, 1),
lr=0.0005,
batch_size=32,
latent_dim=32,
iterations=100000,
view_grid_size=4,
pretrained_model_path='',
checkpoint_path='checkpoints',
use_optimal_sigma=True,
training_view=False):
assert input_shape[2] in [1, 3]
self.lr = lr
self.iterations = iterations
self.training_view = training_view
self.live_view_previous_time = time()
self.input_shape = input_shape
self.batch_size = batch_size
self.latent_dim = latent_dim
self.checkpoint_path = checkpoint_path
self.view_grid_size = view_grid_size
self.use_optimal_sigma = use_optimal_sigma
if self.latent_dim == -1:
self.latent_dim = self.input_shape[0] // 32 * self.input_shape[1] // 32 * 256
self.model = Model(input_shape=input_shape, latent_dim=self.latent_dim)
self.vae, self.decoder = self.model.build()
# if os.path.exists(pretrained_model_path) and os.path.isfile(pretrained_model_path):
# print(f'\npretrained model path : {[pretrained_model_path]}')
# self.decoder = tf.keras.models.load_model(pretrained_model_path, compile=False)
# print(f'input_shape : {self.input_shape}')
self.train_image_paths = self.init_image_paths(train_image_path)
self.train_data_generator = DataGenerator(
image_paths=self.train_image_paths,
input_shape=input_shape,
batch_size=batch_size,
latent_dim=self.latent_dim)
def init_image_paths(self, image_path):
return glob(f'{image_path}/**/*.jpg', recursive=True)
"""
https://arxiv.org/pdf/2006.13202.pdf
"""
@tf.function
def compute_gradient(self, model, optimizer, x, y_true, trainable_log_sigma, use_optimal_sigma):
def softclip(tensor, min_val):
return min_val + tf.keras.backend.softplus(tensor - min_val)
def gaussian_nll(y_true, y_pred, soft_log_sigma):
return 0.5 * tf.square((y_true - y_pred) / tf.exp(soft_log_sigma)) + soft_log_sigma + 0.5 * tf.math.log(np.pi * 2.0)
with tf.GradientTape() as tape:
y_pred, mu, log_var = model(x, training=True)
mu_mean = tf.reduce_mean(mu)
log_var_mean = tf.reduce_mean(log_var)
reconstruction_mse = tf.reduce_mean(tf.square(y_true - y_pred))
if use_optimal_sigma:
log_sigma = tf.math.log(tf.sqrt(reconstruction_mse))
else:
log_sigma = trainable_log_sigma
soft_log_sigma = softclip(log_sigma, -6.0)
reconstruction_loss = tf.reduce_sum(gaussian_nll(y_true, y_pred, soft_log_sigma))
kl_loss = -0.5 * (1.0 + log_var - tf.square(mu) - tf.exp(log_var))
kl_loss_mean = tf.reduce_mean(kl_loss)
kl_loss = tf.reduce_sum(kl_loss)
loss = (reconstruction_loss + kl_loss) / tf.cast(tf.shape(y_true)[0], dtype=tf.float32) / tf.cast(tf.reduce_sum(tf.shape(y_true)[1:]), dtype=tf.float32)
if use_optimal_sigma:
trainable_variables = model.trainable_variables
else:
trainable_variables = model.trainable_variables + [trainable_log_sigma]
gradients = tape.gradient(loss, trainable_variables)
optimizer.apply_gradients(zip(gradients, trainable_variables))
return reconstruction_mse, kl_loss_mean, mu_mean, log_var_mean, log_sigma
def build_loss_str(self, iteration_count, loss_vars):
reconstruction_mse, kl_loss, mu, log_var, log_sigma = loss_vars
loss_str = f'[iteration_count : {iteration_count:6d}]'
loss_str += f' reconstruction_mse : {reconstruction_mse:.4f}'
loss_str += f', kl_loss: {kl_loss:>8.4f}'
loss_str += f', mu : {mu:>8.4f}'
loss_str += f', log_var : {log_var:>8.4f}'
loss_str += f', log_sigma : {log_sigma:>8.4f}'
return loss_str
def fit(self):
self.model.summary()
print(f'\ntrain on {len(self.train_image_paths)} samples.')
print('start training')
iteration_count = 0
optimizer = tf.keras.optimizers.Adam(lr=self.lr)
os.makedirs(self.checkpoint_path, exist_ok=True)
log_sigma = tf.Variable(-1.5, trainable=True)
lr_scheduler = LRScheduler(lr=self.lr, iterations=self.iterations, policy='step')
while True:
for batch_x in self.train_data_generator:
lr_scheduler.update(optimizer, iteration_count)
loss_vars = self.compute_gradient(self.vae, optimizer, batch_x, batch_x, log_sigma, self.use_optimal_sigma)
iteration_count += 1
print(self.build_loss_str(iteration_count, loss_vars))
if self.training_view:
self.training_view_function()
if iteration_count % 1000 == 0:
model_path_without_extention = f'{self.checkpoint_path}/decoder_{iteration_count}_iter'
self.decoder.save(f'{model_path_without_extention}.h5', include_optimizer=False)
generated_images = self.get_generated_images(self.view_grid_size)
cv2.imwrite(f'{model_path_without_extention}.jpg', generated_images)
print(f'[iteration count : {iteration_count:6d}] model with generated images saved with {model_path_without_extention} h5 and jpg\n')
if iteration_count == self.iterations:
print('\n\ntrain end successfully')
while True:
decoded_images = self.get_decoded_images()
generated_images = self.get_generated_images(grid_size=self.view_grid_size)
cv2.imshow('decoded_images', decoded_images)
cv2.imshow('generated_images', generated_images)
key = cv2.waitKey(0)
if key == 27:
exit(0)
@tf.function
def graph_forward(self, model, x):
return model(x, training=False)
def generate_random_image(self, size=1):
z = np.asarray([DataGenerator.get_z_vector(size=self.latent_dim) for _ in range(size)]).astype('float32')
y = np.asarray(self.graph_forward(self.decoder, z))
y = DataGenerator.denormalize(y)
generated_images = np.clip(np.asarray(y).reshape((size,) + self.input_shape), 0.0, 255.0).astype('uint8')
return generated_images[0] if size == 1 else generated_images
def generate_latent_space_2d(self, split_size=10):
assert split_size > 1
assert self.latent_dim == 2
space = np.linspace(-1.0, 1.0, split_size)
z = []
for i in range(split_size):
for j in range(split_size):
z.append([space[i], space[j]])
z = np.asarray(z).reshape((split_size * split_size, 2)).astype('float32')
y = np.asarray(self.graph_forward(self.decoder, z))
y = DataGenerator.denormalize(y)
generated_images = np.clip(np.asarray(y).reshape((split_size * split_size,) + self.input_shape), 0.0, 255.0).astype('uint8')
return generated_images
def predict(self, img):
img = DataGenerator.resize(img, (self.input_shape[1], self.input_shape[0]))
x = np.asarray(img).reshape((1,) + self.input_shape).astype('float32')
x = DataGenerator.normalize(x)
y = np.asarray(self.graph_forward(self.vae, x)[0]).reshape(self.input_shape)
y = DataGenerator.denormalize(y)
decoded_img = np.clip(y, 0.0, 255.0).astype('uint8')
return img, decoded_img
def show_interpolation(self, frame=100):
space = np.linspace(-1.0, 1.0, frame)
for val in space:
z = np.zeros(shape=(1, self.latent_dim), dtype=np.float32) + val
y = np.asarray(self.graph_forward(self.decoder, z))[0]
y = DataGenerator.denormalize(y)
generated_image = np.clip(np.asarray(y).reshape(self.input_shape), 0.0, 255.0).astype('uint8')
cv2.imshow('interpolation', generated_image)
key = cv2.waitKey(10)
if key == 27:
break
def make_border(self, img, size=5):
return cv2.copyMakeBorder(img, size, size, size, size, None, value=(192, 192, 192))
def training_view_function(self):
cur_time = time()
if cur_time - self.live_view_previous_time > 3.0:
decoded_images = self.get_decoded_images()
generated_images = self.get_generated_images(grid_size=self.view_grid_size)
cv2.imshow('decoded_images', decoded_images)
cv2.imshow('generated_images', generated_images)
cv2.waitKey(1)
self.live_view_previous_time = cur_time
def get_decoded_images(self):
img_paths = np.random.choice(self.train_image_paths, size=self.view_grid_size, replace=False)
input_shape = self.vae.input_shape[1:]
decoded_image_grid = None
for img_path in img_paths:
img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), cv2.IMREAD_GRAYSCALE if input_shape[-1] == 1 else cv2.IMREAD_COLOR)
img, output_image= self.predict(img)
img = DataGenerator.resize(img, (input_shape[1], input_shape[0]))
img, output_image = self.make_border(img), self.make_border(output_image)
if self.input_shape[-1] == 1:
img = img.reshape(img.shape + (1,))
output_image = output_image.reshape(output_image.shape + (1,))
grid_row = np.concatenate([img, output_image], axis=1)
if decoded_image_grid is None:
decoded_image_grid = grid_row
else:
decoded_image_grid = np.append(decoded_image_grid, grid_row, axis=0)
return decoded_image_grid
def get_generated_images(self, grid_size):
if self.latent_dim == 2:
generated_images = self.generate_latent_space_2d(split_size=grid_size)
else:
generated_images = self.generate_random_image(size=grid_size * grid_size)
generated_image_grid = None
for i in range(grid_size):
grid_row = None
for j in range(grid_size):
generated_image = self.make_border(generated_images[i*grid_size+j])
if grid_row is None:
grid_row = generated_image
else:
grid_row = np.append(grid_row, generated_image, axis=1)
if generated_image_grid is None:
generated_image_grid = grid_row
else:
generated_image_grid = np.append(generated_image_grid, grid_row, axis=0)
return generated_image_grid
def show_generated_images(self):
while True:
generated_images = self.get_generated_images(grid_size=self.view_grid_size)
cv2.imshow('generated_images', generated_images)
key = cv2.waitKey(0)
if key == 27:
break