Skip to content

15.5 Multimodal eval metrics: WER, CER, VQA, captions, zero-shot

Whisper decoding produces transcripts, and the vision models of the previous chapter produce answers and captions. Nothing so far can say whether they are any good. This section builds the measuring instruments: word and character error rates for speech, VQA accuracy, caption scores and zero-shot accuracy for vision, and a bootstrap confidence interval for every one of them.

Concepts from other sections (9)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Several later parts of the system lean on these numbers. Release gates block a model on a WER ceiling, bias evaluations compare WER across speaker groups, and the audio corpus pipeline normalizes transcripts the same way. All of them need one implementation whose numbers match what papers report (jiwer, sacreBLEU), whose ambiguous cases are pinned down, and whose intervals are reproducible from a seed across languages. The system’s tinyllm eval asr command reports WER with a bootstrap wer_ci using exactly these functions.

You will reuse corpus_bleu and chrf from Sequence metrics: exact match, BLEU, chrF, and from Mathematics the percentile bootstrap of LLN, CLT, confidence intervals, bootstrap and the PCG32 stream of Modular arithmetic, hashing, and PCG32 in Python. It helps to think of text as Unicode code points, as in Injective, surjective, bijective: the GPT-2 byte map. The solution is mm.py, with basic_normalize, edit_ops (Levenshtein with a fixed tie rule), wer_stats, wer, cer, vqa_accuracy, caption_scores, zero_shot_accuracy and bootstrap_ci; the contract is mm.pyi.

Learning objectives

  • Compute a Levenshtein alignment by hand and recover its substitution, deletion and insertion counts with a fixed tie rule.
  • Explain why corpus WER pools edits and reference lengths before dividing, and why it can exceed 1.
  • Apply Whisper’s basic normalizer and explain the order of its steps.
  • Define VQA accuracy, caption attribute recall and top-kk zero-shot accuracy, including their edge rules.
  • Build a bootstrap interval that resamples items, not words, and pools again on each resample.
SymbolMeaningType / shape
r,hr, hreference and hypothesis tokenslists of str
di,jd_{i,j}edits turning r:ir_{:i} into h:jh_{:j}int
S,D,IS, D, Isubstitutions, deletions, insertionsint
NNreference lengthint
mmhuman answers equal to the predictionint

The edit distance satisfies di,0=id_{i,0} = i, d0,j=jd_{0,j} = j, and

di,j=min⁡(di−1,j−1+[ri≠hj], di−1,j+1, di,j−1+1),d_{i,j} = \min\big(d_{i-1,j-1} + [r_i \ne h_j],\ d_{i-1,j} + 1,\ d_{i,j-1} + 1\big),

the three moves being a match or substitution, a deletion of rir_i, and an insertion of hjh_j. Then dn,md_{n,m} is the minimum S+D+IS + D + I.

The table gives only the total. To count each kind of edit, you walk back from (n,m)(n, m) taking any move consistent with the table, and here is the catch: several paths can be consistent. “a b” against “b c” costs 2 either as two substitutions or as delete “a”, keep “b”, insert “c”. The totals never depend on the choice, but the S, D, I split does, so the tie rule is part of the contract. The rule used throughout this book tries the diagonal first, then a deletion, then an insertion, so every implementation (including the Rust and Go ports) reports the same split. jiwer’s backtrace (via rapidfuzz) tries a deletion first: its totals always equal these, but its split can differ.

For a corpus, WER=∑k(Sk+Dk+Ik)/∑kNk\text{WER} = \sum_k (S_k + D_k + I_k) / \sum_k N_k over items kk, with words from str.split(). Pooling weighs every reference word equally; the mean of per-item rates would let a one-word item count as much as a paragraph. An empty hypothesis is all deletions, giving WER 1, and a hypothesis with extra words can exceed 1, since insertions are unbounded while the denominator is fixed by the reference. CER is the same computation over the characters of the stripped strings, spaces included. When every reference is empty the rate is undefined, and the solution raises a ValueError rather than returning 0.

