Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pyctcdecode to high level beam search API #6026

Merged
merged 2 commits into from
Feb 18, 2023

Conversation

titu1994
Copy link
Collaborator

What does this PR do ?

Adds PyCTCDecode to high level beam search API

Collection: [ASR]

Changelog

  • Adds pyctcdecode support.
  • Approximates the tokenized text output since the engine does not return token indices

Usage

import nemo.collections.asr as nemo_asr

filepath = "<Path to audio file>"
kenlm_path = "<Path to ARPA or BIN KenLM File built in CHAR / WORD mode; not SUBWORD mode !>"

model = nemo_asr.models.ASRModel.from_pretrained('stt_en_conformer_ctc_large')  # type: nemo_asr.models.EncDecCTCModelBPE
# Create default decoding config
model.change_decoding_strategy(None)
decoding_cfg = model.cfg.decoding
decoding_cfg.strategy = "pyctcdecode"
decoding_cfg.beam.beam_size = 128
decoding_cfg.beam.return_best_hypothesis = False
decoding_cfg.beam.beam_alpha = 1.0
decoding_cfg.beam.beam_beta = 0.0
decoding_cfg.beam.kenlm_path = kenlm_path

# pyctcdecode config modifications
decoding_cfg.beam.pyctcdecode_cfg.beam_prune_logp = -10.
decoding_cfg.beam.pyctcdecode_cfg.token_min_logp = -5.

# Prepare beam search decoding strategy
model.change_decoding_strategy(decoding_cfg)

hypothesis = model.transcribe([filepath], return_hypotheses=True)
print("Num hypothesis per sample :", len(hypothesis[0]))
print("Result type :", hypothesis[0][0].__class__.__name__)
for candidate in range(len(hypothesis[0])):
    print(f"Beam Search {candidate + 1} :", hypothesis[0][candidate].text, hypothesis[0][candidate].score)

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you add or update any necessary documentation?
  • Does the PR affect components that are optional to install? (Ex: Numba, Pynini, Apex etc)
    • Reviewer: Does the PR have correct import guards for all optional libraries?

PR Type:

  • New Feature
  • Bugfix
  • Documentation

)

try:
import pyctcdecode
Copy link
Collaborator

@tango4j tango4j Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason to import in the function? Seems like BeamSearchDecoderWithLM is imported in the function to avoid circular import.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All beam search are imported directly inside of the forward in order to prevent runtime import issue if the library is not installed.

Copy link
Collaborator

@tango4j tango4j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see anything particular needed changes at the moment, but probably I could request some feature when I add this to diarization-ASR pipeline.

@titu1994 titu1994 merged commit 8e6f36a into NVIDIA:main Feb 18, 2023
@titu1994 titu1994 deleted the pyctc_decode branch February 18, 2023 01:24
@titu1994
Copy link
Collaborator Author

Sounds good, thanks !

titu1994 added a commit to titu1994/NeMo that referenced this pull request Mar 24, 2023
* Add pyctcdecode to high level beam search API

Signed-off-by: smajumdar <[email protected]>

* Remove redundant assignment

Signed-off-by: smajumdar <[email protected]>

---------

Signed-off-by: smajumdar <[email protected]>
hsiehjackson pushed a commit to hsiehjackson/NeMo that referenced this pull request Jun 2, 2023
* Add pyctcdecode to high level beam search API

Signed-off-by: smajumdar <[email protected]>

* Remove redundant assignment

Signed-off-by: smajumdar <[email protected]>

---------

Signed-off-by: smajumdar <[email protected]>
Signed-off-by: hsiehjackson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants