OnlineDeltaInput Class Reference

#include <online-feat-input.h>

Inheritance diagram for OnlineDeltaInput:
Collaboration diagram for OnlineDeltaInput:

Public Member Functions

 OnlineDeltaInput (const DeltaFeaturesOptions &delta_opts, OnlineFeatInputItf *input)
 
virtual bool Compute (Matrix< BaseFloat > *output)
 
virtual int32 Dim () const
 
- Public Member Functions inherited from OnlineFeatInputItf
virtual ~OnlineFeatInputItf ()
 

Private Member Functions

int32 Context () const
 
void DeltaComputation (const MatrixBase< BaseFloat > &input, Matrix< BaseFloat > *output, Matrix< BaseFloat > *remainder) const
 
 KALDI_DISALLOW_COPY_AND_ASSIGN (OnlineDeltaInput)
 

Static Private Member Functions

static void AppendFrames (const MatrixBase< BaseFloat > &input1, const MatrixBase< BaseFloat > &input2, const MatrixBase< BaseFloat > &input3, Matrix< BaseFloat > *output)
 

Private Attributes

OnlineFeatInputItfinput_
 
DeltaFeaturesOptions opts_
 
const int32 input_dim_
 
Matrix< BaseFloatremainder_
 

Detailed Description

Definition at line 232 of file online-feat-input.h.

Constructor & Destructor Documentation

◆ OnlineDeltaInput()

OnlineDeltaInput ( const DeltaFeaturesOptions delta_opts,
OnlineFeatInputItf input 
)

Definition at line 365 of file online-feat-input.cc.

366  :
367  input_(input), opts_(delta_opts), input_dim_(input_->Dim()) { }
virtual int32 Dim() const =0
OnlineFeatInputItf * input_
DeltaFeaturesOptions opts_

Member Function Documentation

◆ AppendFrames()

void AppendFrames ( const MatrixBase< BaseFloat > &  input1,
const MatrixBase< BaseFloat > &  input2,
const MatrixBase< BaseFloat > &  input3,
Matrix< BaseFloat > *  output 
)
staticprivate

Definition at line 371 of file online-feat-input.cc.

References MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::Range(), and Matrix< Real >::Resize().

Referenced by OnlineDeltaInput::Compute().

374  {
375  const int32 size1 = input1.NumRows(), size2 = input2.NumRows(),
376  size3 = input3.NumRows(), size_out = size1 + size2 + size3;
377  if (size_out == 0) {
378  output->Resize(0, 0);
379  return;
380  }
381  // do std::max in case one or more of the input matrices is empty.
382  int32 dim = std::max(input1.NumCols(),
383  std::max(input2.NumCols(), input3.NumCols()));
384 
385  output->Resize(size_out, dim);
386  if (size1 != 0)
387  output->Range(0, size1, 0, dim).CopyFromMat(input1);
388  if (size2 != 0)
389  output->Range(size1, size2, 0, dim).CopyFromMat(input2);
390  if (size3 != 0)
391  output->Range(size1 + size2, size3, 0, dim).CopyFromMat(input3);
392 }
kaldi::int32 int32
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:202
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).

◆ Compute()

bool Compute ( Matrix< BaseFloat > *  output)
virtual

Implements OnlineFeatInputItf.

Definition at line 422 of file online-feat-input.cc.

References OnlineDeltaInput::AppendFrames(), OnlineFeatInputItf::Compute(), OnlineDeltaInput::Context(), OnlineDeltaInput::DeltaComputation(), OnlineDeltaInput::Dim(), rnnlm::i, OnlineDeltaInput::input_, OnlineDeltaInput::input_dim_, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), OnlineDeltaInput::remainder_, Matrix< Real >::Resize(), and MatrixBase< Real >::Row().