“Hello, World!” and “hello world” are the same transcript, so both sides must be normalized before scoring. Whisper’s basic normalizer lowercases, deletes <...> and [...] tags and parenthesized asides, applies Unicode NFKC (folding ligatures and full-width forms), replaces every character whose category is a mark (M), symbol (S), or punctuation (P) with a space, and collapses white space. It keeps letters of every script and their accents, unless the accents are separate combining marks. Both sides must go through the same normalizer, or every comma is an error.

The vision metrics are each short once their edge rules are fixed. VQA collects ten human answers per question; an answer earns min⁡(1,m/3)\min(1, m/3), so three agreeing humans give full credit. Captions use corpus BLEU-4 and chrF with several references per item, plus attribute recall: the fraction of the attributes the references mention that the prediction also mentions. The attributes are the words of a given vocabulary or, without one, the words every reference shares. Zero-shot classification scores each class by similarity and counts an item when the true class is in the top kk; equal scores rank by the lower class index.

A metric over a few hundred clips is uncertain, and a number without an interval invites overreading. bootstrap_ci resamples items with replacement (the percentile bootstrap, with uniforms from PCG32(seed)), recomputes the statistic on each resample, and reports the 2.5 and 97.5 percentiles. For WER the resampled unit is a row (S,D,I,N)(S, D, I, N), and each resample is pooled again, which gives the interval of a ratio rather than of per-item rates.

Take the reference “the cat sat on the mat” (6 words) and the hypothesis “the bat sat the mat too now” (7 words). Align them: the = the; cat to bat is a substitution; sat = sat; “on” has no partner, a deletion; the = the; mat = mat; “too” and “now” are insertions. So S,D,I,N=1,1,2,6S, D, I, N = 1, 1, 2, 6 and WER=4/6=0.667\text{WER} = 4/6 = 0.667. No cheaper alignment exists: the hypothesis is one word longer, so I−D=1I - D = 1, and “cat” cannot match anything.

Now the tie: “a b” against “b c”. The table’s last cell is 2. At (2,2)(2, 2), $r_2 = $ b and $h_2 = $ c differ and d1,1+1=2d_{1,1} + 1 = 2, so the diagonal is consistent and is taken, a substitution. At (1,1)(1, 1), a against b is again a substitution. The result is (2,0,0,2)(2, 0, 0, 2), where a deletion-first rule would give (0,1,1,2)(0, 1, 1, 2).

def basic_normalize(text: str) -> str: ...
def edit_ops(ref, hyp) -> tuple[int, int, int, int]: ... # (S, D, I, N)
def wer_stats(refs, hyps) -> NDArray: ... # int64 [items, 4]
def wer(refs, hyps) -> float: ...
def cer(refs, hyps) -> float: ...
def vqa_accuracy(preds, answers) -> float: ...
def caption_scores(preds, refs, attributes=()) -> dict[str, float]: ... # bleu4, chrf, attr_recall
def zero_shot_accuracy(pred, gold, k=1) -> float: ...
def bootstrap_ci(per_item, stat, n=1000, seed=0, alpha=0.05) -> tuple[float, float, float]: ...

A correct implementation reproduces the hand alignment, (1, 1, 2, 6) and WER 4/6, and the tie split, so every port gives identical breakdowns. It agrees with jiwer on corpus WER and CER, per-item totals and NN, so its numbers are comparable with papers. It satisfies the WER invariants on random token lists: WER(x, x) = 0, an empty hypothesis gives 1, totals are symmetric, and bounds hold. It pools rather than averages, and CER strips and counts spaces. Bad corpora (all-empty references, mismatched lengths, a bare string) fail loudly. The normalizer agrees with Hugging Face’s on tags, punctuation, NFKC, accents, CJK and emoji. VQA credit follows min⁡(1,m/3)\min(1, m/3) on normalized answers; BLEU-4 and chrF agree with sacreBLEU; attribute recall handles vocabulary attributes, skipped items and nan; zero-shot handles ties in top-kk and checks its arguments. The bootstrap interval can be recomputed by hand from PCG32 draws, which keeps the intervals reproducible in Python and Go.

The system’s entry point, tinyllm eval asr --pred SEGMENTS.json --ref TRANSCRIPT.txt in cli_speech.py, joins generated segments before normalization and scoring. One substitution and one deletion in a six-word reference give WER 2/6, and malformed segment text is rejected.

