TwvMetrics Class Reference

#include <kws-scoring.h>

Collaboration diagram for TwvMetrics:

Public Member Functions

 TwvMetrics (const TwvMetricsOptions &opts)
 
 ~TwvMetrics ()
 
void AddAlignment (const KwsAlignment &ali)
 
void Reset ()
 
float Atwv ()
 
float Stwv ()
 
void GetOracleMeasures (float *final_mtwv, float *final_mtwv_threshold, float *final_otwv)
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (TwvMetrics)
 
void AddEvent (const KwsTerm &ref, const KwsTerm &hyp, float ali_score)
 
void RefAndHypSeen (const std::string &kw_id, float score)
 
void OnlyRefSeen (const std::string &kw_id, float score)
 
void OnlyHypSeen (const std::string &kw_id, float score)
 

Private Attributes

float audio_duration_
 
float atwv_decision_threshold_
 
float beta_
 
TwvMetricsStatsstats_
 

Detailed Description

Definition at line 221 of file kws-scoring.h.

Constructor & Destructor Documentation

◆ TwvMetrics()

TwvMetrics ( const TwvMetricsOptions opts)
explicit

Definition at line 321 of file kws-scoring.cc.

References rnnlm::i, TwvMetrics::stats_, TwvMetricsOptions::sweep_step, and TwvMetricsStats::sweep_threshold_values.

321  :
322  audio_duration_(opts.audio_duration),
323  atwv_decision_threshold_(opts.score_threshold),
324  beta_(opts.beta()) {
325  stats_ = new TwvMetricsStats();
326  if (opts.sweep_step > 0.0) {
327  for (float i=0.0; i <= 1; i+=opts.sweep_step) {
328  stats_->sweep_threshold_values.push_back(i);
329  }
330  }
331 }
std::list< float > sweep_threshold_values
Definition: kws-scoring.cc:318
TwvMetricsStats * stats_
Definition: kws-scoring.h:253
float atwv_decision_threshold_
Definition: kws-scoring.h:250

◆ ~TwvMetrics()

~TwvMetrics ( )

Definition at line 333 of file kws-scoring.cc.

References TwvMetrics::stats_.

333  {
334  delete stats_;
335 }
TwvMetricsStats * stats_
Definition: kws-scoring.h:253

Member Function Documentation

◆ AddAlignment()

void AddAlignment ( const KwsAlignment ali)

Definition at line 392 of file kws-scoring.cc.

References TwvMetrics::AddEvent(), KwsAlignment::begin(), KwsAlignment::end(), and KALDI_VLOG.

Referenced by main().

392  {
393  KwsAlignment::AlignedTerms::const_iterator it = ali.begin();
394  int k = 0;
395  while (it != ali.end()) {
396  AddEvent(it->ref, it->hyp, it->aligner_score);
397  ++it;
398  ++k;
399  }
400  KALDI_VLOG(4) << "Processed " << k << " alignment entries";
401 }
void AddEvent(const KwsTerm &ref, const KwsTerm &hyp, float ali_score)
Definition: kws-scoring.cc:337
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156

◆ AddEvent()

void AddEvent ( const KwsTerm ref,
const KwsTerm hyp,
float  ali_score 
)
private

Definition at line 337 of file kws-scoring.cc.

References KALDI_ASSERT, KwsTerm::kw_id(), TwvMetrics::OnlyHypSeen(), TwvMetrics::OnlyRefSeen(), TwvMetrics::RefAndHypSeen(), KwsTerm::score(), and KwsTerm::valid().

Referenced by TwvMetrics::AddAlignment().

339  {
340  if (ref.valid() && hyp.valid()) {
341  RefAndHypSeen(hyp.kw_id(), hyp.score());
342  } else if (hyp.valid()) {
343  OnlyHypSeen(hyp.kw_id(), hyp.score());
344  } else if (ref.valid()) {
345  OnlyRefSeen(ref.kw_id(), ref.score());
346  } else {
347  KALDI_ASSERT(ref.valid() || hyp.valid());
348  }
349 }
void OnlyHypSeen(const std::string &kw_id, float score)
Definition: kws-scoring.cc:369
void RefAndHypSeen(const std::string &kw_id, float score)
Definition: kws-scoring.cc:351
void OnlyRefSeen(const std::string &kw_id, float score)
Definition: kws-scoring.cc:385
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Atwv()

float Atwv ( )

Definition at line 408 of file kws-scoring.cc.

References TwvMetrics::audio_duration_, TwvMetrics::beta_, TwvMetricsStats::keyword_stats, and TwvMetrics::stats_.

Referenced by main().

