-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvisualize.py
More file actions
61 lines (49 loc) · 2.03 KB
/
Copy pathvisualize.py
File metadata and controls
61 lines (49 loc) · 2.03 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
import torch
from torch.autograd import Variable
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
import argparse
from autoencoder import Autoencoder
from gmmn import *
from constants import *
trans = transforms.Compose([transforms.ToTensor()])
test_set = datasets.MNIST(root=root, train=False, transform=trans, download=True)
view_data = [test_set[i][0] for i in range(N_ROWS * N_COLS)]
encoder_net = Autoencoder(N_INP, ENCODED_SIZE)
encoder_net.load_state_dict(torch.load(ENCODER_SAVE_PATH))
plt.gray()
parser = argparse.ArgumentParser(description="visualizations for GMMN")
parser.add_argument("--vis", choices=["autoencoder", "gmmn"], default="gmmn")
args = parser.parse_args()
if args.vis == "autoencoder":
print("Comparision of outputs generated by autoencoder")
for i in range(N_ROWS * N_COLS):
# original image
r = i // N_COLS
c = i % N_COLS + 1
ax = plt.subplot(2 * N_ROWS, N_COLS, 2 * r * N_COLS + c)
plt.imshow(view_data[i].squeeze())
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# reconstructed image
ax = plt.subplot(2 * N_ROWS, N_COLS, 2 * r * N_COLS + c + N_COLS)
x = Variable(view_data[i])
_, y = encoder_net(x.view(1, -1))
plt.imshow(y.detach().squeeze().numpy().reshape(28, 28))
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
elif args.vis == "gmmn":
print("Images generated by GMMN")
gmm_net = GMMN(NOISE_SIZE, ENCODED_SIZE)
gmm_net.load_state_dict(torch.load(GMMN_SAVE_PATH))
for r in range(N_ROWS):
for c in range(N_COLS):
ax = plt.subplot(N_ROWS, N_COLS, r * N_COLS + c + 1)
noise = torch.rand((1, NOISE_SIZE)) * 2 - 1
encoded_x = gmm_net(Variable(noise))
y = encoder_net.decode(encoded_x)
plt.imshow(y.detach().squeeze().numpy().reshape(28, 28))
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()