-
Notifications
You must be signed in to change notification settings - Fork 176
/
eval.py
63 lines (52 loc) · 1.85 KB
/
eval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import json
import torch
import tqdm
import speech
import speech.loader as loader
def eval_loop(model, ldr):
all_preds = []; all_labels = []
for batch in tqdm.tqdm(ldr):
preds = model.infer(batch)
all_preds.extend(preds)
all_labels.extend(batch[1])
return list(zip(all_labels, all_preds))
def run(model_path, dataset_json,
batch_size=8, tag="best",
out_file=None):
use_cuda = torch.cuda.is_available()
model, preproc = speech.load(model_path, tag=tag)
ldr = loader.make_loader(dataset_json,
preproc, batch_size)
model.cuda() if use_cuda else model.cpu()
model.set_eval()
results = eval_loop(model, ldr)
results = [(preproc.decode(label), preproc.decode(pred))
for label, pred in results]
cer = speech.compute_cer(results)
print("CER {:.3f}".format(cer))
if out_file is not None:
with open(out_file, 'w') as fid:
for label, pred in results:
res = {'prediction' : pred,
'label' : label}
json.dump(res, fid)
fid.write("\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Eval a speech model.")
parser.add_argument("model",
help="A path to a stored model.")
parser.add_argument("dataset",
help="A json file with the dataset to evaluate.")
parser.add_argument("--last", action="store_true",
help="Last saved model instead of best on dev set.")
parser.add_argument("--save",
help="Optional file to save predicted results.")
args = parser.parse_args()
run(args.model, args.dataset,
tag=None if args.last else "best",
out_file=args.save)