diff options
author | Ted Yin <[email protected]> | 2015-10-12 09:26:53 +0800 |
---|---|---|
committer | Ted Yin <[email protected]> | 2015-10-12 09:26:53 +0800 |
commit | 0dba4c998fcccb4bae29582b7d8be94de476dd0b (patch) | |
tree | b8529d4f0c2ea0a91ee4b7a4b21a14c0616fc081 /kaldi_seq/tools | |
parent | 7acd14eca701deaffb2d16262528da37ee23263a (diff) | |
parent | e39fb231f64ddc8b79a6eb5434f529aadb3165fe (diff) |
Merge pull request #6 from yimmon/master
add kaldi_seq
Diffstat (limited to 'kaldi_seq/tools')
-rw-r--r-- | kaldi_seq/tools/net_kaldi2nerv.cpp | 85 | ||||
-rw-r--r-- | kaldi_seq/tools/transf_kaldi2nerv.cpp | 106 |
2 files changed, 191 insertions, 0 deletions
diff --git a/kaldi_seq/tools/net_kaldi2nerv.cpp b/kaldi_seq/tools/net_kaldi2nerv.cpp new file mode 100644 index 0000000..bbac3db --- /dev/null +++ b/kaldi_seq/tools/net_kaldi2nerv.cpp @@ -0,0 +1,85 @@ +#include <iostream> +#include <cstdio> +#include <cstring> +#include <cstdlib> +#include <cassert> +using namespace std; + +const char fmt[] = "[%013d]\n"; + +int main(int argc, char *argv[]) +{ + if(argc < 3){ + printf("Usage: %s kaldi_nnet nerv_output\n", argv[0]); + exit(0); + } + + FILE *fin = fopen(argv[1], "r"); + FILE *fout = fopen(argv[2], "w"); + + if(!fin || !fout){ + printf("fopen error\n"); + exit(1); + } + + char buf[1024], tag[64]; + int a, b; + char ***arr; + long start, size; + int affine_ltp = 0, affine_bp = 0; + + while(fgets(buf, 1024, fin)){ + if(sscanf(buf, "%s%d%d", tag, &b, &a) == 3 && strcmp(tag, "<AffineTransform>") == 0){ + fgets(buf, 1024, fin); + arr = new char**[a]; + for(int i = 0; i < a; i++) + arr[i] = new char*[b]; + for(int j = 0; j < b; j++) + for(int i = 0; i < a; i++){ + arr[i][j] = new char[16]; + fscanf(fin, "%s", arr[i][j]); + } + + start = ftell(fout); + fprintf(fout, fmt, 0); + fprintf(fout, "{type=\"nerv.LinearTransParam\",id=\"affine%d_ltp\"}\n", affine_ltp++); + fprintf(fout, "%d %d\n", a, b); + for(int i = 0; i < a; i++){ + for(int j = 0; j < b; j++){ + fprintf(fout, "%s ", arr[i][j]); + delete [] arr[i][j]; + } + fprintf(fout, "\n"); + delete [] arr[i]; + } + delete [] arr; + + size = ftell(fout) - start; + fseek(fout, start, SEEK_SET); + fprintf(fout, fmt, (int)size); + fseek(fout, 0, SEEK_END); + + fgets(buf, 1024, fin); + fscanf(fin, "%*s"); + + start = ftell(fout); + fprintf(fout, fmt, 0); + fprintf(fout, "{type=\"nerv.BiasParam\",id=\"affine%d_bp\"}\n", affine_bp++); + fprintf(fout, "%d %d\n", 1, b); + for(int i = 0; i < b; i++){ + fscanf(fin, "%s", buf); + fprintf(fout, "%s ", buf); + } + fputs("\n", fout); + size = ftell(fout) - start; + fseek(fout, start, SEEK_SET); + fprintf(fout, fmt, (int)size); + fseek(fout, 0, SEEK_END); + } + } + + fclose(fin); + fclose(fout); + + return 0; +} diff --git a/kaldi_seq/tools/transf_kaldi2nerv.cpp b/kaldi_seq/tools/transf_kaldi2nerv.cpp new file mode 100644 index 0000000..525bcda --- /dev/null +++ b/kaldi_seq/tools/transf_kaldi2nerv.cpp @@ -0,0 +1,106 @@ +#include <iostream> +#include <cstdio> +#include <cstring> +#include <cstdlib> +#include <cassert> +using namespace std; + +const char fmt[] = "[%013d]\n"; + +int main(int argc, char *argv[]) +{ + if(argc < 3){ + printf("Usage: %s kaldi_transf nerv_output\n", argv[0]); + exit(1); + } + + FILE *fin = fopen(argv[1], "r"); + FILE *fout = fopen(argv[2], "w"); + if(!fin || !fout){ + puts("fopen error"); + exit(1); + } + + char buf[1024], tag[64]; + int a, b; + int size_window, size_bias; + char **window, **bias; + + while(fgets(buf, sizeof(buf), fin)) + { + if(sscanf(buf, "%s%d%d", tag, &a, &b) == 3){ + if(strcmp(tag, "<AddShift>") == 0){ + assert(a == b); + size_bias = a; + fscanf(fin, "%*s%*s%*s"); + bias = new char*[size_bias]; + for(int i = 0; i < size_bias; i++){ + bias[i] = new char[16]; + fscanf(fin, "%s", bias[i]); + } + } else if(strcmp(tag, "<Rescale>") == 0){ + assert(a == b); + size_window = a; + fscanf(fin, "%*s%*s%*s"); + window = new char*[size_window]; + for(int i = 0; i < size_window; i++){ + window[i] = new char[16]; + fscanf(fin, "%s", window[i]); + } + } + } + } + + long start = ftell(fout), size; + fprintf(fout, fmt, 0); + fprintf(fout, "{id = \"bias1\", type = \"nerv.MatrixParam\"}\n"); + fprintf(fout, "1 %d\n", size_bias); + for(int i = 0; i<size_bias; i++) + fprintf(fout, "0 "); + fputs("\n", fout); + size = ftell(fout) - start; + fseek(fout, start, SEEK_SET); + fprintf(fout, fmt, (int)size); + fseek(fout, 0, SEEK_END); + + start = ftell(fout); + fprintf(fout, fmt, 0); + fprintf(fout, "{id = \"window1\", type = \"nerv.MatrixParam\"}\n"); + fprintf(fout, "1 %d\n", size_window); + for(int i = 0; i<size_window; i++) + fprintf(fout, "1 "); + fputs("\n", fout); + size = ftell(fout) - start; + fseek(fout, start, SEEK_SET); + fprintf(fout, fmt, (int)size); + fseek(fout, 0, SEEK_END); + + start = ftell(fout); + fprintf(fout, fmt, 0); + fprintf(fout, "{id = \"bias2\", type = \"nerv.MatrixParam\"}\n"); + fprintf(fout, "1 %d\n", size_bias); + for(int i = 0; i<size_bias; i++) + fprintf(fout, "%s ", bias[i]); + fputs("\n", fout); + size = ftell(fout) - start; + fseek(fout, start, SEEK_SET); + fprintf(fout, fmt, (int)size); + fseek(fout, 0, SEEK_END); + + start = ftell(fout); + fprintf(fout, fmt, 0); + fprintf(fout, "{id = \"window2\", type = \"nerv.MatrixParam\"}\n"); + fprintf(fout, "1 %d\n", size_window); + for(int i = 0; i<size_window; i++) + fprintf(fout, "%s ", window[i]); + fputs("\n", fout); + size = ftell(fout) - start; + fseek(fout, start, SEEK_SET); + fprintf(fout, fmt, (int)size); + fseek(fout, 0, SEEK_END); + + fclose(fin); + fclose(fout); + + return 0; +} |