53 spk_average.
AddRowSumMat(1.0 / num_utts, utts_of_this_spk);
62 std::ostringstream ostr;
97 if (num_floored > 0) {
98 KALDI_WARN <<
"Floored " << num_floored <<
" eigenvalues of covariance " 109 const std::map<std::string, std::vector<std::string> > &spk2utt,
115 KALDI_ASSERT(dim == utt2ivector.begin()->second->Dim());
120 std::map<std::string, std::vector<std::string> >::const_iterator iter;
121 for (iter = spk2utt.begin(); iter != spk2utt.end(); ++iter) {
122 const std::vector<std::string> &uttlist = iter->second;
125 int32 N = uttlist.size();
128 std::string utt = uttlist[
n];
130 utts_of_this_spk.
Row(
n).CopyFromVec(
131 *(utt2ivector.find(utt)->second));
139 "Too little data for iVector dimension.");
149 mat_to_normalize.
AddSp(total_covariance_factor, total_covar);
150 mat_to_normalize.
AddSp(1.0 - total_covariance_factor, within_covar);
154 static_cast<double>(covariance_floor), &T);
157 between_covar.
AddSp(-1.0, within_covar);
164 between_covar_proj.
Eig(&s, &U);
165 bool sort_on_absolute_value =
false;
169 sort_on_absolute_value);
171 KALDI_LOG <<
"Singular values of between-class covariance after projecting " 172 <<
"with interpolated [total/within] covariance with a weight of " 173 << total_covariance_factor <<
" on the total covariance, are: " << s;
189 int32 dim = utt2ivector.begin()->second->Dim();
190 size_t num_ivectors = utt2ivector.size();
192 std::map<std::string, Vector<BaseFloat> *>::iterator iter;
193 for (iter = utt2ivector.begin(); iter != utt2ivector.end(); ++iter)
194 mean.
AddVec(1.0 / num_ivectors, *(iter->second));
197 for (iter = utt2ivector.begin(); iter != utt2ivector.end(); ++iter)
198 iter->second->AddVec(-1.0, *mean_out);
205 int main(
int argc,
char *argv[]) {
206 using namespace kaldi;
210 "Compute an LDA matrix for iVector system. Reads in iVectors per utterance,\n" 211 "and an utt2spk file which it uses to help work out the within-speaker and\n" 212 "between-speaker covariance matrices. Outputs an LDA projection to a\n" 213 "specified dimension. By default it will normalize so that the projected\n" 214 "within-class covariance is unit, but if you set --normalize-total-covariance\n" 215 "to true, it will normalize the total covariance.\n" 216 "Note: the transform we produce is actually an affine transform which will\n" 217 "also set the global mean to zero.\n" 219 "Usage: ivector-compute-lda [options] <ivector-rspecifier> <utt2spk-rspecifier> " 222 " ivector-compute-lda ark:ivectors.ark ark:utt2spk lda.mat\n";
228 covariance_floor = 1.0e-06;
231 po.
Register(
"dim", &lda_dim,
"Dimension we keep with the LDA transform");
232 po.
Register(
"total-covariance-factor", &total_covariance_factor,
233 "If this is 0.0 we normalize to make the within-class covariance " 234 "unit; if 1.0, the total covariance; if between, we normalize " 235 "an interpolated matrix.");
236 po.
Register(
"covariance-floor", &covariance_floor,
"Floor the eigenvalues " 237 "of the interpolated covariance matrix to the product of its " 238 "largest eigenvalue and this number.");
239 po.
Register(
"binary", &binary,
"Write output in binary mode");
248 std::string ivector_rspecifier = po.
GetArg(1),
249 utt2spk_rspecifier = po.
GetArg(2),
250 lda_wxfilename = po.
GetArg(3);
254 int32 num_done = 0, num_err = 0, dim = 0;
259 std::map<std::string, Vector<BaseFloat> *> utt2ivector;
260 std::map<std::string, std::vector<std::string> > spk2utt;
262 for (; !ivector_reader.
Done(); ivector_reader.
Next()) {
263 std::string utt = ivector_reader.
Key();
265 if (utt2ivector.count(utt) != 0) {
266 KALDI_WARN <<
"Duplicate iVector found for utterance " << utt
271 if (!utt2spk_reader.
HasKey(utt)) {
272 KALDI_WARN <<
"utt2spk has no entry for utterance " << utt
277 std::string spk = utt2spk_reader.
Value(utt);
284 spk2utt[spk].push_back(utt);
288 KALDI_LOG <<
"Read " << num_done <<
" utterances, " 289 << num_err <<
" with errors.";
292 KALDI_ERR <<
"Did not read any utterances.";
294 KALDI_LOG <<
"Computing within-class covariance.";
306 total_covariance_factor,
313 KALDI_VLOG(2) <<
"2-norm of transformed iVector mean is " 321 std::map<std::string, Vector<BaseFloat> *>::iterator iter;
322 for (iter = utt2ivector.begin(); iter != utt2ivector.end(); ++iter)
327 }
catch(
const std::exception &e) {
328 std::cerr << e.what();
void AddMat2(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
rank-N update: if (transM == kNoTrans) (*this) = beta*(*this) + alpha * M * M^T, or (if transM == kTr...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void CopyColFromVec(const VectorBase< Real > &v, const MatrixIndexT col)
Copy vector into specific column of matrix.
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Base class which provides matrix operations not involving resizing or allocation. ...
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
bool SingularTotCovar()
Will return Empty() if the within-class covariance matrix would be zero.
void Eig(VectorBase< Real > *s, MatrixBase< Real > *P=NULL) const
Solves the symmetric eigenvalue problem: at end we should have (*this) = P * diag(s) * P^T...
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
MatrixIndexT NumRows() const
KALDI_DISALLOW_COPY_AND_ASSIGN(CovarianceStats)
int main(int argc, char *argv[])
Real Norm(Real p) const
Compute the p-th norm of the vector.
void Register(const std::string &name, bool *ptr, const std::string &doc)
Allows random access to a collection of objects in an archive or script file; see The Table concept...
static void ComputeNormalizingTransform(const SpMatrix< Real > &covar, MatrixBase< Real > *proj)
This function computes a projection matrix that when applied makes the covariance unit (i...
void ApplyFloor(Real floor_val, MatrixIndexT *floored_count=nullptr)
Applies floor to all elements.
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
CovarianceStats(int32 dim)
void AddVec2(const Real alpha, const VectorBase< OtherReal > &v)
rank-one update, this <– this + alpha v v'
void ComputeLdaTransform(const std::map< std::string, Vector< BaseFloat > *> &utt2ivector, const std::map< std::string, std::vector< std::string > > &spk2utt, BaseFloat total_covariance_factor, BaseFloat covariance_floor, MatrixBase< BaseFloat > *lda_out)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
const T & Value(const std::string &key)
void GetWithinCovar(SpMatrix< double > *within_covar)
void AddSp(const Real alpha, const SpMatrix< Real > &Ma)
void GetTotalCovar(SpMatrix< double > *tot_covar) const
get total covariance, normalized per number of frames.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
void ComputeAndSubtractMean(std::map< std::string, Vector< BaseFloat > *> utt2ivector, Vector< BaseFloat > *mean_out)
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
SpMatrix< double > tot_covar_
MatrixIndexT Dim() const
Returns the dimension of the vector.
bool HasKey(const std::string &key)
void AddMatVec(const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add matrix times vector : this <– beta*this + alpha*M*v.
SpMatrix< double > between_covar_
void AccStats(const Matrix< double > &utts_of_this_spk)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
void AddMat2Sp(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const SpMatrix< Real > &A, const Real beta=0.0)
Extension of rank-N update: this <– beta*this + alpha * M * A * M^T.
void ApplyPow(Real power)
Take all elements of vector to a power.
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
void AddStats(const CovarianceStats &other)
void AddDiagVecMat(const Real alpha, const VectorBase< Real > &v, const MatrixBase< Real > &M, MatrixTransposeType transM, Real beta=1.0)
*this = beta * *this + alpha * diag(v) * M [or M^T].
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
Sub-matrix representation.
void SortSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt, bool sort_on_absolute_value)
Function to ensure that SVD is sorted.