The reasoning:

  1. WER must count the cheapest explanation. Many edit sequences turn rr into hh; the metric is the minimum, which the recurrence computes in an (n+1)×(m+1)(n+1) \times (m+1) table. Each cell depends only on its left, upper and upper-left neighbours, so a row-by-row fill is enough.
  2. The split needs a backtrace, and the backtrace needs a rule. Walking back from (n,m)(n, m) along any move whose cost explains the cell recovers S,D,IS, D, I; when several moves explain it, a fixed preference (diagonal, deletion, insertion) makes the split a function of the inputs, so every port agrees.
  3. The denominator is the reference. A rate per reference word is defined for an empty hypothesis (all deletions, WER 1) and comparable across systems on the same test set. Dividing by the hypothesis length would reward deleting words.
  4. Pool, then divide. Corpus WER is ∑edits/∑N\sum \text{edits} / \sum N, so the per-item result must keep (S,D,I,N)(S, D, I, N) as integers, and the bootstrap must resample those rows and pool again.
  5. Normalize both sides identically. Scoring is about words, not formatting. The normalizer must be deterministic Unicode processing, so it is written from category tests rather than language-specific rules.
  6. Small metrics are definitions. VQA, attribute recall and top-kk accuracy are each a line or two once their edge rules (cap at 1, consensus set, tie order) are fixed.

Tags go first, because they contain punctuation that would otherwise become spaces and leave the words inside. NFKC comes before the category test, so full-width and ligature forms fold first.

def basic_normalize(text: str) -> str:
s = text.lower()
s = re.sub(r"[<\[][^>\]]*[>\]]", "", s) # <noise>, [laughter]
s = re.sub(r"\(([^)]+?)\)", "", s) # (inaudible)
# Marks, symbols, and punctuation become spaces; letters and digits stay.
s = "".join(
" " if unicodedata.category(c)[0] in "MSP" else c
for c in unicodedata.normalize("NFKC", s)
).lower() # lowercase again after NFKC folding
return re.sub(r"\s+", " ", s)

The table fill is the recurrence verbatim, and the backtrace encodes the tie rule in the order of its branches.

def edit_ops(ref, hyp):
r, h = list(ref), list(hyp)
n, m = len(r), len(h)
# d[i][j]: edits to turn r[:i] into h[:j].
d = np.zeros((n + 1, m + 1), dtype=np.int64)
d[:, 0] = np.arange(n + 1) # delete everything
d[0, :] = np.arange(m + 1) # insert everything
for i in range(1, n + 1):
for j in range(1, m + 1):
sub = d[i - 1, j - 1] + (r[i - 1] != h[j - 1]) # match costs 0
d[i, j] = min(sub, d[i - 1, j] + 1, d[i, j - 1] + 1)
# Backtrace from the end; on equal cost prefer the diagonal, then a
# deletion, then an insertion.
S = D = I = 0
i, j = n, m
while i > 0 or j > 0:
if i > 0 and j > 0 and d[i, j] == d[i - 1, j - 1] + (r[i - 1] != h[j - 1]):
S += int(r[i - 1] != h[j - 1]) # a match moves diagonally for free
i, j = i - 1, j - 1
elif i > 0 and d[i, j] == d[i - 1, j] + 1:
D += 1
i -= 1
else:
I += 1 # the only remaining consistent move
j -= 1
return S, D, I, n

On “a b” against “b c”, the first branch is consistent at (2,2)(2, 2) and at (1,1)(1, 1), giving the hand result (2,0,0,2)(2, 0, 0, 2).

wer_stats keeps one integer row per item; _rate sums before dividing and refuses an all-empty reference set. CER reuses edit_ops on character lists of the stripped strings.

def wer_stats(refs, hyps):
_pairs(refs, hyps) # sequences of equal, nonzero length; not a bare str
return np.array([edit_ops(r.split(), h.split()) for r, h in zip(refs, hyps)], dtype=np.int64)
def _rate(st):
total = int(st[:, 3].sum())
if total == 0:
raise ValueError("every reference is empty: the error rate is undefined")
return float(st[:, :3].sum()) / total # pooled: sum of edits / sum of N
def cer(refs, hyps):
_pairs(refs, hyps)
st = np.array(
[edit_ops(list(r.strip()), list(h.strip())) for r, h in zip(refs, hyps)],
dtype=np.int64,
)
return _rate(st)

