Skip to content

Commit

Permalink
Reset pipeline to save memory
Browse files Browse the repository at this point in the history
  • Loading branch information
nshmyrev committed May 31, 2020
1 parent b1e775c commit ee9bacb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/kaldi_recognizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ KaldiRecognizer::KaldiRecognizer(Model *model, float sample_frequency) : model_(
feature_pipeline_);

frame_offset_ = 0;
round_offset_ = 0;
input_finalized_ = false;
spk_feature_ = NULL;

Expand Down Expand Up @@ -89,6 +90,7 @@ KaldiRecognizer::KaldiRecognizer(Model *model, float sample_frequency, char cons
feature_pipeline_);

frame_offset_ = 0;
round_offset_ = 0;
input_finalized_ = false;
spk_feature_ = NULL;

Expand Down Expand Up @@ -121,6 +123,7 @@ KaldiRecognizer::KaldiRecognizer(Model *model, SpkModel *spk_model, float sample
feature_pipeline_);

frame_offset_ = 0;
round_offset_ = 0;
input_finalized_ = false;

spk_feature_ = new OnlineMfcc(spk_model_->spkvector_mfcc_opts);
Expand Down Expand Up @@ -165,7 +168,24 @@ void KaldiRecognizer::CleanUp()
}

frame_offset_ += decoder_->NumFramesDecoded();
decoder_->InitDecoding(frame_offset_);

// Each 10 minutes we drop the pipeline to save memory in continuous processing
if (frame_offset_ > 20000) {
round_offset_ += frame_offset_;
frame_offset_ = 0;

delete decoder_;
delete feature_pipeline_;

feature_pipeline_ = new kaldi::OnlineNnet2FeaturePipeline (model_->feature_info_);
decoder_ = new kaldi::SingleUtteranceNnet3Decoder(model_->nnet3_decoding_config_,
*model_->trans_model_,
*model_->decodable_info_,
model_->hclg_fst_ ? *model_->hclg_fst_ : *decode_fst_,
feature_pipeline_);
} else {
decoder_->InitDecoding(frame_offset_);
}
}

void KaldiRecognizer::UpdateSilenceWeights()
Expand Down Expand Up @@ -352,8 +372,8 @@ const char* KaldiRecognizer::Result()
for (int i = 0; i < size; i++) {
json::JSON word;
word["word"] = model_->word_syms_->Find(words[i]);
word["start"] = (frame_offset_ + times[i].first) * 0.03;
word["end"] = (frame_offset_ + times[i].second) * 0.03;
word["start"] = (round_offset_ + frame_offset_ + times[i].first) * 0.03;
word["end"] = (round_offset_ + frame_offset_ + times[i].second) * 0.03;
word["conf"] = conf[i];
obj["result"].append(word);

Expand Down
1 change: 1 addition & 0 deletions src/kaldi_recognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class KaldiRecognizer {

float sample_frequency_;
int32 frame_offset_;
int32 round_offset_;
bool input_finalized_;
string last_result_;
};

0 comments on commit ee9bacb

Please sign in to comment.