-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgated_pixelcnn_prior.py
296 lines (249 loc) · 9.33 KB
/
gated_pixelcnn_prior.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
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, gated_pixelcnn
seed = 42
np.random.seed(seed)
_ = torch.manual_seed(seed)
################################################################################
#################################### Config ####################################
################################################################################
learning_rate = 1e-4
epochs = 1
batch_size = 32
num_dataloader_workers = 0
image_size = 64
use_noise_images = True
load_data_to_memory = False
experiment_name = f"gated_pixelcnn_v1"
# VQ-VAE Config
mode = "discrete"
vq_vae_experiment_name = f"vq_vae_v5.17"
vq_vae_num_layers = 2
vq_vae_max_filters = 512
vq_vae_use_max_filters = True
vq_vae_num_embeddings = 1024
vq_vae_embedding_dim = 64
vq_vae_commitment_cost = 0.25
vq_vae_small_conv = True # To use the 1x1 convolution layer
# Pixel CNN Config
input_dim = image_size // (2 ** vq_vae_num_layers)
input_channels = 1
hidden_channels = 128
num_classes = vq_vae_num_embeddings
kernel_size = 3
sample_batch_size = batch_size
num_sample_batches = 5
use_bits_per_dimension_loss = False
use_dilation = True
# Data Config
data_prefix = "data\\pokemon\\final\\standard"
output_prefix = f"data\\{experiment_name}"
vq_vae_model_prefix = f"outputs\\{vq_vae_experiment_name}"
vq_vae_model_path = os.path.join(vq_vae_model_prefix, "model.pt")
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_dir = os.path.join(output_prefix, "generated")
loss_output_path = output_prefix
model_output_path = os.path.join(output_prefix, "model.pt")
################################################################################
##################################### 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 & Load VQVAE Model
vq_vae = vqvae.VQVAE(
num_layers=vq_vae_num_layers,
input_image_dimensions=image_size,
small_conv=vq_vae_small_conv,
embedding_dim=vq_vae_embedding_dim,
num_embeddings=vq_vae_num_embeddings,
commitment_cost=vq_vae_commitment_cost,
use_max_filters=vq_vae_use_max_filters,
max_filters=vq_vae_max_filters,
)
vq_vae.load_state_dict(torch.load(vq_vae_model_path, map_location=device))
vq_vae.eval()
vq_vae.to(device)
# Create Model
model = gated_pixelcnn.PixelCNN(
c_in=input_channels,
c_hidden=hidden_channels,
num_classes=num_classes,
kernel_size=kernel_size,
use_dilation=use_dilation,
)
model.to(device)
print(model)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
if use_bits_per_dimension_loss:
criterion = loss.bits_per_dimension_loss
else:
criterion = nn.CrossEntropyLoss()
################################################################################
################################### Training ###################################
################################################################################
# Train
all_train_loss = []
all_val_loss = []
for epoch in range(epochs):
train_loss = 0
val_loss = 0
# 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 # (names), (images)
batch = batch.to(device)
current_batch_size = batch.shape[0]
with torch.no_grad():
# Get Encodings from vq_vae
_, _, _, encodings = vq_vae(batch)
x = encodings.reshape(current_batch_size, 1, input_dim, input_dim)
# Run our model & get outputs
y_hat = model.forward(x)
batch_loss = criterion(y_hat, x)
# 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()
# Validation Loop
model.eval()
with torch.no_grad():
for iteration, batch in enumerate(tqdm(val_dataloader)):
# Move batch to device
_, batch = batch # (names), (images)
batch = batch.to(device)
current_batch_size = batch.shape[0]
with torch.no_grad():
# Get Encodings from vq_vae
_, _, _, encodings = vq_vae(batch)
x = encodings.reshape(current_batch_size, 1, input_dim, input_dim)
# Run our model & get outputs
y_hat = model.forward(x)
batch_loss = criterion(y_hat, x)
# Add the batch's loss to the total loss for the epoch
val_loss += batch_loss.item()
# Compute the average losses for this epoch
train_loss = train_loss / len(train_dataloader)
all_train_loss.append(train_loss)
val_loss = val_loss / len(val_dataloader)
all_val_loss.append(val_loss)
# Print Metrics
print(
f"\nEpoch: {epoch+1}/{epochs}:\
\nTrain Loss = {train_loss}\
\nVal Loss = {val_loss}"
)
################################################################################
################################## Save & Test #################################
################################################################################
# Generate Loss Graph
graphics.draw_loss(all_train_loss, all_val_loss, loss_output_path, mode="autoencoder")
# Save Model
torch.save(
{
"epoch": epoch,
"model_state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
"train_loss": all_train_loss,
"val_loss": all_val_loss,
},
model_output_path,
)
# Evaluation Time
model.eval()
# Compute bpd loss on test images
test_loss = 0
with torch.no_grad():
for iteration, batch in enumerate(tqdm(test_dataloader)):
# Move batch to device
_, batch = batch # (names), (images)
batch = batch.to(device)
current_batch_size = batch.shape[0]
with torch.no_grad():
# Get Encodings from vq_vae
_, _, _, encodings = vq_vae(batch)
x = encodings.reshape(current_batch_size, 1, input_dim, input_dim)
# Run our model & get outputs
y_hat = model.forward(x)
batch_loss = criterion(y_hat, x)
# Add the batch's loss to the total loss for the epoch
test_loss += batch_loss.item()
test_loss = test_loss / len(test_dataloader)
print(f"Test Loss: {test_loss}")
# Generate samples
target_shape = (sample_batch_size, input_dim, input_dim, vq_vae_embedding_dim)
image_shape = (sample_batch_size, input_channels, input_dim, input_dim)
for i in range(num_sample_batches):
# Sample from model
sample = model.sample(image_shape, device)
# Feed into VQ-VAE
sample = sample.flatten(start_dim=1).view(-1, 1)
sample = vq_vae.quantize_and_decode(sample, target_shape, device)
# Convert to image
sample = sample.permute(0, 2, 3, 1).detach().cpu().numpy()
# Save
for filename, image in enumerate(sample):
filename = f"{(i*sample_batch_size)+filename}.png"
plt.imsave(os.path.join(output_dir, filename), image)