41 std::vector<BuildTreeStatsType> split_stats;
44 split_stats.resize(to_pdf_map.
MaxResult() + 1);
48 for (
size_t i = 0;
i < split_stats.size();
i++) {
49 if (split_stats[
i].empty()) {
50 std::vector<int32> bad_pdfs(1,
i), bad_phones;
52 std::ostringstream ss;
53 for (
int32 idx = 0; idx < bad_phones.size(); idx ++)
54 ss << bad_phones[idx] <<
' ';
56 <<
" with no stats; corresponding phone list: " << ss.str();
67 std::vector<Clusterable*> summed_stats;
70 KALDI_ASSERT(avg_stats != NULL &&
"No stats available in gmm-init-model.");
71 for (
size_t i = 0;
i < summed_stats.size();
i++) {
78 std::vector<int32> bad_pdfs(1,
i), bad_phones;
80 std::ostringstream ss;
81 for (
int32 idx = 0; idx < bad_phones.size(); idx ++)
82 ss << bad_phones[idx] <<
' ';
83 KALDI_WARN <<
"Very small count for state " <<
i <<
": " 84 << count <<
"; corresponding phone list: " << ss.str();
97 std::vector<BuildTreeStatsType> split_stats;
99 if (split_stats.size() != to_pdf_map.
MaxResult()+1) {
101 split_stats.resize(to_pdf_map.
MaxResult()+1);
103 occs->
Resize(split_stats.size());
104 for (
int32 pdf = 0; pdf < occs->
Dim(); pdf++)
120 const std::string &old_tree_rxfilename,
121 const std::string &old_model_rxfilename,
130 Input ki(old_model_rxfilename, &binary_in);
136 Input ki(old_tree_rxfilename, &binary_in);
142 std::vector<BuildTreeStatsType> split_stats;
145 for (
size_t i = 0;
i < split_stats.size();
i++) {
146 if (split_stats[
i].empty()) {
147 KALDI_WARN <<
"Leaf " <<
i <<
" of new tree has no stats.";
150 if (static_cast<int32>(split_stats.size()) != to_pdf_map.
MaxResult() + 1) {
153 KALDI_WARN <<
"Tree may have final leaf with no stats.";
154 split_stats.resize(to_pdf_map.
MaxResult() + 1);
163 KALDI_ASSERT(avg_stats_gc != NULL &&
"Empty stats input.");
164 DiagGmm avg_gmm(*avg_stats_gc, var_floor);
172 int32 num_pdfs =
static_cast<int32>(split_stats.size());
173 for (
int32 pdf = 0; pdf < num_pdfs; pdf++) {
181 KALDI_ERR <<
"InitAmGmmFromOld: old system has wider context " 182 "so cannot convert stats.";
186 std::map<int32, BaseFloat> oldpdf_to_count;
187 for (
size_t i = 0;
i < my_stats.size();
i++) {
190 bool ret = old_map.
Map(evec, &ans);
191 if (!ret) {
KALDI_ERR <<
"Could not map context using old tree."; }
193 BaseFloat stats_count = my_stats[
i].second->Normalizer();
194 if (oldpdf_to_count.count(ans) == 0) oldpdf_to_count[ans] = stats_count;
195 else oldpdf_to_count[ans] += stats_count;
198 for (std::map<int32, BaseFloat>::const_iterator iter = oldpdf_to_count.begin();
199 iter != oldpdf_to_count.end();
201 if (iter->second > max_count) {
202 max_count = iter->second;
203 max_old_pdf = iter->first;
206 if (max_count == 0) {
207 KALDI_WARN <<
"Leaf " << pdf <<
" of new tree being initialized with " 208 <<
"globally averaged stats.";
220 int main(
int argc,
char *argv[]) {
221 using namespace kaldi;
223 using namespace kaldi;
227 "Initialize GMM from decision tree and tree stats\n" 228 "Usage: gmm-init-model [options] <tree-in> <tree-stats-in> <topo-file> <model-out> [<old-tree> <old-model>]\n" 230 " gmm-init-model tree treeacc topo 1.mdl\n" 231 "or (initializing GMMs with old model):\n" 232 " gmm-init-model tree treeacc topo 1.mdl prev/tree prev/30.mdl\n";
235 double var_floor = 0.01;
236 std::string occs_out_filename;
240 po.
Register(
"binary", &binary,
"Write output in binary mode");
241 po.
Register(
"write-occs", &occs_out_filename,
"File to write state " 243 po.
Register(
"var-floor", &var_floor,
"Variance floor used while " 244 "initializing Gaussians");
254 tree_filename = po.
GetArg(1),
255 stats_filename = po.
GetArg(2),
256 topo_filename = po.
GetArg(3),
257 model_out_filename = po.
GetArg(4),
268 Input ki(stats_filename, &binary_in);
271 KALDI_LOG <<
"Number of separate statistics is " << stats.size();
282 if (old_tree_filename.empty())
283 InitAmGmm(stats, to_pdf, &am_gmm, trans_model, var_floor);
294 if (!occs_out_filename.empty()) {
297 Output ko(occs_out_filename, binary);
302 Output ko(model_out_filename, binary);
309 }
catch(
const std::exception &e) {
310 std::cerr << e.what();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
BaseFloat SumNormalizer(const BuildTreeStatsType &stats_in)
Sums the normalizer [typically, data-count] over the stats.
void AddPdf(const DiagGmm &gmm)
Adds a GMM to the model, and increments the total number of PDFs.
virtual int32 ContextWidth() const
ContextWidth() returns the value N (e.g.
void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
const EventMap & ToPdfMap() const
bool ConvertStats(int32 oldN, int32 oldP, int32 newN, int32 newP, BuildTreeStatsType *stats)
Converts stats from a given context-window (N) and central-position (P) to a different N and P...
void GetOccs(const BuildTreeStatsType &stats, const EventMap &to_pdf_map, Vector< BaseFloat > *occs)
Get state occupation counts.
A class for storing topology information for phones.
Clusterable * SumStats(const BuildTreeStatsType &stats_in)
Sums stats, or returns NULL stats_in has no non-NULL stats.
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void InitAmGmm(const BuildTreeStatsType &stats, const EventMap &to_pdf_map, AmDiagGmm *am_gmm, const TransitionModel &trans_model, BaseFloat var_floor)
InitAmGmm initializes the GMM with one Gaussian per state.
virtual EventAnswerType MaxResult() const
int main(int argc, char *argv[])
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
void SplitStatsByMap(const BuildTreeStatsType &stats, const EventMap &e, std::vector< BuildTreeStatsType > *stats_out)
Splits stats according to the EventMap, indexing them at output by the leaf type. ...
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
virtual bool Map(const EventType &event, EventAnswerType *ans) const =0
void Register(const std::string &name, bool *ptr, const std::string &doc)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
void DeleteBuildTreeStats(BuildTreeStatsType *stats)
This frees the Clusterable* pointers in "stats", where non-NULL, and sets them to NULL...
void ReadBuildTreeStats(std::istream &is, bool binary, const Clusterable &example, BuildTreeStatsType *stats)
Reads BuildTreeStats object.
bool GetPhonesForPdfs(const TransitionModel &trans_model, const std::vector< int32 > &pdfs, std::vector< int32 > *phones)
Works out which phones might correspond to the given pdfs.
std::vector< std::pair< EventKeyType, EventValueType > > EventType
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
void InitAmGmmFromOld(const BuildTreeStatsType &stats, const EventMap &to_pdf_map, int32 N, int32 P, const std::string &old_tree_rxfilename, const std::string &old_model_rxfilename, BaseFloat var_floor, AmDiagGmm *am_gmm)
InitAmGmmFromOld initializes the GMM based on a previously trained model and tree, which must require no more phonetic context than the current tree.
void Read(std::istream &is, bool binary)
void SumStatsVec(const std::vector< BuildTreeStatsType > &stats_in, std::vector< Clusterable *> *stats_out)
Sum a vector of stats.
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
virtual int32 CentralPosition() const
Central position P of the phone context, in 0-based numbering, e.g.
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.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
A class that is capable of representing a generic mapping from EventType (which is a vector of (key...
void Write(std::ostream &os, bool binary) const
A class representing a vector.
void Read(std::istream &is, bool binary)
Read context-dependency object from disk; throws on error.
#define KALDI_ASSERT(cond)
void Write(std::ostream &out_stream, bool binary) const
Definition for Gaussian Mixture Model with diagonal covariances.
int32 EventAnswerType
As far as the event-map code itself is concerned, things of type EventAnswerType may take any value e...
std::vector< std::pair< EventType, Clusterable * > > BuildTreeStatsType
GaussClusterable wraps Gaussian statistics in a form accessible to generic clustering algorithms...
void Read(std::istream &in_stream, bool binary)
Clusterable * SumClusterable(const std::vector< Clusterable *> &vec)
Sums stats (ptrs may be NULL). Returns NULL if no non-NULL stats present.
std::string GetOptArg(int param) const