lattice-to-post.cc 4.3 KB
Newer Older
1 2 3 4 5
// latbin/lattice-to-post.cc

// Copyright 2009-2011   Saarland University
// Author: Arnab Ghoshal

6 7
// See ../../COPYING for clarification regarding multiple authors
//
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.

#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "fstext/fstext-lib.h"
#include "lat/kaldi-lattice.h"
25
#include "lat/lattice-functions.h"
26 27 28 29 30 31 32 33 34 35

int main(int argc, char *argv[]) {
  try {
    typedef kaldi::int32 int32;
    using fst::SymbolTable;
    using fst::VectorFst;
    using fst::StdArc;

    const char *usage =
        "Do forward-backward and collect posteriors over lattices.\n"
36
        "Usage: lattice-to-post [options] lats-rspecifier posts-wspecifier [loglikes-wspecifier]\n"
37 38
        " e.g.: lattice-to-post --acoustic-scale=0.1 ark:1.lats ark:1.post\n"
        "See also: lattice-to-ctm-conf, post-to-pdf-post\n";
39

40
    kaldi::BaseFloat acoustic_scale = 1.0, lm_scale = 1.0;
41 42 43
    kaldi::ParseOptions po(usage);
    po.Register("acoustic-scale", &acoustic_scale,
                "Scaling factor for acoustic likelihoods");
44 45
    po.Register("lm-scale", &lm_scale,
                "Scaling factor for \"graph costs\" (including LM costs)");
46 47
    po.Read(argc, argv);

48
    if (po.NumArgs() < 2 || po.NumArgs() > 3) {
49 50 51 52 53
      po.PrintUsage();
      exit(1);
    }

    if (acoustic_scale == 0.0)
54
      KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
55 56

    std::string lats_rspecifier = po.GetArg(1),
57
        posteriors_wspecifier = po.GetArg(2),
58
        loglikes_wspecifier = po.GetOptArg(3);
59 60 61 62 63

    // Read as regular lattice
    kaldi::SequentialLatticeReader lattice_reader(lats_rspecifier);

    kaldi::PosteriorWriter posterior_writer(posteriors_wspecifier);
64
    kaldi::BaseFloatWriter loglikes_writer(loglikes_wspecifier);
65

66 67
    int32 n_done = 0;
    double total_like = 0.0, lat_like;
68
    double total_ac_like = 0.0, lat_ac_like; // acoustic likelihood weighted by posterior.
69 70 71 72 73
    double total_time = 0, lat_time;

    for (; !lattice_reader.Done(); lattice_reader.Next()) {
      std::string key = lattice_reader.Key();
      kaldi::Lattice lat = lattice_reader.Value();
74 75
      // FreeCurrent() is an optimization that prevents the lattice from being
      // copied unnecessarily (OpenFst does copy-on-write).
76
      lattice_reader.FreeCurrent();
77 78 79
      if (acoustic_scale != 1.0 || lm_scale != 1.0)
        fst::ScaleLattice(fst::LatticeScale(lm_scale, acoustic_scale), &lat);
      
80 81 82 83 84 85 86
      kaldi::uint64 props = lat.Properties(fst::kFstProperties, false);
      if (!(props & fst::kTopSorted)) {
        if (fst::TopSort(&lat) == false)
          KALDI_ERR << "Cycles detected in lattice.";
      }

      kaldi::Posterior post;
87
      lat_like = kaldi::LatticeForwardBackward(lat, &post, &lat_ac_like);
88 89 90
      total_like += lat_like;
      lat_time = post.size();
      total_time += lat_time;
91
      total_ac_like += lat_ac_like;
92 93 94 95

      KALDI_VLOG(2) << "Processed lattice for utterance: " << key << "; found "
                    << lat.NumStates() << " states and " << fst::NumArcs(lat)
                    << " arcs. Average log-likelihood = " << (lat_like/lat_time)
96 97
                    << " over " << lat_time << " frames.  Average acoustic log-like"
                    << " per frame is " << (lat_ac_like/lat_time);
98 99 100
      
      if (loglikes_writer.IsOpen()) 
        loglikes_writer.Write(key, lat_like);
101

102 103 104 105
      posterior_writer.Write(key, post);
      n_done++;
    }

Dan Povey's avatar
Dan Povey committed
106
    KALDI_LOG << "Overall average log-like/frame is "
107
              << (total_like/total_time) << " over " << total_time
108 109
              << " frames.  Average acoustic like/frame is "
              << (total_ac_like/total_time);
110 111
    KALDI_LOG << "Done " << n_done << " lattices.";
    return (n_done != 0 ? 0 : 1);
112
  } catch(const std::exception &e) {
113 114 115 116
    std::cerr << e.what();
    return -1;
  }
}