Add CTC HLG decoding for JNI (#810)

This commit is contained in:
Fangjun Kuang
2024-04-25 17:20:02 +08:00
committed by GitHub
parent 6686c7d3e6
commit f7b3735621
21 changed files with 429 additions and 13 deletions

View File

@@ -69,6 +69,12 @@ function testOnlineAsr() {
rm sherpa-onnx-streaming-zipformer-ctc-multi-zh-hans-2023-12-13.tar.bz2
fi
if [ ! -d ./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18 ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2
tar xvf sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2
rm sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2
fi
out_filename=test_online_asr.jar
kotlinc-jvm -include-runtime -d $out_filename \
test_online_asr.kt \
@@ -160,6 +166,24 @@ function testOfflineAsr() {
rm sherpa-onnx-whisper-tiny.en.tar.bz2
fi
if [ ! -f ./sherpa-onnx-nemo-ctc-en-citrinet-512/tokens.txt ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-nemo-ctc-en-citrinet-512.tar.bz2
tar xvf sherpa-onnx-nemo-ctc-en-citrinet-512.tar.bz2
rm sherpa-onnx-nemo-ctc-en-citrinet-512.tar.bz2
fi
if [ ! -f ./sherpa-onnx-paraformer-zh-2023-03-28/tokens.txt ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-paraformer-zh-2023-03-28.tar.bz2
tar xvf sherpa-onnx-paraformer-zh-2023-03-28.tar.bz2
rm sherpa-onnx-paraformer-zh-2023-03-28.tar.bz2
fi
if [ ! -f ./sherpa-onnx-zipformer-multi-zh-hans-2023-9-2/tokens.txt ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-zipformer-multi-zh-hans-2023-9-2.tar.bz2
tar xvf sherpa-onnx-zipformer-multi-zh-hans-2023-9-2.tar.bz2
rm sherpa-onnx-zipformer-multi-zh-hans-2023-9-2.tar.bz2
fi
out_filename=test_offline_asr.jar
kotlinc-jvm -include-runtime -d $out_filename \
test_offline_asr.kt \

View File

@@ -1,12 +1,25 @@
package com.k2fsa.sherpa.onnx
fun main() {
val recognizer = createOfflineRecognizer()
val types = arrayOf(0, 2, 5, 6)
for (type in types) {
test(type)
}
}
val waveFilename = "./sherpa-onnx-streaming-zipformer-en-20M-2023-02-17/test_wavs/0.wav"
fun test(type: Int) {
val recognizer = createOfflineRecognizer(type)
val waveFilename = when (type) {
0 -> "./sherpa-onnx-paraformer-zh-2023-03-28/test_wavs/0.wav"
2 -> "./sherpa-onnx-whisper-tiny.en/test_wavs/0.wav"
5 -> "./sherpa-onnx-zipformer-multi-zh-hans-2023-9-2/test_wavs/1.wav"
6 -> "./sherpa-onnx-nemo-ctc-en-citrinet-512/test_wavs/8k.wav"
else -> null
}
val objArray = WaveReader.readWaveFromFile(
filename = waveFilename,
filename = waveFilename!!,
)
val samples: FloatArray = objArray[0] as FloatArray
val sampleRate: Int = objArray[1] as Int
@@ -22,10 +35,10 @@ fun main() {
recognizer.release()
}
fun createOfflineRecognizer(): OfflineRecognizer {
fun createOfflineRecognizer(type: Int): OfflineRecognizer {
val config = OfflineRecognizerConfig(
featConfig = getFeatureConfig(sampleRate = 16000, featureDim = 80),
modelConfig = getOfflineModelConfig(type = 2)!!,
modelConfig = getOfflineModelConfig(type = type)!!,
)
return OfflineRecognizer(config = config)

View File

@@ -3,6 +3,7 @@ package com.k2fsa.sherpa.onnx
fun main() {
testOnlineAsr("transducer")
testOnlineAsr("zipformer2-ctc")
testOnlineAsr("ctc-hlg")
}
fun testOnlineAsr(type: String) {
@@ -11,6 +12,7 @@ fun testOnlineAsr(type: String) {
featureDim = 80,
)
var ctcFstDecoderConfig = OnlineCtcFstDecoderConfig()
val waveFilename: String
val modelConfig: OnlineModelConfig = when (type) {
"transducer" -> {
@@ -40,6 +42,18 @@ fun testOnlineAsr(type: String) {
debug = false,
)
}
"ctc-hlg" -> {
waveFilename = "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/test_wavs/1.wav"
ctcFstDecoderConfig.graph = "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/HLG.fst"
OnlineModelConfig(
zipformer2Ctc = OnlineZipformer2CtcModelConfig(
model = "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/ctc-epoch-30-avg-3-chunk-16-left-128.int8.onnx",
),
tokens = "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/tokens.txt",
numThreads = 1,
debug = false,
)
}
else -> throw IllegalArgumentException(type)
}
@@ -51,6 +65,7 @@ fun testOnlineAsr(type: String) {
modelConfig = modelConfig,
lmConfig = lmConfig,
featConfig = featConfig,
ctcFstDecoderConfig=ctcFstDecoderConfig,
endpointConfig = endpointConfig,
enableEndpoint = true,
decodingMethod = "greedy_search",