eval.py 2.05 KB
Newer Older
1
2
import logging
import pathlib
3
from argparse import ArgumentParser
4
5
6
7
8
9
10
11
12
13

import torch
import torchaudio
from lightning import RNNTModule


logger = logging.getLogger()


def compute_word_level_distance(seq1, seq2):
14
    return torchaudio.functional.edit_distance(seq1.lower().split(), seq2.lower().split())
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37


def run_eval(args):
    model = RNNTModule.load_from_checkpoint(
        args.checkpoint_path,
        librispeech_path=str(args.librispeech_path),
        sp_model_path=str(args.sp_model_path),
        global_stats_path=str(args.global_stats_path),
    ).eval()

    if args.use_cuda:
        model = model.to(device="cuda")

    total_edit_distance = 0
    total_length = 0
    dataloader = model.test_dataloader()
    with torch.no_grad():
        for idx, (batch, sample) in enumerate(dataloader):
            actual = sample[0][2]
            predicted = model(batch)
            total_edit_distance += compute_word_level_distance(actual, predicted)
            total_length += len(actual.split())
            if idx % 100 == 0:
38
                logger.info(f"Processed elem {idx}; WER: {total_edit_distance / total_length}")
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    logger.info(f"Final WER: {total_edit_distance / total_length}")


def cli_main():
    parser = ArgumentParser()
    parser.add_argument(
        "--checkpoint_path",
        type=pathlib.Path,
        help="Path to checkpoint to use for evaluation.",
    )
    parser.add_argument(
        "--global_stats_path",
        default=pathlib.Path("global_stats.json"),
        type=pathlib.Path,
        help="Path to JSON file containing feature means and stddevs.",
    )
    parser.add_argument(
56
57
58
        "--librispeech_path",
        type=pathlib.Path,
        help="Path to LibriSpeech datasets.",
59
60
    )
    parser.add_argument(
61
62
63
        "--sp_model_path",
        type=pathlib.Path,
        help="Path to SentencePiece model.",
64
65
    )
    parser.add_argument(
66
67
68
69
        "--use_cuda",
        action="store_true",
        default=False,
        help="Run using CUDA.",
70
71
72
73
74
75
76
    )
    args = parser.parse_args()
    run_eval(args)


if __name__ == "__main__":
    cli_main()