question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do we save the finetuned ASR model and use them for inference?

See original GitHub issue

Hello, I was able to fine-tune a model on my dataset as given in the tutorial Pretrained Models and Fine-Tuning. However, I couldn’t find a clear way of saving the fine-tuned model ( something equivalent to torch.save or pickle). I have gone through the checkpoints tutorial and tried to do something as follows (steps after brain.fit() – to save and load the fine-tune model):

from speechbrain.utils.checkpoints import Checkpointer
checkpoint_dir = "./full_example_checkpoints"
checkpointer = Checkpointer(checkpoint_dir)

modules = {"enc": asr_model.mods.encoder.model, 
           "emb": asr_model.hparams.emb,
           "dec": asr_model.hparams.dec,
           "compute_features": asr_model.mods.encoder.compute_features,
           "normalize": asr_model.mods.encoder.normalize,
           "seq_lin": asr_model.hparams.seq_lin, 
}
checkpointer.add_recoverables(modules)
ckpt = checkpointer.save_checkpoint()
ckpt_finder = Checkpointer(checkpoint_dir)
get_ckpt = ckpt_finder.find_checkpoint()

**% load a fresh model and then transfer the parameters of fine-tuned models into that**

new_asr_model = EncoderDecoderASR.from_hparams(source="speechbrain/asr-crdnn-rnnlm-librispeech", savedir="./pretrained_ASR")

**% parameter transfer**
sb.utils.checkpoints.torch_parameter_transfer(new_asr_model.mods.encoder.model, get_ckpt.paramfiles['enc'], device='cpu')
sb.utils.checkpoints.torch_parameter_transfer(new_asr_model.hparams.emb, get_ckpt.paramfiles['emb'], device='cpu')
sb.utils.checkpoints.torch_parameter_transfer(new_asr_model.hparams.dec, get_ckpt.paramfiles['dec'], device='cpu')
sb.utils.checkpoints.torch_parameter_transfer(new_asr_model.mods.encoder.compute_features, best_ckpt.paramfiles['compute_features'], device='cpu')
sb.utils.checkpoints.torch_parameter_transfer(new_asr_model.mods.encoder.normalize, get_ckpt.paramfiles['normalize'], device='cpu')
sb.utils.checkpoints.torch_parameter_transfer(new_asr_model.hparams.seq_lin, get_ckpt.paramfiles['seq_lin'], device='cpu')

I believe there must be a better way to do this. Could you please confirm that above code will work? Also, it would be great if you could provide some hints related to this. Thanks in advance.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
Trikaldarshicommented, Jan 14, 2022

Hello, Thank you very much for all the help. It is working fine for me now and is very convenient as well. Closing the ticket. Thanks again!.

0reactions
Gastroncommented, Jan 12, 2022

Ok so you’ve decided to use the Pretrained interface. Looks ok, the paths provided to your Pretrainer seem a little weird, though I think it would work.

Here’s how I’d change it, though:

  1. In your HuggingFace repo, remove CKPT.yaml, brain.ckpt, counter.ckpt, dataloader-TRAIN.ckpt, noam_scheduler.ckpt, and optimizer.ckpt since those are not needed for evaluation.
  2. Upload a copy of the tokenizer.ckpt (just in case this changes in the referenced repo).
  3. Remove the paths argument (the whole block in your YAML) from Pretrainer - these locations are already the default behaviour, there’s no need to specify them again.
  4. Add a README to the HuggingFace repo 😃
Read more comments on GitHub >

github_iconTop Results From Across the Web

Fine-Tune Wav2Vec2 for English ASR with Transformers
In this notebook, we will give an in-detail explanation of how Wav2Vec2's pretrained checkpoints can be fine-tuned on any English ASR dataset.
Read more >
How to Fine-Tune a Riva ASR Acoustic Model (Citrinet) with ...
After the model is trained, evaluated, and there is a need for fine-tuning, the following command can be used to fine-tune the ASR...
Read more >
Fine-tune and deploy a Wav2Vec2 model for speech ...
In this post, we show how data scientists who want to build their own ASR models can fine-tune the pre-trained Wav2Vec2 model on...
Read more >
Fine-tuning Wav2Vec2 with an LM head | TensorFlow Hub
Now that we are satisfied with the training process & have saved the model in save_dir , we will see how this model...
Read more >
How to restore a ckpt conformer model for inference/finetuning?
EncDecCTCModelBPE.restore_from(name+'.nemo') and use it for inference. ... save the new model for finetuning by asr_model.save_to(xxx.nemo) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found