Spaces:
Sleeping
Sleeping
File size: 16,566 Bytes
0d80816 |
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# This module is from [WeNet](https://github.com/wenet-e2e/wenet).
# ## Citations
# ```bibtex
# @inproceedings{yao2021wenet,
# title={WeNet: Production oriented Streaming and Non-streaming End-to-End Speech Recognition Toolkit},
# author={Yao, Zhuoyuan and Wu, Di and Wang, Xiong and Zhang, Binbin and Yu, Fan and Yang, Chao and Peng, Zhendong and Chen, Xiaoyu and Xie, Lei and Lei, Xin},
# booktitle={Proc. Interspeech},
# year={2021},
# address={Brno, Czech Republic },
# organization={IEEE}
# }
# @article{zhang2022wenet,
# title={WeNet 2.0: More Productive End-to-End Speech Recognition Toolkit},
# author={Zhang, Binbin and Wu, Di and Peng, Zhendong and Song, Xingchen and Yao, Zhuoyuan and Lv, Hang and Xie, Lei and Yang, Chao and Pan, Fuping and Niu, Jianwei},
# journal={arXiv preprint arXiv:2203.15455},
# year={2022}
# }
#
from __future__ import print_function
import argparse
import copy
import logging
import os
import sys
import torch
import yaml
from torch.utils.data import DataLoader
from wenet.dataset.dataset import Dataset
from wenet.paraformer.search.beam_search import build_beam_search
from wenet.utils.checkpoint import load_checkpoint
from wenet.utils.file_utils import read_symbol_table, read_non_lang_symbols
from wenet.utils.config import override_config
from wenet.utils.init_model import init_model
def get_args():
parser = argparse.ArgumentParser(description="recognize with your model")
parser.add_argument("--config", required=True, help="config file")
parser.add_argument("--test_data", required=True, help="test data file")
parser.add_argument(
"--data_type",
default="raw",
choices=["raw", "shard"],
help="train and cv data type",
)
parser.add_argument(
"--gpu", type=int, default=-1, help="gpu id for this rank, -1 for cpu"
)
parser.add_argument("--checkpoint", required=True, help="checkpoint model")
parser.add_argument("--dict", required=True, help="dict file")
parser.add_argument(
"--non_lang_syms", help="non-linguistic symbol file. One symbol per line."
)
parser.add_argument(
"--beam_size", type=int, default=10, help="beam size for search"
)
parser.add_argument("--penalty", type=float, default=0.0, help="length penalty")
parser.add_argument("--result_file", required=True, help="asr result file")
parser.add_argument("--batch_size", type=int, default=16, help="asr result file")
parser.add_argument(
"--mode",
choices=[
"attention",
"ctc_greedy_search",
"ctc_prefix_beam_search",
"attention_rescoring",
"rnnt_greedy_search",
"rnnt_beam_search",
"rnnt_beam_attn_rescoring",
"ctc_beam_td_attn_rescoring",
"hlg_onebest",
"hlg_rescore",
"paraformer_greedy_search",
"paraformer_beam_search",
],
default="attention",
help="decoding mode",
)
parser.add_argument(
"--search_ctc_weight",
type=float,
default=1.0,
help="ctc weight for nbest generation",
)
parser.add_argument(
"--search_transducer_weight",
type=float,
default=0.0,
help="transducer weight for nbest generation",
)
parser.add_argument(
"--ctc_weight",
type=float,
default=0.0,
help="ctc weight for rescoring weight in \
attention rescoring decode mode \
ctc weight for rescoring weight in \
transducer attention rescore decode mode",
)
parser.add_argument(
"--transducer_weight",
type=float,
default=0.0,
help="transducer weight for rescoring weight in "
"transducer attention rescore mode",
)
parser.add_argument(
"--attn_weight",
type=float,
default=0.0,
help="attention weight for rescoring weight in "
"transducer attention rescore mode",
)
parser.add_argument(
"--decoding_chunk_size",
type=int,
default=-1,
help="""decoding chunk size,
<0: for decoding, use full chunk.
>0: for decoding, use fixed chunk size as set.
0: used for training, it's prohibited here""",
)
parser.add_argument(
"--num_decoding_left_chunks",
type=int,
default=-1,
help="number of left chunks for decoding",
)
parser.add_argument(
"--simulate_streaming", action="store_true", help="simulate streaming inference"
)
parser.add_argument(
"--reverse_weight",
type=float,
default=0.0,
help="""right to left weight for attention rescoring
decode mode""",
)
parser.add_argument(
"--bpe_model", default=None, type=str, help="bpe model for english part"
)
parser.add_argument(
"--override_config", action="append", default=[], help="override yaml config"
)
parser.add_argument(
"--connect_symbol",
default="",
type=str,
help="used to connect the output characters",
)
parser.add_argument(
"--word", default="", type=str, help="word file, only used for hlg decode"
)
parser.add_argument(
"--hlg", default="", type=str, help="hlg file, only used for hlg decode"
)
parser.add_argument(
"--lm_scale",
type=float,
default=0.0,
help="lm scale for hlg attention rescore decode",
)
parser.add_argument(
"--decoder_scale",
type=float,
default=0.0,
help="lm scale for hlg attention rescore decode",
)
parser.add_argument(
"--r_decoder_scale",
type=float,
default=0.0,
help="lm scale for hlg attention rescore decode",
)
args = parser.parse_args()
print(args)
return args
def main():
args = get_args()
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s"
)
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu)
if (
args.mode
in [
"ctc_prefix_beam_search",
"attention_rescoring",
"paraformer_beam_search",
]
and args.batch_size > 1
):
logging.fatal(
"decoding mode {} must be running with batch_size == 1".format(args.mode)
)
sys.exit(1)
with open(args.config, "r") as fin:
configs = yaml.load(fin, Loader=yaml.FullLoader)
if len(args.override_config) > 0:
configs = override_config(configs, args.override_config)
symbol_table = read_symbol_table(args.dict)
test_conf = copy.deepcopy(configs["dataset_conf"])
test_conf["filter_conf"]["max_length"] = 102400
test_conf["filter_conf"]["min_length"] = 0
test_conf["filter_conf"]["token_max_length"] = 102400
test_conf["filter_conf"]["token_min_length"] = 0
test_conf["filter_conf"]["max_output_input_ratio"] = 102400
test_conf["filter_conf"]["min_output_input_ratio"] = 0
test_conf["speed_perturb"] = False
test_conf["spec_aug"] = False
test_conf["spec_sub"] = False
test_conf["spec_trim"] = False
test_conf["shuffle"] = False
test_conf["sort"] = False
if "fbank_conf" in test_conf:
test_conf["fbank_conf"]["dither"] = 0.0
elif "mfcc_conf" in test_conf:
test_conf["mfcc_conf"]["dither"] = 0.0
test_conf["batch_conf"]["batch_type"] = "static"
test_conf["batch_conf"]["batch_size"] = args.batch_size
non_lang_syms = read_non_lang_symbols(args.non_lang_syms)
test_dataset = Dataset(
args.data_type,
args.test_data,
symbol_table,
test_conf,
args.bpe_model,
non_lang_syms,
partition=False,
)
test_data_loader = DataLoader(test_dataset, batch_size=None, num_workers=0)
# Init asr model from configs
model = init_model(configs)
# Load dict
char_dict = {v: k for k, v in symbol_table.items()}
eos = len(char_dict) - 1
load_checkpoint(model, args.checkpoint)
use_cuda = args.gpu >= 0 and torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
model = model.to(device)
model.eval()
# Build BeamSearchCIF object
if args.mode == "paraformer_beam_search":
paraformer_beam_search = build_beam_search(model, args, device)
else:
paraformer_beam_search = None
with torch.no_grad(), open(args.result_file, "w") as fout:
for batch_idx, batch in enumerate(test_data_loader):
keys, feats, target, feats_lengths, target_lengths = batch
feats = feats.to(device)
target = target.to(device)
feats_lengths = feats_lengths.to(device)
target_lengths = target_lengths.to(device)
if args.mode == "attention":
hyps, _ = model.recognize(
feats,
feats_lengths,
beam_size=args.beam_size,
decoding_chunk_size=args.decoding_chunk_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
simulate_streaming=args.simulate_streaming,
)
hyps = [hyp.tolist() for hyp in hyps]
elif args.mode == "ctc_greedy_search":
hyps, _ = model.ctc_greedy_search(
feats,
feats_lengths,
decoding_chunk_size=args.decoding_chunk_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
simulate_streaming=args.simulate_streaming,
)
elif args.mode == "rnnt_greedy_search":
assert feats.size(0) == 1
assert "predictor" in configs
hyps = model.greedy_search(
feats,
feats_lengths,
decoding_chunk_size=args.decoding_chunk_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
simulate_streaming=args.simulate_streaming,
)
elif args.mode == "rnnt_beam_search":
assert feats.size(0) == 1
assert "predictor" in configs
hyps = model.beam_search(
feats,
feats_lengths,
decoding_chunk_size=args.decoding_chunk_size,
beam_size=args.beam_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
simulate_streaming=args.simulate_streaming,
ctc_weight=args.search_ctc_weight,
transducer_weight=args.search_transducer_weight,
)
elif args.mode == "rnnt_beam_attn_rescoring":
assert feats.size(0) == 1
assert "predictor" in configs
hyps = model.transducer_attention_rescoring(
feats,
feats_lengths,
decoding_chunk_size=args.decoding_chunk_size,
beam_size=args.beam_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
simulate_streaming=args.simulate_streaming,
ctc_weight=args.ctc_weight,
transducer_weight=args.transducer_weight,
attn_weight=args.attn_weight,
reverse_weight=args.reverse_weight,
search_ctc_weight=args.search_ctc_weight,
search_transducer_weight=args.search_transducer_weight,
)
elif args.mode == "ctc_beam_td_attn_rescoring":
assert feats.size(0) == 1
assert "predictor" in configs
hyps = model.transducer_attention_rescoring(
feats,
feats_lengths,
decoding_chunk_size=args.decoding_chunk_size,
beam_size=args.beam_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
simulate_streaming=args.simulate_streaming,
ctc_weight=args.ctc_weight,
transducer_weight=args.transducer_weight,
attn_weight=args.attn_weight,
reverse_weight=args.reverse_weight,
search_ctc_weight=args.search_ctc_weight,
search_transducer_weight=args.search_transducer_weight,
beam_search_type="ctc",
)
# ctc_prefix_beam_search and attention_rescoring only return one
# result in List[int], change it to List[List[int]] for compatible
# with other batch decoding mode
elif args.mode == "ctc_prefix_beam_search":
assert feats.size(0) == 1
hyp, _ = model.ctc_prefix_beam_search(
feats,
feats_lengths,
args.beam_size,
decoding_chunk_size=args.decoding_chunk_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
simulate_streaming=args.simulate_streaming,
)
hyps = [hyp]
elif args.mode == "attention_rescoring":
assert feats.size(0) == 1
hyp, _ = model.attention_rescoring(
feats,
feats_lengths,
args.beam_size,
decoding_chunk_size=args.decoding_chunk_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
ctc_weight=args.ctc_weight,
simulate_streaming=args.simulate_streaming,
reverse_weight=args.reverse_weight,
)
hyps = [hyp]
elif args.mode == "hlg_onebest":
hyps = model.hlg_onebest(
feats,
feats_lengths,
decoding_chunk_size=args.decoding_chunk_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
simulate_streaming=args.simulate_streaming,
hlg=args.hlg,
word=args.word,
symbol_table=symbol_table,
)
elif args.mode == "hlg_rescore":
hyps = model.hlg_rescore(
feats,
feats_lengths,
decoding_chunk_size=args.decoding_chunk_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
simulate_streaming=args.simulate_streaming,
lm_scale=args.lm_scale,
decoder_scale=args.decoder_scale,
r_decoder_scale=args.r_decoder_scale,
hlg=args.hlg,
word=args.word,
symbol_table=symbol_table,
)
elif args.mode == "paraformer_beam_search":
hyps = model.paraformer_beam_search(
feats,
feats_lengths,
beam_search=paraformer_beam_search,
decoding_chunk_size=args.decoding_chunk_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
simulate_streaming=args.simulate_streaming,
)
elif args.mode == "paraformer_greedy_search":
hyps = model.paraformer_greedy_search(
feats,
feats_lengths,
decoding_chunk_size=args.decoding_chunk_size,
num_decoding_left_chunks=args.num_decoding_left_chunks,
simulate_streaming=args.simulate_streaming,
)
for i, key in enumerate(keys):
content = []
for w in hyps[i]:
if w == eos:
break
content.append(char_dict[w])
logging.info("{} {}".format(key, args.connect_symbol.join(content)))
fout.write("{} {}\n".format(key, args.connect_symbol.join(content)))
if __name__ == "__main__":
main()
|