-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvq_vae.py
366 lines (301 loc) · 11.5 KB
/
vq_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
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
"""
Primary code to train the Pixel VQ-VAE.
"""
import os
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import pytorch_msssim
from tqdm import tqdm
import utils.data as data
import utils.graphics as graphics
import utils.loss as loss
from models import vqvae
seed = 42
np.random.seed(seed)
_ = torch.manual_seed(seed)
################################################################################
#################################### Config ####################################
################################################################################
experiment_name = f"vq_vae_v5.10"
# Hyperparameters
learning_rate = 1e-4
epochs = 25
batch_size = 64
num_dataloader_workers = 0
# VQ-VAE Config
num_layers = 0
num_embeddings = 256
embedding_dim = 32
commitment_cost = 0.25
use_max_filters = True # PixelSight Layer
max_filters = 512
small_conv = True # Adapter Layer
# Loss Config
use_sum = False # Use a sum instead of a mean for our loss function
use_ssim_loss = False
mse_weight = 1
ssim_weight = 1
# Data Config
image_size = 64
use_noise_images = True
load_data_to_memory = True
data_prefix = "data\\Pokemon\\final\\standard"
train_data_folder = os.path.join(data_prefix, "train")
val_data_folder = os.path.join(data_prefix, "val")
test_data_folder = os.path.join(data_prefix, "test")
output_prefix = f"data\\{experiment_name}"
output_dir = os.path.join(output_prefix, "generated")
loss_output_path = output_prefix
model_output_path = os.path.join(output_prefix, "model.pt")
animation_output_path = os.path.join(output_prefix, "animation.mp4")
animation_sample_image_name = os.path.join(output_prefix, "animation_base.jpg")
test_sample_input_name = os.path.join(output_prefix, "test_sample_input.jpg")
test_sample_output_name = os.path.join(output_prefix, "test_sample_output.jpg")
################################################################################
##################################### Setup ####################################
################################################################################
# Setup Device
gpu = torch.cuda.is_available()
device = torch.device("cuda" if gpu else "cpu")
print(gpu, device)
# Create Output Paths
if not os.path.exists(output_dir):
os.makedirs(output_dir)
################################################################################
################################## Data Setup ##################################
################################################################################
# Preprocess & Create Data Loaders
transform = data.image2tensor_resize(image_size)
if load_data_to_memory:
# Load Data
train = data.load_images_from_folder(train_data_folder, use_noise_images)
val = data.load_images_from_folder(val_data_folder, use_noise_images)
test = data.load_images_from_folder(test_data_folder, use_noise_images)
train_data = data.CustomDataset(train, transform)
val_data = data.CustomDataset(val, transform)
test_data = data.CustomDataset(test, transform)
else:
train_data = data.CustomDatasetNoMemory(train_data_folder, transform, use_noise_images)
val_data = data.CustomDatasetNoMemory(val_data_folder, transform, use_noise_images)
test_data = data.CustomDatasetNoMemory(test_data_folder, transform, use_noise_images)
train_dataloader = torch.utils.data.DataLoader(
train_data,
batch_size=batch_size,
shuffle=True,
num_workers=num_dataloader_workers,
pin_memory=gpu,
)
val_dataloader = torch.utils.data.DataLoader(
val_data,
batch_size=batch_size,
shuffle=True,
num_workers=num_dataloader_workers,
pin_memory=gpu,
)
test_dataloader = torch.utils.data.DataLoader(
test_data,
batch_size=batch_size,
shuffle=True,
num_workers=num_dataloader_workers,
pin_memory=gpu,
)
# Creating a sample set to visualize the model's training
sample = data.get_samples_from_data(val_data, 16)
################################################################################
##################################### Model ####################################
################################################################################
# Create Model
model = vqvae.VQVAE(
num_layers=num_layers,
input_image_dimensions=image_size,
small_conv=small_conv,
embedding_dim=embedding_dim,
num_embeddings=num_embeddings,
commitment_cost=commitment_cost,
use_max_filters=use_max_filters,
max_filters=max_filters,
)
print(model)
model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
ssim_module = None
if use_ssim_loss:
ssim_module = pytorch_msssim.SSIM(
data_range=1.0, win_size=11, win_sigma=1.5, K=(0.01, 0.03)
)
################################################################################
################################### Training ###################################
################################################################################
# Train
all_samples = []
all_train_loss = []
train_perplexity = []
all_val_loss = []
val_perplexity = []
# Get an initial "epoch 0" sample
model.eval()
with torch.no_grad():
_, epoch_sample, _, _ = model(sample.to(device))
# Add sample reconstruction to our list
all_samples.append(epoch_sample.detach().cpu())
for epoch in range(epochs):
train_loss = 0
train_recon_loss = 0
train_vq_loss = 0
train_epoch_perplexity = []
val_loss = 0
val_recon_loss = 0
val_vq_loss = 0
val_epoch_perplexity = []
# Training Loop
model.train()
for iteration, batch in enumerate(tqdm(train_dataloader)):
# Reset gradients back to zero for this iteration
optimizer.zero_grad()
# Move batch to device
_, batch = batch # Returns key, value for each Pokemon
batch = batch.to(device)
# Run our model & get outputs
vq_loss, reconstructed, perplexity, _ = model(batch)
# Calculate reconstruction loss
batch_loss, loss_dict = loss.mse_ssim_loss(
reconstructed,
batch,
use_sum=use_sum,
ssim_module=ssim_module,
mse_weight=mse_weight,
ssim_weight=ssim_weight,
)
# Add VQ-Loss to Overall Loss
batch_loss += vq_loss
loss_dict["Commitment Loss"] = vq_loss.item()
# Backprop
batch_loss.backward()
# Update our optimizer parameters
optimizer.step()
# Add the batch's loss to the total loss for the epoch
train_loss += batch_loss.item()
train_recon_loss += loss_dict["MSE"] + loss_dict["SSIM"]
train_vq_loss += loss_dict["Commitment Loss"]
train_epoch_perplexity.append(perplexity.item())
# Validation Loop
model.eval()
with torch.no_grad():
for iteration, batch in enumerate(tqdm(val_dataloader)):
# Move batch to device
_, batch = batch # Returns key, value for each Pokemon
batch = batch.to(device)
# Run our model & get outputs
vq_loss, reconstructed, perplexity, _ = model(batch)
# Calculate reconstruction loss
batch_loss, loss_dict = loss.mse_ssim_loss(
reconstructed,
batch,
use_sum=use_sum,
ssim_module=ssim_module,
mse_weight=mse_weight,
ssim_weight=ssim_weight,
)
# Add VQ-Loss to Overall Loss
batch_loss += vq_loss
loss_dict["Commitment Loss"] = vq_loss.item()
# Add the batch's loss to the total loss for the epoch
val_loss += batch_loss.item()
val_recon_loss += loss_dict["MSE"] + loss_dict["SSIM"]
val_vq_loss += loss_dict["Commitment Loss"]
val_epoch_perplexity.append(perplexity.item())
# Get reconstruction of our sample
_, epoch_sample, _, _ = model(sample.to(device))
# Add sample reconstruction to our list
all_samples.append(epoch_sample.detach().cpu())
# Compute the average losses for this epoch
train_loss = train_loss / len(train_dataloader)
train_recon_loss = train_recon_loss / len(train_dataloader)
train_vq_loss = train_vq_loss / len(train_dataloader)
all_train_loss.append((train_loss, train_recon_loss, train_vq_loss))
train_epoch_perplexity = np.mean(train_epoch_perplexity)
train_perplexity.append(train_epoch_perplexity)
val_loss = val_loss / len(val_dataloader)
val_recon_loss = val_recon_loss / len(val_dataloader)
val_vq_loss = val_vq_loss / len(val_dataloader)
all_val_loss.append((val_loss, val_recon_loss, val_vq_loss))
val_epoch_perplexity = np.mean(val_epoch_perplexity)
val_perplexity.append(val_epoch_perplexity)
# Print Metrics
print(
f"\nEpoch: {epoch+1}/{epochs}:\
\nTrain Loss = {train_loss}\
\nTrain Reconstruction Loss = {train_recon_loss}\
\nTrain Commitment Loss = {train_vq_loss}\
\nTrain Perplexity = {train_epoch_perplexity}\
\nVal Loss = {val_loss}\
\nVal Reconstruction Loss = {val_recon_loss}\
\nVal Commitment Loss = {val_vq_loss}\
\nVal Perplexity = {val_epoch_perplexity}"
)
################################################################################
################################## Save & Test #################################
################################################################################
# Generate Loss Graph
graphics.draw_loss(all_train_loss, all_val_loss, loss_output_path, mode="vqvae")
graphics.plot_and_save_loss(
train_perplexity,
"Train Perplexity",
val_perplexity,
"Validation Perplexity",
os.path.join(loss_output_path, "perplexity.jpg"),
)
# Save Model
torch.save(model.state_dict(), model_output_path)
# Plot Animation Sample
fig, axis = graphics.make_grid(("Sample", sample), 4, 4)
plt.savefig(animation_sample_image_name)
# Create & Save Animation
anim = graphics.make_animation(graphics.make_grid, all_samples)
anim.save(animation_output_path)
model.eval()
# Evaluate on Test Images
# Save Generated Images & Calculate Metrics
# Testing Loop - Standard
all_mse = []
all_ssim = []
with torch.no_grad():
for iteration, batch in enumerate(tqdm(test_dataloader)):
# Move batch to device
filenames, image = batch
image = image.to(device)
# Run our model & get outputs
_, reconstructed, _, _ = model(image)
# Calculate Metrics
mse = nn.functional.mse_loss(reconstructed, image)
ssim_score = pytorch_msssim.ssim(
reconstructed,
image,
data_range=1.0,
win_size=11,
win_sigma=1.5,
K=(0.01, 0.03),
)
# Add metrics to tracking list
all_mse.append(mse.detach().cpu().numpy())
all_ssim.append(ssim_score.detach().cpu().numpy())
# Save
reconstructed = reconstructed.permute(0, 2, 3, 1).detach().cpu().numpy()
for image, filename in zip(reconstructed, filenames):
plt.imsave(os.path.join(output_dir, filename), image)
# Print Metrics
mse = np.asarray(all_mse).mean()
ssim_score = np.asarray(all_ssim).mean()
print(f"\nMSE = {mse}, SSIM = {ssim_score}")
# Pick a couple of sample images for an Input v Output comparison
test_sample = data.get_samples_from_data(test_data, 16)
# Plot A Set of Test Images
fig, axis = graphics.make_grid(("Test Sample", test_sample), 4, 4)
plt.savefig(test_sample_input_name)
with torch.no_grad():
reconstructed = model(test_sample.to(device))[1].detach().cpu()
# Plot A Set of Reconstructed Test Images
fig, axis = graphics.make_grid(("Test Sample", reconstructed), 4, 4)
plt.savefig(test_sample_output_name)