All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
select-feats.cc File Reference
#include <sstream>
#include <algorithm>
#include <iterator>
#include <utility>
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "matrix/kaldi-matrix.h"
Include dependency graph for select-feats.cc:

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 30 of file select-feats.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), rnnlm::i, KALDI_ERR, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), MatrixBase< Real >::Range(), ParseOptions::Read(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

30  {
31  try {
32  using namespace kaldi;
33  using namespace std;
34 
35  const char *usage =
36  "Select certain dimensions of the feature file; think of it as the unix\n"
37  "command cut -f ...\n"
38  "Usage: select-feats <selection> <in-rspecifier> <out-wspecifier>\n"
39  " e.g. select-feats 0,24-22,3-12 scp:feats.scp ark,scp:feat-red.ark,feat-red.scp\n"
40  "See also copy-feats, extract-rows, subset-feats, subsample-feats\n";
41 
42  ParseOptions po(usage);
43 
44  po.Read(argc, argv);
45 
46  if (po.NumArgs() != 3) {
47  po.PrintUsage();
48  exit(1);
49  }
50 
51  string sspecifier = po.GetArg(1);
52  string rspecifier = po.GetArg(2);
53  string wspecifier = po.GetArg(3);
54 
55  // set up input (we'll need that to validate the selected indices)
56  SequentialBaseFloatMatrixReader kaldi_reader(rspecifier);
57 
58  if (kaldi_reader.Done()) {
59  KALDI_WARN << "Empty archive provided.";
60  return 0;
61  }
62 
63  int32 dim_in = kaldi_reader.Value().NumCols();
64  int32 dim_out = 0;
65 
66  // figure out the selected dimensions
67  istringstream iss(sspecifier);
68  string token;
69  vector<pair<int32, int32> > ranges;
70  vector<int32> offsets;
71  while (getline(iss, token, ',')) {
72  size_t p = token.find('-');
73  if (p != string::npos) {
74  int s, e;
75  istringstream(token.substr(0, token.length() - p - 1)) >> s;
76  istringstream(token.substr(p+1)) >> e;
77 
78  if (s < 0 || s > (dim_in-1)) {
79  KALDI_ERR << "Invalid range start: " << s;
80  return 1;
81  } else if (e < 0 || e > (dim_in-1)) {
82  KALDI_ERR << "Invalid range end: " << e;
83  return 1;
84  }
85 
86  // reverse range? make individual selections
87  if (s > e) {
88  for (int32 i = s; i >= e; --i) {
89  ranges.push_back(pair<int32, int32>(i, i));
90  offsets.push_back(dim_out);
91  dim_out += 1;
92  }
93  } else {
94  ranges.push_back(pair<int32, int32>(s, e));
95  offsets.push_back(dim_out);
96  dim_out += (e - s + 1);
97  }
98  } else {
99  int i;
100  istringstream(token) >> i;
101 
102  if (i < 0 || i > (dim_in - 1)) {
103  KALDI_ERR << "Invalid selection index: " << i;
104  return 1;
105  }
106 
107  ranges.push_back(pair<int32, int32>(i, i));
108  offsets.push_back(dim_out);
109  dim_out += 1;
110  }
111  }
112 
113  if (ranges.size() < 1) {
114  KALDI_ERR << "No ranges or indices in selection string!";
115  return 1;
116  }
117 
118  // set up output
119  BaseFloatMatrixWriter kaldi_writer(wspecifier);
120 
121  // process all keys
122  for (; !kaldi_reader.Done(); kaldi_reader.Next()) {
123  Matrix<BaseFloat> feats(kaldi_reader.Value().NumRows(), dim_out);
124 
125  // extract the desired ranges
126  for (int32 i = 0; i < ranges.size(); ++i) {
127  int32 f = ranges[i].first;
128  int32 ncol = ranges[i].second - f + 1;
129 
130  feats.Range(0, feats.NumRows(), offsets[i], ncol)
131  .CopyFromMat(kaldi_reader.Value().Range(0, feats.NumRows(), f, ncol));
132  }
133 
134  kaldi_writer.Write(kaldi_reader.Key(), feats);
135  }
136 
137  return 0;
138  } catch(const std::exception &e) {
139  std::cerr << e.what();
140  return -1;
141  }
142 }
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
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:127
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:196
#define KALDI_WARN
Definition: kaldi-error.h:130