-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_principal_components_autoencoder.py
321 lines (272 loc) · 11 KB
/
test_principal_components_autoencoder.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
####################################################################################################
#
# Test the articulatory model using the autoencoder
#
####################################################################################################
import argparse
import funcy
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import seaborn as sns
import torch
import ujson
import yaml
from torch.utils.data import DataLoader
from tqdm import tqdm
from helpers import set_seeds, sequences_from_dict, make_indices_dict
from phoneme_to_articulation.principal_components.metrics import MeanP2CPDistance
from phoneme_to_articulation.principal_components.dataset import PrincipalComponentsAutoencoderDataset2
from phoneme_to_articulation.principal_components.evaluation import run_multiart_autoencoder_test
from phoneme_to_articulation.principal_components.losses import RegularizedLatentsMSELoss2
from phoneme_to_articulation.principal_components.models.autoencoder import MultiArticulatorAutoencoder
from settings import DATASET_CONFIG
PINK = np.array([255, 0, 85, 255]) / 255
BLUE = np.array([0, 139, 231, 255]) / 255
def plot_nomograms(
autoencoder,
normalize,
plots_dir,
device
):
articulators = autoencoder.sorted_articulators
num_articulators = len(articulators)
indices_dict = autoencoder.indices_dict
latent_size = autoencoder.latent_size
for i_PC in range(latent_size):
plt.figure(figsize=(10, 10))
PC_range = np.arange(-1, 1.01, 0.1)
for v in PC_range:
latents = torch.zeros(size=(1, latent_size))
latents[0, i_PC] = v
latents = latents.to(device)
reconstruction = torch.concat([
autoencoder.decoders.decoders[articulator](
latents[:, autoencoder.indices_dict[articulator]]
).unsqueeze(dim=1)
for articulator in autoencoder.sorted_articulators
], dim=1)
reconstruction = reconstruction.detach().cpu()
reconstruction = reconstruction.reshape(1, num_articulators, 2, 50)
for i_art, articulator in enumerate(articulators):
denorm_fn = normalize[articulator].inverse
reconstruction[:, i_art, :, :] = denorm_fn(reconstruction[:, i_art, :, :])
reconstruction = reconstruction.squeeze(dim=0)
for articulator, articulator_rec in zip(articulators, reconstruction):
color = "lightgrey"
if i_PC in indices_dict[articulator]:
color = PINK if v <= 0 else BLUE
plt.plot(*articulator_rec, color=color, lw=3, alpha=0.2)
plt.xlim([0., 1.])
plt.ylim([1., 0.])
plt.axis("off")
plt.tight_layout()
plt.savefig(os.path.join(plots_dir, f"C{i_PC + 1}.pdf"))
plt.savefig(os.path.join(plots_dir, f"C{i_PC + 1}.png"))
plt.close()
def plot_latent_space_distribution(df_latent, plots_dir):
for col in df_latent.columns:
plt.figure(figsize=(10, 10))
sns.histplot(df_latent[col])
plt.savefig(os.path.join(plots_dir, f"C{col}_distribution.pdf"))
plt.savefig(os.path.join(plots_dir, f"C{col}_distribution.png"))
plt.close()
def evaluate_autoencoder(
database_name,
datadir,
dataset_config,
batch_size,
sequences_dict,
model_params,
encoders_filepath,
decoders_filepath,
save_to,
num_workers=0,
):
plots_dir = os.path.join(save_to, "plots")
if not os.path.exists(plots_dir):
os.makedirs(plots_dir)
articulators_indices_dict = model_params["indices_dict"]
articulators = sorted(articulators_indices_dict.keys())
n_articulators = len(articulators)
latent_size = max(funcy.flatten(articulators_indices_dict.values())) + 1
sequences = sequences_from_dict(datadir, sequences_dict)
dataset = PrincipalComponentsAutoencoderDataset2(
database_name=database_name,
datadir=datadir,
sequences=sequences,
articulators=articulators,
)
dataloader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=False,
worker_init_fn=set_seeds,
num_workers=num_workers,
)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
autoencoder = MultiArticulatorAutoencoder(**model_params)
encoders_state_dict = torch.load(encoders_filepath, map_location=device)
autoencoder.encoders.load_state_dict(encoders_state_dict)
decoders_state_dict = torch.load(decoders_filepath, map_location=device)
autoencoder.decoders.load_state_dict(decoders_state_dict)
autoencoder.to(device)
autoencoder.eval()
print(f"""
MultiArticulatorAutoencoder -- {autoencoder.total_parameters} parameters
""")
p2cp_fn = MeanP2CPDistance(reduction="none")
data_frame_names = []
data_p2cp = torch.zeros(size=(0, n_articulators))
data_latents = torch.zeros(size=(0, latent_size))
data_reconstructions = torch.zeros(size=(0, n_articulators, 2, 50))
data_targets = torch.zeros(size=(0, n_articulators, 2, 50))
for frame_names, inputs, _, _ in tqdm(dataloader):
bs, _, _ = inputs.shape
inputs = inputs.to(device)
reconstructions, latents = autoencoder(inputs)
reconstructions = reconstructions.reshape(bs, n_articulators, 2, 50)
targets = inputs.reshape(bs, n_articulators, 2, 50)
for i, articulator in enumerate(articulators):
denorm_fn = dataset.normalize[articulator].inverse
reconstructions[:, i, :, :] = denorm_fn(reconstructions[:, i, :, :])
targets[:, i, :, :] = denorm_fn(targets[:, i, :, :])
reconstructions = reconstructions.detach().cpu()
latents = latents.detach().cpu()
targets = targets.detach().cpu()
p2cp = p2cp_fn(
reconstructions.permute(0, 1, 3, 2),
targets.permute(0, 1, 3, 2)
) * dataset_config.PIXEL_SPACING * dataset_config.RES
data_reconstructions = torch.cat([data_reconstructions, reconstructions])
data_latents = torch.cat([data_latents, latents])
data_targets = torch.cat([data_targets, targets])
data_p2cp = torch.cat([data_p2cp, p2cp])
data_frame_names.extend([frame_name.split("_") for frame_name in frame_names])
latent_space_filepath = os.path.join(save_to, "latent_space.csv")
df_latent = pd.DataFrame(data_latents, columns=[str(i) for i in range(1, latent_size + 1)])
df_latent.to_csv(latent_space_filepath, index=False)
errors_filepath = os.path.join(save_to, "reconstruction_errors.npy")
np.save(errors_filepath, data_p2cp.numpy())
df_errors_filepath = os.path.join(save_to, "reconstruction_errors.csv")
df_errors = pd.DataFrame(data_frame_names, columns=["subject", "sequence", "frame"])
df_errors[articulators] = data_p2cp
df_errors.to_csv(df_errors_filepath, index=False)
df_errors_agg_filepath = os.path.join(save_to, "reconstruction_errors_agg.csv")
df_errors_agg = df_errors.agg({
articulator: ["mean", "std", "median", "min", "max"]
for articulator in articulators
}).reset_index()
df_errors_agg.to_csv(df_errors_agg_filepath, index=False)
# Nomogram plots
plot_nomograms(
autoencoder=autoencoder,
normalize=dataset.normalize,
plots_dir=plots_dir,
device=device,
)
# Latent space distribution plots
plot_latent_space_distribution(
df_latent=df_latent,
plots_dir=plots_dir,
)
def main(
database_name,
datadir,
encoders_filepath,
decoders_filepath,
batch_size,
model_params,
seq_dict,
save_to,
num_workers=0,
clip_tails=True,
):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dataset_config = DATASET_CONFIG[database_name]
articulators_indices_dict = model_params["indices_dict"]
if isinstance(list(articulators_indices_dict.values())[0], int):
articulators_indices_dict = make_indices_dict(articulators_indices_dict)
model_params["indices_dict"] = articulators_indices_dict
test_sequences = sequences_from_dict(datadir, seq_dict)
articulators = sorted(articulators_indices_dict.keys())
test_dataset = PrincipalComponentsAutoencoderDataset2(
database_name=database_name,
datadir=datadir,
sequences=test_sequences,
articulators=articulators,
clip_tails=clip_tails
)
test_dataloader = DataLoader(
test_dataset,
batch_size=batch_size,
shuffle=False,
worker_init_fn=set_seeds,
num_workers=num_workers,
)
best_autoencoder = MultiArticulatorAutoencoder(**model_params)
best_encoders_state_dict = torch.load(encoders_filepath, map_location=device)
best_autoencoder.encoders.load_state_dict(best_encoders_state_dict)
best_decoders_state_dict = torch.load(decoders_filepath, map_location=device)
best_autoencoder.decoders.load_state_dict(best_decoders_state_dict)
best_autoencoder.to(device)
print(f"""
MultiArticulatorAutoencoder -- {best_autoencoder.total_parameters} parameters
""")
test_outputs_dir = os.path.join(save_to, "test_outputs")
if not os.path.exists(test_outputs_dir):
os.makedirs(test_outputs_dir)
plots_dir = os.path.join(save_to, "plots")
if not os.path.exists(plots_dir):
os.makedirs(plots_dir)
loss_fn = RegularizedLatentsMSELoss2(
indices_dict=articulators_indices_dict,
alpha=0.1,
)
info_test = run_multiart_autoencoder_test(
epoch=0,
model=best_autoencoder,
dataloader=test_dataloader,
criterion=loss_fn,
dataset_config=dataset_config,
# outputs_dir=test_outputs_dir,
plots_dir=plots_dir,
indices_dict=articulators_indices_dict,
device=device,
)
with open(os.path.join(save_to, "test_results.json"), "w") as f:
ujson.dump(info_test, f)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", dest="cfg_filepath")
args = parser.parse_args()
with open(args.cfg_filepath) as f:
cfg = yaml.safe_load(f.read())
main(**cfg)
database_name = cfg["database_name"]
dataset_config = DATASET_CONFIG[database_name]
sequences_dict = cfg["seq_dict"]
model_params = cfg["model_params"]
indices_dict = model_params["indices_dict"]
if isinstance(list(indices_dict.values())[0], int):
articulators_indices_dict = make_indices_dict(indices_dict)
model_params["indices_dict"] = articulators_indices_dict
batch_size = cfg["batch_size"]
num_workers = cfg.get("num_workers", 0)
datadir = cfg["datadir"]
save_to = cfg["save_to"]
encoders_filepath = cfg["encoders_filepath"]
decoders_filepath = cfg["decoders_filepath"]
evaluate_autoencoder(
database_name,
datadir,
dataset_config,
batch_size,
sequences_dict,
model_params,
encoders_filepath,
decoders_filepath,
save_to,
num_workers=num_workers,
)