-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnist_vae.py
More file actions
135 lines (113 loc) · 3.69 KB
/
mnist_vae.py
File metadata and controls
135 lines (113 loc) · 3.69 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
import torch
from torch import nn
from torch.utils.data import DataLoader
from torch.functional import F
from torchvision import datasets
from torchvision.transforms import ToTensor
import matplotlib.pyplot as plt
from mygrad.utils import *
from vae import *
import os
import argparse
import random
if torch.cuda.is_available():
DEVICE = "cuda"
elif torch.backends.mps.is_available():
DEVICE = "cpu"
else:
DEVICE = "cpu"
training_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor()
)
test_data = datasets.FashionMNIST(
root="data",
train=False,
download=True,
transform=ToTensor()
)
dig2label = {
0: "T-Shirt",
1: "Trouser",
2: "Pullover",
3: "Dress",
4: "Coat",
5: "Sandal",
6: "Shirt",
7: "Sneaker",
8: "Bag",
9: "Ankle Boot",
}
label2dig = {v:k for k,v in dig2label.items()}
def loss_function(recon_x, x, mu, logvar):
BCE = F.cross_entropy(recon_x, x)
KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
return BCE + KLD
def train(save_path, c=False, dropout=0.1, z_dim=64, lr=0.001, batch_size=64, epochs=5):
if c:
print("Training Conditional VAE")
model = CVAE(z_dim,dropout=dropout)
else:
print("Training VAE")
model = VAE(z_dim, dropout=dropout)
model.to(DEVICE)
train_dataloader = DataLoader(training_data, batch_size=batch_size)
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
model.train()
for i in range(epochs):
for batch, (X,Y) in enumerate(train_dataloader):
X,Y = X.to(DEVICE), Y.to(DEVICE)
X = X.view(X.shape[0],-1)
if c:
Y = torch.tensor([one_hot(y, 10) for y in Y]).to(DEVICE)
pred,m,v = model(X, Y)
else:
pred,m,v = model(X)
loss = loss_fn(pred, X)
#loss = loss_function(pred,X,m,v)
loss.backward()
optimizer.step()
optimizer.zero_grad()
if batch % 100 == 0:
loss, current = loss.item(), (batch+1) * len(X)
size = len(train_dataloader.dataset)
print(f"epoch {i} | loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
torch.save(model.decoder.state_dict(), save_path+f'{i}.pth')
def test(path, c=False, z_dim=64):
if c:
print("Testing Conditional VAE")
decoder = CDecoder(z_dim)
else:
print("Testing VAE")
decoder = Decoder(z_dim)
decoder.load_state_dict(torch.load(path))
decoder.eval()
figure = plt.figure(figsize=(8, 8))
cols, rows = 4, 4
for i in range(1, cols*rows+1):
z = torch.randn((1,z_dim))
dig = 0
dig = random.randint(0,9)
hot = torch.tensor(one_hot(dig,10)).view(1,10)
img = decoder(z,hot).view(1,28,28).detach().numpy()
figure.add_subplot(rows, cols, i)
plt.title(dig2label[dig])
plt.axis("off")
plt.imshow(img.squeeze(), cmap="gray")
plt.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-c","--conditional", type=int, required=False)
args = parser.parse_args()
print(f'Using device: {DEVICE}')
conditional = True if args.conditional != 0 else False
cwd = os.getcwd()
save_path = os.path.join(cwd, "weights/decoder_at_epoch_")
# data = 28x28x3 tensor, and classificaton value (ignore)
z_dim=10
train(save_path, c=conditional, z_dim=z_dim,dropout=0.1, epochs=2)
last = sorted(os.listdir(os.path.join(cwd, "weights")),key=lambda x:int(x[-5]))[-1]
test(os.path.join(cwd, 'weights', last),c=conditional,z_dim=z_dim)