online-feature-test.cc 13.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
// feat/online-feature-test.cc

// Copyright 2014  IMSL, PKU-HKUST (author: Wei Shi)
// Copyright 2014  Yanqing Sun, Junjie Wang,
//                 Daniel Povey, Korbinian Riedhammer

// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.

#include "feat/online-feature.h"
#include "feat/wave-reader.h"
#include "matrix/kaldi-matrix.h"
#include "transform/transform-common.h"

namespace kaldi {

void GetOutput(OnlineFeatureInterface *a,
               Matrix<BaseFloat> *output) {
  int32 dim = a->Dim();
  int32 frame_num = 0;
  OnlineCacheFeature cache(a);

  std::vector<Vector<BaseFloat>* > cached_frames;
  while (true) {
    Vector<BaseFloat> garbage(dim);
    cache.GetFrame(frame_num, &garbage);
    cached_frames.push_back(new Vector<BaseFloat>(garbage));
    if (cache.IsLastFrame(frame_num))
      break;
    frame_num++;
  }

  KALDI_ASSERT(cached_frames.size() == a->NumFramesReady());

  output->Resize(cached_frames.size(), dim);
  for (int32 i = 0; i < cached_frames.size(); i++) {
    output->CopyRowFromVec(*(cached_frames[i]), i);
    delete cached_frames[i];
  }
  cached_frames.clear();
  cache.ClearCache();
}

// Only generate random length for each piece
bool RandomSplit(int32 wav_dim,
                 std::vector<int32> *piece_dim,
                 int32 num_pieces,
                 int32 trials = 5) {
  piece_dim->clear();
  piece_dim->resize(num_pieces, 0);

  int32 dim_mean = wav_dim / (num_pieces * 2);
  int32 cnt = 0;
  while (true) {
    int32 dim_total = 0;
    for (int32 i = 0; i < num_pieces - 1; i++) {
      (*piece_dim)[i] = dim_mean + rand() % dim_mean;
      dim_total += (*piece_dim)[i];
    }
    (*piece_dim)[num_pieces - 1] = wav_dim - dim_total;

    if (dim_total > 0 && dim_total < wav_dim)
      break;
    if (++cnt > trials)
      return false;
  }
  return true;
}

// test the OnlineMatrixFeature and OnlineCacheFeature classes.
void TestOnlineMatrixCacheFeature() {
  int32 dim = 2 + rand() % 5;  // dimension of features.
  int32 num_frames = 100 + rand() % 100;

  Matrix<BaseFloat> input_feats(num_frames, dim);
  input_feats.SetRandn();

  OnlineMatrixFeature matrix_feats(input_feats);

  Matrix<BaseFloat> output_feats;
  GetOutput(&matrix_feats, &output_feats);
  AssertEqual(input_feats, output_feats);
}

void TestOnlineDeltaFeature() {
  int32 dim = 2 + rand() % 5;  // dimension of features.
  int32 num_frames = 100 + rand() % 100;
  DeltaFeaturesOptions opts;
  opts.order = rand() % 3;
  opts.window = 1 + rand() % 3;

  int32 output_dim = dim * (1 + opts.order);

  Matrix<BaseFloat> input_feats(num_frames, dim);
  input_feats.SetRandn();

  OnlineMatrixFeature matrix_feats(input_feats);
  OnlineDeltaFeature delta_feats(opts, &matrix_feats);

  Matrix<BaseFloat> output_feats1;
  GetOutput(&delta_feats, &output_feats1);

  Matrix<BaseFloat> output_feats2(num_frames, output_dim);
  ComputeDeltas(opts, input_feats, &output_feats2);

  KALDI_ASSERT(output_feats1.ApproxEqual(output_feats2));
}

void TestOnlineSpliceFrames() {
  int32 dim = 2 + rand() % 5;  // dimension of features.
  int32 num_frames = 100 + rand() % 100;
  OnlineSpliceOptions opts;
  opts.left_context  = 1 + rand() % 4;
  opts.right_context = 1 + rand() % 4;

  int32 output_dim = dim * (1 + opts.left_context + opts.right_context);

  Matrix<BaseFloat> input_feats(num_frames, dim);
  input_feats.SetRandn();

  OnlineMatrixFeature matrix_feats(input_feats);
  OnlineSpliceFrames splice_frame(opts, &matrix_feats);

  Matrix<BaseFloat> output_feats1;
  GetOutput(&splice_frame, &output_feats1);

  Matrix<BaseFloat> output_feats2(num_frames, output_dim);
  SpliceFrames(input_feats, opts.left_context, opts.right_context,
    &output_feats2);

  KALDI_ASSERT(output_feats1.ApproxEqual(output_feats2));
}

void TestOnlineMfcc() {
  std::ifstream is("../feat/test_data/test.wav", std::ios_base::binary);
  WaveData wave;
  wave.Read(is);
  KALDI_ASSERT(wave.Data().NumRows() == 1);
  SubVector<BaseFloat> waveform(wave.Data(), 0);

  // the parametrization object
  MfccOptions op;
  op.frame_opts.dither = 0.0;
  op.frame_opts.preemph_coeff = 0.0;
  op.frame_opts.window_type = "hamming";
  op.frame_opts.remove_dc_offset = false;
  op.frame_opts.round_to_power_of_two = true;
  op.frame_opts.samp_freq = wave.SampFreq();
  op.mel_opts.low_freq = 0.0;
  op.htk_compat = false;
  op.use_energy = false;  // C0 not energy.
  if (RandInt(0, 1) == 0)
    op.frame_opts.snip_edges = false;
  Mfcc mfcc(op);

  // compute mfcc offline
  Matrix<BaseFloat> mfcc_feats;
  mfcc.Compute(waveform, 1.0, &mfcc_feats);  // vtln not supported

  // compare
  // The test waveform is about 1.44s long, so
  // we try to break it into from 5 pieces to 9(not essential to do so)
  for (int32 num_piece = 5; num_piece < 10; num_piece++) {
    OnlineMfcc online_mfcc(op);
    std::vector<int32> piece_length(num_piece, 0);

    bool ret = RandomSplit(waveform.Dim(), &piece_length, num_piece);
    KALDI_ASSERT(ret);

    int32 offset_start = 0;
    for (int32 i = 0; i < num_piece; i++) {
      Vector<BaseFloat> wave_piece(
        waveform.Range(offset_start, piece_length[i]));
      online_mfcc.AcceptWaveform(wave.SampFreq(), wave_piece);
      offset_start += piece_length[i];
    }
    online_mfcc.InputFinished();

    Matrix<BaseFloat> online_mfcc_feats;
    GetOutput(&online_mfcc, &online_mfcc_feats);

    AssertEqual(mfcc_feats, online_mfcc_feats);
  }
}

void TestOnlinePlp() {
  std::ifstream is("../feat/test_data/test.wav", std::ios_base::binary);
  WaveData wave;
  wave.Read(is);
  KALDI_ASSERT(wave.Data().NumRows() == 1);
  SubVector<BaseFloat> waveform(wave.Data(), 0);

  // the parametrization object
  PlpOptions op;
  op.frame_opts.dither = 0.0;
  op.frame_opts.preemph_coeff = 0.0;
  op.frame_opts.window_type = "hamming";
  op.frame_opts.remove_dc_offset = false;
  op.frame_opts.round_to_power_of_two = true;
  op.frame_opts.samp_freq = wave.SampFreq();
  op.mel_opts.low_freq = 0.0;
  op.htk_compat = false;
  op.use_energy = false;  // C0 not energy.
  Plp plp(op);

  // compute plp offline
  Matrix<BaseFloat> plp_feats;
  plp.Compute(waveform, 1.0, &plp_feats);  // vtln not supported

  // compare
  // The test waveform is about 1.44s long, so
  // we try to break it into from 5 pieces to 9(not essential to do so)
  for (int32 num_piece = 5; num_piece < 10; num_piece++) {
    OnlinePlp online_plp(op);
    std::vector<int32> piece_length(num_piece);
    bool ret = RandomSplit(waveform.Dim(), &piece_length, num_piece);
    KALDI_ASSERT(ret);

    int32 offset_start = 0;
    for (int32 i = 0; i < num_piece; i++) {
      Vector<BaseFloat> wave_piece(
        waveform.Range(offset_start, piece_length[i]));
      online_plp.AcceptWaveform(wave.SampFreq(), wave_piece);
      offset_start += piece_length[i];
    }
    online_plp.InputFinished();

    Matrix<BaseFloat> online_plp_feats;
    GetOutput(&online_plp, &online_plp_feats);

    AssertEqual(plp_feats, online_plp_feats);
  }
}

void TestOnlineTransform() {
  std::ifstream is("../feat/test_data/test.wav", std::ios_base::binary);
  WaveData wave;
  wave.Read(is);
  KALDI_ASSERT(wave.Data().NumRows() == 1);
  SubVector<BaseFloat> waveform(wave.Data(), 0);

  // build online feature interface, take OnlineMfcc as an example
  MfccOptions op;
  op.frame_opts.dither = 0.0;
  op.frame_opts.preemph_coeff = 0.0;
  op.frame_opts.window_type = "hamming";
  op.frame_opts.remove_dc_offset = false;
  op.frame_opts.round_to_power_of_two = true;
  op.frame_opts.samp_freq = wave.SampFreq();
  op.mel_opts.low_freq = 0.0;
  op.htk_compat = false;
  op.use_energy = false;  // C0 not energy.
  OnlineMfcc online_mfcc(op);

  online_mfcc.AcceptWaveform(wave.SampFreq(), waveform);
  online_mfcc.InputFinished();
  Matrix<BaseFloat> mfcc_feats;
  GetOutput(&online_mfcc, &mfcc_feats);

  // Affine transform
  Matrix<BaseFloat> trans(online_mfcc.Dim(), online_mfcc.Dim() + 1);
  trans.SetRandn();
  OnlineTransform online_trans(trans, &online_mfcc);

  Matrix<BaseFloat> trans_feats;
  GetOutput(&online_trans, &trans_feats);

  Matrix<BaseFloat> output_feats(mfcc_feats.NumRows(), mfcc_feats.NumCols());
  for (int32 i = 0; i < mfcc_feats.NumRows(); i++) {
    Vector<BaseFloat> vec_tmp(mfcc_feats.Row(i));
    ApplyAffineTransform(trans, &vec_tmp);
    output_feats.CopyRowFromVec(vec_tmp, i);
  }

  AssertEqual(trans_feats, output_feats);
}

void TestOnlineAppendFeature() {
  std::ifstream is("../feat/test_data/test.wav", std::ios_base::binary);
  WaveData wave;
  wave.Read(is);
  KALDI_ASSERT(wave.Data().NumRows() == 1);
  SubVector<BaseFloat> waveform(wave.Data(), 0);

  // the parametrization object for 1st stream mfcc feature
  MfccOptions mfcc_op;
  mfcc_op.frame_opts.dither = 0.0;
  mfcc_op.frame_opts.preemph_coeff = 0.0;
  mfcc_op.frame_opts.window_type = "hamming";
  mfcc_op.frame_opts.remove_dc_offset = false;
  mfcc_op.frame_opts.round_to_power_of_two = true;
  mfcc_op.frame_opts.samp_freq = wave.SampFreq();
  mfcc_op.mel_opts.low_freq = 0.0;
  mfcc_op.htk_compat = false;
  mfcc_op.use_energy = false;  // C0 not energy.
  Mfcc mfcc(mfcc_op);

  // compute mfcc offline
  Matrix<BaseFloat> mfcc_feats;
  mfcc.Compute(waveform, 1.0, &mfcc_feats);  // vtln not supported

  // the parametrization object for 2nd stream plp feature
  PlpOptions plp_op;
  plp_op.frame_opts.dither = 0.0;
  plp_op.frame_opts.preemph_coeff = 0.0;
  plp_op.frame_opts.window_type = "hamming";
  plp_op.frame_opts.remove_dc_offset = false;
  plp_op.frame_opts.round_to_power_of_two = true;
  plp_op.frame_opts.samp_freq = wave.SampFreq();
  plp_op.mel_opts.low_freq = 0.0;
  plp_op.htk_compat = false;
  plp_op.use_energy = false;  // C0 not energy.
  Plp plp(plp_op);

  // compute plp offline
  Matrix<BaseFloat> plp_feats;
  plp.Compute(waveform, 1.0, &plp_feats);  // vtln not supported

  // compare
  // The test waveform is about 1.44s long, so
  // we try to break it into from 5 pieces to 9(not essential to do so)
  for (int32 num_piece = 5; num_piece < 10; num_piece++) {
    OnlineMfcc online_mfcc(mfcc_op);
    OnlinePlp online_plp(plp_op);
    OnlineAppendFeature online_mfcc_plp(&online_mfcc, &online_plp);

    std::vector<int32> piece_length(num_piece);
    bool ret = RandomSplit(waveform.Dim(), &piece_length, num_piece);
    KALDI_ASSERT(ret);
    int32 offset_start = 0;
    for (int32 i = 0; i < num_piece; i++) {
      Vector<BaseFloat> wave_piece(
        waveform.Range(offset_start, piece_length[i]));
      online_mfcc.AcceptWaveform(wave.SampFreq(), wave_piece);
      online_plp.AcceptWaveform(wave.SampFreq(), wave_piece);
      offset_start += piece_length[i];
    }
    online_mfcc.InputFinished();
    online_plp.InputFinished();

    Matrix<BaseFloat> online_mfcc_plp_feats;
    GetOutput(&online_mfcc_plp, &online_mfcc_plp_feats);

    // compare mfcc_feats & plp_features with online_mfcc_plp_feats
    KALDI_ASSERT(mfcc_feats.NumRows() == online_mfcc_plp_feats.NumRows()
      && plp_feats.NumRows() == online_mfcc_plp_feats.NumRows()
      && mfcc_feats.NumCols() + plp_feats.NumCols()
         == online_mfcc_plp_feats.NumCols());
    for (MatrixIndexT i = 0; i < online_mfcc_plp_feats.NumRows(); i++) {
      for (MatrixIndexT j = 0; j < mfcc_feats.NumCols(); j++) {
        KALDI_ASSERT(std::abs(mfcc_feats(i, j) - online_mfcc_plp_feats(i, j))
          < 0.0001*std::max(1.0, static_cast<double>(std::abs(mfcc_feats(i, j))
                                    + std::abs(online_mfcc_plp_feats(i, j)))));
      }
      for (MatrixIndexT k = 0; k < plp_feats.NumCols(); k++) {
        KALDI_ASSERT(
          std::abs(plp_feats(i, k) -
            online_mfcc_plp_feats(i, mfcc_feats.NumCols() + k))
          < 0.0001*std::max(1.0, static_cast<double>(std::abs(plp_feats(i, k))
            +std::abs(online_mfcc_plp_feats(i, mfcc_feats.NumCols() + k)))));
      }
    }
  }
}

void TestRecyclingVector() {
  RecyclingVector full_vec;
  RecyclingVector shrinking_vec(10);
  for (int i = 0; i != 100; ++i) {
    Vector <BaseFloat> data(1);
    data.Set(i);
    full_vec.PushBack(new Vector<BaseFloat>(data));
    shrinking_vec.PushBack(new Vector<BaseFloat>(data));
  }
  KALDI_ASSERT(full_vec.Size() == 100);
  KALDI_ASSERT(shrinking_vec.Size() == 100);

  // full_vec should contain everything
  for (int i = 0; i != 100; ++i) {
    Vector <BaseFloat> *data = full_vec.At(i);
    KALDI_ASSERT(data != nullptr);
    KALDI_ASSERT((*data)(0) == static_cast<BaseFloat>(i));
  }

  // shrinking_vec may throw an exception for the first 90 elements
  int caught_exceptions = 0;
  for (int i = 0; i != 90; ++i) {
    try {
      shrinking_vec.At(i);
    } catch (const std::runtime_error &) {
      ++caught_exceptions;
    }
  }
  // it may actually store a bit more elements for performance efficiency considerations
  KALDI_ASSERT(caught_exceptions >= 80);

  // shrinking_vec should contain the last 10 elements
  for (int i = 90; i != 100; ++i) {
    Vector <BaseFloat> *data = shrinking_vec.At(i);
    KALDI_ASSERT(data != nullptr);
    KALDI_ASSERT((*data)(0) == static_cast<BaseFloat>(i));
  }
}

}  // end namespace kaldi

int main() {
  using namespace kaldi;
  for (int i = 0; i < 10; i++) {
    TestOnlineMatrixCacheFeature();
    TestOnlineDeltaFeature();
    TestOnlineSpliceFrames();
    TestOnlineMfcc();
    TestOnlinePlp();
    TestOnlineTransform();
    TestOnlineAppendFeature();
    TestRecyclingVector();
  }
  std::cout << "Test OK.\n";
}