forked from flashlight/wav2letter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decode.cpp
432 lines (385 loc) · 14.8 KB
/
Decode.cpp
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <stdlib.h>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <mutex>
#include <string>
#include <vector>
#include <flashlight/flashlight.h>
#include <gflags/gflags.h>
#include <glog/logging.h>
#include "common/Defines.h"
#include "common/Dictionary.h"
#include "common/Transforms.h"
#include "common/Utils.h"
#include "criterion/criterion.h"
#include "data/Featurize.h"
#include "decoder/Decoder.hpp"
#include "decoder/KenLM.hpp"
#include "decoder/Trie.hpp"
#include "module/module.h"
#include "runtime/Data.h"
#include "runtime/Logger.h"
#include "runtime/Serial.h"
using namespace w2l;
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
std::string exec(argv[0]);
std::vector<std::string> argvs;
for (int i = 0; i < argc; i++) {
argvs.emplace_back(argv[i]);
}
gflags::SetUsageMessage(
"Usage: \n " + exec + " [data_path] [dataset_name] [flags]");
if (argc <= 1) {
LOG(FATAL) << gflags::ProgramUsage();
}
/* ===================== Parse Options ===================== */
LOG(INFO) << "Parsing command line flags";
gflags::ParseCommandLineFlags(&argc, &argv, false);
auto flagsfile = FLAGS_flagsfile;
if (!flagsfile.empty()) {
LOG(INFO) << "Reading flags from file " << flagsfile;
gflags::ReadFromFlagsFile(flagsfile, argv[0], true);
}
/* ===================== Create Network ===================== */
if (!(FLAGS_am.empty() ^ FLAGS_emission_dir.empty())) {
LOG(FATAL)
<< "One and only one of flag -am and -emission_dir should be set.";
}
EmissionSet emissionSet;
/* Using acoustic model */
std::shared_ptr<fl::Module> network;
std::shared_ptr<SequenceCriterion> criterion;
if (!FLAGS_am.empty()) {
std::unordered_map<std::string, std::string> cfg;
LOG(INFO) << "[Network] Reading acoustic model from " << FLAGS_am;
W2lSerializer::load(FLAGS_am, cfg, network, criterion);
network->eval();
LOG(INFO) << "[Network] " << network->prettyString();
if (criterion) {
criterion->eval();
LOG(INFO) << "[Network] " << criterion->prettyString();
}
LOG(INFO) << "[Network] Number of params: " << numTotalParams(network);
auto flags = cfg.find(kGflags);
if (flags == cfg.end()) {
LOG(FATAL) << "[Network] Invalid config loaded from " << FLAGS_am;
}
LOG(INFO) << "[Network] Updating flags from config file: " << FLAGS_am;
gflags::ReadFlagsFromString(flags->second, gflags::GetArgv0(), true);
}
/* Using existing emissions */
else {
std::string cleanedTestPath = cleanFilepath(FLAGS_test);
std::string loadPath =
pathsConcat(FLAGS_emission_dir, cleanedTestPath + ".bin");
LOG(INFO) << "[Serialization] Loading file: " << loadPath;
W2lSerializer::load(loadPath, emissionSet);
gflags::ReadFlagsFromString(emissionSet.gflags, gflags::GetArgv0(), true);
}
// override with user-specified flags
gflags::ParseCommandLineFlags(&argc, &argv, false);
if (!flagsfile.empty()) {
gflags::ReadFromFlagsFile(flagsfile, argv[0], true);
}
LOG(INFO) << "Gflags after parsing \n" << serializeGflags("; ");
/* ===================== Create Dictionary ===================== */
auto tokenDict = createTokenDict(pathsConcat(FLAGS_tokensdir, FLAGS_tokens));
int numClasses = tokenDict.indexSize();
LOG(INFO) << "Number of classes (network): " << numClasses;
auto lexicon = loadWords(FLAGS_lexicon, FLAGS_maxword);
auto wordDict = createWordDict(lexicon);
LOG(INFO) << "Number of words: " << wordDict.indexSize();
DictionaryMap dicts = {{kTargetIdx, tokenDict}, {kWordIdx, wordDict}};
/* ===================== Create Dataset ===================== */
if (FLAGS_emission_dir.empty()) {
// Load dataset
int worldRank = 0;
int worldSize = 1;
auto ds =
createDataset(FLAGS_test, dicts, lexicon, 1, worldRank, worldSize);
ds->shuffle(3);
LOG(INFO) << "[Serialization] Running forward pass ...";
int cnt = 0;
for (auto& sample : *ds) {
auto rawEmission =
network->forward({fl::input(sample[kInputIdx])}).front();
int N = rawEmission.dims(0);
int T = rawEmission.dims(1);
auto emission = afToVector<float>(rawEmission);
auto ltrTarget = afToVector<int>(sample[kTargetIdx]);
auto wrdTarget = afToVector<int>(sample[kWordIdx]);
emissionSet.emissions.emplace_back(emission);
emissionSet.wordTargets.emplace_back(wrdTarget);
emissionSet.letterTargets.emplace_back(ltrTarget);
emissionSet.emissionT.emplace_back(T);
emissionSet.emissionN = N;
// while decoding we use batchsize 1 and hence ds only has 1 sampleid
emissionSet.sampleIds.emplace_back(
afToVector<std::string>(sample[kSampleIdx]).front());
++cnt;
if (cnt == FLAGS_maxload) {
break;
}
}
if (FLAGS_criterion == kAsgCriterion) {
emissionSet.transition = afToVector<float>(criterion->param(0).array());
}
}
int nSample = emissionSet.emissions.size();
nSample = FLAGS_maxload > 0 ? std::min(nSample, FLAGS_maxload) : nSample;
int nSamplePerThread =
std::ceil(nSample / static_cast<float>(FLAGS_nthread_decoder));
LOG(INFO) << "[Dataset] Number of samples per thread: " << nSamplePerThread;
/* ===================== Decode ===================== */
// Prepare counters
std::vector<double> sliceWer(FLAGS_nthread_decoder);
std::vector<double> sliceLer(FLAGS_nthread_decoder);
std::vector<int> sliceNumWords(FLAGS_nthread_decoder, 0);
std::vector<int> sliceNumLetters(FLAGS_nthread_decoder, 0);
std::vector<int> sliceNumSamples(FLAGS_nthread_decoder, 0);
std::vector<double> sliceTime(FLAGS_nthread_decoder, 0);
// Prepare criterion
ModelType modelType = ModelType::ASG;
if (FLAGS_criterion == kCtcCriterion) {
modelType = ModelType::CTC;
} else if (FLAGS_criterion != kAsgCriterion) {
LOG(FATAL) << "[Decoder] Invalid model type: " << FLAGS_criterion;
}
const auto& transition = emissionSet.transition;
// Prepare decoder options
DecoderOptions decoderOpt(
FLAGS_beamsize,
static_cast<float>(FLAGS_beamscore),
static_cast<float>(FLAGS_lmweight),
static_cast<float>(FLAGS_wordscore),
static_cast<float>(FLAGS_unkweight),
FLAGS_logadd,
static_cast<float>(FLAGS_silweight),
modelType);
// Prepare log writer
std::mutex hypMutex, refMutex, logMutex;
std::ofstream hypStream, refStream, logStream;
if (!FLAGS_sclite.empty()) {
auto fileName = cleanFilepath(FLAGS_test);
auto hypPath = pathsConcat(FLAGS_sclite, fileName + ".hyp");
auto refPath = pathsConcat(FLAGS_sclite, fileName + ".ref");
auto logPath = pathsConcat(FLAGS_sclite, fileName + ".log");
hypStream.open(hypPath);
refStream.open(refPath);
logStream.open(logPath);
if (!hypStream.is_open() || !hypStream.good()) {
LOG(FATAL) << "Error opening hypothesis file: " << hypPath;
}
if (!refStream.is_open() || !refStream.good()) {
LOG(FATAL) << "Error opening reference file: " << refPath;
}
if (!logStream.is_open() || !logStream.good()) {
LOG(FATAL) << "Error opening log file: " << logPath;
}
}
auto writeHyp = [&](const std::string& hypStr) {
std::lock_guard<std::mutex> lock(hypMutex);
hypStream << hypStr;
};
auto writeRef = [&](const std::string& refStr) {
std::lock_guard<std::mutex> lock(refMutex);
refStream << refStr;
};
auto writeLog = [&](const std::string& logStr) {
std::lock_guard<std::mutex> lock(logMutex);
logStream << logStr;
};
// Build Language Model
std::shared_ptr<LM> lm;
if (FLAGS_lmtype == "kenlm") {
lm = std::make_shared<KenLM>(FLAGS_lm);
if (!lm) {
LOG(FATAL) << "[LM constructing] Failed to load LM: " << FLAGS_lm;
}
} else {
LOG(FATAL) << "[LM constructing] Invalid LM Type: " << FLAGS_lmtype;
}
LOG(INFO) << "[Decoder] LM constructed.\n";
// Build Trie
if (std::strlen(kSilToken) != 1) {
LOG(FATAL) << "[Decoder] Invalid unknown_symbol: " << kSilToken;
}
if (std::strlen(kBlankToken) != 1) {
LOG(FATAL) << "[Decoder] Invalid unknown_symbol: " << kBlankToken;
}
int silIdx = tokenDict.getIndex(kSilToken);
int blankIdx =
FLAGS_criterion == kCtcCriterion ? tokenDict.getIndex(kBlankToken) : -1;
int unkIdx = lm->index(kUnkToken);
std::shared_ptr<Trie> trie =
std::make_shared<Trie>(tokenDict.indexSize(), silIdx);
auto start_state = lm->start(false);
for (auto& it : lexicon) {
std::string word = it.first;
int lmIdx = lm->index(word);
float score;
auto dummyState = lm->score(start_state, lmIdx, score);
for (auto& tokens : it.second) {
auto tokensTensor = tokens2Tensor(tokens, tokenDict);
trie->insert(
tokensTensor,
std::make_shared<TrieLabel>(lmIdx, wordDict.getIndex(word)),
score);
}
}
LOG(INFO) << "[Decoder] Trie planted.\n";
// Smearing
SmearingMode smear_mode = SmearingMode::NONE;
if (FLAGS_smearing == "logadd") {
smear_mode = SmearingMode::LOGADD;
} else if (FLAGS_smearing == "max") {
smear_mode = SmearingMode::MAX;
} else if (FLAGS_smearing != "none") {
LOG(FATAL) << "[Decoder] Invalid smearing mode: " << FLAGS_smearing;
}
trie->smear(smear_mode);
LOG(INFO) << "[Decoder] Trie smeared.\n";
// Decoding
auto runDecoder = [&](int tid, int start, int end) {
try {
// Build Decoder
std::shared_ptr<TrieLabel> unk =
std::make_shared<TrieLabel>(unkIdx, wordDict.getIndex(kUnkToken));
Decoder decoder(decoderOpt, trie, lm, silIdx, blankIdx, unk, transition);
LOG(INFO) << "[Decoder] Decoder loaded in thread: " << tid;
// Get data and run decoder
TestMeters meters;
int sliceSize = end - start;
meters.timer.resume();
for (int s = start; s < end; s++) {
auto emission = emissionSet.emissions[s];
auto wordTarget = emissionSet.wordTargets[s];
auto letterTarget = emissionSet.letterTargets[s];
auto sampleId = emissionSet.sampleIds[s];
auto T = emissionSet.emissionT[s];
auto N = emissionSet.emissionN;
std::vector<float> score;
std::vector<std::vector<int>> wordPredictions;
std::vector<std::vector<int>> letterPredictions;
std::tie(score, wordPredictions, letterPredictions) =
decoder.decode(emission.data(), T, N);
// Cleanup predictions
auto wordPrediction = wordPredictions[0];
auto letterPrediction = letterPredictions[0];
if (FLAGS_criterion == kCtcCriterion ||
FLAGS_criterion == kAsgCriterion) {
uniq(letterPrediction);
}
if (FLAGS_criterion == kCtcCriterion) {
letterPrediction.erase(
std::remove(
letterPrediction.begin(), letterPrediction.end(), blankIdx),
letterPrediction.end());
}
validateTokens(wordPrediction, wordDict.getIndex(kUnkToken));
validateTokens(letterPrediction, -1);
remapLabels(letterTarget, tokenDict);
remapLabels(letterPrediction, tokenDict);
// Update meters & print out predictions
meters.werSlice.add(wordPrediction, wordTarget);
meters.lerSlice.add(letterPrediction, letterTarget);
if (FLAGS_show) {
meters.wer.reset();
meters.ler.reset();
meters.wer.add(wordPrediction, wordTarget);
meters.ler.add(letterPrediction, letterTarget);
auto wordTargetStr = tensor2words(wordTarget, wordDict);
auto wordPredictionStr = tensor2words(wordPrediction, wordDict);
std::stringstream buffer;
buffer << "|T|: " << wordTargetStr << std::endl;
buffer << "|P|: " << wordPredictionStr << std::endl;
if (FLAGS_showletters) {
buffer << "|t|: " << tensor2letters(letterTarget, tokenDict)
<< std::endl;
buffer << "|p|: " << tensor2letters(letterPrediction, tokenDict)
<< std::endl;
}
buffer << "[sample: " << sampleId
<< ", WER: " << meters.wer.value()[0]
<< "\%, LER: " << meters.ler.value()[0]
<< "\%, slice WER: " << meters.werSlice.value()[0]
<< "\%, slice LER: " << meters.lerSlice.value()[0]
<< "\%, progress: "
<< static_cast<float>(s - start + 1) / sliceSize * 100 << "\%]"
<< std::endl;
std::cout << buffer.str();
if (!FLAGS_sclite.empty()) {
std::string suffix = "(" + sampleId + ")\n";
writeHyp(wordPredictionStr + suffix);
writeRef(wordTargetStr + suffix);
writeLog(buffer.str());
}
}
// Update conters
sliceNumWords[tid] += wordTarget.size();
sliceNumLetters[tid] += letterTarget.size();
}
meters.timer.stop();
sliceWer[tid] = meters.werSlice.value()[0];
sliceLer[tid] = meters.lerSlice.value()[0];
sliceNumSamples[tid] = sliceSize;
sliceTime[tid] = meters.timer.value();
} catch (const std::exception& exc) {
LOG(FATAL) << "Exception in thread " << tid << "\n" << exc.what();
}
};
/* Spread threades */
auto startThreads = [&]() {
fl::ThreadPool threadPool(FLAGS_nthread_decoder);
for (int i = 0; i < FLAGS_nthread_decoder; i++) {
int start = i * nSamplePerThread;
if (start >= nSample) {
break;
}
int end = std::min((i + 1) * nSamplePerThread, nSample);
threadPool.enqueue(runDecoder, i, start, end);
}
};
auto timer = fl::TimeMeter();
timer.resume();
startThreads();
timer.stop();
/* Compute statistics */
int totalLetters = 0, totalWords = 0, totalSamples = 0;
for (int i = 0; i < FLAGS_nthread_decoder; i++) {
totalLetters += sliceNumLetters[i];
totalWords += sliceNumWords[i];
totalSamples += sliceNumSamples[i];
}
double totalWer = 0, totalLer = 0, totalTime = 0;
for (int i = 0; i < FLAGS_nthread_decoder; i++) {
totalWer += sliceWer[i] * sliceNumWords[i] / totalWords;
totalLer += sliceLer[i] * sliceNumLetters[i] / totalLetters;
totalTime += sliceTime[i];
}
std::stringstream buffer;
buffer << "------\n";
buffer << "[Decode " << FLAGS_test << " (" << totalSamples << " samples) in "
<< timer.value() << "s (actual decoding time " << std::setprecision(3)
<< totalTime / totalSamples
<< "s/sample) -- WER: " << std::setprecision(6) << totalWer
<< ", LER: " << totalLer << "]" << std::endl;
LOG(INFO) << buffer.str();
if (!FLAGS_sclite.empty()) {
writeLog(buffer.str());
hypStream.close();
refStream.close();
logStream.close();
}
return 0;
}