File size: 2,671 Bytes
d596074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Export to ONNX
==============

In this section, we describe how to export models to `ONNX`_.

.. hint::

   Before you continue, please run:

    .. code-block:: bash

        pip install onnx


In each recipe, there is a file called ``export-onnx.py``, which is used
to export trained models to `ONNX`_.

There is also a file named ``onnx_pretrained.py``, which you can use
the exported `ONNX`_ model in Python with `onnxruntime`_ to decode sound files.

sherpa-onnx
-----------

We have a separate repository `sherpa-onnx`_ for deploying your exported models
on various platforms such as:

  - iOS
  - Android
  - Raspberry Pi
  - Linux/macOS/Windows


Please see the documentation of `sherpa-onnx`_ for details:

  `<https://k2-fsa.github.io/sherpa/onnx/index.html>`_

Example
-------

In the following, we demonstrate how to export a streaming Zipformer pre-trained
model from
`<https://huggingface.co/csukuangfj/icefall-asr-librispeech-pruned-transducer-stateless7-2022-11-11>`_
to `ONNX`_.

Download the pre-trained model
------------------------------

.. hint::

   We assume you have installed `git-lfs`_.

.. code-block:: bash


  cd egs/librispeech/ASR

  repo_url=https://huggingface.co/Zengwei/icefall-asr-librispeech-pruned-transducer-stateless7-streaming-2022-12-29
  GIT_LFS_SKIP_SMUDGE=1 git clone $repo_url
  repo=$(basename $repo_url)

  pushd $repo
  git lfs pull --include "data/lang_bpe_500/bpe.model"
  git lfs pull --include "exp/pretrained.pt"
  cd exp
  ln -s pretrained.pt epoch-99.pt
  popd

Export the model to ONNX
------------------------

.. code-block:: bash

  ./pruned_transducer_stateless7_streaming/export-onnx.py \
    --tokens $repo/data/lang_bpe_500/tokens.txt \
    --use-averaged-model 0 \
    --epoch 99 \
    --avg 1 \
    --decode-chunk-len 32 \
    --exp-dir $repo/exp/

.. warning::

   ``export-onnx.py`` from different recipes has different options.

   In the above example, ``--decode-chunk-len`` is specific for the
   streaming Zipformer. Other models won't have such an option.

It will generate the following 3 files in ``$repo/exp``

  - ``encoder-epoch-99-avg-1.onnx``
  - ``decoder-epoch-99-avg-1.onnx``
  - ``joiner-epoch-99-avg-1.onnx``

Decode sound files with exported ONNX models
--------------------------------------------

.. code-block:: bash

  ./pruned_transducer_stateless7_streaming/onnx_pretrained.py \
    --encoder-model-filename $repo/exp/encoder-epoch-99-avg-1.onnx \
    --decoder-model-filename $repo/exp/decoder-epoch-99-avg-1.onnx \
    --joiner-model-filename $repo/exp/joiner-epoch-99-avg-1.onnx \
    --tokens $repo/data/lang_bpe_500/tokens.txt \
    $repo/test_wavs/1089-134686-0001.wav