Skip to content

Commit

Permalink
Spk vector fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nshmyrev committed Jul 31, 2020
1 parent 9787e8a commit 83486e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
27 changes: 21 additions & 6 deletions src/kaldi_recognizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ static void RunNnetComputation(const MatrixBase<BaseFloat> &features,
xvector->CopyFromVec(cu_output.Row(0));
}

void KaldiRecognizer::GetSpkVector(Vector<BaseFloat> &xvector)
#define MIN_SPK_FEATS 30

bool KaldiRecognizer::GetSpkVector(Vector<BaseFloat> &xvector)
{
vector<int32> nonsilence_frames;
if (silence_weighting_->Active() && feature_pipeline_->NumFramesReady() > 0) {
Expand All @@ -310,14 +312,25 @@ void KaldiRecognizer::GetSpkVector(Vector<BaseFloat> &xvector)

int num_frames = spk_feature_->NumFramesReady();
Matrix<BaseFloat> mfcc(num_frames, spk_feature_->Dim());

// Not very efficient, would be nice to have faster search
int k = 0;
for (int i = 0; i < num_frames; ++i) {
if (std::find(nonsilence_frames.begin(),
nonsilence_frames.end(), i % 3) == nonsilence_frames.end())
nonsilence_frames.end(), i / 3) == nonsilence_frames.end()) {
continue;
}
Vector<BaseFloat> feat(spk_feature_->Dim());
spk_feature_->GetFrame(i, &feat);
mfcc.CopyRowFromVec(feat, i);
mfcc.CopyRowFromVec(feat, k);
k++;
}

// Don't extract vector if not enough data
mfcc.Resize(k, spk_feature_->Dim());
if (mfcc.NumRows() < MIN_SPK_FEATS)
return false;

SlidingWindowCmnOptions cmvn_opts;
Matrix<BaseFloat> features(mfcc.NumRows(), mfcc.NumCols(), kUndefined);
SlidingWindowCmn(cmvn_opts, mfcc, &features);
Expand All @@ -327,6 +340,7 @@ void KaldiRecognizer::GetSpkVector(Vector<BaseFloat> &xvector)
nnet3::CachingOptimizingCompiler compiler(spk_model_->speaker_nnet, opts.optimize_config, compiler_config);

RunNnetComputation(features, spk_model_->speaker_nnet, &compiler, &xvector);
return true;
}

const char* KaldiRecognizer::GetResult()
Expand Down Expand Up @@ -398,9 +412,10 @@ const char* KaldiRecognizer::GetResult()

if (spk_model_) {
Vector<BaseFloat> xvector;
GetSpkVector(xvector);
for (int i = 0; i < xvector.Dim(); i++) {
obj["spk"].append(xvector(i));
if (GetSpkVector(xvector)) {
for (int i = 0; i < xvector.Dim(); i++) {
obj["spk"].append(xvector(i));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/kaldi_recognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class KaldiRecognizer {
void CleanUp();
void UpdateSilenceWeights();
bool AcceptWaveform(Vector<BaseFloat> &wdata);
void GetSpkVector(Vector<BaseFloat> &xvector);
bool GetSpkVector(Vector<BaseFloat> &xvector);
const char *GetResult();
const char *StoreReturn(const string &res);

Expand Down

0 comments on commit 83486e0

Please sign in to comment.