forked from flashlight/wav2letter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.cpp
193 lines (162 loc) · 6.51 KB
/
Test.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
/**
* 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 <fstream>
#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 "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 ===================== */
std::shared_ptr<fl::Module> network;
std::shared_ptr<SequenceCriterion> criterion;
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();
criterion->eval();
LOG(INFO) << "[Network] " << network->prettyString();
LOG(INFO) << "[Criterion] " << 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);
// 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 ===================== */
// Load dataset
int worldRank = 0;
int worldSize = 1;
auto ds = createDataset(FLAGS_test, dicts, lexicon, 1, worldRank, worldSize);
ds->shuffle(3);
int nSamples = ds->size();
if (FLAGS_maxload > 0) {
nSamples = std::min(nSamples, FLAGS_maxload);
}
LOG(INFO) << "[Dataset] Dataset loaded.";
/* ===================== Test ===================== */
TestMeters meters;
EmissionSet emissionSet;
meters.timer.resume();
int cnt = 1;
for (auto& sample : *ds) {
auto rawEmission = network->forward({fl::input(sample[kInputIdx])}).front();
auto emission = afToVector<float>(rawEmission);
auto ltrTarget = afToVector<int>(sample[kTargetIdx]);
auto wrdTarget = afToVector<int>(sample[kWordIdx]);
/* viterbiPath + remove duplication/blank */
auto viterbiPath =
afToVector<int>(criterion->viterbiPath(rawEmission.array()));
if (FLAGS_criterion == kCtcCriterion || FLAGS_criterion == kAsgCriterion) {
uniq(viterbiPath);
}
if (FLAGS_criterion == kCtcCriterion) {
auto blankidx = tokenDict.getIndex(kBlankToken);
viterbiPath.erase(
std::remove(viterbiPath.begin(), viterbiPath.end(), blankidx),
viterbiPath.end());
}
remapLabels(viterbiPath, tokenDict);
remapLabels(ltrTarget, tokenDict);
meters.lerSlice.add(viterbiPath, ltrTarget);
auto wordViterbi = tknTensor2wrdTensor(
viterbiPath, wordDict, tokenDict, tokenDict.getIndex(kSilToken));
meters.werSlice.add(wordViterbi, wrdTarget);
if (FLAGS_show) {
meters.ler.reset();
meters.wer.reset();
meters.ler.add(viterbiPath, ltrTarget);
meters.wer.add(wordViterbi, wrdTarget);
std::cout << "|T|: " << tensor2letters(ltrTarget, tokenDict) << std::endl;
std::cout << "|P|: " << tensor2letters(viterbiPath, tokenDict)
<< std::endl;
std::cout << "[sample: " << cnt << ", WER: " << meters.wer.value()[0]
<< "\%, LER: " << meters.ler.value()[0]
<< "\%, total WER: " << meters.werSlice.value()[0]
<< "\%, total LER: " << meters.lerSlice.value()[0]
<< "\%, progress: " << static_cast<float>(cnt) / nSamples * 100
<< "\%]" << std::endl;
++cnt;
if (cnt == FLAGS_maxload) {
break;
}
}
/* Save emission and targets */
int N = rawEmission.dims(0);
int T = rawEmission.dims(1);
emissionSet.emissions.emplace_back(emission);
emissionSet.letterTargets.emplace_back(ltrTarget);
emissionSet.wordTargets.emplace_back(wrdTarget);
// while testing we use batchsize 1 and hence ds only has 1 sampleid
emissionSet.sampleIds.emplace_back(
afToVector<std::string>(sample[kFileIdIdx]).front());
emissionSet.emissionT.emplace_back(T);
emissionSet.emissionN = N;
}
if (FLAGS_criterion == kAsgCriterion) {
emissionSet.transition = afToVector<float>(criterion->param(0).array());
}
emissionSet.gflags = serializeGflags();
meters.timer.stop();
std::cout << "---\n[total WER: " << meters.werSlice.value()[0]
<< "\%, total LER: " << meters.lerSlice.value()[0]
<< "\%, time: " << meters.timer.value() << "s]" << std::endl;
/* ====== Serialize emission and targets for decoding ====== */
std::string cleanedTestPath = cleanFilepath(FLAGS_test);
std::string savePath =
pathsConcat(FLAGS_emission_dir, cleanedTestPath + ".bin");
LOG(INFO) << "[Serialization] Saving into file: " << savePath;
W2lSerializer::save(savePath, emissionSet);
return 0;
}