The _pairs check for a bare string matters more than it looks: wer("abc", "abd") would otherwise iterate characters and return a plausible number.

def vqa_accuracy(preds, answers):
_per_item(answers, len(preds), "answers") # every item needs >= 1 answer
total = 0.0
for p, ans in zip(preds, answers):
key = basic_normalize(p).strip() # "Two." == "two"
m = sum(basic_normalize(a).strip() == key for a in ans)
total += min(1.0, m / 3.0) # three agreeing humans: full credit
return total / len(preds)

In caption_scores, the attributes an item is scored on are the vocabulary words that any reference mentions (set().union(*words) & vocab), or, without a vocabulary, the words every reference shares (set.intersection(*words)). Items with no such attributes are skipped, and if none remain the recall is nan rather than 0 or 1. BLEU-4 and chrF come from the sequence metrics section.

For zero-shot accuracy, a stable sort does the tie-breaking:

# A stable sort of -score ranks ties by the lower class index.
top = np.argsort(-s, axis=1, kind="stable")[:, :k]
return float((top == g[:, None]).any(axis=1).mean())

zero_shot_accuracy first requires 1 <= k <= classes and integer gold labels in [0, classes).

The percentile bootstrap from Mathematics resamples a 1-D array, so the solution bootstraps item indices and lets stat see the selected rows. For WER, stat is _rate-style pooling over the resampled (S,D,I,N)(S, D, I, N) rows.

def bootstrap_ci(per_item, stat, n=1000, seed=0, alpha=0.05):
a = np.asarray(per_item)
idx = np.arange(len(a), dtype=np.float64)
return _bootstrap(idx, lambda ix: float(stat(a[ix.astype(np.int64)])), n, alpha, PCG32(seed))

The full solution is mm.py.

edit_ops is O(nm)O(nm) time and memory per item; for transcripts of tens to hundreds of words this is negligible next to decoding. The bootstrap costs nn recomputations of the statistic, O(n⋅items)O(n \cdot \text{items}) for pooled WER.

jiwer (rapidfuzz) is faster and reports the same totals, but its deletion-first backtrace gives a different split, and the Rust and Go ports need a rule they can reproduce exactly. Averaging per-item WER is simpler but weights a one-word clip like a paragraph. Bootstrapping words instead of items ignores that errors cluster within an utterance, and gives intervals that are too narrow.

Two tradeoffs are accepted. The basic normalizer does not spell out numbers or unify spellings (Whisper’s English normalizer does), so “2” and “two” still differ. VQA uses the simple min⁡(1,m/3)\min(1, m/3) rule, not the official average over annotator subsets.

The alignment’s failures change the split or the total. A backtrace that tries an insertion before the diagonal gives an S, D, I split that differs from the specification on ties; the branches must run diagonal, deletion, insertion. Swapping deletions and insertions reports missing words as hallucinated ones; an upward move (i -= 1, a reference word consumed) is a deletion. A substitution that costs 2 turns every substitution into a deletion plus an insertion; the diagonal adds r[i-1] != h[j-1], at most 1.

Warning: Averaging per-item rates lets short items dominate the corpus WER, and dividing by the hypothesis length leaves the WER of an empty hypothesis undefined. _rate sums edits and reference lengths before dividing, and divides by NN, the reference length.

The remaining pitfalls, compactly:

MistakeSymptomFix in the solution
no NFKC before the category testfull-width and ligature text never matchesunicodedata.normalize("NFKC", s) first
keeping [NOISE] tagstags count as wordsthe tag regexes run first
CER without stripleading spaces count as errorsaligns list(r.strip()) with list(h.strip())
CER over wordsCER equals WERpasses character lists to edit_ops
VQA credit not cappedaccuracy above 1min(1.0, m / 3.0)
VQA answers compared raw“Two.” never equals “two”basic_normalize(...).strip() on both sides
ties ranked by the higher indextop-1 changes with class orderstable argsort(-s)
a bootstrap that never resamplesa zero-width intervalresamples indices with PCG32(seed)
consensus attributes as the unionrecall measured on words only one annotator usedset.intersection(*words) without a vocabulary
vocabulary attributes as the intersectionattributes one reference mentions are missedset().union(*words) & vocab
all-empty referencesdivision by zero instead of a clear error_rate raises ValueError when ∑N=0\sum N = 0
accepting k = 0accuracy 0 by constructionrequires 1 <= k <= c
a label equal to the class countan impossible label scored as a missrequires gold in [0, c)
an item with no answersthe question silently scores 0_per_item rejects empty answer lists

