28 int main(
int argc,
char *argv[]) {
30 using namespace kaldi;
33 "Computes relative difference between two sets of features\n" 34 "per dimension and an average difference\n" 35 "Can be used to figure out how different two sets of features are.\n" 36 "Inputs must have same dimension. Prints to stdout a similarity\n" 37 "metric vector that is 1.0 per dimension if the features identical,\n" 38 "and <1.0 otherwise, and an average overall similarity value.\n" 40 "Usage: compare-feats [options] <in-rspecifier1> <in-rspecifier2>\n" 41 "e.g.: compare-feats ark:1.ark ark:2.ark\n";
46 po.
Register(
"threshold", &threshold,
"Similarity threshold, affects " 56 std::string rspecifier1 = po.
GetArg(1), rspecifier2 = po.
GetArg(2);
58 int32 num_done = 0, num_err = 0, Dim = 0;
60 double overall_similarity = 0;
65 for (; !feat_reader1.
Done(); feat_reader1.
Next()) {
66 std::string utt = feat_reader1.
Key();
70 if (!feat_reader2.
HasKey(utt)) {
71 KALDI_WARN <<
"Second table has no feature for utterance " 77 if (feat1.NumCols() != feat2.NumCols()) {
78 KALDI_WARN <<
"Feature dimensions differ for utterance " 79 << utt <<
", " << feat1.NumCols() <<
" vs. " 80 << feat2.NumCols() <<
", skipping utterance." 91 similarity_metric.
Resize(Dim);
97 feat2_col.CopyColFromMat(feat2,
i);
98 prod1(
i) +=
VecVec(feat1_col, feat1_col);
99 prod2(
i) +=
VecVec(feat2_col, feat2_col);
100 cross_prod(
i) +=
VecVec(feat1_col, feat2_col);
105 KALDI_LOG <<
"self-product of 1st features for each column dimension: " << prod1;
106 KALDI_LOG <<
"self-product of 2nd features for each column dimension: " << prod2;
107 KALDI_LOG <<
"cross-product for each column dimension: " << cross_prod;
110 similarity_metric.
AddVecDivVec(2.0, cross_prod, prod1, 0.0);
111 KALDI_LOG <<
"Similarity metric for each dimension " << similarity_metric
112 <<
" (1.0 means identical, the smaller the more different)";
114 overall_similarity = similarity_metric.
Sum() /
static_cast<double>(Dim);
116 KALDI_LOG <<
"Overall similarity for the two feats is:" << overall_similarity
117 <<
" (1.0 means identical, the smaller the more different)";
119 KALDI_LOG <<
"Processed " << num_done <<
" feature files, " 120 << num_err <<
" had errors.";
122 bool similar = (overall_similarity >= threshold);
126 KALDI_LOG <<
"Features are considered similar since " 127 << overall_similarity <<
" >= " << threshold;
129 KALDI_LOG <<
"Features are considered dissimilar since " 130 << overall_similarity <<
" < " << threshold;
134 return (num_done > 0 && similar) ? 0 : 1;
135 }
catch(
const std::exception &e) {
136 std::cerr << e.what();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
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...
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
const T & Value(const std::string &key)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
bool HasKey(const std::string &key)
Real Sum() const
Returns sum of the elements.
int main(int argc, char *argv[])
void AddVecDivVec(Real alpha, const VectorBase< Real > &v, const VectorBase< Real > &r, Real beta)
Add element-by-element quotient of two vectors.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void CopyColFromMat(const MatrixBase< OtherReal > &M, MatrixIndexT col)
Extracts a column of the matrix M.
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...