nnet-compute-from-egs.cc File Reference
Include dependency graph for nnet-compute-from-egs.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-from-egs.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), NnetExample::input_frames, KALDI_LOG, NnetExample::left_context, Nnet::LeftContext(), SequentialTableReader< Holder >::Next(), kaldi::nnet2::NnetComputation(), ParseOptions::NumArgs(), CompressedMatrix::NumCols(), Nnet::OutputDim(), ParseOptions::PrintUsage(), MatrixBase< Real >::Range(), ParseOptions::Read(), kaldi::ReadKaldiObject(), Nnet::RightContext(), NnetExample::spk_info, CuMatrix< Real >::Swap(), 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, taking as input the nnet-training examples\n"
36  "(typically an archive with the extension .egs), ignoring the labels; it\n"
37  "outputs as a matrix the result. Used mostly for debugging.\n"
38  "\n"
39  "Usage: nnet-compute-from-egs [options] <raw-nnet-in> <egs-rspecifier> "
40  "<feature-wspecifier>\n"
41  "e.g.: nnet-compute-from-egs 'nnet-to-raw-nnet final.mdl -|' egs.10.1.ark ark:-\n";
42 
43  ParseOptions po(usage);
44 
45  po.Read(argc, argv);
46 
47  if (po.NumArgs() != 3) {
48  po.PrintUsage();
49  exit(1);
50  }
51 
52  std::string raw_nnet_rxfilename = po.GetArg(1),
53  examples_rspecifier = po.GetArg(2),
54  features_or_loglikes_wspecifier = po.GetArg(3);
55 
56  Nnet nnet;
57  ReadKaldiObject(raw_nnet_rxfilename, &nnet);
58 
59  int64 num_egs = 0;
60 
61  SequentialNnetExampleReader example_reader(examples_rspecifier);
62  BaseFloatMatrixWriter writer(features_or_loglikes_wspecifier);
63 
64  int32 left_context = nnet.LeftContext(),
65  context = nnet.LeftContext() + 1 + nnet.RightContext();
66 
67  for (; !example_reader.Done(); example_reader.Next()) {
68  const NnetExample &eg = example_reader.Value();
69  int32 start_offset = eg.left_context - left_context;
70  int32 basic_dim = eg.input_frames.NumCols(),
71  spk_dim = eg.spk_info.Dim(), dim = basic_dim + spk_dim;
72  Matrix<BaseFloat> input_frames(eg.input_frames),
73  input_block(context, dim);
74  input_block.Range(0, context, 0, basic_dim).CopyFromMat(
75  input_frames.Range(start_offset, context, 0, basic_dim));
76  if (spk_dim != 0) {
77  input_block.Range(0, context, basic_dim, spk_dim).CopyRowsFromVec(
78  eg.spk_info);
79  }
80  CuMatrix<BaseFloat> gpu_input_block;
81  gpu_input_block.Swap(&input_block);
82  CuMatrix<BaseFloat> gpu_output_block(1, nnet.OutputDim());
83 
84  bool pad_input = false;
85  NnetComputation(nnet, gpu_input_block, pad_input, &gpu_output_block);
86  writer.Write("global", Matrix<BaseFloat>(gpu_output_block));
87  num_egs++;
88  }
89 
90  KALDI_LOG << "Processed " << num_egs << " examples.";
91 
92  return (num_egs == 0 ? 1 : 0);
93  } catch(const std::exception &e) {
94  std::cerr << e.what() << '\n';
95  return -1;
96  }
97 }
CompressedMatrix input_frames
The input data, with NumRows() >= labels.size() + left_context; it includes features to the left and ...
Definition: nnet-example.h:49
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
NnetExample is the input data and corresponding label (or labels) for one or more frames of input...
Definition: nnet-example.h:36
int32 left_context
The number of frames of left context (we can work out the #frames of right context from input_frames...
Definition: nnet-example.h:53
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
void Swap(Matrix< Real > *mat)
Definition: cu-matrix.cc:123
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
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
#define KALDI_LOG
Definition: kaldi-error.h:153
Vector< BaseFloat > spk_info
The speaker-specific input, if any, or an empty vector if we&#39;re not using this features.
Definition: nnet-example.h:58