Add online LSTM transducer model (#25)
This commit is contained in:
@@ -1,78 +1,22 @@
|
||||
/**
|
||||
* Copyright (c) 2022 Xiaomi Corporation (authors: Fangjun Kuang)
|
||||
*
|
||||
* See LICENSE for clarification regarding multiple authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
// sherpa-onnx/csrc/sherpa-onnx.cc
|
||||
//
|
||||
// Copyright (c) 2022-2023 Xiaomi Corporation
|
||||
|
||||
#include <chrono> // NOLINT
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "kaldi-native-fbank/csrc/online-feature.h"
|
||||
#include "sherpa-onnx/csrc/decode.h"
|
||||
#include "sherpa-onnx/csrc/rnnt-model.h"
|
||||
#include "sherpa-onnx/csrc/features.h"
|
||||
#include "sherpa-onnx/csrc/online-transducer-model-config.h"
|
||||
#include "sherpa-onnx/csrc/online-transducer-model.h"
|
||||
#include "sherpa-onnx/csrc/symbol-table.h"
|
||||
#include "sherpa-onnx/csrc/wave-reader.h"
|
||||
|
||||
/** Compute fbank features of the input wave filename.
|
||||
*
|
||||
* @param wav_filename. Path to a mono wave file.
|
||||
* @param expected_sampling_rate Expected sampling rate of the input wave file.
|
||||
* @param num_frames On return, it contains the number of feature frames.
|
||||
* @return Return the computed feature of shape (num_frames, feature_dim)
|
||||
* stored in row-major.
|
||||
*/
|
||||
static std::vector<float> ComputeFeatures(const std::string &wav_filename,
|
||||
float expected_sampling_rate,
|
||||
int32_t *num_frames) {
|
||||
std::vector<float> samples =
|
||||
sherpa_onnx::ReadWave(wav_filename, expected_sampling_rate);
|
||||
|
||||
float duration = samples.size() / expected_sampling_rate;
|
||||
|
||||
std::cout << "wav filename: " << wav_filename << "\n";
|
||||
std::cout << "wav duration (s): " << duration << "\n";
|
||||
|
||||
knf::FbankOptions opts;
|
||||
opts.frame_opts.dither = 0;
|
||||
opts.frame_opts.snip_edges = false;
|
||||
opts.frame_opts.samp_freq = expected_sampling_rate;
|
||||
|
||||
int32_t feature_dim = 80;
|
||||
|
||||
opts.mel_opts.num_bins = feature_dim;
|
||||
|
||||
knf::OnlineFbank fbank(opts);
|
||||
fbank.AcceptWaveform(expected_sampling_rate, samples.data(), samples.size());
|
||||
fbank.InputFinished();
|
||||
|
||||
*num_frames = fbank.NumFramesReady();
|
||||
|
||||
std::vector<float> features(*num_frames * feature_dim);
|
||||
float *p = features.data();
|
||||
|
||||
for (int32_t i = 0; i != fbank.NumFramesReady(); ++i, p += feature_dim) {
|
||||
const float *f = fbank.GetFrame(i);
|
||||
std::copy(f, f + feature_dim, p);
|
||||
}
|
||||
|
||||
return features;
|
||||
}
|
||||
|
||||
int main(int32_t argc, char *argv[]) {
|
||||
if (argc < 8 || argc > 9) {
|
||||
if (argc < 6 || argc > 7) {
|
||||
const char *usage = R"usage(
|
||||
Usage:
|
||||
./bin/sherpa-onnx \
|
||||
@@ -80,12 +24,11 @@ Usage:
|
||||
/path/to/encoder.onnx \
|
||||
/path/to/decoder.onnx \
|
||||
/path/to/joiner.onnx \
|
||||
/path/to/joiner_encoder_proj.onnx \
|
||||
/path/to/joiner_decoder_proj.onnx \
|
||||
/path/to/foo.wav [num_threads]
|
||||
|
||||
You can download pre-trained models from the following repository:
|
||||
https://huggingface.co/csukuangfj/icefall-asr-librispeech-pruned-transducer-stateless3-2022-05-13
|
||||
Please refer to
|
||||
https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html
|
||||
for a list of pre-trained models to download.
|
||||
)usage";
|
||||
std::cerr << usage << "\n";
|
||||
|
||||
@@ -93,37 +36,102 @@ https://huggingface.co/csukuangfj/icefall-asr-librispeech-pruned-transducer-stat
|
||||
}
|
||||
|
||||
std::string tokens = argv[1];
|
||||
std::string encoder = argv[2];
|
||||
std::string decoder = argv[3];
|
||||
std::string joiner = argv[4];
|
||||
std::string joiner_encoder_proj = argv[5];
|
||||
std::string joiner_decoder_proj = argv[6];
|
||||
std::string wav_filename = argv[7];
|
||||
int32_t num_threads = 4;
|
||||
if (argc == 9) {
|
||||
num_threads = atoi(argv[8]);
|
||||
sherpa_onnx::OnlineTransducerModelConfig config;
|
||||
config.debug = true;
|
||||
config.encoder_filename = argv[2];
|
||||
config.decoder_filename = argv[3];
|
||||
config.joiner_filename = argv[4];
|
||||
std::string wav_filename = argv[5];
|
||||
|
||||
config.num_threads = 2;
|
||||
if (argc == 7) {
|
||||
config.num_threads = atoi(argv[6]);
|
||||
}
|
||||
std::cout << config.ToString().c_str() << "\n";
|
||||
|
||||
auto model = sherpa_onnx::OnlineTransducerModel::Create(config);
|
||||
|
||||
sherpa_onnx::SymbolTable sym(tokens);
|
||||
|
||||
int32_t num_frames;
|
||||
auto features = ComputeFeatures(wav_filename, 16000, &num_frames);
|
||||
int32_t feature_dim = features.size() / num_frames;
|
||||
Ort::AllocatorWithDefaultOptions allocator;
|
||||
|
||||
sherpa_onnx::RnntModel model(encoder, decoder, joiner, joiner_encoder_proj,
|
||||
joiner_decoder_proj, num_threads);
|
||||
Ort::Value encoder_out =
|
||||
model.RunEncoder(features.data(), num_frames, feature_dim);
|
||||
int32_t chunk_size = model->ChunkSize();
|
||||
int32_t chunk_shift = model->ChunkShift();
|
||||
|
||||
auto hyp = sherpa_onnx::GreedySearch(model, encoder_out);
|
||||
auto memory_info =
|
||||
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
|
||||
|
||||
std::string text;
|
||||
for (auto i : hyp) {
|
||||
text += sym[i];
|
||||
std::vector<Ort::Value> states = model->GetEncoderInitStates();
|
||||
|
||||
std::vector<int64_t> hyp(model->ContextSize(), 0);
|
||||
|
||||
int32_t expected_sampling_rate = 16000;
|
||||
|
||||
bool is_ok = false;
|
||||
std::vector<float> samples =
|
||||
sherpa_onnx::ReadWave(wav_filename, expected_sampling_rate, &is_ok);
|
||||
|
||||
if (!is_ok) {
|
||||
std::cerr << "Failed to read " << wav_filename << "\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
const float duration = samples.size() / expected_sampling_rate;
|
||||
|
||||
std::cout << "wav filename: " << wav_filename << "\n";
|
||||
std::cout << "wav duration (s): " << duration << "\n";
|
||||
|
||||
auto begin = std::chrono::steady_clock::now();
|
||||
std::cout << "Started!\n";
|
||||
|
||||
sherpa_onnx::FeatureExtractor feat_extractor;
|
||||
feat_extractor.AcceptWaveform(expected_sampling_rate, samples.data(),
|
||||
samples.size());
|
||||
|
||||
std::vector<float> tail_paddings(
|
||||
static_cast<int>(0.2 * expected_sampling_rate));
|
||||
feat_extractor.AcceptWaveform(expected_sampling_rate, tail_paddings.data(),
|
||||
tail_paddings.size());
|
||||
feat_extractor.InputFinished();
|
||||
|
||||
int32_t num_frames = feat_extractor.NumFramesReady();
|
||||
int32_t feature_dim = feat_extractor.FeatureDim();
|
||||
|
||||
std::array<int64_t, 3> x_shape{1, chunk_size, feature_dim};
|
||||
|
||||
for (int32_t start = 0; start + chunk_size < num_frames;
|
||||
start += chunk_shift) {
|
||||
std::vector<float> features = feat_extractor.GetFrames(start, chunk_size);
|
||||
|
||||
Ort::Value x =
|
||||
Ort::Value::CreateTensor(memory_info, features.data(), features.size(),
|
||||
x_shape.data(), x_shape.size());
|
||||
auto pair = model->RunEncoder(std::move(x), states);
|
||||
states = std::move(pair.second);
|
||||
sherpa_onnx::GreedySearch(model.get(), std::move(pair.first), &hyp);
|
||||
}
|
||||
std::string text;
|
||||
for (size_t i = model->ContextSize(); i != hyp.size(); ++i) {
|
||||
text += sym[hyp[i]];
|
||||
}
|
||||
|
||||
std::cout << "Done!\n";
|
||||
|
||||
std::cout << "Recognition result for " << wav_filename << "\n"
|
||||
<< text << "\n";
|
||||
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
float elapsed_seconds =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(end - begin)
|
||||
.count() /
|
||||
1000.;
|
||||
|
||||
std::cout << "num threads: " << config.num_threads << "\n";
|
||||
|
||||
fprintf(stderr, "Elapsed seconds: %.3f s\n", elapsed_seconds);
|
||||
float rtf = elapsed_seconds / duration;
|
||||
fprintf(stderr, "Real time factor (RTF): %.3f / %.3f = %.3f\n",
|
||||
elapsed_seconds, duration, rtf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user