-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEpistemic_toy.py
149 lines (125 loc) · 5.5 KB
/
Epistemic_toy.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
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import transforms
import tensorflow as tf
from torchvision.utils import save_image
import argparse
# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Create a directory if not exists
sample_dir = 'samples'
if not os.path.exists(sample_dir):
os.makedirs(sample_dir)
# Hyper-parameters
image_size = 784
h_dim = 400
z_dim = 20
num_epochs = 15
batch_size = 128
learning_rate = 1e-3
test_trials = 20
parser = argparse.ArgumentParser(description='practice')
parser.add_argument("--type", type=str, default='train')
parser.add_argument("--model", type=str, default='model.pth')
parser.add_argument("--batchsize", type=int, default=1)
args = parser.parse_args()
# MNIST dataset
dataset = torchvision.datasets.MNIST(root='data',
train=True,
transform=transforms.ToTensor(),
download=True)
testdataset = torchvision.datasets.MNIST(root='data',
train=False,
transform=transforms.ToTensor(),
download=True)
# Data loader
data_loader = torch.utils.data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=True)
testdata_loader = torch.utils.data.DataLoader(dataset=testdataset,
batch_size=args.batchsize,
shuffle=False)
def MCDropout(act_vec, p=0.5, apply=True):
return F.dropout(act_vec, p=p, training=apply)
# VAE model
class VAE(nn.Module):
def __init__(self, image_size=784, h_dim=400, z_dim=20):
super(VAE, self).__init__()
self.fc1 = nn.Linear(image_size, h_dim)
self.fc2 = nn.Linear(h_dim, z_dim)
self.fc3 = nn.Linear(h_dim, z_dim)
self.fc4 = nn.Linear(z_dim, h_dim)
self.fc5 = nn.Linear(h_dim, image_size)
def encode(self, x):
h = F.relu(self.fc1(x))
return self.fc2(h), self.fc3(h)
def reparameterize(self, mu, log_var):
std = torch.exp(log_var/2)
eps = torch.randn_like(std)
return mu + eps * std
def decode(self, z):
h = F.relu(self.fc4(z))
return F.sigmoid(self.fc5(h))
def forward(self, x):
mu, log_var = self.encode(x)
z = self.reparameterize(mu, log_var)
x_reconst = self.decode(z)
MCDropout(x_reconst, apply=True)
return x_reconst, mu, log_var
model = VAE().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Start training
if args.type == 'train' :
for epoch in range(num_epochs):
for i, (x, _) in enumerate(data_loader):
# Forward pass
x = x.to(device).view(-1, image_size)
x_reconst, mu, log_var = model(x)
# Compute reconstruction loss and kl divergence
# For KL divergence, see Appendix B in VAE paper or http://yunjey47.tistory.com/43
reconst_loss = F.binary_cross_entropy(x_reconst, x, size_average=False)
kl_div = - 0.5 * torch.sum(1 + log_var - mu.pow(2) - log_var.exp())
# Backprop and optimize
loss = reconst_loss + kl_div
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 10 == 0:
print ("Epoch[{}/{}], Step [{}/{}], Reconst Loss: {:.4f}, KL Div: {:.4f}"
.format(epoch+1, num_epochs, i+1, len(data_loader), reconst_loss.item(), kl_div.item()))
with torch.no_grad():
# Save the sampled images
z = torch.randn(batch_size, z_dim).to(device)
out = model.decode(z).view(-1, 1, 28, 28)
save_image(out, os.path.join(sample_dir, 'sampled-{}.png'.format(epoch+1)))
# Save the reconstructed images
out, _, _ = model(x)
x_concat = torch.cat([x.view(-1, 1, 28, 28), out.view(-1, 1, 28, 28)], dim=3)
save_image(x_concat, os.path.join(sample_dir, 'reconst-{}.png'.format(epoch+1)))
torch.save(model.state_dict(), '{}'.format(args.model))
elif args.type == 'test' :
if os.path.isfile('{}'.format(args.model)) :
model.load_state_dict(torch.load('{}'.format(args.model)))
model.eval()
with torch.no_grad():
for i, (x, y) in enumerate(testdata_loader):
print('[{}/{}]'.format(i, len(testdata_loader)))
for j in range(test_trials) :
if j == 0 :
x = x.to(device).view(-1, image_size)
output = model(x.to(device))
output = output[0].view(args.batchsize, 28, 28).cpu()
else :
x = x.to(device).view(-1, image_size)
output_ = model(x.to(device))
output_ = output_[0].view(args.batchsize, 28, 28).cpu()
output = tf.concat([output, output_], axis = 0)
output = tf.nn.moments(output, axes=[0])
result = tf.concat([output[1], x.view(28,28).cpu()], axis = 0).numpy()
save_image(torch.from_numpy(result), os.path.join(sample_dir, '{}reconst-{}.png'.format((i+1),y)))
else :
print('need model.pth! so train a model first')
exit()