forked from shakes76/PatternAnalysis-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
308 lines (230 loc) · 11.4 KB
/
train.py
File metadata and controls
308 lines (230 loc) · 11.4 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
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
"""Training of the VQVAE and PixelCNN"""
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
import numpy as np
from tqdm.auto import tqdm
from PIL import Image
import torch.utils.data
from torchvision import datasets, transforms, utils
import matplotlib.pyplot as plt
import modules
import dataset
# Replace with preferred device and local path(s)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Torch version ", torch.__version__)
path = "C:/Users/61423/COMP3710/data/keras_png_slices_data/"
vqvae_save_path = "C:/Users/61423/COMP3710/COMP3710 Report/Models/"
pixelCNN_save_path = "C:/Users/61423/COMP3710/COMP3710 Report/Models/"
# Hyperparameters
batch_size = 128
vqvae_num_epochs = 10
vqvae_lr = 5e-4
cnn_num_epochs = 10
cnn_lr = 5e-4
# Data (If necessary, replace with the local names of the train, validate and test folders)
print("> Loading Data")
processed_data = dataset.DataPreparer(path, "keras_png_slices_train/", "keras_png_slices_validate/", "keras_png_slices_test/", batch_size)
train_dataloader = processed_data.train_dataloader
validate_dataloader = processed_data.validate_dataloader
test_dataloader = processed_data.test_dataloader
# Models (if not already saved)
vqvae_model = modules.VQVAE(num_embeddings=256, embedding_dim=64).to(device)
cnn_model = modules.PixelCNN(in_channels=1, hidden_channels=128, num_embeddings=256).to(device)
# Models (if previously saved).
#vqvae_model = torch.load(vqvae_save_path + "trained_vqvae_n.pth", map_location=device)
#cnn_model = torch.load(pixelCNN_save_path + "PixelCNN model_n.pth", map_location=device)
# Optimisers
vqvae_optimiser = torch.optim.Adam(vqvae_model.parameters(), vqvae_lr)
cnn_optimiser = torch.optim.Adam(cnn_model.parameters(), cnn_lr)
# Initialise losses (for graphing)
vqvae_training_loss = []
vqvae_validation_loss = []
cnn_training_loss = []
cnn_validation_loss = []
# --------------------------------------------------------
# VQVAE functions
# --------------------------------------------------------
def train_vqvae():
print("> Training VQVAE")
for epoch_num, epoch in enumerate(range(vqvae_num_epochs)):
# Train
vqvae_model.train()
for train_batch in train_dataloader:
images = train_batch.to(device, dtype=torch.float32)
output, quant_loss, reconstruction_loss, _ = vqvae_model(images)
training_loss = quant_loss + reconstruction_loss # Can be adjusted if necessary
vqvae_optimiser.zero_grad() # Reset gradients to zero
training_loss.backward() # Calculate grad
vqvae_optimiser.step() # Adjust weights
with torch.no_grad():
vqvae_training_loss.append((quant_loss.detach().cpu(), reconstruction_loss.detach().cpu(), training_loss.detach().cpu()))
# Evaluate
vqvae_model.eval()
for validate_batch in (validate_dataloader):
images = validate_batch.to(device, dtype=torch.float32)
with torch.no_grad():
output, quant_loss, reconstruction_loss, _ = vqvae_model(images)
validation_loss = quant_loss + reconstruction_loss
with torch.no_grad():
vqvae_validation_loss.append((quant_loss.detach().cpu(), reconstruction_loss.detach().cpu(), validation_loss.detach().cpu()))
vqvae_model.epochs_trained += 1
print("Epoch {} of {}. Training Loss: {}, Validation Loss: {}".format(epoch_num+1, vqvae_num_epochs, training_loss, validation_loss))
if (epoch_num+1) % 20 == 0:
print("Saving VQVAE model")
torch.save(vqvae_model, vqvae_save_path + "trained_vqvae_{}.pth".format(vqvae_model.epochs_trained))
def plot_vqvae_losses(show_individual_losses=False):
# Losses are in the order (Quant, Reconstruction, Total)
plt.title("VQVAE Losses")
plt.xlabel("Epoch")
plt.ylabel("Loss")
if show_individual_losses == False:
plt.plot([loss[2] for loss in vqvae_training_loss], color='blue')
plt.plot([loss[2] for loss in vqvae_validation_loss], color='red')
plt.legend(["Training Loss", "Validation Loss"])
else:
plt.plot([loss[0] for loss in vqvae_training_loss], color='blue', ls='--')
plt.plot([loss[0] for loss in vqvae_validation_loss], color='red', ls='--')
plt.plot([loss[1] for loss in vqvae_training_loss], color='blue')
plt.plot([loss[1] for loss in vqvae_validation_loss], color='red')
plt.legend(["Training Quantisation Loss", "Validation Quantisation Loss", "Training Reconstruction Loss", "Validation Reconstruction Loss"])
plt.show()
"""Function to test the VQVAE. Input the number of samples to show"""
def test_vqvae(num_shown=0):
print("> Testing VQVAE")
# Calculate losses
vqvae_model.eval()
test_losses = []
for test_batch in (test_dataloader):
images = test_batch.to(device, dtype=torch.float32)
with torch.no_grad():
output, quant_loss, r_loss, _ = vqvae_model(images)
# For averaging loss
test_losses.append((quant_loss + r_loss).cpu())
print("Average loss during testing is: ", np.mean(np.array(test_losses)))
# Show N Reconstructed Images.
if (num_shown != 0):
input_imgs = processed_data.test_dataset[0:num_shown]
input_imgs = input_imgs.to(device, dtype=torch.float32)
with torch.no_grad():
output_imgs, _, _, encoding_indices = vqvae_model(input_imgs)
fig, ax = plt.subplots(num_shown, 3)
plt.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9, wspace=0)
ax[0, 0].set_title("Input Image")
ax[0, 1].set_title("CodeBook Indices")
ax[0, 2].set_title("Reconstructed Image")
for i in range(num_shown):
for j in range(3):
ax[i, j].axis('off')
ax[i, 0].imshow(input_imgs[i][0].cpu().numpy(), cmap='gray')
ax[i, 1].imshow(encoding_indices[i].cpu().numpy(), cmap='gray')
ax[i, 2].imshow(output_imgs[i][0].cpu().numpy(), cmap='gray')
plt.show()
# Uncomment any functions to call
train_vqvae()
plot_vqvae_losses()
test_vqvae(num_shown=3)
# --------------------------------------------------------
# PixCNN functions
# --------------------------------------------------------
encoder = vqvae_model.__getattr__("encoder")
quantiser = vqvae_model.__getattr__("quantiser")
decoder = vqvae_model.__getattr__("decoder")
def train_pixcnn():
print("> Training PixelCNN")
for epoch_num, epoch in enumerate(range(cnn_num_epochs)):
cnn_model.train()
for train_batch in train_dataloader:
# Get the quantised outputs
with torch.no_grad():
encoder_output = encoder(train_batch.to(device))
_, _, indices = quantiser(encoder_output)
indices = indices.reshape(indices.size(0), 1, indices.size(1), indices.size(2)).to(device)
output = cnn_model(indices)
# Compute loss
nll = F.cross_entropy(output, indices, reduction='none') # Negative log-likelihood
bpd = nll.mean(dim=[1,2,3]) * np.log2(np.exp(1)) # Bits per dimension
training_loss = bpd.mean()
cnn_optimiser.zero_grad() # Reset gradients to zero for back-prop (not cumulative)
training_loss.backward() # Calculate grad
cnn_optimiser.step() # Adjust weights
with torch.no_grad():
cnn_training_loss.append(training_loss.cpu())
cnn_model.eval()
for validate_batch in validate_dataloader:
with torch.no_grad():
# Get the quantised outputs
encoder_output = encoder(validate_batch.to(device))
_, _, indices = quantiser(encoder_output)
indices = indices.reshape(indices.size(0), 1, indices.size(1), indices.size(2)).to(device)
output = cnn_model(indices)
# Compute loss
nll = F.cross_entropy(output, indices, reduction='none') # Negative log-likelihood
bpd = nll.mean(dim=[1,2,3]) * np.log2(np.exp(1)) # Bits per dimension
validation_loss = bpd.mean()
with torch.no_grad():
cnn_validation_loss.append(validation_loss.cpu())
cnn_model.epochs_trained += 1
print("Epoch {} of {}. Training Loss: {}, Validation Loss: {}".format(epoch_num+1, cnn_num_epochs, training_loss, validation_loss))
if (epoch_num+1) % 20 == 0:
print("Saving Pixel CNN")
torch.save(cnn_model, pixelCNN_save_path + "PixelCNN model_{}.pth".format(cnn_model.epochs_trained))
def plot_cnn_loss():
# Losses are in the order (Quant, Reconstruction, Total)
plt.title("PixelCNN Losses")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.plot(cnn_training_loss, color='blue')
plt.plot(cnn_validation_loss, color='red')
plt.legend(["Training Loss", "Validation Loss"])
plt.show()
def test_cnn(shown_imgs=0):
print("> Testing PixelCNN")
test_loss = []
cnn_model.eval()
with torch.no_grad():
for test_batch in test_dataloader:
# Get the quantised outputs
encoder_output = encoder(test_batch.to(device))
_, _, indices = quantiser(encoder_output)
indices = indices.reshape(indices.size(0), 1, indices.size(1), indices.size(2)).to(device)
output = cnn_model(indices)
# Compute loss
nll = F.cross_entropy(output, indices, reduction='none') # Negative log-likelihood
bpd = nll.mean(dim=[1,2,3]) * np.log2(np.exp(1)) # Bits per dimension
validation_loss = bpd.mean()
test_loss.append(validation_loss.cpu())
print("Average loss during testing is: ", np.mean(np.array(test_loss)))
if shown_imgs != 0:
print(" > Showing Images")
# Inputs
test_batch = processed_data.test_dataset[0:shown_imgs]
encoder_output = encoder(test_batch.to(device))
_, _, indices = quantiser(encoder_output)
indices_shape = indices.cpu().numpy().shape
print("Indices shape is: ", indices.cpu().numpy().shape)
indices = indices.reshape((indices_shape[0], 1,indices_shape[1], indices_shape[2]))
print("Indices shape is: ", indices.cpu().numpy().shape)
# Masked Inputs (only top quarter shown)
masked_indices = 1*indices
masked_indices[:,:,16:,:] = -1
gen_indices = cnn_model.sample((shown_imgs, 1, 32, 32), ind=masked_indices*1)
fig, ax = plt.subplots(shown_imgs, 3)
for a in ax.flatten():
a.axis('off')
ax[0, 0].set_title("Real")
ax[0, 1].set_title("Masked")
ax[0, 2].set_title("Generated")
for i in range(shown_imgs):
ax[i, 0].imshow(indices[i][0].long().cpu().numpy(), cmap='gray')
ax[i, 1].imshow(masked_indices[i][0].cpu().numpy(), cmap='gray')
ax[i, 2].imshow(gen_indices[i][0].cpu().numpy(), cmap='gray')
plt.show()
plt.imshow(vqvae_model.img_from_indices(gen_indices[0], (1, 32, 32, 32))[0][0].cpu().numpy(), cmap='gray')
plt.show()
# Uncomment any functions to call
train_pixcnn()
plot_cnn_loss()
test_cnn(shown_imgs=3)