nnet3-get-egs-simple.cc
Go to the documentation of this file.
1 // nnet3bin/nnet3-get-egs-simple.cc
2 
3 // Copyright 2017 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include <sstream>
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "hmm/transition-model.h"
24 #include "hmm/posterior.h"
25 #include "nnet3/nnet-example.h"
27 
28 namespace kaldi {
29 
30 // This function attempts to split a command-line argument of the
31 // form 'name=rspecifier', on the first equals sign. Will
32 // call KALDI_ERR if something seems wrong.
33 void SplitArgOnEquals(const std::string &arg,
34  std::string *name,
35  std::string *rspecifier) {
36  size_t pos = arg.find_first_of('=');
37  if (pos == std::string::npos) {
38  KALDI_ERR << "Bad command line argument (expecting '='): "
39  << arg;
40  }
41  *name = std::string(arg, 0, pos);
42  *rspecifier = std::string(arg, pos + 1);
43  if (!IsToken(*name)) {
44  KALDI_ERR << "Bad command line argument (expecting a valid name before '='): "
45  << arg;
46  }
47  if (ClassifyRspecifier(*rspecifier, NULL, NULL) == kNoRspecifier) {
48  KALDI_ERR << "Bad command line argument (expecting an rspecifier after '='): "
49  << arg;
50  }
51 }
52 
53 } // namespace kaldi
54 
55 int main(int argc, char *argv[]) {
56  try {
57  using namespace kaldi;
58  using namespace kaldi::nnet3;
59  typedef kaldi::int32 int32;
60  typedef kaldi::int64 int64;
61 
62  const char *usage =
63  "Get frame-by-frame examples of data for nnet3 neural network training.\n"
64  "This is like nnet3-get-egs, but does not split up its inputs into pieces\n"
65  "and allows more general generation of egs. E.g. this is usable for image\n"
66  "recognition tasks.\n"
67  "\n"
68  "Usage: nnet3-get-egs-simple [options] <name1>=<rspecifier1> "
69  "<name2>=<rspecifier2> ...\n"
70  "\n"
71  "e.g.:\n"
72  "nnet3-get-egs-simple input=scp:images.scp \\\n"
73  "output='ark,o:ali-to-post ark:labels.txt ark:- | post-to-smat --dim=10 ark:- ark:-' ark:egs.ark\n"
74  "\n"
75  "See also: nnet3-get-egs\n";
76 
77 
78  ParseOptions po(usage);
79 
80 
81  po.Read(argc, argv);
82 
83  if (po.NumArgs() < 2) {
84  po.PrintUsage();
85  exit(1);
86  }
87 
88 
89  std::string examples_wspecifier = po.GetArg(po.NumArgs());
90  NnetExampleWriter example_writer(examples_wspecifier);
91 
92 
93  std::string first_name, first_reader_name;
94  SplitArgOnEquals(po.GetArg(1), &first_name, &first_reader_name);
95  SequentialGeneralMatrixReader first_reader(first_reader_name);
96 
97  std::vector<std::string> other_names;
98 
99  std::vector<RandomAccessGeneralMatrixReader*> other_readers;
100 
101  for (int32 i = 2; i < po.NumArgs(); i++) {
102  std::string name, rspecifier;
103  SplitArgOnEquals(po.GetArg(i), &name, &rspecifier);
104  other_names.push_back(name);
105  other_readers.push_back(new RandomAccessGeneralMatrixReader(rspecifier));
106  }
107 
108  int32 num_done = 0, num_err = 0;
109 
110  for (; !first_reader.Done(); first_reader.Next()) {
111  std::string key = first_reader.Key();
112  NnetExample eg;
113  const GeneralMatrix &feats = first_reader.Value();
114  int32 t = 0; // first 't' value; each row of the matrix gets
115  // its own 't' value.
116  eg.io.push_back(NnetIo(first_name, t, feats));
117  bool all_ok = true;
118  for (size_t i = 0; i < other_names.size(); i++) {
119  if (!other_readers[i]->HasKey(key)) {
120  KALDI_WARN << "Could not find input for key " << key
121  << " for io-name=" << other_names[i];
122  all_ok = false;
123  break;
124  }
125  const GeneralMatrix &other_feats = other_readers[i]->Value(key);
126  eg.io.push_back(NnetIo(other_names[i], t, other_feats));
127  }
128  if (!all_ok) {
129  num_err++;
130  } else {
131  example_writer.Write(key, eg);
132  num_done++;
133  }
134  }
135  if (num_err > 0)
136  KALDI_WARN << num_err << " utterances had errors and could "
137  "not be processed.";
138  return (num_done > 0 ? 0 : 1);
139  } catch(const std::exception &e) {
140  std::cerr << e.what() << '\n';
141  return -1;
142  }
143 }
NnetExample is the input data and corresponding label (or labels) for one or more frames of input...
Definition: nnet-example.h:111
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
This class is a wrapper that enables you to store a matrix in one of three forms: either as a Matrix<...
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void SplitArgOnEquals(const std::string &arg, std::string *name, std::string *rspecifier)
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
bool IsToken(const std::string &token)
Returns true if "token" is nonempty, and all characters are printable and whitespace-free.
Definition: text-utils.cc:105
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
int main(int argc, char *argv[])
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
RandomAccessTableReader< KaldiObjectHolder< GeneralMatrix > > RandomAccessGeneralMatrixReader
Definition: table-types.h:177
std::vector< NnetIo > io
"io" contains the input and output.
Definition: nnet-example.h:116