Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/bin/vector-sum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ int32 TypeOneUsage(const ParseOptions &po) {
}

int32 TypeTwoUsage(const ParseOptions &po,
bool binary) {
bool binary,
bool average = false) {
KALDI_ASSERT(po.NumArgs() == 2);
KALDI_ASSERT(ClassifyRspecifier(po.GetArg(1), NULL, NULL) != kNoRspecifier &&
"vector-sum: first argument must be an rspecifier");
Expand Down Expand Up @@ -133,6 +134,8 @@ int32 TypeTwoUsage(const ParseOptions &po,
}
}
}

if (num_done > 0 && average) sum.Scale(1.0 / num_done);

Vector<BaseFloat> sum_float(sum);
WriteKaldiObject(sum_float, po.GetArg(2), binary);
Expand Down Expand Up @@ -199,12 +202,13 @@ int main(int argc, char *argv[]) {
" e.g.: vector-sum --binary=false 1.vec 2.vec 3.vec sum.vec\n"
"See also: copy-vector, dot-weights\n";

bool binary;
bool binary, average = false;

ParseOptions po(usage);

po.Register("binary", &binary, "If true, write output as binary (only "
"relevant for usage types two or three");
po.Register("average", &average, "Do average instead of sum");

po.Read(argc, argv);

Expand All @@ -219,7 +223,7 @@ int main(int argc, char *argv[]) {
ClassifyWspecifier(po.GetArg(N), NULL, NULL, NULL) ==
kNoWspecifier) {
// input from a single table, output not to table.
exit_status = TypeTwoUsage(po, binary);
exit_status = TypeTwoUsage(po, binary, average);
} else if (po.NumArgs() >= 2 &&
ClassifyRspecifier(po.GetArg(1), NULL, NULL) == kNoRspecifier &&
ClassifyWspecifier(po.GetArg(N), NULL, NULL, NULL) ==
Expand Down
30 changes: 1 addition & 29 deletions src/chainbin/nnet3-chain-get-egs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "hmm/posterior.h"
#include "nnet3/nnet-example.h"
#include "nnet3/nnet-chain-example.h"
#include "nnet3/nnet-example-utils.h"

namespace kaldi {
namespace nnet3 {
Expand Down Expand Up @@ -207,35 +208,6 @@ static bool ProcessFile(const fst::StdVectorFst &normalization_fst,
return true;
}

void RoundUpNumFrames(int32 frame_subsampling_factor,
int32 *num_frames,
int32 *num_frames_overlap) {
if (*num_frames % frame_subsampling_factor != 0) {
int32 new_num_frames = frame_subsampling_factor *
(*num_frames / frame_subsampling_factor + 1);
KALDI_LOG << "Rounding up --num-frames=" << (*num_frames)
<< " to a multiple of --frame-subsampling-factor="
<< frame_subsampling_factor
<< ", now --num-frames=" << new_num_frames;
*num_frames = new_num_frames;
}
if (*num_frames_overlap % frame_subsampling_factor != 0) {
int32 new_num_frames_overlap = frame_subsampling_factor *
(*num_frames_overlap / frame_subsampling_factor + 1);
KALDI_LOG << "Rounding up --num-frames-overlap=" << (*num_frames_overlap)
<< " to a multiple of --frame-subsampling-factor="
<< frame_subsampling_factor
<< ", now --num-frames-overlap=" << new_num_frames_overlap;
*num_frames_overlap = new_num_frames_overlap;
}
if (*num_frames_overlap < 0 || *num_frames_overlap >= *num_frames) {
KALDI_ERR << "--num-frames-overlap=" << (*num_frames_overlap) << " < "
<< "--num-frames=" << (*num_frames);
}

}


} // namespace nnet2
} // namespace kaldi

Expand Down
12 changes: 0 additions & 12 deletions src/hmm/posterior.cc
Original file line number Diff line number Diff line change
Expand Up @@ -429,18 +429,6 @@ void WeightSilencePostDistributed(const TransitionModel &trans_model,
}
}

// comparator object that can be used to sort from greatest to
// least posterior.
struct CompareReverseSecond {
// view this as an "<" operator used for sorting, except it behaves like
// a ">" operator on the .second field of the pair because we want the
// sort to be in reverse order (greatest to least) on posterior.
bool operator() (const std::pair<int32, BaseFloat> &a,
const std::pair<int32, BaseFloat> &b) {
return (a.second > b.second);
}
};

BaseFloat VectorToPosteriorEntry(
const VectorBase<BaseFloat> &log_likes,
int32 num_gselect,
Expand Down
12 changes: 12 additions & 0 deletions src/hmm/posterior.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ int32 MergePosteriors(const Posterior &post1,
bool drop_frames,
Posterior *post);

// comparator object that can be used to sort from greatest to
// least posterior.
struct CompareReverseSecond {
// view this as an "<" operator used for sorting, except it behaves like
// a ">" operator on the .second field of the pair because we want the
// sort to be in reverse order (greatest to least) on posterior.
bool operator() (const std::pair<int32, BaseFloat> &a,
const std::pair<int32, BaseFloat> &b) {
return (a.second > b.second);
}
};

/// Given a vector of log-likelihoods (typically of Gaussians in a GMM
/// but could be of pdf-ids), a number gselect >= 1 and a minimum posterior
/// 0 <= min_post < 1, it gets the posterior for each element of log-likes
Expand Down
25 changes: 17 additions & 8 deletions src/lat/lattice-functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,11 @@ static inline double LogAddOrMax(bool viterbi, double a, double b) {
return LogAdd(a, b);
}

// Computes (normal or Viterbi) alphas and betas; returns (total-prob, or
// best-path negated cost) Note: in either case, the alphas and betas are
// negated costs. Requires that lat be topologically sorted. This code
// will work for either CompactLattice or Latice.
template<typename LatticeType>
static double ComputeLatticeAlphasAndBetas(const LatticeType &lat,
bool viterbi,
vector<double> *alpha,
vector<double> *beta) {
double ComputeLatticeAlphasAndBetas(const LatticeType &lat,
bool viterbi,
vector<double> *alpha,
vector<double> *beta) {
typedef typename LatticeType::Arc Arc;
typedef typename Arc::Weight Weight;
typedef typename Arc::StateId StateId;
Expand Down Expand Up @@ -462,6 +458,19 @@ static double ComputeLatticeAlphasAndBetas(const LatticeType &lat,
return 0.5 * (tot_backward_prob + tot_forward_prob);
}

// instantiate the template for Lattice and CompactLattice
template
double ComputeLatticeAlphasAndBetas(const Lattice &lat,
bool viterbi,
vector<double> *alpha,
vector<double> *beta);

template
double ComputeLatticeAlphasAndBetas(const CompactLattice &lat,
bool viterbi,
vector<double> *alpha,
vector<double> *beta);



/// This is used in CompactLatticeLimitDepth.
Expand Down
12 changes: 12 additions & 0 deletions src/lat/lattice-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ bool ComputeCompactLatticeAlphas(const CompactLattice &lat,
bool ComputeCompactLatticeBetas(const CompactLattice &lat,
vector<double> *beta);


// Computes (normal or Viterbi) alphas and betas; returns (total-prob, or
// best-path negated cost) Note: in either case, the alphas and betas are
// negated costs. Requires that lat be topologically sorted. This code
// will work for either CompactLattice or Latice.
template<typename LatticeType>
double ComputeLatticeAlphasAndBetas(const LatticeType &lat,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vimal, it's not good for compilation speed to define this in the header.
Just declare it in the header, and in the .cc file, define it and instantiate it for float and double.

bool viterbi,
vector<double> *alpha,
vector<double> *beta);


/// Topologically sort the compact lattice if not already topologically sorted.
/// Will crash if the lattice cannot be topologically sorted.
void TopSortCompactLatticeIfNeeded(CompactLattice *clat);
Expand Down
155 changes: 153 additions & 2 deletions src/latbin/lattice-copy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,108 @@
#include "fstext/fstext-lib.h"
#include "lat/kaldi-lattice.h"

namespace kaldi {
int32 CopySubsetLattices(std::string filename,
SequentialLatticeReader *lattice_reader,
LatticeWriter *lattice_writer,
bool include = true, bool ignore_missing = false
) {
unordered_set<std::string, StringHasher> subset;
std::set<std::string> subset_list;

bool binary;
Input ki(filename, &binary);
KALDI_ASSERT(!binary);
std::string line;
while (std::getline(ki.Stream(), line)) {
std::vector<std::string> split_line;
SplitStringToVector(line, " \t\r", true, &split_line);
if(split_line.empty()) {
KALDI_ERR << "Unable to parse line \"" << line << "\" encountered in input in " << filename;
}
subset.insert(split_line[0]);
subset_list.insert(split_line[0]);
}

int32 num_total = 0;
size_t num_success = 0;
for (; !lattice_reader->Done(); lattice_reader->Next(), num_total++) {
if (include && lattice_reader->Key() > *(subset_list.rbegin())) {
KALDI_LOG << "The utterance " << lattice_reader->Key()
<< " is larger than "
<< "the last key in the include list. Not reading further.";
KALDI_LOG << "Wrote " << num_success << " utterances";
return 0;
}

if (include && subset.count(lattice_reader->Key()) > 0) {
lattice_writer->Write(lattice_reader->Key(), lattice_reader->Value());
num_success++;
} else if (!include && subset.count(lattice_reader->Key()) == 0) {
lattice_writer->Write(lattice_reader->Key(), lattice_reader->Value());
num_success++;
}
}

KALDI_LOG << "Wrote " << num_success << " out of " << num_total
<< " utterances.";

if (ignore_missing) return 0;

return (num_success != 0 ? 0 : 1);
}

int32 CopySubsetLattices(std::string filename,
SequentialCompactLatticeReader *lattice_reader,
CompactLatticeWriter *lattice_writer,
bool include = true, bool ignore_missing = false
) {
unordered_set<std::string, StringHasher> subset;
std::set<std::string> subset_list;

bool binary;
Input ki(filename, &binary);
KALDI_ASSERT(!binary);
std::string line;
while (std::getline(ki.Stream(), line)) {
std::vector<std::string> split_line;
SplitStringToVector(line, " \t\r", true, &split_line);
if(split_line.empty()) {
KALDI_ERR << "Unable to parse line \"" << line << "\" encountered in input in " << filename;
}
subset.insert(split_line[0]);
subset_list.insert(split_line[0]);
}

int32 num_total = 0;
size_t num_success = 0;
for (; !lattice_reader->Done(); lattice_reader->Next(), num_total++) {
if (include && lattice_reader->Key() > *(subset_list.rbegin())) {
KALDI_LOG << "The utterance " << lattice_reader->Key()
<< " is larger than "
<< "the last key in the include list. Not reading further.";
KALDI_LOG << "Wrote " << num_success << " utterances";
return 0;
}

if (include && subset.count(lattice_reader->Key()) > 0) {
lattice_writer->Write(lattice_reader->Key(), lattice_reader->Value());
num_success++;
} else if (!include && subset.count(lattice_reader->Key()) == 0) {
lattice_writer->Write(lattice_reader->Key(), lattice_reader->Value());
num_success++;
}
}

KALDI_LOG << " Wrote " << num_success << " out of " << num_total
<< " utterances.";

if (ignore_missing) return 0;

return (num_success != 0 ? 0 : 1);
}
}

int main(int argc, char *argv[]) {
try {
using namespace kaldi;
Expand All @@ -36,14 +138,32 @@ int main(int argc, char *argv[]) {
const char *usage =
"Copy lattices (e.g. useful for changing to text mode or changing\n"
"format to standard from compact lattice.)\n"
"The --include and --exclude options can be used to copy only a subset "
"of lattices, where are the --include option specifies the "
"whitelisted utterances that would be copied and --exclude option "
"specifies the blacklisted utterances that would not be copied.\n"
"Only one of --include and --exclude can be supplied.\n"
"Usage: lattice-copy [options] lattice-rspecifier lattice-wspecifier\n"
" e.g.: lattice-copy --write-compact=false ark:1.lats ark,t:text.lats\n"
"See also: lattice-to-fst, and the script egs/wsj/s5/utils/convert_slf.pl\n";

ParseOptions po(usage);
bool write_compact = true;
bool write_compact = true, ignore_missing = false;
std::string include_rxfilename;
std::string exclude_rxfilename;

po.Register("write-compact", &write_compact, "If true, write in normal (compact) form.");

po.Register("include", &include_rxfilename,
"Text file, the first field of each "
"line being interpreted as the "
"utterance-id whose lattices will be included");
po.Register("exclude", &exclude_rxfilename,
"Text file, the first field of each "
"line being interpreted as an utterance-id "
"whose lattices will be excluded");
po.Register("ignore-missing", &ignore_missing,
"Exit with status 0 even if no lattices are copied");

po.Read(argc, argv);

if (po.NumArgs() != 2) {
Expand All @@ -59,15 +179,46 @@ int main(int argc, char *argv[]) {
if (write_compact) {
SequentialCompactLatticeReader lattice_reader(lats_rspecifier);
CompactLatticeWriter lattice_writer(lats_wspecifier);

if (include_rxfilename != "") {
if (exclude_rxfilename != "") {
KALDI_ERR << "should not have both --exclude and --include option!";
}
return CopySubsetLattices(include_rxfilename,
&lattice_reader, &lattice_writer,
true, ignore_missing);
} else if (exclude_rxfilename != "") {
return CopySubsetLattices(exclude_rxfilename,
&lattice_reader, &lattice_writer,
false, ignore_missing);
}

for (; !lattice_reader.Done(); lattice_reader.Next(), n_done++)
lattice_writer.Write(lattice_reader.Key(), lattice_reader.Value());
} else {
SequentialLatticeReader lattice_reader(lats_rspecifier);
LatticeWriter lattice_writer(lats_wspecifier);

if (include_rxfilename != "") {
if (exclude_rxfilename != "") {
KALDI_ERR << "should not have both --exclude and --include option!";
}
return CopySubsetLattices(include_rxfilename,
&lattice_reader, &lattice_writer,
true, ignore_missing);
} else if (exclude_rxfilename != "") {
return CopySubsetLattices(exclude_rxfilename,
&lattice_reader, &lattice_writer,
true, ignore_missing);
}

for (; !lattice_reader.Done(); lattice_reader.Next(), n_done++)
lattice_writer.Write(lattice_reader.Key(), lattice_reader.Value());
}
KALDI_LOG << "Done copying " << n_done << " lattices.";

if (ignore_missing) return 0;

return (n_done != 0 ? 0 : 1);
} catch(const std::exception &e) {
std::cerr << e.what();
Expand Down
Loading