copy-feats.cc File Reference
Include dependency graph for copy-feats.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 26 of file copy-feats.cc.

References kaldi::ClassifyRspecifier(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_ASSERT, KALDI_ERR, KALDI_LOG, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), kaldi::PrintableRxfilename(), kaldi::PrintableWxfilename(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadHtk(), kaldi::ReadKaldiObject(), ParseOptions::Register(), Input::Stream(), SequentialTableReader< Holder >::Value(), TableWriter< Holder >::Write(), and kaldi::WriteKaldiObject().

26  {
27  try {
28  using namespace kaldi;
29 
30  const char *usage =
31  "Copy features [and possibly change format]\n"
32  "Usage: copy-feats [options] <feature-rspecifier> <feature-wspecifier>\n"
33  "or: copy-feats [options] <feats-rxfilename> <feats-wxfilename>\n"
34  "e.g.: copy-feats ark:- ark,scp:foo.ark,foo.scp\n"
35  " or: copy-feats ark:foo.ark ark,t:txt.ark\n"
36  "See also: copy-matrix, copy-feats-to-htk, copy-feats-to-sphinx, select-feats,\n"
37  "extract-feature-segments, subset-feats, subsample-feats, splice-feats, paste-feats,\n"
38  "concat-feats\n";
39 
40  ParseOptions po(usage);
41  bool binary = true;
42  bool htk_in = false;
43  bool sphinx_in = false;
44  bool compress = false;
45  int32 compression_method_in = 1;
46  std::string num_frames_wspecifier;
47  po.Register("htk-in", &htk_in, "Read input as HTK features");
48  po.Register("sphinx-in", &sphinx_in, "Read input as Sphinx features");
49  po.Register("binary", &binary, "Binary-mode output (not relevant if writing "
50  "to archive)");
51  po.Register("compress", &compress, "If true, write output in compressed form"
52  "(only currently supported for wxfilename, i.e. archive/script,"
53  "output)");
54  po.Register("compression-method", &compression_method_in,
55  "Only relevant if --compress=true; the method (1 through 7) to "
56  "compress the matrix. Search for CompressionMethod in "
57  "src/matrix/compressed-matrix.h.");
58  po.Register("write-num-frames", &num_frames_wspecifier,
59  "Wspecifier to write length in frames of each utterance. "
60  "e.g. 'ark,t:utt2num_frames'. Only applicable if writing tables, "
61  "not when this program is writing individual files. See also "
62  "feat-to-len.");
63 
64  po.Read(argc, argv);
65 
66  if (po.NumArgs() != 2) {
67  po.PrintUsage();
68  exit(1);
69  }
70 
71  int32 num_done = 0;
72 
73  CompressionMethod compression_method = static_cast<CompressionMethod>(
74  compression_method_in);
75 
76  if (ClassifyRspecifier(po.GetArg(1), NULL, NULL) != kNoRspecifier) {
77  // Copying tables of features.
78  std::string rspecifier = po.GetArg(1);
79  std::string wspecifier = po.GetArg(2);
80  Int32Writer num_frames_writer(num_frames_wspecifier);
81 
82  if (!compress) {
83  BaseFloatMatrixWriter kaldi_writer(wspecifier);
84  if (htk_in) {
85  SequentialTableReader<HtkMatrixHolder> htk_reader(rspecifier);
86  for (; !htk_reader.Done(); htk_reader.Next(), num_done++) {
87  kaldi_writer.Write(htk_reader.Key(), htk_reader.Value().first);
88  if (!num_frames_wspecifier.empty())
89  num_frames_writer.Write(htk_reader.Key(),
90  htk_reader.Value().first.NumRows());
91  }
92  } else if (sphinx_in) {
93  SequentialTableReader<SphinxMatrixHolder<> > sphinx_reader(rspecifier);
94  for (; !sphinx_reader.Done(); sphinx_reader.Next(), num_done++) {
95  kaldi_writer.Write(sphinx_reader.Key(), sphinx_reader.Value());
96  if (!num_frames_wspecifier.empty())
97  num_frames_writer.Write(sphinx_reader.Key(),
98  sphinx_reader.Value().NumRows());
99  }
100  } else {
101  SequentialBaseFloatMatrixReader kaldi_reader(rspecifier);
102  for (; !kaldi_reader.Done(); kaldi_reader.Next(), num_done++) {
103  kaldi_writer.Write(kaldi_reader.Key(), kaldi_reader.Value());
104  if (!num_frames_wspecifier.empty())
105  num_frames_writer.Write(kaldi_reader.Key(),
106  kaldi_reader.Value().NumRows());
107  }
108  }
109  } else {
110  CompressedMatrixWriter kaldi_writer(wspecifier);
111  if (htk_in) {
112  SequentialTableReader<HtkMatrixHolder> htk_reader(rspecifier);
113  for (; !htk_reader.Done(); htk_reader.Next(), num_done++) {
114  kaldi_writer.Write(htk_reader.Key(),
115  CompressedMatrix(htk_reader.Value().first,
116  compression_method));
117  if (!num_frames_wspecifier.empty())
118  num_frames_writer.Write(htk_reader.Key(),
119  htk_reader.Value().first.NumRows());
120  }
121  } else if (sphinx_in) {
122  SequentialTableReader<SphinxMatrixHolder<> > sphinx_reader(rspecifier);
123  for (; !sphinx_reader.Done(); sphinx_reader.Next(), num_done++) {
124  kaldi_writer.Write(sphinx_reader.Key(),
125  CompressedMatrix(sphinx_reader.Value(),
126  compression_method));
127  if (!num_frames_wspecifier.empty())
128  num_frames_writer.Write(sphinx_reader.Key(),
129  sphinx_reader.Value().NumRows());
130  }
131  } else {
132  SequentialBaseFloatMatrixReader kaldi_reader(rspecifier);
133  for (; !kaldi_reader.Done(); kaldi_reader.Next(), num_done++) {
134  kaldi_writer.Write(kaldi_reader.Key(),
135  CompressedMatrix(kaldi_reader.Value(),
136  compression_method));
137  if (!num_frames_wspecifier.empty())
138  num_frames_writer.Write(kaldi_reader.Key(),
139  kaldi_reader.Value().NumRows());
140  }
141  }
142  }
143  KALDI_LOG << "Copied " << num_done << " feature matrices.";
144  return (num_done != 0 ? 0 : 1);
145  } else {
146  KALDI_ASSERT(!compress && "Compression not yet supported for single files");
147  if (!num_frames_wspecifier.empty())
148  KALDI_ERR << "--write-num-frames option not supported when writing/reading "
149  << "single files.";
150 
151  std::string feat_rxfilename = po.GetArg(1), feat_wxfilename = po.GetArg(2);
152 
153  Matrix<BaseFloat> feat_matrix;
154  if (htk_in) {
155  Input ki(feat_rxfilename); // Doesn't look for read binary header \0B, because
156  // no bool* pointer supplied.
157  HtkHeader header; // we discard this info.
158  ReadHtk(ki.Stream(), &feat_matrix, &header);
159  } else if (sphinx_in) {
160  KALDI_ERR << "For single files, sphinx input is not yet supported.";
161  } else {
162  ReadKaldiObject(feat_rxfilename, &feat_matrix);
163  }
164  WriteKaldiObject(feat_matrix, feat_wxfilename, binary);
165  KALDI_LOG << "Copied features from " << PrintableRxfilename(feat_rxfilename)
166  << " to " << PrintableWxfilename(feat_wxfilename);
167  }
168  } catch(const std::exception &e) {
169  std::cerr << e.what();
170  return -1;
171  }
172 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
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_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73
#define KALDI_LOG
Definition: kaldi-error.h:153
bool ReadHtk(std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
Extension of the HTK header.
A structure containing the HTK header.
Definition: kaldi-matrix.h:955