dlxj commited on
Commit
1db10fa
·
1 Parent(s): 71b7f49

成功推理 完全正确(用了高级 Debug 技巧,只有一条训练数据)

Browse files
Files changed (2) hide show
  1. examples/asr/transcribe_speech.py +35 -8
  2. readme.txt +4 -2
examples/asr/transcribe_speech.py CHANGED
@@ -32,7 +32,6 @@ from nemo.collections.asr.parts.utils.eval_utils import cal_write_wer
32
  from nemo.collections.asr.parts.utils.rnnt_utils import Hypothesis
33
  from nemo.collections.asr.parts.utils.transcribe_utils import (
34
  compute_output_filename,
35
- get_inference_dtype,
36
  prepare_audio_data,
37
  restore_transcription_order,
38
  setup_model,
@@ -276,12 +275,10 @@ def main(cfg: TranscriptionConfig) -> Union[TranscriptionConfig, List[Hypothesis
276
 
277
  amp_dtype = torch.float16 if cfg.amp_dtype == "float16" else torch.bfloat16
278
 
279
- compute_dtype: torch.dtype
280
  if cfg.amp:
281
- # with amp model weights required to be in float32
282
- compute_dtype = torch.float32
283
  else:
284
- compute_dtype = get_inference_dtype(compute_dtype=cfg.compute_dtype, device=map_location)
285
 
286
  asr_model.to(compute_dtype)
287
 
@@ -358,15 +355,20 @@ def main(cfg: TranscriptionConfig) -> Union[TranscriptionConfig, List[Hypothesis
358
  else:
359
  cfg.decoding = cfg.rnnt_decoding
360
 
 
 
 
361
  filepaths, sorted_manifest_path = prepare_audio_data(cfg)
362
 
 
 
 
 
 
363
  remove_path_after_done = sorted_manifest_path if sorted_manifest_path is not None else None
364
 
365
  filepaths = sorted_manifest_path if sorted_manifest_path is not None else filepaths
366
 
367
- # Compute output filename
368
- cfg = compute_output_filename(cfg, model_name)
369
-
370
  # if transcripts should not be overwritten, and already exists, skip re-transcription step and return
371
  if not cfg.return_transcriptions and not cfg.overwrite_transcripts and os.path.exists(cfg.output_filename):
372
  logging.info(
@@ -466,6 +468,19 @@ def main(cfg: TranscriptionConfig) -> Union[TranscriptionConfig, List[Hypothesis
466
  compute_langs=compute_langs,
467
  timestamps=cfg.timestamps,
468
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
469
  logging.info(f"Finished writing predictions to {output_filename}!")
470
 
471
  # clean-up
@@ -498,4 +513,16 @@ def main(cfg: TranscriptionConfig) -> Union[TranscriptionConfig, List[Hypothesis
498
 
499
 
500
  if __name__ == '__main__':
 
 
 
 
 
 
 
 
 
 
 
 
501
  main() # noqa pylint: disable=no-value-for-parameter
 
32
  from nemo.collections.asr.parts.utils.rnnt_utils import Hypothesis
33
  from nemo.collections.asr.parts.utils.transcribe_utils import (
34
  compute_output_filename,
 
35
  prepare_audio_data,
36
  restore_transcription_order,
37
  setup_model,
 
275
 
276
  amp_dtype = torch.float16 if cfg.amp_dtype == "float16" else torch.bfloat16
277
 
 
278
  if cfg.amp:
279
+ compute_dtype = torch.float16 if cfg.amp_dtype == "float16" else torch.bfloat16
 
280
  else:
281
+ compute_dtype = torch.float32
282
 
283
  asr_model.to(compute_dtype)
284
 
 
355
  else:
356
  cfg.decoding = cfg.rnnt_decoding
357
 
358
+ # Compute output filename
359
+ cfg = compute_output_filename(cfg, model_name)
360
+
361
  filepaths, sorted_manifest_path = prepare_audio_data(cfg)
362
 
363
+ # 如果 audio_dir 本身就是一个文件,则直接使用该文件
364
+ if cfg.audio_dir is not None and os.path.isfile(cfg.audio_dir):
365
+ filepaths = [cfg.audio_dir]
366
+ sorted_manifest_path = None
367
+
368
  remove_path_after_done = sorted_manifest_path if sorted_manifest_path is not None else None
369
 
370
  filepaths = sorted_manifest_path if sorted_manifest_path is not None else filepaths
371
 
 
 
 
372
  # if transcripts should not be overwritten, and already exists, skip re-transcription step and return
373
  if not cfg.return_transcriptions and not cfg.overwrite_transcripts and os.path.exists(cfg.output_filename):
374
  logging.info(
 
468
  compute_langs=compute_langs,
469
  timestamps=cfg.timestamps,
470
  )
471
+
472
+ # 强制将输出的 JSON 文件中的 unicode 转为 utf-8 字符
473
+ if os.path.exists(output_filename):
474
+ with open(output_filename, 'r', encoding='utf-8') as f:
475
+ lines = f.readlines()
476
+ with open(output_filename, 'w', encoding='utf-8') as f:
477
+ for line in lines:
478
+ try:
479
+ data = json.loads(line)
480
+ f.write(json.dumps(data, ensure_ascii=False) + '\n')
481
+ except:
482
+ f.write(line)
483
+
484
  logging.info(f"Finished writing predictions to {output_filename}!")
485
 
486
  # clean-up
 
513
 
514
 
515
  if __name__ == '__main__':
516
+
517
+ import sys
518
+ sys.argv.extend([
519
+ "model_path=results/NeMo_Ja/checkpoints/NeMo_Ja.nemo",
520
+ "audio_dir=E:/huggingface_echodict/NeMo/data/common_voice_11_0/ja/train/ja_train_0/common_voice_ja_25372057.wav",
521
+ "output_filename=transcripts.json",
522
+ "batch_size=16",
523
+ "amp=True",
524
+ "cuda=0",
525
+
526
+ ])
527
+
528
  main() # noqa pylint: disable=no-value-for-parameter
readme.txt CHANGED
@@ -220,9 +220,11 @@ python examples/asr/asr_ctc/speech_to_text_ctc_bpe.py \
220
  model.test_ds.manifest_filepath=$TEST_MANIFEST
221
 
222
 
223
- python examples\asr\asr_ctc\speech_to_text_ctc_bpe.py
224
- 开始训练
225
 
 
 
226
 
227
 
228
 
 
220
  model.test_ds.manifest_filepath=$TEST_MANIFEST
221
 
222
 
223
+ 开始训练
224
+ python examples\asr\asr_ctc\speech_to_text_ctc_bpe.py
225
 
226
+ 成功推理 完全正确(用了高级 Debug 技巧,只有一条训练数据)
227
+ python examples/asr/transcribe_speech.py
228
 
229
 
230