forked from TensorSpeech/TensorFlowASR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_asr.py
More file actions
85 lines (68 loc) · 3.42 KB
/
Copy pathevaluate_asr.py
File metadata and controls
85 lines (68 loc) · 3.42 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from datetime import datetime
import argparse
import os
import logging
logging.getLogger("tensorflow").setLevel(logging.INFO)
import tensorflow as tf
#from tensorflow_asr.utils import app_util
#from tensorflow_asr.helpers import exec_helpers
from tqdm import tqdm
devices = [0]
gpus = tf.config.list_physical_devices("GPU")
visible_gpus = [gpus[i] for i in devices]
tf.config.set_visible_devices(visible_gpus, "GPU")
from tensorflow_asr.metrics.error_rates import ErrorRate
from tensorflow_asr.utils.file_util import read_file
from tensorflow_asr.utils.metric_util import cer, wer
logger = tf.get_logger()
def compare_test_output(filepath: str):
logger.info(f"Evaluating result from {filepath} ...")
metrics = {
"greedy_wer": ErrorRate(wer, name="greedy_wer", dtype=tf.float32),
"greedy_cer": ErrorRate(cer, name="greedy_cer", dtype=tf.float32),
"beamsearch_wer": ErrorRate(wer, name="beamsearch_wer", dtype=tf.float32),
"beamsearch_cer": ErrorRate(cer, name="beamsearch_cer", dtype=tf.float32),
}
with read_file(filepath) as path:
with open(path, "r", encoding="utf-8") as openfile:
lines = openfile.read().splitlines()
lines = lines[1:] # skip header
# clean_lines = []
for eachline in tqdm(lines):
_, _, groundtruth, greedy, beamsearch = eachline.split("\t")
# clean_lines.append(groundtruth + "\t" + greedy)
groundtruth = tf.convert_to_tensor([groundtruth], dtype=tf.string)
greedy = tf.convert_to_tensor([greedy], dtype=tf.string)
beamsearch = tf.convert_to_tensor([beamsearch], dtype=tf.string)
metrics["greedy_wer"].update_state(decode=greedy, target=groundtruth)
metrics["greedy_cer"].update_state(decode=greedy, target=groundtruth)
metrics["beamsearch_wer"].update_state(decode=beamsearch, target=groundtruth)
metrics["beamsearch_cer"].update_state(decode=beamsearch, target=groundtruth)
for key, value in metrics.items():
logger.info(f"{key}: {value.result().numpy()}")
def compare_normal_output(filepath: str):
logger.info(f"Evaluating result from {filepath} ...")
metrics = {
"wer": ErrorRate(wer, name="wer", dtype=tf.float32),
"cer": ErrorRate(cer, name="cer", dtype=tf.float32),
}
with read_file(filepath) as path:
with open(path, "r", encoding="utf-8") as openfile:
lines = openfile.read().splitlines()
for eachline in tqdm(lines):
groundtruth, asr_output = eachline.split("\t")
groundtruth = tf.convert_to_tensor([groundtruth], dtype=tf.string)
asr_output = tf.convert_to_tensor([asr_output], dtype=tf.string)
metrics["wer"].update_state(decode=asr_output, target=groundtruth)
metrics["cer"].update_state(decode=asr_output, target=groundtruth)
for key, value in metrics.items():
logger.info(f"{key}: {value.result().numpy()}")
if __name__ == "__main__":
time_s = datetime.now()
parser = argparse.ArgumentParser(description = "control the functions for conformer")
parser.add_argument("--result_file", action='store', type=str, default = "/home/liuyi/TensorFlowASR/examples/conformer/test_outputs/librispeech_testems.tsv", help="get the wer and cer", required=True)
args = parser.parse_args()
# compare_test_output(args.result_file)
compare_normal_output(args.result_file)
time_t = datetime.now() - time_s
print("This run takes %s" % time_t)