422  {
423  KALDI_ASSERT(output->NumRows() > 0 &&
424  output->NumCols() == Dim());
425  // If output->NumRows() == 0, it corresponds to a request for zero frames,
426  // which makes no sense.
427 
428  // We request the same number of frames of data that we were requested.
429  Matrix<BaseFloat> input(output->NumRows(), input_dim_);
430  bool ans = input_->Compute(&input);
431 
432  // If we got no input (timed out) and we're not at the end, we return
433  // empty output.
434  if (input.NumRows() == 0 && ans) {
435  output->Resize(0, 0);
436  return ans;
437  } else if (input.NumRows() == 0 && !ans) {
438  // The end of the input stream, but no input this time.
439  if (remainder_.NumRows() == 0) {
440  output->Resize(0, 0);
441  return ans;
442  }
443  }
444 
445  // If this is the first segment of the utterance, we put in the
446  // initial duplicates of the first frame, numbered "Context()"
447  if (remainder_.NumRows() == 0 && input.NumRows() != 0 && Context() != 0) {
449  for (int32 i = 0; i < Context(); i++)
450  remainder_.Row(i).CopyFromVec(input.Row(0));
451  }
452 
453  // If this is the last segment, we put in the final duplicates of the
454  // last frame, numbered "Context()".
455  Matrix<BaseFloat> tail;
456  if (!ans && Context() > 0) {
457  tail.Resize(Context(), input_dim_);
458  for (int32 i = 0; i < Context(); i++) {
459  if (input.NumRows() > 0)
460  tail.Row(i).CopyFromVec(input.Row(input.NumRows() - 1));
461  else
462  tail.Row(i).CopyFromVec(remainder_.Row(remainder_.NumRows() - 1));
463  }
464  }
465 
466  Matrix<BaseFloat> appended_feats;
467  AppendFrames(remainder_, input, tail, &appended_feats);
468  DeltaComputation(appended_feats, output, &remainder_);
469  return ans;
470 }
Matrix< BaseFloat > remainder_
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
kaldi::int32 int32
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
void DeltaComputation(const MatrixBase< BaseFloat > &input, Matrix< BaseFloat > *output, Matrix< BaseFloat > *remainder) const
virtual int32 Dim() const
static void AppendFrames(const MatrixBase< BaseFloat > &input1, const MatrixBase< BaseFloat > &input2, const MatrixBase< BaseFloat > &input3, Matrix< BaseFloat > *output)
OnlineFeatInputItf * input_
#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).
virtual bool Compute(Matrix< BaseFloat > *output)=0

◆ Context()

int32 Context ( ) const
inlineprivate

◆ DeltaComputation()

void DeltaComputation ( const MatrixBase< BaseFloat > &  input,
Matrix< BaseFloat > *  output,
Matrix< BaseFloat > *  remainder 
) const
private

Definition at line 394 of file online-feat-input.cc.

References OnlineDeltaInput::Context(), MatrixBase< Real >::CopyFromMat(), OnlineDeltaInput::Dim(), OnlineDeltaInput::input_dim_, MatrixBase< Real >::NumRows(), OnlineDeltaInput::opts_, DeltaFeatures::Process(), MatrixBase< Real >::Range(), and Matrix< Real >::Resize().

Referenced by OnlineDeltaInput::Compute().

396  {
397  int32 input_rows = input.NumRows(),
398  output_rows = std::max(0, input_rows - Context() * 2),
399  remainder_rows = std::min(input_rows, Context() * 2),
400  input_dim = input_dim_,
401  output_dim = Dim();
402  if (remainder_rows > 0) {
403  remainder->Resize(remainder_rows, input_dim);
404  remainder->CopyFromMat(input.Range(input_rows - remainder_rows,
405  remainder_rows, 0, input_dim));
406  } else {
407  remainder->Resize(0, 0);
408  }
409  if (output_rows > 0) {
410  output->Resize(output_rows, output_dim);
411  DeltaFeatures delta(opts_);
412  for (int32 output_frame = 0; output_frame < output_rows; output_frame++) {
413  int32 input_frame = output_frame + Context();
414  SubVector<BaseFloat> output_row(*output, output_frame);
415  delta.Process(input, input_frame, &output_row);
416  }
417  } else {
418  output->Resize(0, 0);
419  }
420 }
kaldi::int32 int32
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
virtual int32 Dim() const
DeltaFeaturesOptions opts_
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).

◆ Dim()

virtual int32 Dim ( ) const
inlinevirtual

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( OnlineDeltaInput  )
private

Member Data Documentation

◆ input_

OnlineFeatInputItf* input_
private

◆ input_dim_

const int32 input_dim_
private

◆ opts_

◆ remainder_

Matrix<BaseFloat> remainder_
private

Definition at line 263 of file online-feat-input.h.

Referenced by OnlineDeltaInput::Compute().


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