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

Go to the source code of this file.

Namespaces

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

Functions

void AppendVectorToFeats (const Matrix< BaseFloat > &in, const Vector< BaseFloat > &vec, Matrix< BaseFloat > *out)
 
int main (int argc, char *argv[])
 

Function Documentation

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

Definition at line 42 of file append-vector-to-feats.cc.

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

42  {
43  try {
44  using namespace kaldi;
45  using namespace std;
46 
47  const char *usage =
48  "Append a vector to each row of input feature files\n"
49  "\n"
50  "Usage: append-vector-to-feats <in-rspecifier1> <in-rspecifier2> <out-wspecifier>\n"
51  " or: append-vector-to-feats <in-rxfilename1> <in-rxfilename2> <out-wxfilename>\n"
52  "See also: paste-feats, concat-feats\n";
53 
54  ParseOptions po(usage);
55 
56  bool binary = true;
57  po.Register("binary", &binary, "If true, output files in binary "
58  "(only relevant for single-file operation, i.e. no tables)");
59 
60  po.Read(argc, argv);
61 
62  if (po.NumArgs() != 3) {
63  po.PrintUsage();
64  exit(1);
65  }
66 
67  if (ClassifyRspecifier(po.GetArg(1), NULL, NULL)
68  != kNoRspecifier) {
69  // We're operating on tables, e.g. archives.
70 
71 
72  string feat_rspecifier = po.GetArg(1);
73  SequentialBaseFloatMatrixReader feat_reader(feat_rspecifier);
74 
75  string vec_rspecifier = po.GetArg(2);
76  RandomAccessBaseFloatVectorReader vec_reader(vec_rspecifier);
77 
78  string wspecifier = po.GetArg(3);
79  BaseFloatMatrixWriter feat_writer(wspecifier);
80 
81  int32 num_done = 0, num_err = 0;
82  // Main loop
83  for (; !feat_reader.Done(); feat_reader.Next()) {
84  string utt = feat_reader.Key();
85  KALDI_VLOG(2) << "Processing utterance " << utt;
86 
87  const Matrix<BaseFloat> &feats(feat_reader.Value());
88 
89  if (!vec_reader.HasKey(utt)) {
90  KALDI_WARN << "Could not read vector for utterance " << utt;
91  num_err++;
92  continue;
93  }
94  const Vector<BaseFloat> &vec(vec_reader.Value(utt));
95 
96  Matrix<BaseFloat> output;
97  AppendVectorToFeats(feats, vec, &output);
98  feat_writer.Write(utt, output);
99  num_done++;
100  }
101  KALDI_LOG << "Done " << num_done << " utts, errors on "
102  << num_err;
103 
104  return (num_done == 0 ? -1 : 0);
105  } else {
106  // We're operating on rxfilenames|wxfilenames, most likely files.
107  Matrix<BaseFloat> mat;
108  ReadKaldiObject(po.GetArg(1), &mat);
109  Vector<BaseFloat> vec;
110  ReadKaldiObject(po.GetArg(2), &vec);
111  Matrix<BaseFloat> output;
112  AppendVectorToFeats(mat, vec, &output);
113  std::string output_wxfilename = po.GetArg(3);
114  WriteKaldiObject(output, output_wxfilename, binary);
115  KALDI_LOG << "Wrote appended features to " << output_wxfilename;
116  return 0;
117  }
118  } catch(const std::exception &e) {
119  std::cerr << e.what();
120  return -1;
121  }
122 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
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
#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
void AppendVectorToFeats(const Matrix< BaseFloat > &in, const Vector< BaseFloat > &vec, Matrix< BaseFloat > *out)
#define KALDI_LOG
Definition: kaldi-error.h:133