nnet-get-egs-discriminative.cc File Reference
Include dependency graph for nnet-get-egs-discriminative.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 27 of file nnet-get-egs-discriminative.cc.

References fst::CreateSuperFinal(), SequentialTableReader< Holder >::Done(), kaldi::nnet2::ExciseDiscriminativeExample(), ParseOptions::GetArg(), AmNnet::GetNnet(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, rnnlm::j, KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), kaldi::nnet2::LatticeToDiscriminativeExample(), Nnet::LeftContext(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), SplitExampleStats::Print(), ParseOptions::PrintUsage(), AmNnet::Read(), ParseOptions::Read(), TransitionModel::Read(), SplitDiscriminativeExampleConfig::Register(), Nnet::RightContext(), kaldi::nnet2::SplitDiscriminativeExample(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

27  {
28  try {
29  using namespace kaldi;
30  using namespace kaldi::nnet2;
31  typedef kaldi::int32 int32;
32  typedef kaldi::int64 int64;
33 
34  const char *usage =
35  "Get examples of data for discriminative neural network training;\n"
36  "each one corresponds to part of a file, of variable (and configurable)\n"
37  "length.\n"
38  "\n"
39  "Usage: nnet-get-egs-discriminative [options] <model> "
40  "<features-rspecifier> <ali-rspecifier> <den-lat-rspecifier> "
41  "<training-examples-out>\n"
42  "\n"
43  "An example [where $feats expands to the actual features]:\n"
44  "nnet-get-egs-discriminative --acoustic-scale=0.1 \\\n"
45  " 1.mdl '$feats' 'ark,s,cs:gunzip -c ali.1.gz|' 'ark,s,cs:gunzip -c lat.1.gz|' ark:1.degs\n";
46 
48 
49  ParseOptions po(usage);
50  split_config.Register(&po);
51 
52  po.Read(argc, argv);
53 
54  if (po.NumArgs() != 5) {
55  po.PrintUsage();
56  exit(1);
57  }
58 
59  std::string nnet_rxfilename = po.GetArg(1),
60  feature_rspecifier = po.GetArg(2),
61  ali_rspecifier = po.GetArg(3),
62  clat_rspecifier = po.GetArg(4),
63  examples_wspecifier = po.GetArg(5);
64 
65 
66  TransitionModel trans_model;
67  AmNnet am_nnet;
68  {
69  bool binary;
70  Input ki(nnet_rxfilename, &binary);
71  trans_model.Read(ki.Stream(), binary);
72  am_nnet.Read(ki.Stream(), binary);
73  }
74 
75  int32 left_context = am_nnet.GetNnet().LeftContext(),
76  right_context = am_nnet.GetNnet().RightContext();
77 
78 
79  // Read in all the training files.
80  SequentialBaseFloatMatrixReader feat_reader(feature_rspecifier);
81  RandomAccessInt32VectorReader ali_reader(ali_rspecifier);
82  RandomAccessCompactLatticeReader clat_reader(clat_rspecifier);
83  DiscriminativeNnetExampleWriter example_writer(examples_wspecifier);
84 
85  int32 num_done = 0, num_err = 0;
86  int64 examples_count = 0; // used in generating id's.
87 
88  SplitExampleStats stats; // diagnostic.
89 
90  for (; !feat_reader.Done(); feat_reader.Next()) {
91  std::string key = feat_reader.Key();
92  const Matrix<BaseFloat> &feats = feat_reader.Value();
93  if (!ali_reader.HasKey(key)) {
94  KALDI_WARN << "No pdf-level posterior for key " << key;
95  num_err++;
96  continue;
97  }
98  const std::vector<int32> &alignment = ali_reader.Value(key);
99  if (!clat_reader.HasKey(key)) {
100  KALDI_WARN << "No denominator lattice for key " << key;
101  num_err++;
102  continue;
103  }
104  CompactLattice clat = clat_reader.Value(key);
105  CreateSuperFinal(&clat); // make sure only one state has a final-prob (of One()).
106  if (clat.Properties(fst::kTopSorted, true) == 0) {
107  TopSort(&clat);
108  }
109 
110  BaseFloat weight = 1.0;
112 
113  if (!LatticeToDiscriminativeExample(alignment, feats, clat, weight,
114  left_context, right_context, &eg)) {
115  KALDI_WARN << "Error converting lattice to example.";
116  num_err++;
117  continue;
118  }
119 
120  std::vector<DiscriminativeNnetExample> egs;
121  SplitDiscriminativeExample(split_config, trans_model, eg,
122  &egs, &stats);
123 
124  KALDI_VLOG(2) << "Split lattice " << key << " into "
125  << egs.size() << " pieces.";
126  for (size_t i = 0; i < egs.size(); i++) {
127  // Note: excised_egs will be of size 0 or 1.
128  std::vector<DiscriminativeNnetExample> excised_egs;
129  ExciseDiscriminativeExample(split_config, trans_model, egs[i],
130  &excised_egs, &stats);
131  for (size_t j = 0; j < excised_egs.size(); j++) {
132  std::ostringstream os;
133  os << (examples_count++);
134  std::string example_key = os.str();
135  example_writer.Write(example_key, excised_egs[j]);
136  }
137  }
138  num_done++;
139  }
140 
141  if (num_done > 0) stats.Print();
142 
143  KALDI_LOG << "Finished generating examples, "
144  << "successfully processed " << num_done
145  << " feature files, " << num_err << " had errors.";
146  return (num_done == 0 ? 1 : 0);
147  } catch(const std::exception &e) {
148  std::cerr << e.what() << '\n';
149  return -1;
150  }
151 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 LeftContext() const
Returns the left-context summed over all the Components...
Definition: nnet-nnet.cc:42
void SplitDiscriminativeExample(const SplitDiscriminativeExampleConfig &config, const TransitionModel &tmodel, const DiscriminativeNnetExample &eg, std::vector< DiscriminativeNnetExample > *egs_out, SplitExampleStats *stats_out)
Split a "discriminative example" into multiple pieces, splitting where the lattice has "pinch points"...
void ExciseDiscriminativeExample(const SplitDiscriminativeExampleConfig &config, const TransitionModel &tmodel, const DiscriminativeNnetExample &eg, std::vector< DiscriminativeNnetExample > *egs_out, SplitExampleStats *stats_out)
Remove unnecessary frames from discriminative training example.
void Read(std::istream &is, bool binary)
Definition: am-nnet.cc:39
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
This struct exists only for diagnostic purposes.
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
bool LatticeToDiscriminativeExample(const std::vector< int32 > &alignment, const Matrix< BaseFloat > &feats, const CompactLattice &clat, BaseFloat weight, int32 left_context, int32 right_context, DiscriminativeNnetExample *eg)
Converts lattice to discriminative training example.
void Read(std::istream &is, bool binary)
int32 RightContext() const
Returns the right-context summed over all the Components...
Definition: nnet-nnet.cc:56
Arc::StateId CreateSuperFinal(MutableFst< Arc > *fst)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
This struct is used to store the information we need for discriminative training (MMI or MPE)...
Definition: nnet-example.h:136
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
#define KALDI_LOG
Definition: kaldi-error.h:153
const Nnet & GetNnet() const
Definition: am-nnet.h:61
Config structure for SplitExample, for splitting discriminative training examples.