All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
paste-feats.cc File Reference
Include dependency graph for paste-feats.cc:

Go to the source code of this file.

Namespaces

 kaldi
 Relabels neural network egs with the read pdf-id alignments.
 

Functions

bool AppendFeats (const std::vector< Matrix< BaseFloat > > &in, std::string utt, int32 tolerance, Matrix< BaseFloat > *out)
 
int main (int argc, char *argv[])
 

Function Documentation

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

Definition at line 70 of file paste-feats.cc.

References kaldi::AppendFeats(), kaldi::ClassifyRspecifier(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), rnnlm::i, KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), SequentialTableReader< Holder >::Value(), TableWriter< Holder >::Write(), and kaldi::WriteKaldiObject().

70  {
71  try {
72  using namespace kaldi;
73  using namespace std;
74 
75  const char *usage =
76  "Paste feature files (assuming they have about the same durations,\n"
77  "see --length-tolerance), appending the features on each frame;\n"
78  "think of the unix command 'paste'.\n"
79  "Usage: paste-feats <in-rspecifier1> <in-rspecifier2> [<in-rspecifier3> ...] <out-wspecifier>\n"
80  " or: paste-feats <in-rxfilename1> <in-rxfilename2> [<in-rxfilename3> ...] <out-wxfilename>\n"
81  " e.g. paste-feats ark:feats1.ark \"ark:select-feats 0-3 ark:feats2.ark ark:- |\" ark:feats-out.ark\n"
82  " or: paste-feats foo.mat bar.mat baz.mat\n"
83  "See also: copy-feats, copy-matrix, append-vector-to-feats, concat-feats\n";
84 
85  ParseOptions po(usage);
86 
87  int32 length_tolerance = 0;
88  bool binary = true;
89  po.Register("length-tolerance", &length_tolerance,
90  "If length is different, trim as shortest up to a frame "
91  " difference of length-tolerance, otherwise exclude segment.");
92  po.Register("binary", &binary, "If true, output files in binary "
93  "(only relevant for single-file operation, i.e. no tables)");
94 
95  po.Read(argc, argv);
96 
97  if (po.NumArgs() < 3) {
98  po.PrintUsage();
99  exit(1);
100  }
101 
102  if (ClassifyRspecifier(po.GetArg(1), NULL, NULL)
103  != kNoRspecifier) {
104  // We're operating on tables, e.g. archives.
105 
106  // Last argument is output
107  string wspecifier = po.GetArg(po.NumArgs());
108  BaseFloatMatrixWriter feat_writer(wspecifier);
109 
110  // First input is sequential
111  string rspecifier1 = po.GetArg(1);
112  SequentialBaseFloatMatrixReader input1(rspecifier1);
113 
114  // Assemble vector of other input readers (with random-access)
115  vector<RandomAccessBaseFloatMatrixReader *> input;
116  for (int32 i = 2; i < po.NumArgs(); i++) {
117  string rspecifier = po.GetArg(i);
119  input.push_back(rd);
120  }
121 
122  int32 num_done = 0, num_err = 0;
123 
124  // Main loop
125  for (; !input1.Done(); input1.Next()) {
126  string utt = input1.Key();
127  KALDI_VLOG(2) << "Merging features for utterance " << utt;
128 
129  // Collect features from streams to vector 'feats'
130  vector<Matrix<BaseFloat> > feats(po.NumArgs() - 1);
131  feats[0] = input1.Value();
132  int32 i;
133  for (i = 0; i < static_cast<int32>(input.size()); i++) {
134  if (input[i]->HasKey(utt)) {
135  feats[i + 1] = input[i]->Value(utt);
136  } else {
137  KALDI_WARN << "Missing utt " << utt << " from input "
138  << po.GetArg(i+2);
139  num_err++;
140  break;
141  }
142  }
143  if (i != static_cast<int32>(input.size()))
144  continue;
145  Matrix<BaseFloat> output;
146  if (!AppendFeats(feats, utt, length_tolerance, &output)) {
147  num_err++;
148  continue; // it will have printed a warning.
149  }
150  feat_writer.Write(utt, output);
151  num_done++;
152  }
153 
154  for (int32 i=0; i < input.size(); i++)
155  delete input[i];
156  input.clear();
157 
158  KALDI_LOG << "Done " << num_done << " utts, errors on "
159  << num_err;
160 
161  return (num_done == 0 ? -1 : 0);
162  } else {
163  // We're operating on rxfilenames|wxfilenames, most likely files.
164  std::vector<Matrix<BaseFloat> > feats(po.NumArgs() - 1);
165  for (int32 i = 1; i < po.NumArgs(); i++)
166  ReadKaldiObject(po.GetArg(i), &(feats[i-1]));
167  Matrix<BaseFloat> output;
168  if (!AppendFeats(feats, "", length_tolerance, &output))
169  return 1; // it will have printed a warning.
170  std::string output_wxfilename = po.GetArg(po.NumArgs());
171  WriteKaldiObject(output, output_wxfilename, binary);
172  KALDI_LOG << "Wrote appended features to " << output_wxfilename;
173  return 0;
174  }
175  } catch(const std::exception &e) {
176  std::cerr << e.what();
177  return -1;
178  }
179 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
RandomAccessTableReader< KaldiObjectHolder< Matrix< BaseFloat > > > RandomAccessBaseFloatMatrixReader
Definition: table-types.h:41
bool AppendFeats(const std::vector< Matrix< BaseFloat > > &in, std::string utt, int32 tolerance, Matrix< BaseFloat > *out)
Definition: paste-feats.cc:30
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
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
void Write(std::ostream &out, bool binary) const
write to stream.
#define KALDI_WARN
Definition: kaldi-error.h:130
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *wxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
#define KALDI_LOG
Definition: kaldi-error.h:133