408  {
409  typedef kws_internal::KwStats::iterator KwIterator;
410  int32 nof_kw = 0;
411  float atwv = 0;
412 
413  for (KwIterator it = stats_->keyword_stats.begin();
414  it != stats_->keyword_stats.end(); ++it ) {
415  if (it->second.nof_targets == 0) {
416  continue;
417  }
418  float nof_targets = static_cast<float>(it->second.nof_targets);
419  float pmiss = 1 - it->second.nof_corr / nof_targets;
420  float pfa = it->second.nof_fa / (audio_duration_ - nof_targets);
421  float twv = 1 - pmiss - beta_ * pfa;
422 
423  atwv = atwv * (nof_kw)/(nof_kw + 1.0) + twv / (nof_kw + 1.0);
424  nof_kw++;
425  }
426  return atwv;
427 }
kws_internal::KwStats keyword_stats
Definition: kws-scoring.cc:316
kaldi::int32 int32
TwvMetricsStats * stats_
Definition: kws-scoring.h:253

◆ GetOracleMeasures()

void GetOracleMeasures ( float final_mtwv,
float final_mtwv_threshold,
float final_otwv 
)

Definition at line 448 of file kws-scoring.cc.

References TwvMetrics::audio_duration_, TwvMetrics::beta_, rnnlm::i, KALDI_ASSERT, TwvMetricsStats::keyword_stats, TwvMetricsStats::otwv_sweep_cache, TwvMetrics::stats_, and TwvMetricsStats::sweep_threshold_values.

Referenced by main().

450  {
451  typedef kws_internal::KwStats::iterator KwIterator;
452 
453  int32 nof_kw = 0;
454  float otwv = 0;
455 
456  unordered_map<float, double> mtwv_sweep;
457  for (KwIterator it = stats_->keyword_stats.begin();
458  it != stats_->keyword_stats.end(); ++it ) {
459  if (it->second.nof_targets == 0) {
460  continue;
461  }
462  std::string kw_id = it->first;
463 
464  float local_otwv = -9999;
465  float local_otwv_threshold = -1.0;
466  std::list<float>::iterator i = stats_->sweep_threshold_values.begin();
467  for (; i != stats_->sweep_threshold_values.end(); ++i) {
468  float decision_threshold = *i;
469 
470  float nof_targets = static_cast<float>(it->second.nof_targets);
471  float nof_true = stats_->otwv_sweep_cache[kw_id][decision_threshold].nof_corr;
472  float nof_fa = stats_->otwv_sweep_cache[kw_id][decision_threshold].nof_fa;
473  float pmiss = 1 - nof_true / nof_targets;
474  float pfa = nof_fa / (audio_duration_ - nof_targets);
475  float twv = 1 - pmiss - beta_ * pfa;
476 
477  if (twv > local_otwv) {
478  local_otwv = twv;
479  local_otwv_threshold = decision_threshold;
480  }
481  mtwv_sweep[decision_threshold] = twv / (nof_kw + 1.0) +
482  mtwv_sweep[decision_threshold] * (nof_kw)/(nof_kw + 1.0);
483  }
484  KALDI_ASSERT(local_otwv_threshold >= 0);
485  otwv = otwv * (nof_kw)/(nof_kw + 1.0) + local_otwv / (nof_kw + 1.0);
486  nof_kw++;
487  }
488 
489  float mtwv = -9999;
490  float mtwv_threshold = -1;
491  std::list<float>::iterator i = stats_->sweep_threshold_values.begin();
492  for (; i != stats_->sweep_threshold_values.end(); ++i) {
493  float decision_threshold = *i;
494 
495  if (mtwv_sweep[decision_threshold] > mtwv) {
496  mtwv = mtwv_sweep[decision_threshold];
497  mtwv_threshold = decision_threshold;
498  }
499  }
500  KALDI_ASSERT(mtwv_threshold >= 0);
501  *final_mtwv = mtwv;
502  *final_mtwv_threshold = mtwv_threshold;
503  *final_otwv = otwv;
504 }
kws_internal::KwStats keyword_stats
Definition: kws-scoring.cc:316
kaldi::int32 int32
kws_internal::PerKwSweepStats otwv_sweep_cache
Definition: kws-scoring.cc:317
std::list< float > sweep_threshold_values
Definition: kws-scoring.cc:318
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
TwvMetricsStats * stats_
Definition: kws-scoring.h:253

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( TwvMetrics  )
private

◆ OnlyHypSeen()

void OnlyHypSeen ( const std::string &  kw_id,
float  score 
)
private

Definition at line 369 of file kws-scoring.cc.

References TwvMetrics::atwv_decision_threshold_, TwvMetricsStats::global_keyword_stats, rnnlm::i, TwvMetricsStats::keyword_stats, KwScoreStats::nof_corr_ndet, KwScoreStats::nof_fa, TwvMetricsStats::otwv_sweep_cache, TwvMetrics::stats_, and TwvMetricsStats::sweep_threshold_values.

Referenced by TwvMetrics::AddEvent().

