nnet-compute.cc File Reference
Include dependency graph for nnet-compute.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 27 of file nnet-compute.cc.

References CuMatrixBase< Real >::ApplyFloor(), CuMatrixBase< Real >::ApplyLog(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), Nnet::LeftContext(), SequentialTableReader< Holder >::Next(), kaldi::nnet2::NnetComputation(), ParseOptions::NumArgs(), CuMatrixBase< Real >::NumRows(), Nnet::OutputDim(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), Nnet::RightContext(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

27  {
28  try {
29  using namespace kaldi;
30  using namespace kaldi::nnet2;
31  typedef kaldi::int32 int32;
32  typedef kaldi::int64 int64;
33 
34  const char *usage =
35  "Does the neural net computation for each file of input features, and\n"
36  "outputs as a matrix the result. Used mostly for debugging.\n"
37  "Note: if you want it to apply a log (e.g. for log-likelihoods), use\n"
38  "--apply-log=true. Unlike nnet-am-compute, this version reads a 'raw'\n"
39  "neural net\n"
40  "\n"
41  "Usage: nnet-compute [options] <raw-nnet-in> <feature-rspecifier> "
42  "<feature-or-loglikes-wspecifier>\n";
43 
44  bool apply_log = false;
45  bool pad_input = true;
46  ParseOptions po(usage);
47  po.Register("apply-log", &apply_log, "Apply a log to the result of the computation "
48  "before outputting.");
49  po.Register("pad-input", &pad_input, "If true, duplicate the first and last frames "
50  "of input features as required for temporal context, to prevent #frames "
51  "of output being less than those of input.");
52 
53  po.Read(argc, argv);
54 
55  if (po.NumArgs() != 3) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60  std::string raw_nnet_rxfilename = po.GetArg(1),
61  features_rspecifier = po.GetArg(2),
62  features_or_loglikes_wspecifier = po.GetArg(3);
63 
64  Nnet nnet;
65  ReadKaldiObject(raw_nnet_rxfilename, &nnet);
66 
67  int64 num_done = 0, num_frames = 0;
68  SequentialBaseFloatCuMatrixReader feature_reader(features_rspecifier);
69  BaseFloatCuMatrixWriter writer(features_or_loglikes_wspecifier);
70 
71  for (; !feature_reader.Done(); feature_reader.Next()) {
72  std::string utt = feature_reader.Key();
73  const CuMatrix<BaseFloat> &feats = feature_reader.Value();
74 
75  int32 output_frames = feats.NumRows(), output_dim = nnet.OutputDim();
76  if (!pad_input)
77  output_frames -= nnet.LeftContext() + nnet.RightContext();
78  if (output_frames <= 0) {
79  KALDI_WARN << "Skipping utterance " << utt << " because output "
80  << "would be empty.";
81  continue;
82  }
83  CuMatrix<BaseFloat> output(output_frames, output_dim);
84  NnetComputation(nnet, feats, pad_input, &output);
85 
86  if (apply_log) {
87  output.ApplyFloor(1.0e-20);
88  output.ApplyLog();
89  }
90  writer.Write(utt, output);
91  num_frames += feats.NumRows();
92  num_done++;
93  }
94 
95  KALDI_LOG << "Processed " << num_done << " feature files, "
96  << num_frames << " frames of input were processed.";
97 
98  return (num_done == 0 ? 1 : 0);
99  } catch(const std::exception &e) {
100  std::cerr << e.what() << '\n';
101  return -1;
102  }
103 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 LeftContext() const
Returns the left-context summed over all the Components...
Definition: nnet-nnet.cc:42
int32 OutputDim() const
The output dimension of the network – typically the number of pdfs.
Definition: nnet-nnet.cc:31
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
This class represents a matrix that&#39;s stored on the GPU if we have one, and in memory if not...
Definition: matrix-common.h:71
void NnetComputation(const Nnet &nnet, const CuMatrixBase< BaseFloat > &input, bool pad_input, CuMatrixBase< BaseFloat > *output)
Does the basic neural net computation, on a sequence of data (e.g.
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
int32 RightContext() const
Returns the right-context summed over all the Components...
Definition: nnet-nnet.cc:56
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_WARN
Definition: kaldi-error.h:150
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:215
#define KALDI_LOG
Definition: kaldi-error.h:153