Silent Speech Synthesis¶
The transduction model converts EMG signals into audio features (MFCCs), which are then reconstructed into waveforms using a HiFi-GAN vocoder.
Core API¶
Main Script¶
The transduction_model.py script handles training, validation, and evaluation using ASR models to compute WER on generated audio.
align_from_distances
¶
Computes an alignment between two sequences given a distance matrix using Dynamic Time Warping (DTW).
Parameters:
-
distance_matrix(ndarray) –A 2D array of shape (seq1_len, seq2_len) containing pairwise distances.
-
debug(bool, default:False) –If True, will display a visualization of the alignment.
Returns:
-
List[int]–A list of indices where the i-th element is the index in the second sequence that best aligns with the i-th element of the first sequence.
Source code in transduction_model.py
dtw_loss
¶
Computes a loss between prediction and audio using Dynamic Time Warping (DTW) for silent speech. Also calculates phoneme classification accuracy for both silent and voiced speech.
Parameters:
-
predictions(Tensor) –Audio feature predictions from the model.
-
phoneme_predictions(Tensor) –Phoneme class log-probabilities or logits.
-
example(dict) –A batch from the dataloader containing ground truth.
-
phoneme_eval(bool, default:False) –Whether to calculate confusion matrix and accuracy.
-
phoneme_confusion(ndarray, default:None) –Accumulator of phoneme confusion.
Returns:
-
tuple[Tensor, float]–tuple[torch.Tensor, float]: Mean loss per sequence frame and phoneme accuracy.
Source code in transduction_model.py
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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
evaluate
¶
Evaluates the model by transcribing generated audio using a pre-trained ASR model and calculating the Word Error Rate (WER).
Parameters:
-
testset(H5EmgDataset) –The dataset to evaluate on.
-
audio_directory(str) –The directory where the generated audio files are stored.
Source code in transduction_model.py
get_aligned_prediction
¶
Gets model predictions and optionally aligns them with target features using DTW if silent.
Parameters:
-
model(Module) –The model to use.
-
datapoint(dict) –The data sample.
-
device(str) –The device to run calculation on.
-
audio_normalizer(object) –Normalizer for scaling features back.
Returns:
-
Tensor–torch.Tensor: The predicted (and possibly aligned) audio features.
Source code in transduction_model.py
main
¶
Main entry point for training the EMG to audio transduction model.
Source code in transduction_model.py
save_output
¶
Generates audio from a model prediction for a single datapoint and saves it to a file.
Parameters:
-
model(Module) –The model used for inference.
-
datapoint(dict) –The sample to use for generating audio.
-
filename(str) –The output filename to save the audio.
-
device(str) –The device to use for computation.
-
audio_normalizer(object) –Object used for inverse normalization of MFCC features.
-
vocoder(Vocoder) –The vocoder used to generate wav files from features.
Source code in transduction_model.py
test
¶
Performs validation on the provided test set, calculating loss and phoneme accuracy.
Parameters:
-
model(Module) –The model to evaluate.
-
testset(H5EmgDataset) –The dataset to use for validation.
-
device(str) –The device (cpu or cuda) to run evaluation on.
Returns:
-
tuple[float, float, ndarray]–tuple[float, float, np.ndarray]: A tuple containing mean loss, mean phoneme accuracy, and the phoneme confusion matrix.
Source code in transduction_model.py
time_warp
¶
Computes the Dynamic Time Warping (DTW) cost matrix for the given distance matrix.
Parameters:
-
costs(ndarray) –A 2D array of shape (seq1_len, seq2_len) containing pairwise distances.
Returns:
-
dtw(ndarray) –A 2D array of the same shape as costs, where dtw[i, j] is the minimum cumulative cost
Source code in transduction_model.py
train_model
¶
Sets up the model, optimizer, scheduler, and runs the training loop over multiple epochs.
Parameters:
-
trainset(H5EmgDataset) –Dataset for training.
-
devset(H5EmgDataset) –Dataset for validation.
-
device(str) –Device to run training on.
-
save_sound_outputs(bool, default:True) –Whether to generate audio samples and evaluate them during training.
Returns:
-
Module–torch.nn.Module: The trained model with best validation loss.
Source code in transduction_model.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | |
Vocoder Utilities¶
Handles the conversion from MFCC features back to audio waveforms.
vocoder
¶
Vocoder
¶
Bases: object
Source code in vocoder.py
__call__
¶
Generates audio from a mel-spectrogram.
Parameters:
-
mel_spectrogram(Tensor) –Mel-spectrogram tensor of shape (seq_len, 80).
Returns:
-
Tensor–torch.Tensor: 1D audio tensor.
Source code in vocoder.py
download_and_extract_pretrained
¶
Downloads a zip file from a URL, extracts it, and moves the HiFi-GAN model to the expected location.
Parameters:
-
url(str) –The URL to download the zip file from.
-
dest_dir(str) –The destination directory (e.g., './').