Blame view

src/cudafeatbin/compute-mfcc-feats-cuda.cc 7.15 KB
8dcb6dfcb   Yannick Estève   first commit
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
  // cudafeatbin/compute-mfcc-feats-cuda.cc
  //
  // Copyright (c) 2019, NVIDIA CORPORATION.  All rights reserved.
  // Justin Luitjens
  //
  // 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
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  
  #include "base/kaldi-common.h"
  #include "util/common-utils.h"
  #include "cudafeat/feature-mfcc-cuda.h"
  #include "feat/wave-reader.h"
  #include "cudamatrix/cu-matrix.h"
  #include "cudamatrix/cu-vector.h"
  int main(int argc, char *argv[]) {
    try {
      using namespace kaldi;
      const char *usage =
          "Create MFCC feature files.
  "
          "Usage:  compute-mfcc-feats [options...] <wav-rspecifier> <feats-wspecifier>
  ";
  
      // construct all the global objects
      ParseOptions po(usage);
      MfccOptions mfcc_opts;
      bool subtract_mean = false;
      BaseFloat vtln_warp = 1.0;
      std::string vtln_map_rspecifier;
      std::string utt2spk_rspecifier;
      int32 channel = -1;
      BaseFloat min_duration = 0.0;
      // Define defaults for gobal options
      std::string output_format = "kaldi";
  
      // Register the MFCC option struct
      mfcc_opts.Register(&po);
  
      // Register the options
      po.Register("output-format", &output_format, "Format of the output "
                  "files [kaldi, htk]");
      po.Register("subtract-mean", &subtract_mean, "Subtract mean of each "
                  "feature file [CMS]; not recommended to do it this way. ");
      po.Register("vtln-warp", &vtln_warp, "Vtln warp factor (only applicable "
                  "if vtln-map not specified)");
      po.Register("vtln-map", &vtln_map_rspecifier, "Map from utterance or "
                  "speaker-id to vtln warp factor (rspecifier)");
      po.Register("utt2spk", &utt2spk_rspecifier, "Utterance to speaker-id map "
                  "rspecifier (if doing VTLN and you have warps per speaker)");
      po.Register("channel", &channel, "Channel to extract (-1 -> expect mono, "
                  "0 -> left, 1 -> right)");
      po.Register("min-duration", &min_duration, "Minimum duration of segments "
                  "to process (in seconds).");
  
      po.Read(argc, argv);
  
      if (po.NumArgs() != 2) {
        po.PrintUsage();
        exit(1);
      }
      
      g_cuda_allocator.SetOptions(g_allocator_options);
      CuDevice::Instantiate().SelectGpuId("yes");
      CuDevice::Instantiate().AllowMultithreading();
  
  
      std::string wav_rspecifier = po.GetArg(1);
  
      std::string output_wspecifier = po.GetArg(2);
  
      CudaMfcc mfcc(mfcc_opts);
  
      SequentialTableReader<WaveHolder> reader(wav_rspecifier);
      BaseFloatMatrixWriter kaldi_writer;  // typedef to TableWriter<something>.
      TableWriter<HtkMatrixHolder> htk_writer;
  
      if (utt2spk_rspecifier != "")
        KALDI_ASSERT(vtln_map_rspecifier != "" && "the utt2spk option is only "
                     "needed if the vtln-map option is used.");
      RandomAccessBaseFloatReaderMapped vtln_map_reader(vtln_map_rspecifier,
                                                        utt2spk_rspecifier);
      
      if (output_format == "kaldi") {
        if (!kaldi_writer.Open(output_wspecifier))
          KALDI_ERR << "Could not initialize output with wspecifier "
                    << output_wspecifier;
      } else if (output_format == "htk") {
        if (!htk_writer.Open(output_wspecifier))
          KALDI_ERR << "Could not initialize output with wspecifier "
                    << output_wspecifier;
      } else {
        KALDI_ERR << "Invalid output_format string " << output_format;
      }
  
      int32 num_utts = 0, num_success = 0;
      for (; !reader.Done(); reader.Next()) {
        num_utts++;
        std::string utt = reader.Key();
        const WaveData &wave_data = reader.Value();
        if (wave_data.Duration() < min_duration) {
          KALDI_WARN << "File: " << utt << " is too short ("
                     << wave_data.Duration() << " sec): producing no output.";
          continue;
        }
        int32 num_chan = wave_data.Data().NumRows(), this_chan = channel;
        {  // This block works out the channel (0=left, 1=right...)
          KALDI_ASSERT(num_chan > 0);  // should have been caught in
          // reading code if no channels.
          if (channel == -1) {
            this_chan = 0;
            if (num_chan != 1)
              KALDI_WARN << "Channel not specified but you have data with "
                         << num_chan  << " channels; defaulting to zero";
          } else {
            if (this_chan >= num_chan) {
              KALDI_WARN << "File with id " << utt << " has "
                         << num_chan << " channels but you specified channel "
                         << channel << ", producing no output.";
              continue;
            }
          }
        }
        BaseFloat vtln_warp_local;  // Work out VTLN warp factor.
        if (vtln_map_rspecifier != "") {
          if (!vtln_map_reader.HasKey(utt)) {
            KALDI_WARN << "No vtln-map entry for utterance-id (or speaker-id) "
                       << utt;
            continue;
          }
          vtln_warp_local = vtln_map_reader.Value(utt);
        } else {
          vtln_warp_local = vtln_warp;
        }
  
        SubVector<BaseFloat> waveform(wave_data.Data(), this_chan);
        Matrix<BaseFloat> features;
        try {
          CuVector<BaseFloat> cu_waveform(waveform);
          CuMatrix<BaseFloat> cu_features;
          mfcc.ComputeFeatures(cu_waveform, wave_data.SampFreq(), vtln_warp_local, &cu_features);
          features.Resize(cu_features.NumRows(), cu_features.NumCols());
          features.CopyFromMat(cu_features);
        } catch (...) {
          KALDI_WARN << "Failed to compute features for utterance "
                     << utt;
          continue;
        }
        if (subtract_mean) {
          Vector<BaseFloat> mean(features.NumCols());
          mean.AddRowSumMat(1.0, features);
          mean.Scale(1.0 / features.NumRows());
          for (int32 i = 0; i < features.NumRows(); i++)
            features.Row(i).AddVec(-1.0, mean);
        }
        if (output_format == "kaldi") {
          kaldi_writer.Write(utt, features);
        } else {
          std::pair<Matrix<BaseFloat>, HtkHeader> p;
          p.first.Resize(features.NumRows(), features.NumCols());
          p.first.CopyFromMat(features);
          HtkHeader header = {
            features.NumRows(),
            100000,  // 10ms shift
            static_cast<int16>(sizeof(float)*(features.NumCols())),
            static_cast<uint16>( 006 | // MFCC
            (mfcc_opts.use_energy ? 0100 : 020000)) // energy; otherwise c0
          };
          p.second = header;
          htk_writer.Write(utt, p);
        }
        if (num_utts % 10 == 0)
          KALDI_LOG << "Processed " << num_utts << " utterances";
        KALDI_VLOG(2) << "Processed features for key " << utt;
        num_success++;
      }
      KALDI_LOG << " Done " << num_success << " out of " << num_utts
                << " utterances.";
      return (num_success != 0 ? 0 : 1);
    } catch(const std::exception &e) {
      std::cerr << e.what();
      return -1;
    }
  }