These functions are the shared scoring layer for the rest of the system. In AI Engineering, Consent and biometric evidence in model cards calls them to compute WER and CER evidence. Later chapters build on them as well: the bias evaluations compute per-group WER and gaps with two-sample bootstrap intervals, the audio corpus pipeline applies basic_normalize to transcripts before deduplication and splits, and the durable evaluation suites score their asr, vqa, caption and zeroshot rows with these functions.

  • Levenshtein, “Binary codes capable of correcting deletions, insertions, and reversals” (1966).
  • Antol et al., “VQA: Visual Question Answering” (2015), section 4.
  • Radford et al. (2022), appendix C, on text normalization.
  • Efron and Tibshirani, An Introduction to the Bootstrap (1993), chapter 13.
  • jiwer (rapidfuzz) adds bit-parallel Levenshtein and alignment visualization; see jiwer/process.py.
  • Whisper’s EnglishTextNormalizer adds spelled-out numbers, British to American spelling, and contractions; see whisper/normalizers/english.py.
  • The official VQA evaluation averages over the 10 choose 9 annotator subsets and normalizes numbers and articles; see VQA/PythonEvaluationTools/vqaEvaluation.
  • pycocoevalcap adds CIDEr and SPICE, consensus-based caption metrics; see pycocoevalcap/cider.
  • word error rate (WER): pooled substitutions, deletions and insertions divided by the total reference word count.
  • character error rate (CER): the same rate computed over the characters of the stripped strings, spaces included.
  • tie rule: the fixed backtrace preference (diagonal, deletion, insertion) that makes the S, D, I split deterministic.
  • basic normalizer: Whisper’s lowercase, tag-removal, NFKC and category-based punctuation stripping applied to both sides before scoring.
  • attribute recall: the fraction of reference-mentioned attributes that a caption also mentions.
  • item bootstrap: resampling whole items with replacement and recomputing the pooled statistic on each resample.
  • WER is the minimum number of word substitutions, deletions and insertions, pooled over the corpus and divided by the reference word count; it can exceed 1.
  • Equal-cost alignments exist; totals never depend on the choice, but the S, D, I split does, so the tie rule is part of the contract.
  • Normalize before scoring: Whisper’s basic normalizer drops bracketed tags and turns punctuation and symbols into spaces.
  • VQA, caption attribute recall and zero-shot accuracy are short definitions whose edge rules (cap at 1, consensus set, tie order) must be fixed.
  • Every metric gets a bootstrap interval that resamples items, not words.
  1. Why can WER exceed 1?

    Answer

    Insertions are unbounded: a hypothesis with many extra words has II larger than NN. The denominator is the reference length, which does not grow with the hypothesis.

  2. Why does the order of branches in the backtrace not change the WER?

    Answer

    Every branch taken is consistent with the table, so each step subtracts exactly the cost of its move, and the path’s total is dn,md_{n,m} whichever path is followed. Only the attribution of that total to S, D or I depends on the order.

  3. Why are tags removed before punctuation is turned into spaces?

    Answer

    After punctuation becomes spaces, [laughter] is the word laughter, which the tag pattern can no longer find. Removing tags first deletes the whole annotation.

  4. Why does the WER bootstrap resample rows of (S, D, I, N) rather than per-item WER values?

    Answer

    The statistic is a pooled ratio. Resampling rows and pooling again gives the interval of that ratio; resampling per-item rates would give the interval of their mean, a different (and differently weighted) statistic.

  5. Align “the cat sat on the mat” with “the bat sat the mat too now”. What are S,D,I,NS, D, I, N and the WER?

    Answer

    One substitution (cat to bat), one deletion (“on”), two insertions (“too”, “now”): (1,1,2,6)(1, 1, 2, 6), so WER =4/6=0.667= 4/6 = 0.667.