ignite icon indicating copy to clipboard operation
ignite copied to clipboard

[Feature] Add Word Error Rate (WER) and Character Error Rate (CER) metrics to ignite.metrics.nlp

Open aniketpandey05 opened this issue 8 months ago • 11 comments

🚀 Feature

ignite/metrics/nlp/ currently has two metrics:

  • Bleu
  • Rouge

I would like to propose adding WordErrorRate (WER) and CharErrorRate (CER).

Motivation: Both existing metrics in ignite/metrics/nlp/ are designed for generative tasks where multiple valid outputs exist. Bleu and Rouge reward partial matches and alternative wordings.

WER and CER provide a necessary counter-perspective they evaluate sequences strictly on exact transcription using Levenshtein edit distance counting the absolute minimum substitutions, deletions, and insertions. A single hallucinated word or dropped character lowers the score immediately, making them strict exact-match validation metrics.

Naively averaging batch-level WER gives mathematically incorrect results because sequences have variable lengths. The correct approach requires global accumulation across the entire epoch:

WER = (total_S + total_D + total_I) / total_N

Users currently have no native way to do this correctly they have to install external libraries like jiwer and write custom handlers.

TorchMetrics and HuggingFace evaluate already include WordErrorRate and CharErrorRate as core NLP metrics.

Use Cases:

  • A user fine-tuning Whisper or Wav2Vec2 can simply attach WordErrorRate to their Ignite Engine to monitor exact transcription quality epoch by epoch. This is helpful for ensuring their ASR model isn't quietly hallucinating or dropping words as training progresses.

  • If a user needs smarter checkpointing, they can log WER directly to their EarlyStopping handler. This is highly useful since cross-entropy loss often plateaus before actual transcription quality does — the lowest WER score points right to the optimal checkpoint.

  • If a user is training an OCR or Document AI model, attaching CharErrorRate to their validation loop gives a strict character-level accuracy check critical when a single character error (like misreading a financial figure) is a severe failure.

References showing real usage:

  • HuggingFace (2021) — Official Wav2Vec2 fine-tuning blog explicitly installs pip install jiwer just to compute WER: https://huggingface.co/blog/fine-tune-wav2vec2-english

  • OpenAI Whisper original paper (arxiv, 2022) — uses WER and CER as the primary evaluation metrics throughout all experiments: https://arxiv.org/abs/2212.04356

  • Towards Data Science (2021) — practical blog showing WER and CER used together to evaluate OCR models with Python code examples using TesseractOCR: https://towardsdatascience.com/evaluating-ocr-output-quality-with-character-error-rate-cer-and-word-error-rate-wer-853175297510/

Would love to hear your thoughts on whether this addition would be helpful!

aniketpandey05 avatar Mar 05 '26 08:03 aniketpandey05

@kzkadc for feedback on whether this would be a good addition.

aniketpandey05 avatar Mar 05 '26 10:03 aniketpandey05

Hi @rogueslasher and @kzkadc! I would love to work on implementing the WER and CER metrics. I will follow the standard structure in ignite.metrics.nlp and open a PR shortly!

Manimaran-tech avatar Mar 05 '26 17:03 Manimaran-tech

Hi @rogueslasher,

My sincere apologies for implementing this before reaching out. I saw the issue and, as it aligned perfectly with the feature set I am preparing for my GSoC proposal (specifically around NLP metrics), I eagerly went ahead and submitted PR #3638 without realizing you were planning to implement it yourself.

I would be extremely grateful and honored if you could review the PR, since you originally proposed the feature and understand the requirements best. I implemented the Levenshtein distance computations natively to avoid external dependencies, but your feedback on the overall API design, tokenization approach, or any edge cases you had in mind would be highly appreciated.

I am fully open to collaborating and incorporating any changes you suggest.

Thank you for your understanding!

Manimaran-tech avatar Mar 05 '26 17:03 Manimaran-tech

@rogueslasher sounds good to me, thanks for proposing that and providing references. Let's move forward with these metrics. If you would like to drive this development by coding and/or reviewing the PRs if would be awesome!

vfdev-5 avatar Mar 08 '26 00:03 vfdev-5

@Manimaran-tech I have reviewed your PR and the implementation looks good! I would suggest making different files for different metrics as Bleu and Rouge also have their own separate files, it would make sense to stay consistent with the existing conventions. Similarly, the test files in tests/ignite/metrics/nlp/ follow the same pattern — test_bleu.py and test_rouge.py are separate files. It might be worth splitting the tests into test_word_error_rate.py and test_char_error_rate.py as well.

aniketpandey05 avatar Mar 08 '26 08:03 aniketpandey05

@rogueslasher Thanks for the review! I've updated the PR (#3638) to split the metrics into separate files - word_error_rate.py and character_error_rate.py - along with their corresponding test files (test_word_error_rate.py and test_character_error_rate.py), consistent with how bleu.py and rouge.py are structured. All tests are passing.

Manimaran-tech avatar Mar 08 '26 09:03 Manimaran-tech

@Manimaran-tech a good practice is to send one PR per a metric. Start with ignite/metrics/nlp/word_error_rate.py if the second one can inherit from a common structure.

vfdev-5 avatar Mar 08 '26 22:03 vfdev-5

@vfdev-5 Thanks for the feedback! I've updated the PR to only include the Word Error Rate (WER) metric. I have removed the Character Error Rate (CER) implementation from this branch and will submit it as a subsequent PR as suggested. from here after i will follow this practice !!!

Manimaran-tech avatar Mar 09 '26 13:03 Manimaran-tech

Hey @Manimaran-tech, I reviewed the PR and i think we should remove imports for CharacterErrorRate as that file is currently not the part of the pr . also the current bleu file does have a distributed coverage nccl, gloo, horovod, multinode, and TPU. The test file here imports idist but never uses it, so distributed tests are still missing. i would suggest to look at bleu and rogue files for reference and also their test files .

aniketpandey05 avatar Mar 27 '26 16:03 aniketpandey05

I'd like to implement the CharErrorRate (CER) metric as a follow-up to PR #3638. I'll follow the same structure as word_error_rate.py with separate files character_error_rate.py and test_character_error_rate.py, including distributed test coverage (nccl, gloo) consistent with the bleu and rouge implementations.

Shubh3005 avatar Jun 08 '26 02:06 Shubh3005

I've submitted PR #3785 implementing CharacterErrorRate as a follow-up to #3638. Follows the same structure as word_error_rate.py. Separate file, inherits _BaseErrorRate, 15 test cases covering edge cases including unicode, whitespace, batch accumulation, and reset.

Shubh3005 avatar Jun 09 '26 18:06 Shubh3005