Skip to content

Commit

Permalink
[egs] Enable WER eval with GPU (asteroid-team#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobel861017 authored Aug 11, 2021
1 parent 98d1241 commit 64e10e9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 6 additions & 2 deletions asteroid/metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import torch
import warnings
import traceback
from typing import List
Expand Down Expand Up @@ -218,17 +219,19 @@ class WERTracker:
model_name (str): Name of the petrained model to use.
trans_df (dataframe): Containing field `utt_id` and `text`.
See librimix/ConvTasNet recipe.
use_gpu (bool): Whether to use GPU for forward caculation.
"""

def __init__(self, model_name, trans_df):
def __init__(self, model_name, trans_df, use_gpu=True):

from espnet2.bin.asr_inference import Speech2Text
from espnet_model_zoo.downloader import ModelDownloader
import jiwer

self.model_name = model_name
self.device = "cuda" if use_gpu else "cpu"
d = ModelDownloader()
self.asr_model = Speech2Text(**d.download_and_unpack(model_name))
self.asr_model = Speech2Text(**d.download_and_unpack(model_name), device=self.device)
self.input_txt_list = []
self.clean_txt_list = []
self.output_txt_list = []
Expand Down Expand Up @@ -342,6 +345,7 @@ def hsdi(truth, hypothesis, transformation):
return {k: v for k, v in out if k in keep}

def predict_hypothesis(self, wav):
wav = torch.from_numpy(wav).to(self.device)
nbests = self.asr_model(wav)
text, *_ = nbests[0]
return text
Expand Down
4 changes: 3 additions & 1 deletion egs/librimix/ConvTasNet/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def main(conf):
compute_metrics = update_compute_metrics(conf["compute_wer"], COMPUTE_METRICS)
anno_df = pd.read_csv(Path(conf["test_dir"]).parent.parent.parent / "test_annotations.csv")
wer_tracker = (
MockWERTracker() if not conf["compute_wer"] else WERTracker(ASR_MODEL_PATH, anno_df)
MockWERTracker()
if not conf["compute_wer"]
else WERTracker(ASR_MODEL_PATH, anno_df, use_gpu=conf["use_gpu"])
)
model_path = os.path.join(conf["exp_dir"], "best_model.pth")
model = ConvTasNet.from_pretrained(model_path)
Expand Down

0 comments on commit 64e10e9

Please sign in to comment.