forked from kaldi-asr/kaldi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnnet-forward.cc
208 lines (168 loc) · 6.5 KB
/
nnet-forward.cc
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
// nnetbin/nnet-forward.cc
// Copyright 2011-2013 Brno University of Technology (Author: Karel Vesely)
// 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 <limits>
#include "nnet/nnet-nnet.h"
#include "nnet/nnet-loss.h"
#include "nnet/nnet-pdf-prior.h"
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "base/timer.h"
int main(int argc, char *argv[]) {
using namespace kaldi;
using namespace kaldi::nnet1;
try {
const char *usage =
"Perform forward pass through Neural Network.\n"
"Usage: nnet-forward [options] <nnet1-in> <feature-rspecifier> <feature-wspecifier>\n"
"e.g.: nnet-forward final.nnet ark:input.ark ark:output.ark\n";
ParseOptions po(usage);
PdfPriorOptions prior_opts;
prior_opts.Register(&po);
std::string feature_transform;
po.Register("feature-transform", &feature_transform,
"Feature transform in front of main network (in nnet format)");
bool no_softmax = false;
po.Register("no-softmax", &no_softmax,
"Removes the last component with Softmax, if found. The pre-softmax "
"activations are the output of the network. Decoding them leads to "
"the same lattices as if we had used 'log-posteriors'.");
bool apply_log = false;
po.Register("apply-log", &apply_log, "Transform NN output by log()");
std::string use_gpu="no";
po.Register("use-gpu", &use_gpu,
"yes|no|optional, only has effect if compiled with CUDA");
using namespace kaldi;
using namespace kaldi::nnet1;
typedef kaldi::int32 int32;
po.Read(argc, argv);
if (po.NumArgs() != 3) {
po.PrintUsage();
exit(1);
}
std::string model_filename = po.GetArg(1),
feature_rspecifier = po.GetArg(2),
feature_wspecifier = po.GetArg(3);
// Select the GPU
#if HAVE_CUDA == 1
CuDevice::Instantiate().SelectGpuId(use_gpu);
#endif
Nnet nnet_transf;
if (feature_transform != "") {
nnet_transf.Read(feature_transform);
}
Nnet nnet;
nnet.Read(model_filename);
// optionally remove softmax,
Component::ComponentType last_comp_type = nnet.GetLastComponent().GetType();
if (no_softmax) {
if (last_comp_type == Component::kSoftmax ||
last_comp_type == Component::kBlockSoftmax) {
KALDI_LOG << "Removing " << Component::TypeToMarker(last_comp_type)
<< " from the nnet " << model_filename;
nnet.RemoveLastComponent();
} else {
KALDI_WARN << "Last component 'NOT-REMOVED' by --no-softmax=true, "
<< "the component was " << Component::TypeToMarker(last_comp_type);
}
}
// avoid some bad option combinations,
if (apply_log && no_softmax) {
KALDI_ERR << "Cannot use both --apply-log=true --no-softmax=true, "
<< "use only one of the two!";
}
// we will subtract log-priors later,
PdfPrior pdf_prior(prior_opts);
// disable dropout,
nnet_transf.SetDropoutRate(0.0);
nnet.SetDropoutRate(0.0);
kaldi::int64 tot_t = 0;
SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
BaseFloatMatrixWriter feature_writer(feature_wspecifier);
CuMatrix<BaseFloat> feats, feats_transf, nnet_out;
Matrix<BaseFloat> nnet_out_host;
Timer time;
double time_now = 0;
int32 num_done = 0;
// main loop,
for (; !feature_reader.Done(); feature_reader.Next()) {
// read
Matrix<BaseFloat> mat = feature_reader.Value();
std::string utt = feature_reader.Key();
KALDI_VLOG(2) << "Processing utterance " << num_done+1
<< ", " << utt
<< ", " << mat.NumRows() << "frm";
if (!KALDI_ISFINITE(mat.Sum())) { // check there's no nan/inf,
KALDI_ERR << "NaN or inf found in features for " << utt;
}
// push it to gpu,
feats = mat;
// fwd-pass, feature transform,
nnet_transf.Feedforward(feats, &feats_transf);
if (!KALDI_ISFINITE(feats_transf.Sum())) { // check there's no nan/inf,
KALDI_ERR << "NaN or inf found in transformed-features for " << utt;
}
// fwd-pass, nnet,
nnet.Feedforward(feats_transf, &nnet_out);
if (!KALDI_ISFINITE(nnet_out.Sum())) { // check there's no nan/inf,
KALDI_ERR << "NaN or inf found in nn-output for " << utt;
}
// convert posteriors to log-posteriors,
if (apply_log) {
if (!(nnet_out.Min() >= 0.0 && nnet_out.Max() <= 1.0)) {
KALDI_WARN << "Applying 'log()' to data which don't seem to be "
<< "probabilities," << utt;
}
nnet_out.Add(1e-20); // avoid log(0),
nnet_out.ApplyLog();
}
// subtract log-priors from log-posteriors or pre-softmax,
if (prior_opts.class_frame_counts != "") {
pdf_prior.SubtractOnLogpost(&nnet_out);
}
// download from GPU,
nnet_out_host = Matrix<BaseFloat>(nnet_out);
// write,
if (!KALDI_ISFINITE(nnet_out_host.Sum())) { // check there's no nan/inf,
KALDI_ERR << "NaN or inf found in final output nn-output for " << utt;
}
feature_writer.Write(feature_reader.Key(), nnet_out_host);
// progress log,
if (num_done % 100 == 0) {
time_now = time.Elapsed();
KALDI_VLOG(1) << "After " << num_done << " utterances: time elapsed = "
<< time_now/60 << " min; processed " << tot_t/time_now
<< " frames per second.";
}
num_done++;
tot_t += mat.NumRows();
}
// final message,
KALDI_LOG << "Done " << num_done << " files"
<< " in " << time.Elapsed()/60 << "min,"
<< " (fps " << tot_t/time.Elapsed() << ")";
#if HAVE_CUDA == 1
if (GetVerboseLevel() >= 1) {
CuDevice::Instantiate().PrintProfile();
}
#endif
if (num_done == 0) return -1;
return 0;
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}