append-vector-to-feats.cc
Go to the documentation of this file.
1 // featbin/append-vector-to-feats.cc
2 
3 // Copyright 2012 Korbinian Riedhammer
4 // 2013 Brno University of Technology (Author: Karel Vesely)
5 // 2013-2014 Johns Hopkins University (Author: Daniel Povey)
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 
23 #include "base/kaldi-common.h"
24 #include "util/common-utils.h"
25 #include "matrix/kaldi-matrix.h"
26 
27 namespace kaldi {
28 
30  const Vector<BaseFloat> &vec,
31  Matrix<BaseFloat> *out) {
32  // Check inputs,
33  KALDI_ASSERT(in.NumRows() != 0);
34  // Build the output matrix,
35  out->Resize(in.NumRows(), in.NumCols() + vec.Dim());
36  out->ColRange(0, in.NumCols()).CopyFromMat(in);
37  out->ColRange(in.NumCols(), vec.Dim()).CopyRowsFromVec(vec);
38 }
39 
40 } // namespace kaldi,
41 
42 int main(int argc, char *argv[]) {
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 }
123 
124 /*
125  Testing:
126 
127 cat <<EOF >1.mat
128 [ 0 1 2
129  3 4 5
130  8 9 10 ]
131 EOF
132 cat <<EOF > 2.vec
133  [ 0 1 ]
134 EOF
135 append-vector-to-feats --binary=false 1.mat 2.vec 3a.mat
136 cat <<EOF > 3b.mat
137  [ 0 1 2 0 1
138  3 4 5 0 1
139  8 9 10 0 1 ]
140 EOF
141 cmp <(../bin/copy-matrix 3b.mat -) <(../bin/copy-matrix 3a.mat -) || echo 'Bad!'
142 
143 append-vector-to-feats 'scp:echo foo 1.mat|' 'scp:echo foo 2.vec|' 'scp,t:echo foo 3a.mat|'
144 cmp <(../bin/copy-matrix 3b.mat -) <(../bin/copy-matrix 3a.mat -) || echo 'Bad!'
145 
146 rm {1,3?}.mat 2.vec
147  */
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
SubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Definition: kaldi-matrix.h:213
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
const T & Value(const std::string &key)
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_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.
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
bool HasKey(const std::string &key)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
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)
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
int main(int argc, char *argv[])
#define KALDI_LOG
Definition: kaldi-error.h:153