# scheck-k/ste-gan architecture

> Architecture diagram of the GitHub repository scheck-k/ste-gan, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/scheck-k/ste-gan
- Repository: https://github.com/scheck-k/ste-gan

## Overview

1. **Repository purpose**
   - This is a research training codebase for **STE-GAN**, a speech-to-EMG generation system.
   - The repo is centered on two training pipelines: a **pretrained EMG encoder** and the main **GAN-based speech-to-EMG model**.

2. **Top-level workflow**
   - The architecture is data-first: raw corpus data is preprocessed into a structured dataset, then used to train the EMG encoder, then used again to train STE-GAN.
   - The main outputs are model checkpoints and training artifacts, not a deployed service.

3. **Data preparation boundary**
   - `scripts/clean_audio.py` cleans raw audio before feature extraction.
   - `scripts/prep_data_gaddy_and_klein.py` converts the Gaddy & Klein corpus into the repo’s internal `data/gaddy_complete/{train,valid,test}` layout.
   - This preprocessing step creates multiple synchronized modalities per utterance: audio, EMG, EMG features, MFCCs, Soft Speech Units, phonemes, and transcriptions.

4. **Dataset and batching subsystem**
   - `ste_gan/data/emg_dataset.py` and `ste_gan/data/loader.py` form the data ingestion layer for training.
   - `ste_gan/data/collate.py` handles batching and variable-length sequence assembly.
   - A key boundary here is that **silent EMG samples are filtered out for training**, because the audio is not synchronous with EMG; the training path focuses on **normal/voiced utterances**.

5. **Feature and modality handling**
   - The repo treats EMG, acoustic features, and linguistic labels as separate modalities.
   - `ste_gan/utils/audio_utils.py`, `emg_utils.py`, and `common.py` provide shared data/feature handling.
   - The main speech input feature for STE-GAN is **Soft Speech Units** from the acoustic side, with MFCCs and phonemes also available in the dataset structure.

6. **EMG encoder subsystem**
   - `ste_gan/emg_encoder/train.py` trains a standalone model that predicts **Soft Speech Units from EMG signals**.
   - `ste_gan/models/emg_encoder.py` contains the encoder model used for that task.
   - `ste_gan/losses/emg_encoder_loss.py` and `ste_gan/emg_encoder/utils.py` support the encoder training loop.
   - This pretrained encoder is then reused in the main STE-GAN training as an auxiliary supervisory signal.

7. **Main STE-GAN model subsystem**
   - `ste_gan/models/generator.py` implements the generator that maps speech features to EMG.
   - `ste_gan/models/discriminator.py` implements the adversarial discriminator.
   - The generator is designed around the repo’s stated goals: predict EMG at **800 Hz**, use **Soft Speech Units** as speech input, and incorporate **learnable session embeddings** to account for recording-session variability.

8. **Neural building blocks**
   - `ste_gan/layers/conv.py`, `transformer.py`, and `average_filter.py` provide the reusable network primitives used by the models.
   - The presence of both convolutional and transformer components suggests the generator/encoder stack mixes local temporal modeling with longer-range sequence modeling.

9. **Loss and optimization structure**
   - `ste_gan/losses/time_domain_loss.py` provides the EMG-oriented waveform/time-domain objective.
   - `ste_gan/losses/emg_encoder_loss.py` supports the auxiliary EMG-encoder objective.
   - `ste_gan/train.py` combines multiple losses, including:
     - speech-unit loss
     - phoneme loss
     - multi-time-domain loss
     - feature-matching loss
   - These losses are weighted via command-line flags and configuration files.

10. **Training orchestration**
   - `ste_gan/train.py` is the main experiment entrypoint.
   - `ste_gan/train_utils.py` appears to hold shared training loop utilities, checkpointing, and run management.
   - The training script supports resuming from checkpoints, debug mode, overriding batch/chunk sizes, and changing the speech feature type.

11. **Configuration system**
   - YAML configs in `configs/` define experiment setups:
     - `configs/data/gaddy_and_klein_corpus.yaml` for dataset paths/splits
     - `configs/emg_encoder/conv_transformer.yaml` for the EMG encoder architecture/training
     - `configs/ste_gan_base_gantts.yaml` for the main STE-GAN run
   - The architecture is therefore config-driven rather than hardcoded, with scripts resolving the selected data, model, and training parameters at runtime.

