Skip to main content

A joint French phoneme recognition and alignment model

PyPI

Joint phone recognition and phone alignment for French speech, with one wav2vec2 model. A CTC head transcribes the speech into phones. A cross-attention aligner, trained in the same network, places each phone in time, with optional silences. The same model gives the phone sequence and its segmentation, at 10 ms resolution.

This repository has the code to train the models from scratch and to transcribe recordings into Praat TextGrids (phones, boundaries, pauses and the model's top-5 candidates for each phone), with no transcript needed.

Phoneticians: start with the step-by-step guide: install with two commands, then transcribe a corpus with one command (with an optional HTML report of the plots), or from Praat.

Contents

Models

Training runs in three stages. Each stage starts from the previous one.

Stage Model Starts from Frame Recipe
1 CTC 20 ms: wav2vec2 phone recogniser LeBenchmark/wav2vec2-FR-7K-large 20 ms recipes/1_ctc_20ms.sh
2 CTC 10 ms: same, last conv layer with stride 1 CTC 20 ms 10 ms recipes/2_ctc_10ms.sh
3 Joint aligner 10 ms + latent silence CTC 10 ms 10 ms recipes/3_aligner_10ms_sil.sh

Stage 3 has two variants. They differ in how strongly the alignment losses train the encoder: ALPHA=0.1 (default) and ALPHA=1.0.

CTC phone recogniser (stages 1 and 2)

Wav2Vec2ForCTC fine-tuned with CTC on phone sequences. The conv front-end is frozen, and the epoch with the best validation phone error rate (PER) is kept.

Stage 2 changes the stride of the last conv layer from 2 to 1. Frames are then 10 ms instead of 20 ms. Every pretrained weight still loads, and the model is fine-tuned again at the new rate.

Joint recognition + alignment model (stage 3)

jointrecalign.model.Wav2Vec2ForCTCPoolContrastive, built on the CTC 10 ms model:

waveform ─► wav2vec2 encoder ─┬─► CTC head ──────────────────────────► phones        (L_CTC)
                              │
                              ├─► ConvBank (kernels 1,5,9) ─► queries ┐
                              │                                       ├─► attention energy  (L_FS)
                              └─► phone keys ─────────────────────────┘        │
                                  (CTC-posterior × Gaussian window              ▼
                                   pooling of encoder frames)          masked reconstruction (L_m)
  • Phone keys. Each phone of the target sequence gets one vector. It is a weighted average of encoder frames: the weights are that phone's CTC posterior times a Gaussian window (σ = 10 frames) centred on the phone. No separate text encoder is needed.
  • L_FS: ForwardSum alignment loss. It is computed on the frame × phone attention energy, with a beta-binomial prior that is annealed away between 15 % and 50 % of training.
  • L_m: masked reconstruction loss.
    • The encoder frames are masked (p ~ U[0.1, 0.5]).
    • They are reconstructed from the masked frames and the alignment-weighted phone keys.
    • The reconstruction is scored with InfoNCE against codes from an EMA vector quantizer of the encoder output (nearest-code assignment, 320 codes).
    • A good reconstruction needs the right phone at the right frame, so L_m also trains the alignment.
  • L_energy. A small head predicts each frame's waveform log-energy from the alignment content alone. It is added to L_m. A loud frame assigned to silence is then predicted quiet, and the error corrects the alignment.
  • Latent optional silence (SIL) starts at step 2000, in the style of Kaldi/MFA.
    • SIL slots are inserted at word boundaries: [SIL] p1 p2 [SIL] p3 … [SIL]. They are optional and all share one learned key.
    • Entering a slot costs log p = −2, and a used SIL lasts at least 4 frames (40 ms).
    • The ForwardSum dynamic programming sums over paths with and without each SIL (jointrecalign/sil_fs.py).
    • L_m takes its attention from the forward-backward occupancy.
    • At inference, a Viterbi pass over the same topology gives phone boundaries and pauses.
  • Gradient routing.
    • L_CTC trains the whole encoder.
    • L_FS and L_m reach only the top 2 transformer layers, through the queries.
    • Their gradient is scaled by ALPHA (--encoder_alpha, --lm_encoder_alpha), ramped in between steps 2000 and 5000.
    • Phone keys and reconstruction targets are always detached.

At inference the model aligns its own CTC decode, so it needs no transcription.

Installation

Python ≥ 3.10. A CUDA GPU for training; transcription also runs on CPU.

To transcribe recordings only, see the step-by-step guide (no Python knowledge needed), or, in an existing Python environment:

pip install JointRecAlign

(pip install git+https://github.com/Imenbaa/Joint_fr_phoneme_recognition_alignment.git installs the latest code from GitHub instead of the latest release.)

The model runs on PyTorch, and the PyTorch build that pip installs depends on the computer:

Computer pip installs To do
Linux with an NVIDIA GPU the CUDA build nothing
Linux without an NVIDIA GPU the CUDA build (about 5 GB of NVIDIA libraries it does not use) install the CPU build first: pip install torch torchaudio --index-url https://download.pytorch.org/whl/cpu
Windows with an NVIDIA GPU the CPU build (the GPU stays unused) install a CUDA build first, with the command for your setup from pytorch.org
macOS, Windows without an NVIDIA GPU the right build nothing

When torch is already installed, pip install JointRecAlign keeps it. With uv, uv pip install --torch-backend auto JointRecAlign chooses the build that matches the computer (GUIDE.md does the same with uv tool install). The held-out alignment scores of train_aligner.py also need pip install "JointRecAlign[eval]" (librosa, pandas), which requirements.txt covers.

Both install the JointRecAlign command.

To train, clone the repository:

git clone https://github.com/Imenbaa/Joint_fr_phoneme_recognition_alignment.git
cd Joint_fr_phoneme_recognition_alignment
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt     # versions used to train the models
pip install -e .                    # the jointrecalign package

The scripts at the repository root (train_ctc.py, train_aligner.py, transcribe.py) also run from the repository root without pip install -e .. pip install -e . also installs the JointRecAlign command.

Transcribing recordings

JointRecAlign (or python transcribe.py from the repository root) gives, for each recording, the phones the model hears, their boundaries, the pauses, and the model's five best candidates for each phone. It runs on GPU or CPU.

JointRecAlign validate recordings_folder/                 # check the audio files (no model)
JointRecAlign transcribe recordings_folder/ [-o out/]     # check, then transcribe
JointRecAlign transcribe recordings_folder/ --report      # + one HTML page per recording, index.html
JointRecAlign transcribe recordings_folder/ --reference textgrids/   # + comparison with reference TextGrids
JointRecAlign align recordings_folder/ --phones phones_folder/        # forced alignment of your phones
JointRecAlign install-praat-plugin                        # a "JointRecAlign: transcribe..." button for Sounds in Praat

transcribe checks every file first (readable, at least 0.5 s, not silent; warnings for a sampling rate under 16 kHz, several channels, clipping, very low level, two recordings that would write the same result files), skips the files that cannot be transcribed, goes on when one fails, and ends with a summary, also written to JointRecAlign.log. The exit code is 1 when a recording was not transcribed.

The model (aligner_10ms_sil_alpha0.1.zip, 1.2 GB, release v0.1.0, licence CC BY-NC 4.0) is downloaded and checked the first time (or with JointRecAlign download) into a cache folder: ~/.cache/JointRecAlign (Linux), ~/Library/Caches/JointRecAlign (macOS), %LOCALAPPDATA%\JointRecAlign (Windows); JOINTRECALIGN_HOME changes it. Any stage-3 checkpoint you train yourself (joint aligner with latent silence, see Training) works with --model, e.g. --model $WORK_DIR/aligner_10ms_sil_alpha0.1/checkpoint-43000.

Audio: WAV, FLAC, MP3, OGG or AIFF, any sampling rate, mono or stereo (resampled to 16 kHz and downmixed). A folder means every audio file in it. A recording that cannot be read is reported and the others are still transcribed.

For each recording, in a folder JointRecAlign/ next to the recording (or in -o), and JointRecAlign.log:

file content
<name>.TextGrid Praat interval tiers, all covering the whole recording
<name>.csv one row per phone: start, end, phone, then top1, p1 ... top5, p5
<name>.html with --report: waveform, spectrogram, phones and pauses, the aligner's attention with the alignment path, the CTC posteriors of every phone, the five candidates of each phone coloured by probability, on one time axis that scrolls and zooms; self-contained, no server (index.html lists the recordings)
tier label of each interval
phones the recognised phone (empty between phones)
confidence probability of that phone
top2 ... top5 the next candidates, as symbol probability (e.g. p 0.050)
pauses SIL on every pause the model detected
option meaning
-o folder where the results go (default: JointRecAlign/ next to each recording)
--report also the HTML pages (about 3.5 MB per minute of audio)
--copy_audio with --report: copy each recording into <results>/audio/, so the pages still play it after the results folder is moved or copied to another computer
--model folder another model (default: the released one, downloaded the first time)
--symbols ipa (default) / model ipa writes the nasal vowels and œ in IPA (ɑ̃ ɛ̃ ɔ̃ œ); model keeps the model's one-character codes (@ 1 § 9)
--phone_map map.json your own labels, applied last, e.g. {"ə": "ø"} for a corpus that writes the schwa ø
--max_chunk 10 longest piece of audio the model sees at once, in seconds
--vocab_path phone vocabulary (default: vocab.json in the checkpoint)
--device cuda / cpu default: cuda when available

Forced alignment. align takes the phones from --phones folder: <name>.TextGrid (a phone tier, --phones_tier; its times are not used) or <name>.txt (phones separated by white space), and aligns them in place of the CTC decode, with the same SIL-Viterbi decode (boundaries and pauses) and the same outputs. Labels are read as IPA or model symbols, plus g, r, ʀ, ɥ (-> y), œ̃ (-> ɛ̃); --phones_map map.json maps the others; pause labels are dropped. The candidate tiers remain the CTC posteriors inside each aligned phone, so they show where the model hears something else. A recording longer than --max_chunk is decoded chunk by chunk first, and each given phone goes to the chunk of the decoded phone it matches (edit-distance alignment).

Comparison with a reference (--reference FOLDER, jointrecalign/reference.py): for <name>.wav, the phone tier of FOLDER/<name>.TextGrid (--ref_tier, default the tier named phones/phonemes/..., else the first interval tier) is compared with the transcription with these metrics:

figure meaning
PER substitutions + deletions + insertions over the reference phones
F1 20 ms / 50 ms boundary detection: every phone onset and offset; each reference boundary is matched one-to-one to the nearest recognised boundary within 20 / 50 ms
median error median |error| of the starts and ends of the phones recognised with the reference label
pause P / R / F1 reference pauses matched one-to-one by a recognised pause (time not covered by a phone) with intersection over union ≥ 0.5

Empty intervals and --pause_labels (default SIL sil sp spn _ # <p:> ...) are reference pauses, --skip_labels (e.g. noise,overlap) are left out, and --ref_map map.json rewrites the other labels into the model's phones (e.g. {"ø": "ə"} for a corpus that writes the schwa ø). The summary lists the reference labels that are not phones of the model; they count as errors. Results: comparison.csv (one row per recording and a TOTAL row; corpus figures pool the counts of all recordings), the scores in the summary and, with --report, in the pages: reference lane under the recognised phones (amber: substituted, red: inserted / deleted), reference boundaries and pauses in red on the attention, reference phones as red boxes on the posteriors, on the top 5 a reference strip with each recognised phone's candidates outlined in green (reference = 1st) or red (reference among the others), and a "show reference" switch. A reference TextGrid is never overwritten (-o into the reference folder is refused when the names collide).

How it works (jointrecalign/transcribe.py):

  1. The recording is cut into pieces of at most 10 s, back to back, so no audio is dropped. Silero VAD finds the pauses; each cut goes in the middle of the latest pause within 10 s (or, with no pause, where speech is least likely).
  2. For each piece: the CTC head gives the phones; a Viterbi pass over [SIL] p1 [SIL] p2 ... [SIL] on the aligner gives their boundaries and the pauses. A pause cut in two by a piece boundary is joined back into one.
  3. The candidates of a phone are the CTC posteriors at the frame where that phone is most probable, over the phone symbols only, renormalised to sum to 1.

Read the probabilities with care: they are the model's own and are not calibrated. The top-1 is often given about 0.99 even when it is wrong, but the order of the candidates is informative: when the top-1 is wrong, the right phone is 2nd in 29 to 57 % of the cases and in the top 5 in 53 to 79 %, depending on the kind of speech (measured against reference transcriptions; see GUIDE.md, "How far to trust the candidates").

The model cannot see the first and last ~10 ms of each piece, so a gap of 15 to 25 ms with no label is left at each cut (about every 10 s) and at the start and end of the recording.

Data

Training corpus

The models were trained on French speech with phone transcriptions. Utterances containing an unknown word were removed. Any French (or other) corpus with phone-level transcriptions can be used in the same format.

Format

The code reads a Hugging Face audiofolder:

DATA_DIR/
  train/        metadata.csv + audio files
  dev/          (or validation/)
  test/

metadata.csv has one row per utterance:

column required content
file_name yes audio file, relative to the split folder
phonemes_single_v2 yes the phone sequence, one symbol per phone, separated by spaces: p a t ʁ i s j a
phonemes_single stage 3 the same phones written as one string per word, words separated by spaces: patʁisja maʁt1. It places the SIL slots at word boundaries, so each phone symbol must be a single character, identical in both columns
phonemes no if present, rows containing |ø|n|k| (unknown word) are dropped

Any other column (for example transcription) is ignored. Producing the phone transcriptions (lexicon / grapheme-to-phoneme conversion) is not part of this repository. The phone vocabulary is built from the data in stage 0.

Training

Every recipe reads two environment variables. Settings can be overridden with extra flags, which are passed through to the Python script.

  • WORK_DIR: receives the preprocessed data and all models.
  • DATA_DIR: the audiofolder above.
export DATA_DIR=/path/to/audiofolder WORK_DIR=/path/to/work

bash recipes/0_prepare_data.sh            # -> $WORK_DIR/data/{preprocessed_16k, vocab.json}
NGPU=2 bash recipes/1_ctc_20ms.sh         # -> $WORK_DIR/ctc_20ms
bash recipes/2_ctc_10ms.sh                # -> $WORK_DIR/ctc_10ms
bash recipes/3_aligner_10ms_sil.sh        # -> $WORK_DIR/aligner_10ms_sil_alpha0.1
ALPHA=1.0 bash recipes/3_aligner_10ms_sil.sh   # -> $WORK_DIR/aligner_10ms_sil_alpha1.0

Choose the GPU with CUDA_VISIBLE_DEVICES. Stage 1 uses torchrun with NGPU processes. The gradient accumulation adapts so that the effective batch stays 128. TensorBoard logs go to <output>/runs.

Stage 0: data preparation (train_ctc.py --preprocess_only)

The audio is resampled to 16 kHz and normalised, and the phone strings are converted to ids. The result is an Arrow dataset, plus a vocabulary file vocab.json ([PAD] = CTC blank, [UNK], then the phones). All later stages read this dataset.

Stages 1–2: CTC (train_ctc.py)

stage 1 stage 2
--init_model LeBenchmark/wav2vec2-FR-7K-large $WORK_DIR/ctc_20ms
--last_conv_stride 2 (20 ms) 1 (10 ms)
--epochs 30 10
--lr 3e-5 (linear decay, 500 warm-up steps) 3e-5
batch 32 × 2 GPUs × grad_acc 2 = 128 16 × grad_acc 4 = 64
precision bf16 on Ampere GPUs, else fp16 same

Other settings, the same for both stages:

  • frozen conv front-end (--train_cnn unfreezes it);
  • weight decay 0.005, gradient clipping 2.0;
  • gradient checkpointing;
  • the epoch with the best validation PER is kept.

Run python train_ctc.py --help for every option.

Stage 3: joint aligner (train_aligner.py)

Main settings of recipes/3_aligner_10ms_sil.sh:

group flags
length, batch --max_steps 43750, batch 4 × grad_acc 8 = 32, --fp16, --seed 42
learning rates encoder + CTC head --backbone_lr 1e-5, aligner --alignment_lr 1e-4, weight decay 1e-4, no warm-up, clipping 1.0
losses --ctc_weight 1 --align_weight 1 --contrastive_weight 1; L_FS ramps in over --align_warmup_steps 1000
aligner --pool_mode window --window_sigma_frames 10 --convbank_kernels 1,5,9; attention dimension = encoder size / 2
prior --prior_type beta_binomial --prior_scaling_factor 0.5 --prior_schedule anneal from 0.15 to 0.5 of training
L_m --quantizer_assign nearest --lm_mask_same_code --no_position_features, 50 negatives, temperature 0.1
L_energy --lm_energy_weight 1.0
routing --encoder_alpha ALPHA --lm_encoder_alpha ALPHA --encoder_top_layers 2, ramp --encoder_alpha_start_steps 2000 --encoder_alpha_ramp_steps 3000
silence --sil --sil_positions word --sil_logp -2.0 --sil_min_frames 4 --sil_init lowenergy --sil_start_steps 2000 --lm_att_source fs_posterior
bookkeeping checkpoint every 1000 steps (--optimizer_last_only keeps optimizer.pt in the newest one only); validation PER every 1000 steps

Run python train_aligner.py --help for every option and its meaning.

Notes:

  • Held-out alignment metrics. EVAL_MANIFEST=/path/to/manifest.json (format: jointrecalign/evaluation/attention_eval.py) also scores each checkpoint as an aligner on a held-out manifest. The scores are logged to TensorBoard under heldout/ and saved in <output>/heldout/. They are not used to select checkpoints.
  • Resuming. Training resumes from the newest checkpoint in the output folder, so a run that is stopped can be restarted with the same command.
  • Expected warning. The trainer warns that 7 trainable tensors are in no optimizer group. These are the wav2vec2 pretraining heads (quantizer, project_hid, project_q), which this model does not use.

Tests

CPU only. They need no data or pretrained weights:

python tests/test_sil_fs.py   # latent-SIL DP vs brute force, gradients, batching, == ForwardSumLoss without SIL
python tests/test_model.py    # full model on a tiny random wav2vec2: losses, gradient routing, SIL, L_energy, decoders
python tests/test_transcribe.py   # chunking, pause joining, candidates, TextGrid/CSV, end to end on a tiny model
python tests/test_reference.py    # TextGrid reading, reference labels, comparison scores

The end-to-end part of test_transcribe.py needs silero-vad (in requirements.txt) and is skipped without it.

Repository layout

train_ctc.py              stages 0-2: data preparation, CTC fine-tuning (20 or 10 ms)
train_aligner.py          stage 3: joint recognition + alignment model
transcribe.py             recordings -> TextGrids (= JointRecAlign)
GUIDE.md                  step-by-step guide for phoneticians
recipes/                  one script per stage, with the settings of the trained models
jointrecalign/
  transcribe.py           Silero chunking, decoding, top-5 candidates, TextGrid / CSV writers
  transcribe_cli.py       the JointRecAlign command
  report.py, report_assets/  the HTML report
  reference.py            comparison with reference TextGrids
  download.py             download of the released model into a cache folder
  praat.py, praat_plugin/ the Praat plugin
  modules.py              ForwardSum loss, ConvBank, cross-attention, prior helpers
  pool_alignment.py       CTC + aligner with pooled phone keys, latent SIL, gradient routing
  model.py                + masked reconstruction (L_m), EMA quantizer, L_energy
  sil_fs.py               latent optional SIL: topology, forward-backward, Viterbi
  evaluation/
    attention_eval.py     manifest loop, DTW decode, frame timing
    sil_eval.py           SIL Viterbi decode and silence scores
    ctcfa_eval.py         CTC forced-alignment baseline
    metrics.py            boundary F1, errors, PER
tests/

Generative AI Use Disclosure

The code in this repository was refactored, organised and refined with the aid of Claude Code (Anthropic).

License

Code: MIT, see LICENSE. Released model weights: CC BY-NC 4.0 (non-commercial use, with attribution).

Metadata

Release files for JointRecAlign 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for JointRecAlign 0.1.4
File Size Uploaded Provenance Actions
jointrecalign-0.1.4.tar.gz 111.1 kB Without provenance Details for jointrecalign-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for JointRecAlign 0.1.4
File Interpreter ABI Platform Actions
jointrecalign-0.1.4-py3-none-any.whl Python 3 none any Details for jointrecalign-0.1.4-py3-none-any.whl

Total release size: 208.6 kB

Release files / jointrecalign-0.1.4.tar.gz

Download URL jointrecalign-0.1.4.tar.gz
Size 111.1 kB
Tags Source
SHA-256 checksum
How to use checksums
33102bd867691dc3a029bfd4a4b6d4ef482ea8c5544b099a209e24c423c566dc
BLAKE2b-256 checksum
How to use checksums
c313fd924c307a3899f124ca484ededdebdf5a534d8d34ecd61d881dd4b7e735
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.11

Release files / jointrecalign-0.1.4-py3-none-any.whl

Download URL jointrecalign-0.1.4-py3-none-any.whl
Size 97.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be1c38ef2c5ee724cf02ccebc9f1221de8b3d7254ab8abfeb6c126d5b089ccfd
BLAKE2b-256 checksum
How to use checksums
f3ab41e50dede86a8b78390a0a59799118d63d5e84aa3b4cc7f4d2ffe2fe0ae3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.11

Security for JointRecAlign 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page