Blame view
src/bin/copy-matrix.cc
4.92 KB
8dcb6dfcb first commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
// bin/copy-matrix.cc // Copyright 2009-2011 Microsoft Corporation // See ../../COPYING for clarification regarding multiple authors // // 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 "matrix/kaldi-matrix.h" #include "transform/transform-common.h" namespace kaldi { void ApplySoftMaxPerRow(MatrixBase<BaseFloat> *mat) { for (int32 i = 0; i < mat->NumRows(); i++) { mat->Row(i).ApplySoftMax(); } } } // namespace kaldi int main(int argc, char *argv[]) { try { using namespace kaldi; const char *usage = "Copy matrices, or archives of matrices (e.g. features or transforms) " "Also see copy-feats which has other format options " " " "Usage: copy-matrix [options] <matrix-in-rspecifier> <matrix-out-wspecifier> " " or: copy-matrix [options] <matrix-in-rxfilename> <matrix-out-wxfilename> " " e.g.: copy-matrix --binary=false 1.mat - " " copy-matrix ark:2.trans ark,t:- " "See also: copy-feats, matrix-sum "; bool binary = true; bool apply_log = false; bool apply_exp = false; bool apply_softmax_per_row = false; BaseFloat apply_power = 1.0; BaseFloat scale = 1.0; ParseOptions po(usage); po.Register("binary", &binary, "Write in binary mode (only relevant if output is a wxfilename)"); po.Register("scale", &scale, "This option can be used to scale the matrices being copied."); po.Register("apply-log", &apply_log, "This option can be used to apply log on the matrices. " "Must be avoided if matrix has negative quantities."); po.Register("apply-exp", &apply_exp, "This option can be used to apply exp on the matrices"); po.Register("apply-power", &apply_power, "This option can be used to apply a power on the matrices"); po.Register("apply-softmax-per-row", &apply_softmax_per_row, "This option can be used to apply softmax per row of the matrices"); po.Read(argc, argv); if (po.NumArgs() != 2) { po.PrintUsage(); exit(1); } if ( (apply_log && apply_exp) || (apply_softmax_per_row && apply_exp) || (apply_softmax_per_row && apply_log) ) KALDI_ERR << "Only one of apply-log, apply-exp and " << "apply-softmax-per-row can be given"; std::string matrix_in_fn = po.GetArg(1), matrix_out_fn = po.GetArg(2); // all these "fn"'s are either rspecifiers or filenames. bool in_is_rspecifier = (ClassifyRspecifier(matrix_in_fn, NULL, NULL) != kNoRspecifier), out_is_wspecifier = (ClassifyWspecifier(matrix_out_fn, NULL, NULL, NULL) != kNoWspecifier); if (in_is_rspecifier != out_is_wspecifier) KALDI_ERR << "Cannot mix archives with regular files (copying matrices)"; if (!in_is_rspecifier) { Matrix<BaseFloat> mat; ReadKaldiObject(matrix_in_fn, &mat); if (scale != 1.0) mat.Scale(scale); if (apply_log) { mat.ApplyFloor(1.0e-20); mat.ApplyLog(); } if (apply_exp) mat.ApplyExp(); if (apply_softmax_per_row) ApplySoftMaxPerRow(&mat); if (apply_power != 1.0) mat.ApplyPow(apply_power); Output ko(matrix_out_fn, binary); mat.Write(ko.Stream(), binary); KALDI_LOG << "Copied matrix to " << matrix_out_fn; return 0; } else { int num_done = 0; BaseFloatMatrixWriter writer(matrix_out_fn); SequentialBaseFloatMatrixReader reader(matrix_in_fn); for (; !reader.Done(); reader.Next(), num_done++) { if (scale != 1.0 || apply_log || apply_exp || apply_power != 1.0 || apply_softmax_per_row) { Matrix<BaseFloat> mat(reader.Value()); if (scale != 1.0) mat.Scale(scale); if (apply_log) { mat.ApplyFloor(1.0e-20); mat.ApplyLog(); } if (apply_exp) mat.ApplyExp(); if (apply_softmax_per_row) ApplySoftMaxPerRow(&mat); if (apply_power != 1.0) mat.ApplyPow(apply_power); writer.Write(reader.Key(), mat); } else { writer.Write(reader.Key(), reader.Value()); } } KALDI_LOG << "Copied " << num_done << " matrices."; return (num_done != 0 ? 0 : 1); } } catch(const std::exception &e) { std::cerr << e.what(); return -1; } } |