concat-feats.cc
3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// featbin/concat-feats.cc
// Copyright 2013 Johns Hopkins University (Author: Daniel Povey)
// 2015 Tom Ko
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "matrix/kaldi-matrix.h"
namespace kaldi {
/*
This function concatenates several sets of feature vectors
to form a longer set. The length of the output will be equal
to the sum of lengths of the inputs but the dimension will be
the same to the inputs.
*/
void ConcatFeats(const std::vector<Matrix<BaseFloat> > &in,
Matrix<BaseFloat> *out) {
KALDI_ASSERT(in.size() >= 1);
int32 tot_len = in[0].NumRows(),
dim = in[0].NumCols();
for (int32 i = 1; i < in.size(); i++) {
KALDI_ASSERT(in[i].NumCols() == dim);
tot_len += in[i].NumRows();
}
out->Resize(tot_len, dim);
int32 len_offset = 0;
for (int32 i = 0; i < in.size(); i++) {
int32 this_len = in[i].NumRows();
out->Range(len_offset, this_len, 0, dim).CopyFromMat(
in[i]);
len_offset += this_len;
}
}
}
int main(int argc, char *argv[]) {
try {
using namespace kaldi;
using namespace std;
const char *usage =
"Concatenate feature files (assuming they have the same dimensions),\n"
"so the output file has the sum of the num-frames of the inputs.\n"
"Usage: concat-feats <in-rxfilename1> <in-rxfilename2> [<in-rxfilename3> ...] <out-wxfilename>\n"
" e.g. concat-feats mfcc/foo.ark:12343 mfcc/foo.ark:56789 -\n"
"See also: copy-feats, append-vector-to-feats, paste-feats\n";
ParseOptions po(usage);
bool binary = true;
po.Register("binary", &binary, "If true, output files in binary "
"(only relevant for single-file operation, i.e. no tables)");
po.Read(argc, argv);
if (po.NumArgs() < 3) {
po.PrintUsage();
exit(1);
}
std::vector<Matrix<BaseFloat> > feats(po.NumArgs() - 1);
for (int32 i = 1; i < po.NumArgs(); i++)
ReadKaldiObject(po.GetArg(i), &(feats[i-1]));
Matrix<BaseFloat> output;
ConcatFeats(feats, &output);
std::string output_wxfilename = po.GetArg(po.NumArgs());
WriteKaldiObject(output, output_wxfilename, binary);
// This will tend to produce too much output if we have a logging mesage.
// KALDI_LOG << "Wrote concatenated features to " << output_wxfilename;
return 0;
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}