27 using namespace kaldi;
29 typedef std::string string;
32 "Computes log-likelihood ratios for trials using PLDA model\n" 33 "Note: the 'trials-file' has lines of the form\n" 35 "and the output will have the form\n" 36 "<key1> <key2> [<dot-product>]\n" 37 "(if either key could not be found, the dot-product field in the output\n" 38 "will be absent, and this program will print a warning)\n" 39 "For training examples, the input is the iVectors averaged over speakers;\n" 40 "a separate archive containing the number of utterances per speaker may be\n" 41 "optionally supplied using the --num-utts option; this affects the PLDA\n" 42 "scoring (if not supplied, it defaults to 1 per speaker).\n" 44 "Usage: ivector-plda-scoring <plda> <train-ivector-rspecifier> <test-ivector-rspecifier>\n" 45 " <trials-rxfilename> <scores-wxfilename>\n" 47 "e.g.: ivector-plda-scoring --num-utts=ark:exp/train/num_utts.ark plda " 48 "ark:exp/train/spk_ivectors.ark ark:exp/test/ivectors.ark trials scores\n" 49 "See also: ivector-compute-dot-products, ivector-compute-plda\n";
53 std::string num_utts_rspecifier;
57 po.Register(
"num-utts", &num_utts_rspecifier,
"Table to read the number of " 58 "utterances per speaker, e.g. ark:num_utts.ark\n");
62 if (po.NumArgs() != 5) {
67 std::string plda_rxfilename = po.GetArg(1),
68 train_ivector_rspecifier = po.GetArg(2),
69 test_ivector_rspecifier = po.GetArg(3),
70 trials_rxfilename = po.GetArg(4),
71 scores_wxfilename = po.GetArg(5);
74 double tot_test_renorm_scale = 0.0, tot_train_renorm_scale = 0.0;
75 int64 num_train_ivectors = 0, num_train_errs = 0, num_test_ivectors = 0;
77 int64 num_trials_done = 0, num_trials_err = 0;
82 int32 dim = plda.
Dim();
88 typedef unordered_map<string, Vector<BaseFloat>*,
StringHasher> HashType;
94 HashType train_ivectors, test_ivectors;
97 for (; !train_ivector_reader.Done(); train_ivector_reader.Next()) {
98 std::string spk = train_ivector_reader.Key();
99 if (train_ivectors.count(spk) != 0) {
100 KALDI_ERR <<
"Duplicate training iVector found for speaker " << spk;
104 if (!num_utts_rspecifier.empty()) {
105 if (!num_utts_reader.HasKey(spk)) {
106 KALDI_WARN <<
"Number of utterances not given for speaker " << spk;
110 num_examples = num_utts_reader.Value(spk);
118 transformed_ivector);
119 train_ivectors[spk] = transformed_ivector;
120 num_train_ivectors++;
122 KALDI_LOG <<
"Read " << num_train_ivectors <<
" training iVectors, " 123 <<
"errors on " << num_train_errs;
124 if (num_train_ivectors == 0)
125 KALDI_ERR <<
"No training iVectors present.";
126 KALDI_LOG <<
"Average renormalization scale on training iVectors was " 127 << (tot_train_renorm_scale / num_train_ivectors);
130 for (; !test_ivector_reader.Done(); test_ivector_reader.Next()) {
131 std::string utt = test_ivector_reader.Key();
132 if (test_ivectors.count(utt) != 0) {
133 KALDI_ERR <<
"Duplicate test iVector found for utterance " << utt;
136 int32 num_examples = 1;
143 transformed_ivector);
144 test_ivectors[utt] = transformed_ivector;
147 KALDI_LOG <<
"Read " << num_test_ivectors <<
" test iVectors.";
148 if (num_test_ivectors == 0)
149 KALDI_ERR <<
"No test iVectors present.";
150 KALDI_LOG <<
"Average renormalization scale on test iVectors was " 151 << (tot_test_renorm_scale / num_test_ivectors);
154 Input ki(trials_rxfilename);
156 Output ko(scores_wxfilename, binary);
158 double sum = 0.0, sumsq = 0.0;
161 while (std::getline(ki.Stream(), line)) {
162 std::vector<std::string> fields;
164 if (fields.size() != 2) {
165 KALDI_ERR <<
"Bad line " << (num_trials_done + num_trials_err)
166 <<
"in input (expected two fields: key1 key2): " << line;
168 std::string key1 = fields[0], key2 = fields[1];
169 if (train_ivectors.count(key1) == 0) {
170 KALDI_WARN <<
"Key " << key1 <<
" not present in training iVectors.";
174 if (test_ivectors.count(key2) == 0) {
175 KALDI_WARN <<
"Key " << key2 <<
" not present in test iVectors.";
180 *test_ivector = test_ivectors[key2];
183 test_ivector_dbl(*test_ivector);
185 int32 num_train_examples;
186 if (!num_utts_rspecifier.empty()) {
188 num_train_examples = num_utts_reader.Value(key1);
190 num_train_examples = 1;
198 sumsq += score * score;
200 ko.Stream() << key1 <<
' ' << key2 <<
' ' << score << std::endl;
203 for (HashType::iterator iter = train_ivectors.begin();
204 iter != train_ivectors.end(); ++iter)
206 for (HashType::iterator iter = test_ivectors.begin();
207 iter != test_ivectors.end(); ++iter)
211 if (num_trials_done != 0) {
212 BaseFloat mean = sum / num_trials_done, scatter = sumsq / num_trials_done,
213 variance = scatter - mean * mean, stddev = sqrt(variance);
214 KALDI_LOG <<
"Mean score was " << mean <<
", standard deviation was " 217 KALDI_LOG <<
"Processed " << num_trials_done <<
" trials, " << num_trials_err
219 return (num_trials_done != 0 ? 0 : 1);
220 }
catch(
const std::exception &e) {
221 std::cerr << e.what();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
double TransformIvector(const PldaConfig &config, const VectorBase< double > &ivector, int32 num_enroll_examples, VectorBase< double > *transformed_ivector) const
Transforms an iVector into a space where the within-class variance is unit and between-class variance...
A hashing function object for strings.
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Allows random access to a collection of objects in an archive or script file; see The Table concept...
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
void SplitStringToVector(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< std::string > *out)
Split a string using any of the single character delimiters.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
double LogLikelihoodRatio(const VectorBase< double > &transformed_enroll_ivector, int32 num_enroll_utts, const VectorBase< double > &transformed_test_ivector) const
Returns the log-likelihood ratio log (p(test_ivector | same) / p(test_ivector | different)).
void Register(OptionsItf *opts)
A class representing a vector.