online-feat-test.cc
Go to the documentation of this file.
1 // online/online-feat-test.cc
2 
3 // Copyright 2013 Daniel Povey
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
21 
22 namespace kaldi {
23 
24 // This class is for testing and prototyping purposes, it
25 // does not really do anything except wrap a matrix of features
26 // in this class. Note: it maintains a reference to the input
27 // matrix, so be careful not to delete it while this object is alive.
28 // Since this is intended for testing purposes, it may occasionally
29 // "time out" and return fewer than requested
31  public:
33  position_(0), feats_(feats) { }
34 
35  virtual int32 Dim() const { return feats_.NumCols(); }
36 
37  virtual bool Compute(Matrix<BaseFloat> *output) {
38  if (feats_.NumRows() == 0) { // empty input.
39  output->Resize(0, 0);
40  return false;
41  }
42 
43  KALDI_ASSERT(output->NumRows() > 0 &&
44  output->NumCols() == feats_.NumCols());
45 
46  // Because this is a kind of stress test, we completely ignore
47  // the number of frames requested, and return whatever number of
48  // frames we please.
49 
50  int32 num_frames_left = feats_.NumRows() - position_;
51  int32 num_frames_return = std::min((Rand() % 5), num_frames_left);
52  if (num_frames_return == 0) {
53  output->Resize(0, 0);
54  } else {
55  output->Resize(num_frames_return, feats_.NumCols());
56  output->CopyFromMat(feats_.Range(position_, num_frames_return,
57  0, feats_.NumCols()));
58  }
59  position_ += num_frames_return;
60  if (position_ == feats_.NumRows()) return false;
61  else return true;
62  }
63 
64  private:
67 };
68 
69 template<class Real> static void AssertEqual(const Matrix<Real> &A,
70  const Matrix<Real> &B,
71  float tol = 0.001) {
72  KALDI_ASSERT(A.NumRows() == B.NumRows()&&A.NumCols() == B.NumCols());
73  for (MatrixIndexT i = 0;i < A.NumRows();i++)
74  for (MatrixIndexT j = 0;j < A.NumCols();j++) {
75  KALDI_ASSERT(std::abs(A(i, j)-B(i, j)) < tol*std::max(1.0, (double) (std::abs(A(i, j))+std::abs(B(i, j)))));
76  }
77 }
78 
79 // This function will crash if the two objects do not
80 // give the same output.
82  Matrix<BaseFloat> *output) {
83  int32 dim = a->Dim();
84  OnlineCacheInput cache(a);
85  while (true) {
86  Matrix<BaseFloat> garbage;
87  int32 batch_size = 1 + Rand() % 10;
88  garbage.Resize(batch_size, dim); // some random requested amount.
89  if (!cache.Compute(&garbage)) // returns false when done.
90  break;
91  }
92  cache.GetCachedData(output);
93 }
94 
95 // test the MatrixInput and CacheInput classes.
97  int32 dim = 2 + Rand() % 5; // dimension of features.
98  int32 num_frames = 100 + Rand() % 100;
99 
100  Matrix<BaseFloat> input_feats(num_frames, dim);
101  input_feats.SetRandn();
102 
103  OnlineMatrixInput matrix_input(input_feats);
104 
105  Matrix<BaseFloat> output_feats;
106  GetOutput(&matrix_input, &output_feats);
107  AssertEqual(input_feats, output_feats);
108 }
109 
111  int32 dim = 2 + Rand() % 5; // dimension of features.
112  int32 num_frames = 100 + Rand() % 100;
113 
114  Matrix<BaseFloat> input_feats(num_frames, dim);
115  input_feats.SetRandn();
116 
117  OnlineMatrixInput matrix_input(input_feats);
119  opts.num_tries = 100; // makes it very unlikely we'll get that many timeouts.
120  OnlineFeatureMatrix online_feature_matrix(opts, &matrix_input);
121 
122  for (int32 frame = 0; frame < num_frames; frame++) {
123  KALDI_ASSERT(online_feature_matrix.IsValidFrame(frame));
124  KALDI_ASSERT(online_feature_matrix.GetFrame(frame).ApproxEqual(input_feats.Row(frame)));
125  }
126  KALDI_ASSERT(!online_feature_matrix.IsValidFrame(num_frames));
127 }
128 
129 
130 
132  int32 dim = 2 + Rand() % 5; // dimension of features.
133  int32 num_frames = 100 + Rand() % 100;
134  int32 left_context = Rand() % 3, right_context = Rand() % 3;
135  bool have_offset = (Rand() % 2 == 0);
136  int32 lda_input_dim = (dim * (left_context + 1 + right_context)),
137  lda_output_dim = 1 + Rand() % 5; // this can even be more than
138  // the input dim, the class doesn't care.
139 
140  Matrix<BaseFloat> transform(lda_output_dim, lda_input_dim +
141  (have_offset ? 1 : 0));
142  transform.SetRandn();
143  Matrix<BaseFloat> input_feats(num_frames, dim);
144  input_feats.SetRandn();
145 
146  OnlineMatrixInput matrix_input(input_feats);
147  OnlineLdaInput lda_input(&matrix_input, transform, left_context, right_context);
148 
149  Matrix<BaseFloat> output_feats1;
150  GetOutput(&lda_input, &output_feats1);
151  Matrix<BaseFloat> temp_feats;
152  SpliceFrames(input_feats, left_context, right_context, &temp_feats);
153  Matrix<BaseFloat> output_feats2(temp_feats.NumRows(), transform.NumRows());
154  if (!have_offset) {
155  output_feats2.AddMatMat(1.0, temp_feats, kNoTrans, transform, kTrans, 0.0);
156  } else {
157  SubMatrix<BaseFloat> linear_part(transform, 0, transform.NumRows(),
158  0, transform.NumCols() - 1);
159  output_feats2.AddMatMat(1.0, temp_feats, kNoTrans, linear_part, kTrans, 0.0);
160  Vector<BaseFloat> offset(transform.NumRows());
161  offset.CopyColFromMat(transform, transform.NumCols() - 1);
162  output_feats2.AddVecToRows(1.0, offset);
163  }
164  KALDI_ASSERT(output_feats1.ApproxEqual(output_feats2));
165 }
166 
167 
169  int32 dim = 2 + Rand() % 5; // dimension of features.
170  int32 num_frames = 100 + Rand() % 100;
172  opts.order = Rand() % 3;
173  opts.window = 1 + Rand() % 3;
174 
175  int32 output_dim = dim * (1 + opts.order);
176 
177  Matrix<BaseFloat> input_feats(num_frames, dim);
178  input_feats.SetRandn();
179 
180  OnlineMatrixInput matrix_input(input_feats);
181  OnlineDeltaInput delta_input(opts, &matrix_input);
182 
183  Matrix<BaseFloat> output_feats1;
184  GetOutput(&delta_input, &output_feats1);
185 
186  Matrix<BaseFloat> output_feats2(num_frames, output_dim);
187  ComputeDeltas(opts, input_feats, &output_feats2);
188 
189  KALDI_ASSERT(output_feats1.ApproxEqual(output_feats2));
190 }
191 
192 
193 void TestOnlineCmnInput() { // We're also testing OnlineCacheInput here.
194  int32 dim = 2 + Rand() % 5; // dimension of features.
195  int32 num_frames = 10 + Rand() % 10;
196 
197  Matrix<BaseFloat> input_feats(num_frames, dim);
198  input_feats.SetRandn();
199 
200  OnlineMatrixInput matrix_input(input_feats);
201  int32 cmn_window = 10 + Rand() % 20;
202  int32 min_window = 1 + Rand() % (cmn_window - 1);
203  if (Rand() % 3 == 0) min_window = cmn_window;
204 
205  OnlineCmnInput cmn_input(&matrix_input, cmn_window,
206  min_window);
207  OnlineCacheInput cache_input(&cmn_input);
208 
209  Matrix<BaseFloat> output_feats1;
210  GetOutput(&cache_input, &output_feats1);
211 
212  Matrix<BaseFloat> output_feats2(input_feats);
213  for (int32 i = 0; i < output_feats2.NumRows(); i++) {
214  SubVector<BaseFloat> this_row(output_feats2, i);
215  if (i == 0 && min_window == 0) this_row.SetZero();
216  else if (i < min_window) {
217  int32 window_nframes = std::min(min_window, input_feats.NumRows());
218  Vector<BaseFloat> this_sum(dim);
219  SubMatrix<BaseFloat> this_block(input_feats, 0, window_nframes,
220  0, dim);
221  this_sum.AddRowSumMat(1.0, this_block, 0.0);
222  this_row.AddVec(-1.0 / window_nframes, this_sum);
223  } else {
224  int32 window_nframes = std::min(i, cmn_window);
225  Vector<BaseFloat> this_sum(dim);
226  SubMatrix<BaseFloat> this_block(input_feats, i - window_nframes, window_nframes,
227  0, dim);
228  this_sum.AddRowSumMat(1.0, this_block, 0.0);
229  this_row.AddVec(-1.0 / window_nframes, this_sum);
230  }
231  }
232  KALDI_ASSERT(output_feats1.NumRows() == output_feats2.NumRows());
233  for (int32 i = 0; i < output_feats2.NumRows(); i++) {
234  if (!output_feats1.Row(i).ApproxEqual(output_feats2.Row(i))) {
235  KALDI_ERR << "Rows differ " << i << ", " << input_feats.Row(i) << output_feats1.Row(i)
236  << output_feats2.Row(i);
237  }
238  }
239  KALDI_ASSERT(output_feats1.ApproxEqual(output_feats2));
240  Matrix<BaseFloat> output_feats3;
241  cache_input.GetCachedData(&output_feats3);
242  KALDI_ASSERT(output_feats1.ApproxEqual(output_feats3));
243 }
244 
245 
246 
247 } // end namespace kaldi
248 
249 int main() {
250  using namespace kaldi;
251  for (int i = 0; i < 40; i++) {
256  TestOnlineCmnInput(); // also tests cache input.
257  // I have not tested the delta input yet.
258  }
259  std::cout << "Test OK.\n";
260 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int main()
virtual int32 Dim() const
virtual int32 Dim() const =0
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
OnlineMatrixInput(const Matrix< BaseFloat > &feats)
Matrix< BaseFloat > feats_
void TestOnlineDeltaInput()
void GetOutput(OnlineFeatureInterface *a, Matrix< BaseFloat > *output)
bool ApproxEqual(const MatrixBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).FrobeniusNorm() <= tol * (*this).FrobeniusNorm(). ...
void TestOnlineFeatureMatrix()
kaldi::int32 int32
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
int32 MatrixIndexT
Definition: matrix-common.h:98
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
void SetRandn()
Sets to random values of a normal distribution.
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
#define KALDI_ERR
Definition: kaldi-error.h:147
virtual bool Compute(Matrix< BaseFloat > *output)
void TestOnlineCmnInput()
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
void GetCachedData(Matrix< BaseFloat > *output)
void CopyColFromMat(const MatrixBase< OtherReal > &M, MatrixIndexT col)
Extracts a column of the matrix M.
A class representing a vector.
Definition: kaldi-vector.h:406
SubVector< BaseFloat > GetFrame(int32 frame)
#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 ComputeDeltas(const DeltaFeaturesOptions &delta_opts, const MatrixBase< BaseFloat > &input_features, Matrix< BaseFloat > *output_features)
void SpliceFrames(const MatrixBase< BaseFloat > &input_features, int32 left_context, int32 right_context, Matrix< BaseFloat > *output_features)
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
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).
virtual bool Compute(Matrix< BaseFloat > *output)
void TestOnlineLdaInput()
void TestOnlineMatrixInput()
void SetZero()
Set vector to all zeros.
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
Sub-matrix representation.
Definition: kaldi-matrix.h:988
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501