12. **External dependencies and research integrations**
   - The repo builds on ideas and code from **CarGAN** and **HiFi-GAN**.
   - It depends on **Soft-VC** for extracting Soft Speech Units and for speech synthesis from those units.
   - It uses **MetricGAN+** to denoise audio during preprocessing.
   - The EMG encoder training is adapted from the **silent_speech** project.
   - These are important architectural boundaries because they supply key upstream features and preprocessing steps rather than being implemented locally.

13. **Overall architecture diagram takeaway**
   - A useful diagram would show three layers:
     1. **Raw corpus + preprocessing** → cleaned audio and extracted multimodal dataset
     2. **EMG encoder training** → pretrained encoder checkpoint
     3. **STE-GAN training** → generator/discriminator plus auxiliary encoder-guided losses
   - The main data flow is from **speech/audio features and session metadata** into the generator, and from **EMG targets** into the loss functions and discriminator during adversarial training.

Main entry points: [`ste_gan/train.py`](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/train.py) (Train loop), [`ste_gan/models/generator.py`](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/models/generator.py) (Generator), [`ste_gan/emg_encoder/train.py`](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/emg_encoder/train.py) (Encoder train), [`scripts/prep_data_gaddy_and_klein.py`](https://github.com/scheck-k/ste-gan/blob/master/scripts/prep_data_gaddy_and_klein.py) (Prep corpus)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g1["Preprocessing"]
  node_n1[("Raw audio<br/>corpus")]
  node_n2["Clean audio<br/>preprocess<br/>[clean_audio.py]"]
  node_n3["Prep corpus<br/>preprocess"]
  node_n4["Dataset config"]
end

subgraph group_g2["Data"]
  node_n5[("Multimodal data<br/>dataset")]
  node_n6["Loader<br/>[loader.py]"]
  node_n7[("EMG dataset<br/>[emg_dataset.py]")]
  node_n8["Batching<br/>collate<br/>[collate.py]"]
end

subgraph group_g3["EMG encoder"]
  node_n9["EMG encoder cfg<br/>config"]
  node_n10["Encoder train<br/>train script<br/>[train.py]"]
  node_n11["Encoder model<br/>[emg_encoder.py]"]
  node_n12["Encoder loss"]
end

subgraph group_g4["STE-GAN"]
  node_n13["STE-GAN cfg<br/>config"]
  node_n14["Train loop<br/>training<br/>[train.py]"]
  node_n15["Train utils<br/>training utils<br/>[train_utils.py]"]
  node_n16["Generator<br/>model<br/>[generator.py]"]
  node_n17["Discriminator<br/>model<br/>[discriminator.py]"]
  node_n18["STE-GAN losses<br/>loss bundle"]
end

subgraph group_g5["Shared core"]
  node_n19["Layers<br/>building blocks<br/>[conv.py]"]
  node_n20["Audio utils<br/>[audio_utils.py]"]
  node_n21["EMG utils<br/>[emg_utils.py]"]
  node_n22["Common utils<br/>[common.py]"]
end

node_n23(("Soft-VC<br/>external dependency"))
node_n24(("MetricGAN+<br/>external dependency"))
node_n25(("CarGAN / HiFi-GAN<br/>external dependency"))
node_n26(("silent_speech<br/>external dependency"))

node_n1 -->|"clean"| node_n2
node_n2 -->|"prepare"| node_n3
node_n4 -->|"configure"| node_n3
node_n3 -->|"materialize"| node_n5
node_n24 -.->|"denoise"| node_n2
node_n23 -.->|"units"| node_n5
node_n20 -.->|"features"| node_n3
node_n21 -.->|"signals"| node_n3
node_n5 -->|"load"| node_n7
node_n7 -->|"batch"| node_n8
node_n8 -->|"iterate"| node_n6
node_n5 -->|"train data"| node_n10
node_n9 -->|"configure"| node_n10
node_n10 -->|"build"| node_n11
node_n10 -->|"optimize"| node_n12
node_n26 -.->|"adapted from"| node_n10
node_n13 -->|"configure"| node_n14
node_n6 -->|"feed"| node_n14
node_n15 -->|"orchestrate"| node_n14
node_n14 -->|"train"| node_n16
node_n14 -->|"train"| node_n17
node_n14 -->|"combine"| node_n18
node_n16 -->|"uses"| node_n19
node_n11 -->|"uses"| node_n19
node_n18 -->|"aux signal"| node_n11
node_n18 -->|"supervise"| node_n16
node_n18 -->|"adversarial"| node_n17
node_n16 -.->|"speech feats"| node_n20
node_n16 -.->|"EMG target"| node_n21
node_n14 -.->|"shared"| node_n22
node_n23 -.->|"speech units"| node_n16
node_n25 -.->|"inspired by"| node_n16

click node_n2 "https://github.com/scheck-k/ste-gan/blob/master/scripts/clean_audio.py"
click node_n3 "https://github.com/scheck-k/ste-gan/blob/master/scripts/prep_data_gaddy_and_klein.py"
click node_n4 "https://github.com/scheck-k/ste-gan/blob/master/configs/data/gaddy_and_klein_corpus.yaml"
click node_n6 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/data/loader.py"
click node_n7 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/data/emg_dataset.py"
click node_n8 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/data/collate.py"
click node_n9 "https://github.com/scheck-k/ste-gan/blob/master/configs/emg_encoder/conv_transformer.yaml"
click node_n10 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/emg_encoder/train.py"
click node_n11 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/models/emg_encoder.py"
click node_n12 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/losses/emg_encoder_loss.py"
click node_n13 "https://github.com/scheck-k/ste-gan/blob/master/configs/ste_gan_base_gantts.yaml"
click node_n14 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/train.py"
click node_n15 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/train_utils.py"
click node_n16 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/models/generator.py"
click node_n17 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/models/discriminator.py"
click node_n18 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/losses/time_domain_loss.py"
click node_n19 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/layers/conv.py"
click node_n20 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/utils/audio_utils.py"
click node_n21 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/utils/emg_utils.py"
click node_n22 "https://github.com/scheck-k/ste-gan/blob/master/ste_gan/utils/common.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n1,node_n2,node_n3,node_n4 toneBlue
class node_n5,node_n6,node_n7,node_n8 toneAmber
class node_n9,node_n10,node_n11,node_n12 toneMint
class node_n13,node_n14,node_n15,node_n16,node_n17,node_n18 toneRose
class node_n19,node_n20,node_n21,node_n22 toneIndigo
class node_n23,node_n24,node_n25,node_n26 toneNeutral
```

## Components

### Preprocessing

Raw corpus cleaning and multimodal dataset preparation.

- **Raw audio**: corpus · Source corpus and recordings before preprocessing.
- [Clean audio](https://github.com/scheck-k/ste-gan/blob/master/scripts/clean_audio.py): preprocess · Audio denoising and cleanup before feature extraction. · `scripts/clean_audio.py`
- [Prep corpus](https://github.com/scheck-k/ste-gan/blob/master/scripts/prep_data_gaddy_and_klein.py): preprocess · Builds the internal train/valid/test multimodal dataset layout. · `scripts/prep_data_gaddy_and_klein.py`
- [Dataset config](https://github.com/scheck-k/ste-gan/blob/master/configs/data/gaddy_and_klein_corpus.yaml): config · Corpus paths, splits, and dataset settings. · `configs/data/gaddy_and_klein_corpus.yaml`

### Data

Dataset loading, filtering, and batching.

- **Multimodal data**: dataset · Structured utterances with audio, EMG, EMG features, MFCCs, Soft Speech Units, phonemes, and transcripts.
- [Loader](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/data/loader.py): dataloader · Builds training and validation iterators. · `ste_gan/data/loader.py`
- [EMG dataset](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/data/emg_dataset.py): dataset · Dataset access layer with silent-sample filtering for training. · `ste_gan/data/emg_dataset.py`
- [Batching](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/data/collate.py): collate · Assembles variable-length sequences into batches. · `ste_gan/data/collate.py`

### EMG encoder

Standalone pretrained encoder pipeline.

- [EMG encoder cfg](https://github.com/scheck-k/ste-gan/blob/master/configs/emg_encoder/conv_transformer.yaml): config · Architecture and training settings for the encoder model. · `configs/emg_encoder/conv_transformer.yaml`
- [Encoder train](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/emg_encoder/train.py): train script · Standalone training loop that predicts Soft Speech Units from EMG. · `ste_gan/emg_encoder/train.py`
- [Encoder model](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/models/emg_encoder.py): model · Pretrained EMG-to-Soft-Speech-Units encoder. · `ste_gan/models/emg_encoder.py`
- [Encoder loss](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/losses/emg_encoder_loss.py): loss · Objective used to train the EMG encoder. · `ste_gan/losses/emg_encoder_loss.py`

### STE-GAN

Main adversarial speech-to-EMG training pipeline.

- [STE-GAN cfg](https://github.com/scheck-k/ste-gan/blob/master/configs/ste_gan_base_gantts.yaml): config · Main GAN experiment configuration. · `configs/ste_gan_base_gantts.yaml`
- [Train loop](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/train.py): training · Main experiment entrypoint with checkpointing, resuming, and CLI overrides. · `ste_gan/train.py`
- [Train utils](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/train_utils.py): training utils · Shared run management and orchestration helpers. · `ste_gan/train_utils.py`
- [Generator](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/models/generator.py): model · Maps speech features and session context to EMG at 800 Hz. · `ste_gan/models/generator.py`
- [Discriminator](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/models/discriminator.py): model · Adversarial discriminator for EMG realism. · `ste_gan/models/discriminator.py`
- [STE-GAN losses](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/losses/time_domain_loss.py): loss bundle · Speech-unit, phoneme, multi-time-domain, and feature-matching objectives. · `ste_gan/losses/time_domain_loss.py`

### Shared core

Reusable layers, losses, and utilities used by both training paths.

- [Layers](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/layers/conv.py): building blocks · Reusable convolution, transformer, and averaging primitives. · `ste_gan/layers/conv.py`
- [Audio utils](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/utils/audio_utils.py): utils · Audio and feature handling helpers. · `ste_gan/utils/audio_utils.py`
- [EMG utils](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/utils/emg_utils.py): utils · EMG feature and signal helpers. · `ste_gan/utils/emg_utils.py`
- [Common utils](https://github.com/scheck-k/ste-gan/blob/master/ste_gan/utils/common.py): utils · Shared constants and general helpers. · `ste_gan/utils/common.py`

### Other components

- **Soft-VC**: external dependency · Provides Soft Speech Units and related speech-unit workflow.
- **MetricGAN+**: external dependency · Used for audio denoising during preprocessing.
- **CarGAN / HiFi-GAN**: external dependency · Upstream GAN ideas and implementation references.
- **silent_speech**: external dependency · Reference for the EMG encoder training adaptation.

## Connections

- Raw audio → Clean audio: clean · Raw audio is denoised and cleaned before downstream processing.
- Clean audio → Prep corpus: prepare · Cleaned audio feeds multimodal corpus preparation.
- Dataset config → Prep corpus: configure · Dataset config selects paths and split layout for preprocessing.
- Prep corpus → Multimodal data: materialize · Preprocessing writes the structured multimodal dataset.
- MetricGAN+ → Clean audio: denoise · External denoising capability informs audio cleanup.
- Soft-VC → Multimodal data: units · Soft Speech Units are one of the prepared modalities.
- Audio utils → Prep corpus: features · Audio feature helpers support preprocessing.
- EMG utils → Prep corpus: signals · EMG helpers support preprocessing and feature extraction.
- Multimodal data → EMG dataset: load · Dataset objects read the prepared multimodal corpus.
- EMG dataset → Batching: batch · Dataset samples are assembled into variable-length batches.
- Batching → Loader: iterate · Collated batches feed the training data loader.
- Multimodal data → Encoder train: train data · Prepared dataset is consumed by the EMG encoder training loop.
- EMG encoder cfg → Encoder train: configure · Encoder config selects model and training parameters.
- Encoder train → Encoder model: build · Training instantiates the EMG encoder model.
- Encoder train → Encoder loss: optimize · Encoder training uses the dedicated encoder loss.
- silent_speech → Encoder train: adapted from · The encoder training approach is adapted from silent_speech.
- STE-GAN cfg → Train loop: configure · Main STE-GAN run is driven by YAML configuration.
- Loader → Train loop: feed · Training batches are provided to the main loop.
- Train utils → Train loop: orchestrate · Run management and checkpoint utilities support training.
- Train loop → Generator: train · Main loop optimizes the generator.
- Train loop → Discriminator: train · Main loop optimizes the discriminator adversarially.
- Train loop → STE-GAN losses: combine · Training aggregates multiple objectives and weighted losses.
- Generator → Layers: uses · Generator relies on reusable neural building blocks.
- Encoder model → Layers: uses · Encoder also reuses the shared layer primitives.
- STE-GAN losses → Encoder model: aux signal · Pretrained encoder provides auxiliary supervisory signal to main training.
- STE-GAN losses → Generator: supervise · Losses constrain generator outputs against EMG targets and speech-side objectives.
- STE-GAN losses → Discriminator: adversarial · Feature matching and discriminator objectives shape GAN training.
- Generator → Audio utils: speech feats · Generator consumes speech-side features and utilities.
- Generator → EMG utils: EMG target · Generator is trained against EMG signal structure.
- Train loop → Common utils: shared · Common constants and helpers are used by training code.
- Soft-VC → Generator: speech units · Soft Speech Units are the primary speech input representation.
- CarGAN / HiFi-GAN → Generator: inspired by · Generator design builds on CarGAN and HiFi-GAN ideas.
