-
Notifications
You must be signed in to change notification settings - Fork 0
/
VAE.py
151 lines (121 loc) · 4.85 KB
/
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
import pathlib
from networks import VAE_net, VAE_net_64, VanillaVAE
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
from torchvision.transforms import ToTensor, Lambda, Compose
from torchvision.utils import save_image
import numpy as np
import cv2
import time
from PIL import Image
from matplotlib import pyplot as plt
import gc
import wandb
torch.cuda.empty_cache()
gc.collect()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
wandb.login()
print("training")
hyperparameters = dict(
batch_size=16,
learning_rate=1e-3,
num_epochs=100,
width=64,
beta=16 / 100000,
)
def npy_loader(path):
np_load_old = np.load
np.load = lambda *a, **k: np_load_old(*a, allow_pickle=True, **k)
sample = torch.from_numpy(np.load(path))
np.load = np_load_old
return sample
def make(config):
train_dataset = datasets.DatasetFolder(
root='data/data_train_64_game',
loader=npy_loader,
extensions='.npy',
)
# test_dataset = datasets.DatasetFolder(
# root='data/data_test_64_video',
# loader=npy_loader,
# extensions='.npy',
# )
test_dataset = train_dataset
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=config.batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=config.batch_size, shuffle=True)
net = VanillaVAE().to(device)
print("loading")
net.load_state_dict(torch.load("models/VAE.model"))
optimizer = torch.optim.Adam(net.parameters(), lr=config.learning_rate)
return net, train_loader, test_loader, optimizer
def train(net, train_loader, optimizer, config):
net.train()
wandb.watch(net, log="all", log_freq=1)
# training loop
for epoch in range(config.num_epochs):
total_loss = 0
for idx, data in enumerate(train_loader, 0):
imgs, _ = data
imgs = imgs.to(device)
imgs = imgs.permute(0, 1, 4, 2, 3) # switch from NHWC to NCHW
imgs = transforms.Grayscale().forward(imgs) # convert to grayscale
# print(imgs.shape)
for file in imgs:
file = file / 256
# print(file.shape)
# plt.imshow(file[0][0].to("cpu"), "gray")
# plt.show()
# iterate over batch
for batch in torch.split(file, config.batch_size):
# print(batch.shape)
# Feeding a batch of images into the network to obtain the output image, mu, and logVar
# out, mu, logVar = net(batch)
out, original, mu, logVar = net(batch) # Vanilla_VAE
loss, recon_loss, kld_loss = net.loss_function(out, original, mu, logVar,
M_N=config.beta) # Vanilla_VAE
# TODO: only going for recon loss now
loss = recon_loss
total_loss += loss
# The loss is the BCE loss combined with the KL divergence to ensure the distribution is learnt
# kl_divergence = 0.5 * torch.sum(1 + logVar - mu.pow(2) - logVar.exp())
# loss = F.binary_cross_entropy(out, batch, reduction='sum') - kl_divergence
# Backpropagation based on the loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
wandb.log({"epoch": epoch, "loss": total_loss})
print('Epoch {}: Loss {}'.format(epoch, total_loss))
results_dir = pathlib.Path("models")
save_dir = results_dir / f"VAE.model"
results_dir.mkdir(parents=True, exist_ok=True)
torch.save(net.state_dict(), save_dir)
with wandb.init(project="Trackmania", config=hyperparameters):
config = wandb.config
net, train_loader, test_loader, optimizer = make(config)
train(net, train_loader, optimizer, config)
#
# import matplotlib.pyplot as plt
# import numpy as np
# import random
# net.eval()
# with torch.no_grad():
# for data in random.sample(list(test_loader), 1):
# imgs, _ = data
# imgs = imgs.to(device)
# imgs = imgs.permute(0, 1, 4, 2, 3) # switch from NHWC to NCHW
# imgs = transforms.Grayscale().forward(imgs) # convert to grayscale
# imgs = imgs[0]
# imgs = imgs / 256
# # img = np.transpose(imgs[0].cpu().numpy(), [1, 2, 0])
# # plt.subplot(121)
# # plt.imshow(np.squeeze(img))
# plt.imshow(imgs[0][0].to("cpu"), "gray")
# plt.show()
# out, mu, logVAR = net(imgs)
# # outimg = np.transpose(out[0].cpu().numpy(), [1, 2, 0])
# # plt.subplot(122)
# # plt.imshow(np.squeeze(outimg))
# plt.imshow(out[0][0].to("cpu"), "gray")
# plt.show()