copy-feats.cc
7.5 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// featbin/copy-feats.cc
// Copyright 2009-2011 Microsoft Corporation
// 2013 Johns Hopkins University (author: Daniel Povey)
// 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"
int main(int argc, char *argv[]) {
try {
using namespace kaldi;
const char *usage =
"Copy features [and possibly change format]\n"
"Usage: copy-feats [options] <feature-rspecifier> <feature-wspecifier>\n"
"or: copy-feats [options] <feats-rxfilename> <feats-wxfilename>\n"
"e.g.: copy-feats ark:- ark,scp:foo.ark,foo.scp\n"
" or: copy-feats ark:foo.ark ark,t:txt.ark\n"
"See also: copy-matrix, copy-feats-to-htk, copy-feats-to-sphinx, select-feats,\n"
"extract-feature-segments, subset-feats, subsample-feats, splice-feats, paste-feats,\n"
"concat-feats\n";
ParseOptions po(usage);
bool binary = true;
bool htk_in = false;
bool sphinx_in = false;
bool compress = false;
int32 compression_method_in = 1;
std::string num_frames_wspecifier;
po.Register("htk-in", &htk_in, "Read input as HTK features");
po.Register("sphinx-in", &sphinx_in, "Read input as Sphinx features");
po.Register("binary", &binary, "Binary-mode output (not relevant if writing "
"to archive)");
po.Register("compress", &compress, "If true, write output in compressed form"
"(only currently supported for wxfilename, i.e. archive/script,"
"output)");
po.Register("compression-method", &compression_method_in,
"Only relevant if --compress=true; the method (1 through 7) to "
"compress the matrix. Search for CompressionMethod in "
"src/matrix/compressed-matrix.h.");
po.Register("write-num-frames", &num_frames_wspecifier,
"Wspecifier to write length in frames of each utterance. "
"e.g. 'ark,t:utt2num_frames'. Only applicable if writing tables, "
"not when this program is writing individual files. See also "
"feat-to-len.");
po.Read(argc, argv);
if (po.NumArgs() != 2) {
po.PrintUsage();
exit(1);
}
int32 num_done = 0;
CompressionMethod compression_method = static_cast<CompressionMethod>(
compression_method_in);
if (ClassifyRspecifier(po.GetArg(1), NULL, NULL) != kNoRspecifier) {
// Copying tables of features.
std::string rspecifier = po.GetArg(1);
std::string wspecifier = po.GetArg(2);
Int32Writer num_frames_writer(num_frames_wspecifier);
if (!compress) {
BaseFloatMatrixWriter kaldi_writer(wspecifier);
if (htk_in) {
SequentialTableReader<HtkMatrixHolder> htk_reader(rspecifier);
for (; !htk_reader.Done(); htk_reader.Next(), num_done++) {
kaldi_writer.Write(htk_reader.Key(), htk_reader.Value().first);
if (!num_frames_wspecifier.empty())
num_frames_writer.Write(htk_reader.Key(),
htk_reader.Value().first.NumRows());
}
} else if (sphinx_in) {
SequentialTableReader<SphinxMatrixHolder<> > sphinx_reader(rspecifier);
for (; !sphinx_reader.Done(); sphinx_reader.Next(), num_done++) {
kaldi_writer.Write(sphinx_reader.Key(), sphinx_reader.Value());
if (!num_frames_wspecifier.empty())
num_frames_writer.Write(sphinx_reader.Key(),
sphinx_reader.Value().NumRows());
}
} else {
SequentialBaseFloatMatrixReader kaldi_reader(rspecifier);
for (; !kaldi_reader.Done(); kaldi_reader.Next(), num_done++) {
kaldi_writer.Write(kaldi_reader.Key(), kaldi_reader.Value());
if (!num_frames_wspecifier.empty())
num_frames_writer.Write(kaldi_reader.Key(),
kaldi_reader.Value().NumRows());
}
}
} else {
CompressedMatrixWriter kaldi_writer(wspecifier);
if (htk_in) {
SequentialTableReader<HtkMatrixHolder> htk_reader(rspecifier);
for (; !htk_reader.Done(); htk_reader.Next(), num_done++) {
kaldi_writer.Write(htk_reader.Key(),
CompressedMatrix(htk_reader.Value().first,
compression_method));
if (!num_frames_wspecifier.empty())
num_frames_writer.Write(htk_reader.Key(),
htk_reader.Value().first.NumRows());
}
} else if (sphinx_in) {
SequentialTableReader<SphinxMatrixHolder<> > sphinx_reader(rspecifier);
for (; !sphinx_reader.Done(); sphinx_reader.Next(), num_done++) {
kaldi_writer.Write(sphinx_reader.Key(),
CompressedMatrix(sphinx_reader.Value(),
compression_method));
if (!num_frames_wspecifier.empty())
num_frames_writer.Write(sphinx_reader.Key(),
sphinx_reader.Value().NumRows());
}
} else {
SequentialBaseFloatMatrixReader kaldi_reader(rspecifier);
for (; !kaldi_reader.Done(); kaldi_reader.Next(), num_done++) {
kaldi_writer.Write(kaldi_reader.Key(),
CompressedMatrix(kaldi_reader.Value(),
compression_method));
if (!num_frames_wspecifier.empty())
num_frames_writer.Write(kaldi_reader.Key(),
kaldi_reader.Value().NumRows());
}
}
}
KALDI_LOG << "Copied " << num_done << " feature matrices.";
return (num_done != 0 ? 0 : 1);
} else {
KALDI_ASSERT(!compress && "Compression not yet supported for single files");
if (!num_frames_wspecifier.empty())
KALDI_ERR << "--write-num-frames option not supported when writing/reading "
<< "single files.";
std::string feat_rxfilename = po.GetArg(1), feat_wxfilename = po.GetArg(2);
Matrix<BaseFloat> feat_matrix;
if (htk_in) {
Input ki(feat_rxfilename); // Doesn't look for read binary header \0B, because
// no bool* pointer supplied.
HtkHeader header; // we discard this info.
ReadHtk(ki.Stream(), &feat_matrix, &header);
} else if (sphinx_in) {
KALDI_ERR << "For single files, sphinx input is not yet supported.";
} else {
ReadKaldiObject(feat_rxfilename, &feat_matrix);
}
WriteKaldiObject(feat_matrix, feat_wxfilename, binary);
KALDI_LOG << "Copied features from " << PrintableRxfilename(feat_rxfilename)
<< " to " << PrintableWxfilename(feat_wxfilename);
}
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}