nnet-compute-from-egs.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-compute-from-egs.cc
2 
3 // Copyright 2012-2013 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "hmm/transition-model.h"
23 #include "nnet2/train-nnet.h"
24 #include "nnet2/am-nnet.h"
25 
26 
27 int main(int argc, char *argv[]) {
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 }
98 
99 
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
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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 Write(const std::string &key, const T &value) const
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
int main(int argc, char *argv[])
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.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:202
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