-
Notifications
You must be signed in to change notification settings - Fork 6
/
script_create_result_plots.py
289 lines (278 loc) · 11.1 KB
/
script_create_result_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import click
import os
import matplotlib.pyplot as plt
import torch
import numpy as np
import pandas as pd
import seaborn as sns
from data.dataloaders.mnist_dataset import MorphoMnistDataset
from data.dataloaders.dsprites_dataset import DspritesDataset
from data.dataloaders.bar_dataset import ChoraleNBarDataset, FolkNBarDataset
from imagevae.mnist_vae import MnistVAE
from imagevae.dsprites_vae import DspritesVAE
from measurevae.measure_vae import MeasureVAE
from imagevae.image_vae_trainer import ImageVAETrainer, MNIST_REG_TYPES, DSPRITES_REG_TYPE, get_reg_dim
from measurevae.measure_vae_trainer import MeasureVAETrainer, MUSIC_REG_TYPE
from utils.plotting import create_box_plot, create_pair_plot
from utils.evaluation import EVAL_METRIC_DICT
def main():
dataset_dict = {
'dsprites': {
'repr': '2-d sprites',
'attr_dict': DSPRITES_REG_TYPE,
'dataset': DspritesDataset(),
'model': DspritesVAE(),
'trainer': ImageVAETrainer,
'model_dict': {
r'$\beta$-VAE': {
'metric_dlist': [],
'reg_type': (),
'reg_dim': tuple([0]),
'beta': 4.0,
'capacity': 0.0,
'gamma': 0.0
},
'AR-VAE': {
'metric_dlist': [],
'reg_type': (['all']),
'reg_dim': get_reg_dim(DSPRITES_REG_TYPE),
'beta': 1.0,
'capacity': 0.0,
'gamma': 10.0
}
},
},
'mnist': {
'repr': 'Morpho-MNIST',
'attr_dict': MNIST_REG_TYPES,
'dataset': MorphoMnistDataset(),
'model': MnistVAE(),
'trainer': ImageVAETrainer,
'model_dict': {
r'$\beta$-VAE': {
'metric_dlist': [],
'reg_type': (),
'reg_dim': tuple([0]),
'beta': 4.0,
'capacity': 0.0,
'gamma': 0.0
},
'AR-VAE': {
'metric_dlist': [],
'reg_type': (['all']),
'reg_dim': get_reg_dim(MNIST_REG_TYPES),
'beta': 1.0,
'capacity': 0.0,
'gamma': 10.0
}
},
},
'bach': {
'repr': 'Bach Chorales',
'attr_dict': MUSIC_REG_TYPE,
'dataset': ChoraleNBarDataset(
dataset_type='train',
is_short=False,
num_bars=1,
),
'model': MeasureVAE(
dataset=ChoraleNBarDataset(
dataset_type='train',
is_short=False,
num_bars=1,
),
note_embedding_dim=10,
metadata_embedding_dim=2,
num_encoder_layers=2,
encoder_hidden_size=128,
encoder_dropout_prob=0.5,
latent_space_dim=32,
num_decoder_layers=2,
decoder_hidden_size=128,
decoder_dropout_prob=0.5,
has_metadata=False,
dataset_type='bach',
),
'trainer': MeasureVAETrainer,
'model_dict': {
r'$\beta$-VAE': {
'metric_dlist': [],
'reg_type': (),
'reg_dim': tuple([0]),
'beta': 0.001,
'capacity': 0.0,
'gamma': 0.0
},
'AR-VAE': {
'metric_dlist': [],
'reg_type': (['all']),
'reg_dim': get_reg_dim(MUSIC_REG_TYPE),
'beta': 0.001,
'capacity': 0.0,
'gamma': 1.0
},
},
},
'folk': {
'repr': 'Folk Music',
'attr_dict': MUSIC_REG_TYPE,
'dataset': FolkNBarDataset(
dataset_type='train',
is_short=False,
num_bars=1,
),
'model': MeasureVAE(
dataset=FolkNBarDataset(
dataset_type='train',
is_short=False,
num_bars=1,
),
note_embedding_dim=10,
metadata_embedding_dim=2,
num_encoder_layers=2,
encoder_hidden_size=128,
encoder_dropout_prob=0.5,
latent_space_dim=32,
num_decoder_layers=2,
decoder_hidden_size=128,
decoder_dropout_prob=0.5,
has_metadata=False,
dataset_type='folk',
),
'trainer': MeasureVAETrainer,
'model_dict': {
r'$\beta$-VAE': {
'metric_dlist': [],
'reg_type': (),
'reg_dim': tuple([0]),
'beta': 0.001,
'capacity': 0.0,
'gamma': 0.0
},
'AR-VAE': {
'metric_dlist': [],
'reg_type': (['all']),
'reg_dim': get_reg_dim(MUSIC_REG_TYPE),
'beta': 0.001,
'capacity': 0.0,
'gamma': 1.0
},
},
},
}
rand = range(0, 10)
for d in dataset_dict.keys():
for m in dataset_dict[d]['model_dict'].keys():
for r in rand:
# instantiate trainer
trainer = dataset_dict[d]['trainer'](
dataset=dataset_dict[d]['dataset'],
model=dataset_dict[d]['model'],
lr=1e-4,
reg_type=dataset_dict[d]['model_dict'][m]['reg_type'],
reg_dim=dataset_dict[d]['model_dict'][m]['reg_dim'],
beta=dataset_dict[d]['model_dict'][m]['beta'],
capacity=dataset_dict[d]['model_dict'][m]['capacity'],
gamma=dataset_dict[d]['model_dict'][m]['gamma'],
rand=r
)
# compute and print evaluation metrics
trainer.load_model()
trainer.writer = None
dataset_dict[d]['model_dict'][m]['metric_dlist'].append(
trainer.compute_eval_metrics()
)
# Plot Digit Prediction Plot
digit_pred_data = []
digit_pred_crit = {
'recons': 'Reconstructed',
'interp': 'Interpolated',
}
for k in digit_pred_crit:
for m in dataset_dict['mnist']['model_dict'].keys():
temp_list = list()
temp_list.append(
[r['digit_pred_acc'][k]*100 for r in dataset_dict['mnist']['model_dict'][m]['metric_dlist']]
)
n = len(dataset_dict['mnist']['model_dict'][m]['metric_dlist'])
temp_list.append(n * [digit_pred_crit[k]])
temp_list.append(n * [m])
digit_pred_data.append(temp_list)
digit_pred_data = np.concatenate(digit_pred_data, axis=1)
df = pd.DataFrame(columns=['Accuracy (in %)', 'Criteria', 'Model'], data=digit_pred_data.T)
df['Accuracy (in %)'] = df['Accuracy (in %)'].astype(float)
save_path = os.path.join(
os.path.realpath(os.path.dirname(__file__)), 'plots', 'digit_pred_acc.pdf'
)
fig, ax = create_box_plot(df, 'Criteria', 'Accuracy (in %)', 'Model', width=0.5)
plt.plot(0.5, 96.15, 'x', color='k')
ax.annotate(r'MNIST Test Set', (0.4, 97.55))
plt.savefig(save_path)
# Plot Reconstruction accuracy plots
recons_data = []
for d in dataset_dict.keys():
for m in dataset_dict[d]['model_dict'].keys():
temp_list = list()
temp_list.append([r['test_acc']*100 for r in dataset_dict[d]['model_dict'][m]['metric_dlist']])
n = len(dataset_dict[d]['model_dict'][m]['metric_dlist'])
temp_list.append(n * [dataset_dict[d]['repr']])
temp_list.append(n * [m])
recons_data.append(temp_list)
recons_data = np.concatenate(recons_data, axis=1)
df = pd.DataFrame(columns=['Reconstruction Accuracy (in %)', 'Datasets', 'Model'], data=recons_data.T)
df['Reconstruction Accuracy (in %)'] = df['Reconstruction Accuracy (in %)'].astype(float)
save_path = os.path.join(
os.path.realpath(os.path.dirname(__file__)), 'plots', 'reconstruction.pdf'
)
create_box_plot(df, 'Datasets', 'Reconstruction Accuracy (in %)', 'Model', save_path=save_path)
# Plot Disentanglement Evaluation metrics
data = {}
for k in EVAL_METRIC_DICT.keys():
data[k] = []
for d in dataset_dict.keys():
for m in dataset_dict[d]['model_dict'].keys():
temp_list = list()
if k == 'interpretability':
temp_list.append([r[k]['mean'][1] for r in dataset_dict[d]['model_dict'][m]['metric_dlist']])
else:
temp_list.append([r[k] for r in dataset_dict[d]['model_dict'][m]['metric_dlist']])
n = len(dataset_dict[d]['model_dict'][m]['metric_dlist'])
temp_list.append(n * [dataset_dict[d]['repr']])
temp_list.append(n * [m])
data[k].append(temp_list)
data[k] = np.concatenate(data[k], axis=1)
df = pd.DataFrame(columns=[EVAL_METRIC_DICT[k], 'Datasets', 'Model'], data=data[k].T)
df[EVAL_METRIC_DICT[k]] = df[EVAL_METRIC_DICT[k]].astype(float)
save_path = os.path.join(
os.path.realpath(os.path.dirname(__file__)), 'plots', f'evaluation_{EVAL_METRIC_DICT[k]}.pdf'
)
create_box_plot(df, 'Datasets', EVAL_METRIC_DICT[k], 'Model', save_path=save_path)
# Plot Pairplot
pairplot_data = []
for d in dataset_dict.keys():
for m in dataset_dict[d]['model_dict'].keys():
N = len(dataset_dict[d]['model_dict'][m]['metric_dlist'])
for n in range(N):
temp_list = list()
for k in EVAL_METRIC_DICT.keys():
if k == 'interpretability':
temp_list.append(dataset_dict[d]['model_dict'][m]['metric_dlist'][n][k]['mean'][1])
else:
temp_list.append(dataset_dict[d]['model_dict'][m]['metric_dlist'][n][k])
if d == 'folk' or d == 'bach':
temp_list.append(f'{m}:Music')
else:
temp_list.append(f'{m}:Image')
pairplot_data.append(temp_list)
pairplot_data = np.stack(pairplot_data, axis=1)
columnlist = [EVAL_METRIC_DICT[k] for k in EVAL_METRIC_DICT.keys()]
columnlist.append('Model')
df = pd.DataFrame(columns=columnlist, data=pairplot_data.T)
for k in EVAL_METRIC_DICT.keys():
df[EVAL_METRIC_DICT[k]] = df[EVAL_METRIC_DICT[k]].astype(float)
save_path = os.path.join(
os.path.realpath(os.path.dirname(__file__)), 'plots', f'pair_plot.pdf'
)
create_pair_plot(df, 'Model', save_path=save_path)
if __name__ == '__main__':
main()