lattice-determinize-phone-pruned-parallel.cc
5.89 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
// latbin/lattice-determinize-phone-pruned-parallel.cc
// Copyright 2014 Guoguo Chen
// 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 "base/kaldi-common.h"
#include "hmm/transition-model.h"
#include "lat/kaldi-lattice.h"
#include "lat/determinize-lattice-pruned.h"
#include "lat/lattice-functions.h"
#include "lat/push-lattice.h"
#include "util/common-utils.h"
#include "util/kaldi-thread.h"
namespace kaldi {
class DeterminizeLatticeTask {
public:
// Initializer takes ownership of "lat".
DeterminizeLatticeTask(
const TransitionModel &trans_model,
fst::DeterminizeLatticePhonePrunedOptions &opts,
std::string key,
BaseFloat acoustic_scale,
BaseFloat beam,
Lattice *lat,
CompactLatticeWriter *clat_writer,
int32 *num_warn):
trans_model_(&trans_model), opts_(opts), key_(key),
acoustic_scale_(acoustic_scale), beam_(beam),
lat_(lat), clat_writer_(clat_writer), num_warn_(num_warn) { }
void operator () () {
// We apply the acoustic scale before determinization and will undo it
// afterward, since it can affect the result.
fst::ScaleLattice(fst::AcousticLatticeScale(acoustic_scale_), lat_);
if (!DeterminizeLatticePhonePrunedWrapper(
*trans_model_, lat_, beam_, &det_clat_, opts_)) {
KALDI_WARN << "For key " << key_ << ", determinization did not succeed"
"(partial output will be pruned tighter than the specified beam.)";
(*num_warn_)++;
}
delete lat_;
lat_ = NULL;
// Invert the original acoustic scaling
fst::ScaleLattice(fst::AcousticLatticeScale(1.0/acoustic_scale_),
&det_clat_);
}
~DeterminizeLatticeTask() {
KALDI_VLOG(2) << "Wrote lattice with " << det_clat_.NumStates()
<< " for key " << key_;
clat_writer_->Write(key_, det_clat_);
}
private:
const TransitionModel *trans_model_;
const fst::DeterminizeLatticePhonePrunedOptions &opts_;
std::string key_;
BaseFloat acoustic_scale_;
BaseFloat beam_;
// The lattice we're working on. Owned locally.
Lattice *lat_;
// The output of our process. Will be written to clat_writer_ in the
// destructor.
CompactLattice det_clat_;
CompactLatticeWriter *clat_writer_;
int32 *num_warn_;
};
} // namespace kaldi
int main(int argc, char *argv[]) {
try {
using namespace kaldi;
typedef kaldi::int32 int32;
const char *usage =
"Determinize lattices, keeping only the best path (sequence of\n"
"acoustic states) for each input-symbol sequence. This is a version\n"
"of lattice-determinize-phone-pruned that accepts the --num-threads\n"
"option. The program does phone insertion when doing a first pass\n"
"determinization, it then removes the inserted symbols and does a\n"
"second pass determinization. It also does pruning as part of the\n"
"determinization algorithm, which is more efficient and prevents\n"
"blowup.\n"
"\n"
"Usage: lattice-determinize-phone-pruned-parallel [options] \\\n"
" <model> <lattice-rspecifier> <lattice-wspecifier>\n"
" e.g.: lattice-determinize-phone-pruned-parallel \\\n"
" --acoustic-scale=0.1 final.mdl ark:in.lats ark:det.lats\n";
ParseOptions po(usage);
BaseFloat acoustic_scale = 1.0;
BaseFloat beam = 10.0;
TaskSequencerConfig sequencer_opts;
fst::DeterminizeLatticePhonePrunedOptions determinize_opts;
determinize_opts.max_mem = 50000000;
po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic"
" likelihoods.");
po.Register("beam", &beam, "Pruning beam [applied after acoustic scaling].");
determinize_opts.Register(&po);
sequencer_opts.Register(&po);
po.Read(argc, argv);
if (po.NumArgs() != 3) {
po.PrintUsage();
exit(1);
}
std::string model_rxfilename = po.GetArg(1),
lats_rspecifier = po.GetArg(2),
lats_wspecifier = po.GetArg(3);
TransitionModel trans_model;
ReadKaldiObject(model_rxfilename, &trans_model);
// Reads as regular lattice-- this is the form the determinization code
// accepts.
SequentialLatticeReader lat_reader(lats_rspecifier);
// Writes as compact lattice.
CompactLatticeWriter compact_lat_writer(lats_wspecifier);
TaskSequencer<DeterminizeLatticeTask> sequencer(sequencer_opts);
int32 n_done = 0, n_warn = 0;
if (acoustic_scale == 0.0)
KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
for (; !lat_reader.Done(); lat_reader.Next()) {
std::string key = lat_reader.Key();
// Will give ownership to "task" below.
Lattice *lat = lat_reader.Value().Copy();
KALDI_VLOG(2) << "Processing lattice " << key;
DeterminizeLatticeTask *task = new DeterminizeLatticeTask(
trans_model, determinize_opts, key, acoustic_scale, beam,
lat, &compact_lat_writer, &n_warn);
sequencer.Run(task);
n_done++;
}
sequencer.Wait();
KALDI_LOG << "Done " << n_done << " lattices, determinization finished "
<< "earlier than specified by the beam on " << n_warn << " of "
<< "these.";
return (n_done != 0 ? 0 : 1);
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}