OnlineFeaturePipeline Class Reference

OnlineFeaturePipeline is a class that's responsible for putting together the various stages of the feature-processing pipeline, in an online setting. More...

#include <online-feature-pipeline.h>

Inheritance diagram for OnlineFeaturePipeline:
Collaboration diagram for OnlineFeaturePipeline:

Public Member Functions

 OnlineFeaturePipeline (const OnlineFeaturePipelineConfig &cfg)
 
virtual int32 Dim () const
 Member functions from OnlineFeatureInterface: More...
 
virtual bool IsLastFrame (int32 frame) const
 Returns true if this is the last frame. More...
 
virtual int32 NumFramesReady () const
 returns the feature dimension. More...
 
virtual void GetFrame (int32 frame, VectorBase< BaseFloat > *feat)
 Gets the feature vector for this frame. More...
 
void GetAsMatrix (Matrix< BaseFloat > *feats)
 
void FreezeCmvn ()
 
void SetCmvnState (const OnlineCmvnState &cmvn_state)
 Set the CMVN state to a particular value (will generally be called after Copy(). More...
 
void GetCmvnState (OnlineCmvnState *cmvn_state)
 
void AcceptWaveform (BaseFloat sampling_rate, const VectorBase< BaseFloat > &waveform)
 Accept more data to process (won't actually process it, will just copy it). More...
 
BaseFloat FrameShiftInSeconds () const
 
void InputFinished ()
 
void SetTransform (const MatrixBase< BaseFloat > &transform)
 
bool HaveFmllrTransform ()
 
OnlineFeaturePipelineNew () const
 returns a newly initialized copy of *this– this does not duplicate all the internal state or the speaker-adaptation state, but gives you a freshly initialized version of this object, as if you had initialized it using the constructor that takes the config file. More...
 
virtual ~OnlineFeaturePipeline ()
 
- Public Member Functions inherited from OnlineFeatureInterface
virtual void GetFrames (const std::vector< int32 > &frames, MatrixBase< BaseFloat > *feats)
 This is like GetFrame() but for a collection of frames. More...
 
virtual ~OnlineFeatureInterface ()
 Virtual destructor. More...
 

Private Member Functions

 OnlineFeaturePipeline (const OnlineFeaturePipelineConfig &cfg, const Matrix< BaseFloat > &lda_mat, const Matrix< BaseFloat > &global_cmvn_stats)
 The following constructor is used internally in the New() function; it has the same effect as initializing from just "cfg", but avoids re-reading the LDA transform from disk. More...
 
void Init ()
 Init() is to be called from the constructor; it assumes the pointer members are all uninitialized but config_ and lda_mat_ are initialized. More...
 
OnlineFeatureInterfaceUnadaptedFeature () const
 returns lda_ if it exists, else splice_or_delta_, else cmvn_. More...
 
OnlineFeatureInterfaceAdaptedFeature () const
 returns adapted feature if fmllr_ exists, else UnadaptedFeature(). More...
 

Private Attributes

OnlineFeaturePipelineConfig config_
 
Matrix< BaseFloatlda_mat_
 
Matrix< BaseFloatglobal_cmvn_stats_
 
OnlineBaseFeaturebase_feature_
 
OnlinePitchFeaturepitch_
 
OnlineProcessPitchpitch_feature_
 
OnlineFeatureInterfacefeature_
 
OnlineCmvncmvn_
 
OnlineFeatureInterfacesplice_or_delta_
 
OnlineFeatureInterfacelda_
 
OnlineFeatureInterfacefmllr_
 

Detailed Description

OnlineFeaturePipeline is a class that's responsible for putting together the various stages of the feature-processing pipeline, in an online setting.

This does not attempt to be fully generic, we just try to handle the common case. Since the online-decoding code needs to "know about" things like CMN and fMLLR in order to do adaptation, it's hard to make this completely generic.

Definition at line 157 of file online-feature-pipeline.h.

Constructor & Destructor Documentation

◆ OnlineFeaturePipeline() [1/2]

Definition at line 113 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::global_cmvn_stats_, OnlineFeaturePipelineConfig::global_cmvn_stats_rxfilename, OnlineFeaturePipeline::Init(), OnlineFeaturePipeline::lda_mat_, OnlineFeaturePipelineConfig::lda_rxfilename, and kaldi::ReadKaldiObject().

Referenced by OnlineFeaturePipeline::New().

114  :
115  config_(config) {
116  if (config.lda_rxfilename != "")
117  ReadKaldiObject(config.lda_rxfilename, &lda_mat_);
118  if (config.global_cmvn_stats_rxfilename != "")
119  ReadKaldiObject(config.global_cmvn_stats_rxfilename,
121  Init();
122 }
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
void Init()
Init() is to be called from the constructor; it assumes the pointer members are all uninitialized but...
OnlineFeaturePipelineConfig config_

◆ ~OnlineFeaturePipeline()

~OnlineFeaturePipeline ( )
virtual

Definition at line 256 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::base_feature_, OnlineFeaturePipeline::cmvn_, OnlineFeaturePipeline::feature_, OnlineFeaturePipeline::fmllr_, OnlineFeaturePipeline::lda_, OnlineFeaturePipeline::pitch_, OnlineFeaturePipeline::pitch_feature_, and OnlineFeaturePipeline::splice_or_delta_.

256  {
257  // Note: the delete command only deletes pointers that are non-NULL. Not all
258  // of the pointers below will be non-NULL.
259  delete fmllr_;
260  delete lda_;
261  delete splice_or_delta_;
262  // Guard against double deleting the cmvn_ ptr
263  if (pitch_feature_) {
264  delete feature_; // equal to cmvn_ if pitch feats are not appended
265  delete pitch_feature_;
266  delete pitch_;
267  }
268  delete cmvn_;
269  delete base_feature_;
270 }
OnlineFeatureInterface * feature_
OnlineFeatureInterface * splice_or_delta_

◆ OnlineFeaturePipeline() [2/2]

OnlineFeaturePipeline ( const OnlineFeaturePipelineConfig cfg,
const Matrix< BaseFloat > &  lda_mat,
const Matrix< BaseFloat > &  global_cmvn_stats 
)
private

The following constructor is used internally in the New() function; it has the same effect as initializing from just "cfg", but avoids re-reading the LDA transform from disk.

Definition at line 104 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::Init().

107  :
108  config_(config), lda_mat_(lda_mat), global_cmvn_stats_(global_cmvn_stats) {
109  Init();
110 }
void Init()
Init() is to be called from the constructor; it assumes the pointer members are all uninitialized but...
OnlineFeaturePipelineConfig config_

Member Function Documentation

◆ AcceptWaveform()

void AcceptWaveform ( BaseFloat  sampling_rate,
const VectorBase< BaseFloat > &  waveform 
)

Accept more data to process (won't actually process it, will just copy it).

sampling_rate is necessary just to assert it equals what's in the config.

Definition at line 272 of file online-feature-pipeline.cc.

References OnlineBaseFeature::AcceptWaveform(), OnlinePitchFeature::AcceptWaveform(), OnlineFeaturePipeline::base_feature_, and OnlineFeaturePipeline::pitch_.

274  {
275  base_feature_->AcceptWaveform(sampling_rate, waveform);
276  if (pitch_)
277  pitch_->AcceptWaveform(sampling_rate, waveform);
278 }
virtual void AcceptWaveform(BaseFloat sampling_rate, const VectorBase< BaseFloat > &waveform)
This would be called from the application, when you get more wave data.
virtual void AcceptWaveform(BaseFloat sampling_rate, const VectorBase< BaseFloat > &waveform)=0
This would be called from the application, when you get more wave data.

◆ AdaptedFeature()

OnlineFeatureInterface * AdaptedFeature ( ) const
private

returns adapted feature if fmllr_ exists, else UnadaptedFeature().

If this were not private we would have const and non-const versions returning const and non-const pointers.

Definition at line 138 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::fmllr_, and OnlineFeaturePipeline::UnadaptedFeature().

Referenced by OnlineFeaturePipeline::Dim(), OnlineFeaturePipeline::GetFrame(), OnlineFeaturePipeline::IsLastFrame(), and OnlineFeaturePipeline::NumFramesReady().

138  {
139  if (fmllr_) return fmllr_;
140  else
141  return UnadaptedFeature();
142 }
OnlineFeatureInterface * UnadaptedFeature() const
returns lda_ if it exists, else splice_or_delta_, else cmvn_.

◆ Dim()

int32 Dim ( ) const
virtual

Member functions from OnlineFeatureInterface:

Implements OnlineFeatureInterface.

Definition at line 241 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::AdaptedFeature(), and OnlineFeatureInterface::Dim().

Referenced by SingleUtteranceGmmDecoder::EstimateFmllr(), and SingleUtteranceGmmDecoder::GetGaussianPosteriors().

241  {
242  return AdaptedFeature()->Dim();
243 }
OnlineFeatureInterface * AdaptedFeature() const
returns adapted feature if fmllr_ exists, else UnadaptedFeature().
virtual int32 Dim() const =0

◆ FrameShiftInSeconds()

BaseFloat FrameShiftInSeconds ( ) const
inlinevirtual

◆ FreezeCmvn()

void FreezeCmvn ( )

Definition at line 237 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::cmvn_, OnlineCmvn::Freeze(), and OnlineCmvn::NumFramesReady().

Referenced by SingleUtteranceGmmDecoder::EstimateFmllr().

237  {
238  cmvn_->Freeze(cmvn_->NumFramesReady() - 1);
239 }
void Freeze(int32 cur_frame)
virtual int32 NumFramesReady() const
returns the feature dimension.

◆ GetAsMatrix()

void GetAsMatrix ( Matrix< BaseFloat > *  feats)

Definition at line 299 of file online-feature-pipeline.cc.

References OnlineProcessPitch::Dim(), OnlineProcessPitch::GetFrame(), rnnlm::i, OnlineFeaturePipeline::NumFramesReady(), OnlineFeaturePipeline::pitch_, OnlineFeaturePipeline::pitch_feature_, and Matrix< Real >::Resize().

Referenced by SingleUtteranceGmmDecoder::EstimateFmllr().

299  {
300  if (pitch_) {
301  feats->Resize(NumFramesReady(), pitch_feature_->Dim());
302  for (int32 i = 0; i < NumFramesReady(); i++) {
303  SubVector<BaseFloat> row(*feats, i);
304  pitch_feature_->GetFrame(i, &row);
305  }
306  }
307 }
virtual int32 NumFramesReady() const
returns the feature dimension.
virtual void GetFrame(int32 frame, VectorBase< BaseFloat > *feat)
Gets the feature vector for this frame.
virtual int32 Dim() const
kaldi::int32 int32
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).

◆ GetCmvnState()

void GetCmvnState ( OnlineCmvnState cmvn_state)

Definition at line 149 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::cmvn_, OnlineCmvn::GetState(), and OnlineCmvn::NumFramesReady().

Referenced by SingleUtteranceGmmDecoder::GetAdaptationState().

149  {
150  int32 frame = cmvn_->NumFramesReady() - 1;
151  // the following call will crash if no frames are ready.
152  cmvn_->GetState(frame, cmvn_state);
153 }
kaldi::int32 int32
void GetState(int32 cur_frame, OnlineCmvnState *cmvn_state)
virtual int32 NumFramesReady() const
returns the feature dimension.

◆ GetFrame()

void GetFrame ( int32  frame,
VectorBase< BaseFloat > *  feat 
)
virtual

Gets the feature vector for this frame.

Before calling this for a given frame, it is assumed that you called NumFramesReady() and it returned a number greater than "frame". Otherwise this call will likely crash with an assert failure. This function is not declared const, in case there is some kind of caching going on, but most of the time it shouldn't modify the class.

Implements OnlineFeatureInterface.

Definition at line 251 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::AdaptedFeature(), and OnlineFeatureInterface::GetFrame().

Referenced by SingleUtteranceGmmDecoder::EstimateFmllr(), and SingleUtteranceGmmDecoder::GetGaussianPosteriors().

252  {
253  AdaptedFeature()->GetFrame(frame, feat);
254 }
OnlineFeatureInterface * AdaptedFeature() const
returns adapted feature if fmllr_ exists, else UnadaptedFeature().
virtual void GetFrame(int32 frame, VectorBase< BaseFloat > *feat)=0
Gets the feature vector for this frame.

◆ HaveFmllrTransform()

bool HaveFmllrTransform ( )
inline

Definition at line 201 of file online-feature-pipeline.h.

Referenced by SingleUtteranceGmmDecoder::HaveTransform().

201 { return fmllr_ != NULL; }

◆ Init()

void Init ( )
private

Init() is to be called from the constructor; it assumes the pointer members are all uninitialized but config_ and lda_mat_ are initialized.

Definition at line 159 of file online-feature-pipeline.cc.

References OnlineFeaturePipelineConfig::add_deltas, OnlineFeaturePipelineConfig::add_pitch, OnlineFeaturePipeline::base_feature_, OnlineFeaturePipeline::cmvn_, OnlineFeaturePipelineConfig::cmvn_opts, MatrixBase< Real >::ColRange(), OnlineFeaturePipeline::config_, OnlineFeaturePipelineConfig::delta_opts, OnlineFeatureInterface::Dim(), OnlineFeaturePipelineConfig::fbank_opts, OnlineFeaturePipeline::feature_, OnlineFeaturePipelineConfig::feature_type, OnlineFeaturePipeline::fmllr_, OnlineFeaturePipeline::global_cmvn_stats_, KALDI_ASSERT, KALDI_ERR, kaldi::kCopyData, OnlineFeaturePipeline::lda_, OnlineFeaturePipeline::lda_mat_, OnlineFeaturePipelineConfig::mfcc_opts, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), OnlineFeaturePipeline::pitch_, OnlineFeaturePipeline::pitch_feature_, OnlineFeaturePipelineConfig::pitch_opts, OnlineFeaturePipelineConfig::pitch_process_opts, OnlineFeaturePipelineConfig::plp_opts, Matrix< Real >::Resize(), OnlineFeaturePipelineConfig::splice_feats, OnlineFeaturePipelineConfig::splice_opts, and OnlineFeaturePipeline::splice_or_delta_.

Referenced by OnlineFeaturePipeline::OnlineFeaturePipeline().

159  {
160  if (config_.feature_type == "mfcc") {
162  } else if (config_.feature_type == "plp") {
164  } else if (config_.feature_type == "fbank") {
166  } else {
167  KALDI_ERR << "Code error: invalid feature type " << config_.feature_type;
168  }
169 
170  {
172  if (config_.add_pitch) {
173  int32 global_dim = global_cmvn_stats_.NumCols() - 1;
174  int32 dim = base_feature_->Dim();
175  KALDI_ASSERT(global_dim >= dim);
176  if (global_dim > dim) {
177  Matrix<BaseFloat> last_col(global_cmvn_stats_.ColRange(global_dim, 1));
179  kCopyData);
180  global_cmvn_stats_.ColRange(dim, 1).CopyFromMat(last_col);
181  }
182  }
183  Matrix<double> global_cmvn_stats_dbl(global_cmvn_stats_);
184  OnlineCmvnState initial_state(global_cmvn_stats_dbl);
185  cmvn_ = new OnlineCmvn(config_.cmvn_opts, initial_state, base_feature_);
186  }
187 
188  if (config_.add_pitch) {
189  pitch_ = new OnlinePitchFeature(config_.pitch_opts);
190  pitch_feature_ = new OnlineProcessPitch(config_.pitch_process_opts,
191  pitch_);
192  feature_ = new OnlineAppendFeature(cmvn_, pitch_feature_);
193  } else {
194  pitch_ = NULL;
195  pitch_feature_ = NULL;
196  feature_ = cmvn_;
197  }
198 
200  KALDI_ERR << "You cannot supply both --add-deltas and "
201  << "--splice-feats options.";
202  } else if (config_.splice_feats) {
203  splice_or_delta_ = new OnlineSpliceFrames(config_.splice_opts,
204  feature_);
205  } else if (config_.add_deltas) {
206  splice_or_delta_ = new OnlineDeltaFeature(config_.delta_opts,
207  feature_);
208  } else {
209  splice_or_delta_ = NULL;
210  }
211 
212  if (lda_mat_.NumRows() != 0) {
213  lda_ = new OnlineTransform(lda_mat_,
214  (splice_or_delta_ != NULL ?
216  } else {
217  lda_ = NULL;
218  }
219 
220  fmllr_ = NULL; // This will be set up if the user calls SetTransform().
221 }
OnlineGenericBaseFeature< PlpComputer > OnlinePlp
OnlineGenericBaseFeature< MfccComputer > OnlineMfcc
OnlineGenericBaseFeature< FbankComputer > OnlineFbank
OnlineFeatureInterface * feature_
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
kaldi::int32 int32
SubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Definition: kaldi-matrix.h:213
#define KALDI_ERR
Definition: kaldi-error.h:147
OnlineFeatureInterface * splice_or_delta_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
OnlineFeaturePipelineConfig config_
virtual int32 Dim() const =0

◆ InputFinished()

void InputFinished ( )

Definition at line 280 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::base_feature_, OnlineBaseFeature::InputFinished(), OnlinePitchFeature::InputFinished(), and OnlineFeaturePipeline::pitch_.

280  {
282  if (pitch_)
284 }
virtual void InputFinished()
InputFinished() tells the class you won&#39;t be providing any more waveform.
virtual void InputFinished()=0
InputFinished() tells the class you won&#39;t be providing any more waveform.

◆ IsLastFrame()

bool IsLastFrame ( int32  frame) const
virtual

Returns true if this is the last frame.

Frame indices are zero-based, so the first frame is zero. IsLastFrame(-1) will return false, unless the file is empty (which is a case that I'm not sure all the code will handle, so be careful). This function may return false for some frame if we haven't yet decided to terminate decoding, but later true if we decide to terminate decoding. This function exists mainly to correctly handle end effects in feature extraction, and is not a mechanism to determine how many frames are in the decodable object (as it used to be, and for backward compatibility, still is, in the Decodable interface).

Implements OnlineFeatureInterface.

Definition at line 244 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::AdaptedFeature(), and OnlineFeatureInterface::IsLastFrame().

244  {
245  return AdaptedFeature()->IsLastFrame(frame);
246 }
OnlineFeatureInterface * AdaptedFeature() const
returns adapted feature if fmllr_ exists, else UnadaptedFeature().
virtual bool IsLastFrame(int32 frame) const =0
Returns true if this is the last frame.

◆ New()

OnlineFeaturePipeline * New ( ) const

returns a newly initialized copy of *this– this does not duplicate all the internal state or the speaker-adaptation state, but gives you a freshly initialized version of this object, as if you had initialized it using the constructor that takes the config file.

After calling this you may want to call SetCmvnState() and SetTransform().

Definition at line 124 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::config_, OnlineFeaturePipeline::global_cmvn_stats_, OnlineFeaturePipeline::lda_mat_, and OnlineFeaturePipeline::OnlineFeaturePipeline().

124  {
127 }
OnlineFeaturePipeline(const OnlineFeaturePipelineConfig &cfg)
OnlineFeaturePipelineConfig config_

◆ NumFramesReady()

int32 NumFramesReady ( ) const
virtual

returns the feature dimension.

Returns the total number of frames, since the start of the utterance, that are now available. In an online-decoding context, this will likely increase with time as more data becomes available.

Implements OnlineFeatureInterface.

Definition at line 247 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::AdaptedFeature(), and OnlineFeatureInterface::NumFramesReady().

Referenced by OnlineFeaturePipeline::GetAsMatrix().

247  {
248  return AdaptedFeature()->NumFramesReady();
249 }
OnlineFeatureInterface * AdaptedFeature() const
returns adapted feature if fmllr_ exists, else UnadaptedFeature().
virtual int32 NumFramesReady() const =0
returns the feature dimension.

◆ SetCmvnState()

void SetCmvnState ( const OnlineCmvnState cmvn_state)

Set the CMVN state to a particular value (will generally be called after Copy().

Definition at line 145 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::cmvn_, and OnlineCmvn::SetState().

145  {
146  cmvn_->SetState(cmvn_state);
147 }
void SetState(const OnlineCmvnState &cmvn_state)

◆ SetTransform()

void SetTransform ( const MatrixBase< BaseFloat > &  transform)

Definition at line 223 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::fmllr_, MatrixBase< Real >::NumRows(), and OnlineFeaturePipeline::UnadaptedFeature().

Referenced by SingleUtteranceGmmDecoder::EstimateFmllr(), and SingleUtteranceGmmDecoder::SingleUtteranceGmmDecoder().

224  {
225  if (fmllr_ != NULL) { // we already had a transform; delete this
226  // object.
227  delete fmllr_;
228  fmllr_ = NULL;
229  }
230  if (transform.NumRows() != 0) {
231  OnlineFeatureInterface *feat = UnadaptedFeature();
232  fmllr_ = new OnlineTransform(transform, feat);
233  }
234 }
OnlineFeatureInterface * UnadaptedFeature() const
returns lda_ if it exists, else splice_or_delta_, else cmvn_.

◆ UnadaptedFeature()

OnlineFeatureInterface * UnadaptedFeature ( ) const
private

returns lda_ if it exists, else splice_or_delta_, else cmvn_.

If this were not private we would have const and non-const versions returning const and non-const pointers.

Definition at line 129 of file online-feature-pipeline.cc.

References OnlineFeaturePipeline::feature_, KALDI_ASSERT, OnlineFeaturePipeline::lda_, and OnlineFeaturePipeline::splice_or_delta_.

Referenced by OnlineFeaturePipeline::AdaptedFeature(), and OnlineFeaturePipeline::SetTransform().

129  {
130  if (lda_) return lda_;
131  else if (splice_or_delta_) return splice_or_delta_;
132  else {
133  KALDI_ASSERT(feature_ != NULL);
134  return feature_;
135  }
136 }
OnlineFeatureInterface * feature_
OnlineFeatureInterface * splice_or_delta_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

Member Data Documentation

◆ base_feature_

◆ cmvn_

◆ config_

◆ feature_

◆ fmllr_

◆ global_cmvn_stats_

◆ lda_

◆ lda_mat_

◆ pitch_

◆ pitch_feature_

◆ splice_or_delta_


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