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 to load pre-trained wav2vec2.0 models

See original GitHub issue

❓ Questions and Help

Before asking:

  1. search the issues.
  2. search the docs.

What is your question?

I would like to load pre-trained models for wav2vec2 as given in the instructions README (https://github.com/pytorch/fairseq/tree/master/examples/wav2vec). My goal is to export the pre-trained model to ONNX following the instructions given here: https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html.

In order to run torch.onnx.export(), I need the model as an input. I am having problems loading the pre-trained model in order to export it.

Code

I have tried the code snippets in the wav2vec2 README (https://github.com/pytorch/fairseq/tree/master/examples/wav2vec):

import torch
import fairseq
cp = torch.load('path/to/file/wav2vec_small_10m.pt', map_location=torch.device('cpu'))
model, cfg, task = fairseq.checkpoint_utils.load_model_ensemble_and_task([cp])

I am getting the following error:

AttributeError: ‘dict’ object has no attribute ‘replace’

Looking at the documentation here: https://pytorch.org/tutorials/beginner/saving_loading_models.html, I tried

# Model class must be defined somewhere
model = torch.load(PATH)
model.eval()

But not sure how to exactly get the model in order to pass it to torch.onnx.export()

What have you tried?

What’s your environment?

  • fairseq Version (e.g., 1.0 or master): master
  • PyTorch Version (e.g., 1.0): 1.7.0
  • OS (e.g., Linux): macOS 10.15.7
  • How you installed fairseq (pip, source): source
  • Build command you used (if compiling from source): CFLAGS="-stdlib=libc++"; pip install --editable ./
  • Python version: Python 3.8.5
  • CUDA/cuDNN version: None
  • GPU models and configuration: None
  • Any other relevant information:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
othrifcommented, Dec 16, 2020

Resolved by doing the following:

import torch
import fairseq
from torch import nn

model, cfg, task = fairseq.checkpoint_utils.load_model_ensemble_and_task(['../models/wav2vec_large.pt'])
model = model[0]
model.eval()

wav_input_16khz = torch.randn(1,10000)
z = model.feature_extractor(wav_input_16khz)
c = model.feature_aggregator(z)
4reactions
othrifcommented, Dec 17, 2020

Testing with the wav2vec2.0 model with the same prescription, I get the error:

FileNotFoundError: [Errno 2] No such file or directory: ‘/checkpoint/abaevski/data/speech/libri/10m/wav2vec/raw/dict.ltr.txt’

@alexeib Could you please clarify how to pass the letter dictionary to the model loading sequence.

I run

import torch
import fairseq
model, cfg, task = fairseq.checkpoint_utils.load_model_ensemble_and_task(['path/to/wav2vec_small_10m.pt'])

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to load pre-trained wav2vec2.0 models #3025 - GitHub
In order to run torch.onnx.export() , I need the model as an input. I am having problems loading the pre-trained model in order...
Read more >
Wav2Vec2 - Hugging Face
Check out the from_pretrained() method to load the model weights. The bare Wav2Vec2 Model transformer outputting raw hidden-states without any specific head on ......
Read more >
torchaudio.models.wav2vec2.utils.import_fairseq - PyTorch
"""Import fariseq's wav2vec2.0 pretrained weights to torchaudios's format. For this module to work, you need `fairseq`. """ import re from torch.nn import ......
Read more >
Self-training and pre-training, understanding the wav2vec ...
If a pre-trained model captures the structure of speech, then it should require few labeled examples to fine-tune it for speech recognition. The ......
Read more >
Fine-tuning Wav2Vec2 with an LM head | TensorFlow Hub
In this notebook, we will load the pre-trained wav2vec2 model from TFHub and ... libflac8 1 upgraded, 4 newly installed, 0 to remove...
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