-
Notifications
You must be signed in to change notification settings - Fork 0
/
RQ4-plots.py
187 lines (169 loc) · 8.45 KB
/
RQ4-plots.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
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
"""
Generate Figure 9: A closer look at the latent space which compares latent
variable mappings between covers and containers.
"""
from utils.utils import lsb_eval_latent_all,\
ddh_eval_latent_all,\
udh_eval_latent_all,\
load_vae_suds,\
load_udh_mnist,\
load_ddh_mnist,\
load_data
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
import numpy as np
import random
import argparse
import torch
from tqdm import tqdm
import os
np.random.seed(4)
random.seed(4)
sns.set()
font = {'weight' : 'normal',
# 'family' : 'Verdana', # docker does not like. removed.
'size' : 30}
mpl.rc('font', **font)
# csfont = {'family':'Verdana', 'fontsize':14}
csfont = {'fontsize':14} # docker does not like
def get_args():
"""
Get training arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument("--savedir", type=str, default="results/RQ4-plots")
parser.add_argument("--model", type=int, default=8)
args = parser.parse_args()
return args
def get_mean_std(data):
"""
helper function. Get mean and standard deviation.
"""
return torch.mean(data, dim=0), torch.std(data, dim=0)
def main(args):
#
# Load models
#
print("\nLoading Models ...\n")
Hnet, Rnet = load_udh_mnist()
HnetD, RnetD = load_ddh_mnist()
vae_model = load_vae_suds(z_size=args.model)
train_loader, test_loader = load_data("mnist")
vae_size = args.model
#
# Create a master list for containers
#
print("Mapping inputs to latent variables ...\n")
master_index = []
for i, data in enumerate(test_loader, 0):
covers, labels = data
idx = torch.randperm(covers.shape[0])
master_index.append(idx)
#
# Generate z data
#
z_cover_lsb, z_container_lsb, master_l_lsb = lsb_eval_latent_all(test_loader, master_index, vae_model)
z_cover_ddh, z_container_ddh, master_l_ddh = ddh_eval_latent_all(test_loader, master_index, HnetD, vae_model)
z_cover_udh, z_container_udh, master_l_udh = udh_eval_latent_all(test_loader, master_index, Hnet, vae_model)
#
# Generate plot
#
print("Plotting ...\n")
get_digit_idx = lambda d, master_l: [i for i in range(10000) if master_l[i].item() == d]
cap_size = 3 # cap size of error bar
l_size = 1 # line size of error bars
fig, ax = plt.subplots(2, 2, figsize=(7,7))
for i in range(2):
for j in range(2):
if i == 1 and j == 1:
ax[i, j].errorbar(0, 0, 0, linestyle='None', label='cover', capsize=cap_size, elinewidth=l_size)
ax[i, j].errorbar(0, 0, 0, linestyle='None', label='lsb container', capsize=cap_size, elinewidth=l_size)
ax[i, j].errorbar(0, 0, 0, linestyle='None', label='ddh container', capsize=cap_size, elinewidth=l_size)
ax[i, j].errorbar(0, 0, 0, linestyle='None', label='udh container', capsize=cap_size, elinewidth=l_size)
ax[i, j].set_axis_off()
ax[i, j].legend(loc='center')
else:
l = (i*2)+j
index = get_digit_idx(l, master_l_lsb) # all master lists are the same
mu_cover, e_cover = get_mean_std(z_cover_lsb[index])
mu_container_lsb, e_container_lsb = get_mean_std(z_container_lsb[index])
mu_container_ddh, e_container_ddh = get_mean_std(z_container_ddh[index])
mu_container_udh, e_container_udh = get_mean_std(z_container_udh[index])
ax[i, j].errorbar(np.linspace(1, vae_size, vae_size)-0.2, mu_cover, e_cover, linestyle='None', marker='.', label='cover', capsize=cap_size, elinewidth=l_size)
ax[i, j].errorbar(np.linspace(1, vae_size, vae_size)-0.1, mu_container_lsb, e_container_lsb, linestyle='None', marker='.', label='lsb container', capsize=cap_size, elinewidth=l_size)
ax[i, j].errorbar(np.linspace(1, vae_size, vae_size), mu_container_ddh, e_container_ddh, linestyle='None', marker='.', label='ddh container', capsize=cap_size, elinewidth=l_size)
ax[i, j].errorbar(np.linspace(1, vae_size, vae_size)+0.1, mu_container_udh, e_container_udh, linestyle='None', marker='.', label='udh container', capsize=cap_size, elinewidth=l_size)
ax[i, j].set_xticks([0,2,4,6, 8])
ax[i, j].set_yticks([-2,0,2,4])
ax[i, j].set_xlim([0, 9])
ax[i, j].set_ylim([-3, 4])
ax[i, j].set_title(f"Label = {l}", **csfont)
#
# Save
#
plt.tight_layout()
print(f"Saving to: {args.savedir}/latent_mappings_plot_compact.pdf")
if os.path.exists(args.savedir) == 0:
os.mkdir(args.savedir)
plt.savefig(f"{args.savedir}/latent_mappings_plot_compact.pdf")
# plt.show()
#
# Generate Complete Entire Image
#
fig = plt.figure(figsize=(12, 10))
for i in range(10):
if i == 9:
ax = fig.add_subplot(3, 4, i+3)
ax.errorbar(0, 0, 0, linestyle='None', label='cover', capsize=cap_size, elinewidth=l_size)
ax.errorbar(0, 0, 0, linestyle='None', label='lsb container', capsize=cap_size, elinewidth=l_size)
ax.errorbar(0, 0, 0, linestyle='None', label='ddh container', capsize=cap_size, elinewidth=l_size)
ax.errorbar(0, 0, 0, linestyle='None', label='udh container', capsize=cap_size, elinewidth=l_size)
ax.set_axis_off()
ax.legend(loc='center')
elif i < 9:
if i == 8:
ax = fig.add_subplot(3, 4, i+2)
else:
ax = fig.add_subplot(3, 4, i+1)
index = get_digit_idx(i, master_l_lsb) # all master lists are the same
mu_cover, e_cover = get_mean_std(z_cover_lsb[index])
mu_container_lsb, e_container_lsb = get_mean_std(z_container_lsb[index])
mu_container_ddh, e_container_ddh = get_mean_std(z_container_ddh[index])
mu_container_udh, e_container_udh = get_mean_std(z_container_udh[index])
ax.errorbar(np.linspace(1, vae_size, vae_size)-0.2, mu_cover, e_cover, linestyle='None', marker='.', label='cover', capsize=cap_size, elinewidth=l_size)
ax.errorbar(np.linspace(1, vae_size, vae_size)-0.1, mu_container_lsb, e_container_lsb, linestyle='None', marker='.', label='lsb container', capsize=cap_size, elinewidth=l_size)
ax.errorbar(np.linspace(1, vae_size, vae_size), mu_container_ddh, e_container_ddh, linestyle='None', marker='.', label='ddh container', capsize=cap_size, elinewidth=l_size)
ax.errorbar(np.linspace(1, vae_size, vae_size)+0.1, mu_container_udh, e_container_udh, linestyle='None', marker='.', label='udh container', capsize=cap_size, elinewidth=l_size)
ax.set_xticks([0,2,4,6, 8])
ax.set_yticks([-2,0,2,4])
ax.set_xlim([0, 9])
ax.set_ylim([-3, 4])
ax.set_title(f"Label = {i}", **csfont)
ax = fig.add_subplot(3, 4, 11)
index = get_digit_idx(9, master_l_lsb) # all master lists are the same
mu_cover, e_cover = get_mean_std(z_cover_lsb[index])
mu_container_lsb, e_container_lsb = get_mean_std(z_container_lsb[index])
mu_container_ddh, e_container_ddh = get_mean_std(z_container_ddh[index])
mu_container_udh, e_container_udh = get_mean_std(z_container_udh[index])
ax.errorbar(np.linspace(1, vae_size, vae_size)-0.2, mu_cover, e_cover, linestyle='None', marker='.', label='cover', capsize=cap_size, elinewidth=l_size)
ax.errorbar(np.linspace(1, vae_size, vae_size)-0.1, mu_container_lsb, e_container_lsb, linestyle='None', marker='.', label='lsb container', capsize=cap_size, elinewidth=l_size)
ax.errorbar(np.linspace(1, vae_size, vae_size), mu_container_ddh, e_container_ddh, linestyle='None', marker='.', label='ddh container', capsize=cap_size, elinewidth=l_size)
ax.errorbar(np.linspace(1, vae_size, vae_size)+0.1, mu_container_udh, e_container_udh, linestyle='None', marker='.', label='udh container', capsize=cap_size, elinewidth=l_size)
ax.set_xticks([0,2,4,6, 8])
ax.set_yticks([-2,0,2,4])
ax.set_xlim([0, 9])
ax.set_ylim([-3, 4])
ax.set_title(f"Label = {9}", **csfont)
#
# Save plot
#
print(f"Saving to: {args.savedir}/latent_mappings_plot.pdf")
if os.path.exists(args.savedir) == 0:
os.mkdir(args.savedir)
plt.tight_layout()
plt.savefig(f'{args.savedir}/latent_mappings_plot.pdf', bbox_inches='tight')
# plt.show()
if __name__ == "__main__":
args = get_args()
main(args)