sgmm2-project.cc
3.94 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
// sgmm2bin/sgmm2-project.cc
// Copyright 2012 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 "util/kaldi-thread.h"
#include "hmm/transition-model.h"
#include "sgmm2/am-sgmm2-project.h"
int main(int argc, char *argv[]) {
try {
using namespace kaldi;
typedef kaldi::int32 int32;
const char *usage =
"Compute SGMM model projection that only models a part of a pre-LDA space.\n"
"Used in predictive SGMMs. Takes as input an LDA+MLLT transform,\n"
"and outputs a transform from the pre-LDA+MLLT space to the space that\n"
"we want to model\n"
"Usage: sgmm2-project [options] <model-in> <lda-mllt-mat-in> <model-out> <new-projection-out>\n"
"e.g.: sgmm2-project --start-dim=0 --end-dim=52 final.mdl final.inv_full_mat final_proj1.mdl proj1.mat\n";
std::string write_flags_str = "gsnu";
bool binary_write = false;
int32 start_dim = 0;
int32 end_dim = 0;
ParseOptions po(usage);
po.Register("binary", &binary_write, "Write output in binary mode");
po.Register("start-dim", &start_dim, "Starting dimension to keep in "
"pre-LDA-MLLT space.");
po.Register("end-dim", &end_dim, "Ending dimension to keep in "
"pre-LDA-MLLT space (equals last retained dimension plus one)");
po.Read(argc, argv);
if (po.NumArgs() != 4) {
po.PrintUsage();
exit(1);
}
std::string model_rxfilename = po.GetArg(1),
lda_mllt_rxfilename = po.GetArg(2),
model_wxfilename = po.GetArg(3),
proj_wxfilename = po.GetArg(4);
kaldi::SgmmWriteFlagsType write_flags =
StringToSgmmWriteFlags(write_flags_str);
AmSgmm2 am_sgmm;
TransitionModel trans_model;
{
bool binary;
Input ki(model_rxfilename, &binary);
trans_model.Read(ki.Stream(), binary);
am_sgmm.Read(ki.Stream(), binary);
}
Matrix<BaseFloat> lda_mllt_mat;
ReadKaldiObject(lda_mllt_rxfilename, &lda_mllt_mat);
// Need the full LDA+MLLT matrix, including the extra rows.
// See featbin/extend-transform.cc
KALDI_ASSERT(lda_mllt_mat.NumRows() == lda_mllt_mat.NumCols());
Matrix<BaseFloat> inv_lda_mllt_mat(lda_mllt_mat);
inv_lda_mllt_mat.Invert();
Matrix<BaseFloat> projection;
Sgmm2Project sgmm_project;
sgmm_project.ComputeProjection(am_sgmm, inv_lda_mllt_mat, start_dim, end_dim,
&projection);
Matrix<BaseFloat> total_projection(projection.NumRows(), projection.NumCols());
total_projection.AddMatMat(1.0, projection, kNoTrans,
inv_lda_mllt_mat, kNoTrans, 0.0);
sgmm_project.ApplyProjection(total_projection, &am_sgmm);
am_sgmm.ComputeDerivedVars(); // recompute normalizers, and possibly
// weights.
{
Output ko(model_wxfilename, binary_write);
trans_model.Write(ko.Stream(), binary_write);
am_sgmm.Write(ko.Stream(), binary_write, write_flags);
}
KALDI_LOG << "Wrote model to " << model_wxfilename;
WriteKaldiObject(projection, proj_wxfilename, binary_write);
KALDI_LOG << "Wrote projection matrix to " << proj_wxfilename;
return 0;
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}