Views
No views yet
model.onnx: The ONNX-converted Wav2Vec2 modelvocab.json: Vocabulary mapping for the tokenizertokenizer.json: Fast Tokenizers library configurationtokenizer_config.json: Tokenizer configurationmetadata.json: Information about the model and conversion process1# Clone this repository
2git clone https://huggingface.co/YOUR_USERNAME/wav2vec2-onnx-models
3
4# Install required dependencies
5pip install onnxruntime
6pip install transformers
7pip install soundfile1import soundfile as sf
2import numpy as np
3import onnxruntime as ort
4import json
5from transformers import Wav2Vec2Processor
6
7# Load audio file
8audio, sampling_rate = sf.read("audio.wav")
9if len(audio.shape) > 1:
10 audio = audio[:, 0] # Take first channel if stereo
11if sampling_rate != 16000:
12 # You'll need to resample to 16kHz
13
14# Load processor - using the same one from the original model
15processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
16# Or load from local files in the model directory
17
18# Preprocess audio
19inputs = processor(audio, sampling_rate=16000, return_tensors="np", padding=True)
20input_values = inputs.input_values
21
22# Load ONNX model and run inference
23ort_session = ort.InferenceSession("en_wav2vec2-base-960h/model.onnx")
24ort_inputs = {ort_session.get_inputs()[0].name: input_values}
25ort_outs = ort_session.run(None, ort_inputs)
26
27# Decode predictions
28predicted_ids = np.argmax(ort_outs[0], axis=-1)
29transcription = processor.batch_decode(predicted_ids)
30print(transcription)python convert_wav2vec2_onnx.pybenchmark.py script on sample audio data. To run the benchmarks yourself:1# Install dependencies
2pip install -r requirements-benchmark.txt
3
4# Run benchmark across all languages
5python benchmark.py --onnx_models_dir "path/to/onnx/models" --output_dir "benchmark_results"| Language | Model | PyTorch (ms) | ONNX (ms) | Speedup |
|---|---|---|---|---|
| en | wav2vec2-base-960h | 142.5 | 61.4 | 2.3x |
| fr | wav2vec2-base-10k-voxpopuli-ft-fr | 137.8 | 63.5 | 2.2x |
| de | wav2vec2-base-10k-voxpopuli-ft-de | 138.2 | 62.1 | 2.2x |
| es | wav2vec2-base-10k-voxpopuli-ft-es | 139.4 | 62.8 | 2.2x |
| it | wav2vec2-base-10k-voxpopuli-ft-it | 141.2 | 65.3 | 2.2x |

1@misc{wav2vec2,
2 author = {Alexei Baevski and Henry Zhou and Abdelrahman Mohamed and Michael Auli},
3 title = {wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations},
4 year = {2020},
5 publisher = {arXiv},
6 howpublished = {\url{https://arxiv.org/abs/2006.11477}},
7}
8
9@misc{voxpopuli,
10 title={VoxPopuli: A Large-Scale Multilingual Speech Corpus for Representation Learning, Semi-Supervised Learning and Interpretation},
11 author={Wang, Changhan and Rivière, Morgane and Lee, Ann and Wu, Anne and Talnikar, Chaitanya and Haziza, Daniel and Williamson, Mary and Pino, Juan and Dupoux, Emmanuel},
12 year={2021},
13 publisher={arXiv},
14 howpublished={\url{https://arxiv.org/abs/2101.00390}},
15}