369  {
370  std::list<float>::iterator i = stats_->sweep_threshold_values.begin();
371  for (; i != stats_->sweep_threshold_values.end(); ++i) {
372  float decision_threshold = *i;
373  if ( score >= decision_threshold )
374  stats_->otwv_sweep_cache[kw_id][decision_threshold].nof_fa++;
375  }
376  if (score >= atwv_decision_threshold_) {
378  stats_->keyword_stats[kw_id].nof_fa++;
379  } else {
381  stats_->keyword_stats[kw_id].nof_corr_ndet++;
382  }
383 }
kws_internal::KwScoreStats global_keyword_stats
Definition: kws-scoring.cc:315
kws_internal::KwStats keyword_stats
Definition: kws-scoring.cc:316
kws_internal::PerKwSweepStats otwv_sweep_cache
Definition: kws-scoring.cc:317
std::list< float > sweep_threshold_values
Definition: kws-scoring.cc:318
TwvMetricsStats * stats_
Definition: kws-scoring.h:253
float atwv_decision_threshold_
Definition: kws-scoring.h:250

◆ OnlyRefSeen()

void OnlyRefSeen ( const std::string &  kw_id,
float  score 
)
private

◆ RefAndHypSeen()

void RefAndHypSeen ( const std::string &  kw_id,
float  score 
)
private

Definition at line 351 of file kws-scoring.cc.

References TwvMetrics::atwv_decision_threshold_, TwvMetricsStats::global_keyword_stats, rnnlm::i, TwvMetricsStats::keyword_stats, KwScoreStats::nof_corr, KwScoreStats::nof_misses, KwScoreStats::nof_targets, TwvMetricsStats::otwv_sweep_cache, TwvMetrics::stats_, and TwvMetricsStats::sweep_threshold_values.

Referenced by TwvMetrics::AddEvent().

351  {
352  std::list<float>::iterator i = stats_->sweep_threshold_values.begin();
353  for (; i != stats_->sweep_threshold_values.end(); ++i) {
354  float decision_threshold = *i;
355  if ( score >= decision_threshold )
356  stats_->otwv_sweep_cache[kw_id][decision_threshold].nof_corr++;
357  }
358  if (score >= atwv_decision_threshold_) {
360  stats_->keyword_stats[kw_id].nof_corr++;
361  } else {
363  stats_->keyword_stats[kw_id].nof_misses++;
364  }
366  stats_->keyword_stats[kw_id].nof_targets++;
367 }
kws_internal::KwScoreStats global_keyword_stats
Definition: kws-scoring.cc:315
kws_internal::KwStats keyword_stats
Definition: kws-scoring.cc:316
kws_internal::PerKwSweepStats otwv_sweep_cache
Definition: kws-scoring.cc:317
std::list< float > sweep_threshold_values
Definition: kws-scoring.cc:318
TwvMetricsStats * stats_
Definition: kws-scoring.h:253
float atwv_decision_threshold_
Definition: kws-scoring.h:250

◆ Reset()

void Reset ( )

Definition at line 403 of file kws-scoring.cc.

References TwvMetrics::stats_.

403  {
404  delete stats_;
405  stats_ = new TwvMetricsStats;
406 }
TwvMetricsStats * stats_
Definition: kws-scoring.h:253

◆ Stwv()

float Stwv ( )

Definition at line 429 of file kws-scoring.cc.

References TwvMetricsStats::keyword_stats, and TwvMetrics::stats_.

Referenced by main().

429  {
430  typedef kws_internal::KwStats::iterator KwIterator;
431  int32 nof_kw = 0;
432  float stwv = 0;
433 
434  for (KwIterator it = stats_->keyword_stats.begin();
435  it != stats_->keyword_stats.end(); ++it ) {
436  if (it->second.nof_targets == 0) {
437  continue;
438  }
439  float nof_targets = static_cast<float>(it->second.nof_targets);
440  float recall = 1 - it->second.nof_unseen / nof_targets;
441 
442  stwv = stwv * (nof_kw)/(nof_kw + 1.0) + recall / (nof_kw + 1.0);
443  nof_kw++;
444  }
445  return stwv;
446 }
kws_internal::KwStats keyword_stats
Definition: kws-scoring.cc:316
kaldi::int32 int32
TwvMetricsStats * stats_
Definition: kws-scoring.h:253

Member Data Documentation

◆ atwv_decision_threshold_

float atwv_decision_threshold_
private

Definition at line 250 of file kws-scoring.h.

Referenced by TwvMetrics::OnlyHypSeen(), and TwvMetrics::RefAndHypSeen().

◆ audio_duration_

float audio_duration_
private

Definition at line 249 of file kws-scoring.h.

Referenced by TwvMetrics::Atwv(), and TwvMetrics::GetOracleMeasures().

◆ beta_

float beta_
private

Definition at line 251 of file kws-scoring.h.

Referenced by TwvMetrics::Atwv(), and TwvMetrics::GetOracleMeasures().

◆ stats_


The documentation for this class was generated from the following files: