From e56c91ff6eecdb1663bb1722a4081ef2f190e9c0 Mon Sep 17 00:00:00 2001 From: uphantom Date: Fri, 28 Aug 2015 18:37:12 +0800 Subject: suport multi-thread reader --- htk_io/tools/tnet_to_nerv.cpp | 78 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 8 deletions(-) (limited to 'htk_io/tools') diff --git a/htk_io/tools/tnet_to_nerv.cpp b/htk_io/tools/tnet_to_nerv.cpp index a779a25..f96781a 100644 --- a/htk_io/tools/tnet_to_nerv.cpp +++ b/htk_io/tools/tnet_to_nerv.cpp @@ -2,14 +2,22 @@ #include #include #include -#include +#include char token[1024]; char output[1024]; double **mat; int main(int argc, char **argv) { + + if (argc != 3) + { + printf("%s tnet.model.in nerv.model.out\n", argv[0]); + } + std::ofstream fout; - fout.open(argv[1]); - int cnt = 0; + freopen(argv[1], "r", stdin); + fout.open(argv[2]); + int cnt = 0, bias = 1, win = 1; + long length = 0, base = 0; while (scanf("%s", token) != EOF) { int nrow, ncol; @@ -19,13 +27,13 @@ int main(int argc, char **argv) { scanf("%d %d", &ncol, &nrow); scanf("%s %d %d", token, &ncol, &nrow); printf("%d %d\n", nrow, ncol); - mat = (double **)malloc(nrow * sizeof(double *)); + mat = (double **)malloc(nrow * sizeof(double *)); for (i = 0; i < nrow; i++) mat[i] = (double *)malloc(ncol * sizeof(double)); for (j = 0; j < ncol; j++) for (i = 0; i < nrow; i++) scanf("%lf", mat[i] + j); - long base = fout.tellp(); + base = fout.tellp(); sprintf(output, "%16d", 0); fout << output; sprintf(output, "{type=\"nerv.LinearTransParam\",id=\"affine%d_ltp\"}\n", @@ -38,10 +46,8 @@ int main(int argc, char **argv) { for (j = 0; j < ncol; j++) fout << mat[i][j] << " "; fout << std::endl; - free(mat[i]); } - free(mat); - long length = fout.tellp() - base; + length = fout.tellp() - base; fout.seekp(base); sprintf(output, "[%13lu]\n", length); fout << output; @@ -69,6 +75,62 @@ int main(int argc, char **argv) { cnt++; } } + else if (strcmp(token, "") == 0) + { + scanf("%d %d", &ncol, &nrow); + scanf("%s %d", token, &ncol); + base = fout.tellp(); + nrow = 1; + mat = (double **)malloc(nrow * sizeof(double *)); + for (i = 0; i < nrow; i++) + mat[i] = (double *)malloc(ncol * sizeof(double)); + for (j = 0; j < ncol; j++) + for (i = 0; i < nrow; i++) + scanf("%lf", mat[i] + j); + sprintf(output, "%16d", 0); + fout << output; + sprintf(output, "{type=\"nerv.MatrixParam\",id=\"bias%d\"}\n",bias); + fout << output; + sprintf(output, "1 %d\n", ncol); + fout << output; + for (j = 0; j < ncol; j++) + fout << mat[0][j] << " "; + fout << std::endl; + length = fout.tellp() - base; + fout.seekp(base); + sprintf(output, "[%13lu]\n", length); + fout << output; + fout.seekp(0, std::ios_base::end); + bias++; + } + else if (strcmp(token, "") == 0) + { + scanf("%d %d", &ncol, &nrow); + scanf("%s %d", token, &ncol); + base = fout.tellp(); + nrow = 1; + mat = (double **)malloc(nrow * sizeof(double *)); + for (i = 0; i < nrow; i++) + mat[i] = (double *)malloc(ncol * sizeof(double)); + for (j = 0; j < ncol; j++) + for (i = 0; i < nrow; i++) + scanf("%lf", mat[i] + j); + sprintf(output, "%16d", 0); + fout << output; + sprintf(output, "{type=\"nerv.MatrixParam\",id=\"window%d\"}\n",win); + fout << output; + sprintf(output, "1 %d\n", ncol); + fout << output; + for (j = 0; j < ncol; j++) + fout << mat[0][j] << " "; + fout << std::endl; + length = fout.tellp() - base; + fout.seekp(base); + sprintf(output, "[%13lu]\n", length); + fout << output; + fout.seekp(0, std::ios_base::end); + win++; + } } return 0; } -- cgit v1.2.3 From 2196e0a591b9bc254aa95e180adf188fd70ded68 Mon Sep 17 00:00:00 2001 From: uphantom Date: Mon, 31 Aug 2015 09:01:10 +0800 Subject: transfer nerv to tnet model. --- htk_io/tools/nerv_to_tnet.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 htk_io/tools/nerv_to_tnet.cpp (limited to 'htk_io/tools') diff --git a/htk_io/tools/nerv_to_tnet.cpp b/htk_io/tools/nerv_to_tnet.cpp new file mode 100644 index 0000000..91be7db --- /dev/null +++ b/htk_io/tools/nerv_to_tnet.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include + +char token[1024]; +typedef struct node +{ + double **mat; + int nrow; + int ncol; + int index; +}node; + +int main(int argc, char **argv) +{ + if (argc != 3) + { + printf("%s nerv.model.in tnet.model.out\n", argv[0]); + } + + freopen(argv[1], "r", stdin); + freopen(argv[2], "w", stdout); + node* trans[100], *bp[100]; + int layers = 0; + int i, j, k; + char type[128]; + int idx = 0; + char *flag = "affine"; + while (NULL != gets(token)) + { + int nrow, ncol; + if (token[0] == '{') + { + char *ss = strstr(token, flag); + if (sscanf(ss, "affine%d_%[a-z]", &idx, type) == 2) + { + node *no = new node; + scanf("%d %d", &nrow, &ncol); + no->nrow = nrow; + no->ncol = ncol; + no->index = idx; + no->mat = (double **)malloc(nrow * sizeof(double *)); + for (i = 0; i < nrow; i++) + no->mat[i] = (double *)malloc(ncol * sizeof(double)); + for (i = 0; i < nrow; i++) + for (j = 0; j < ncol; j++) + scanf("%lf", no->mat[i] + j); + + if (nrow == 1) + bp[idx] = no; + else + trans[idx] = no; + layers++; + } + } + } + if (layers%2) + { + perror("number layers of LinearTrans and Bias is not the same.\n"); + return -1; + } + + layers /= 2; + for (i = 0; i < layers; i++) + { + printf(" %d %d\n", trans[i]->ncol, trans[i]->nrow); + printf("m %d %d\n", trans[i]->ncol, trans[i]->nrow); + for (j = 0; j < trans[i]->ncol; j++) + { + for (k = 0; k < trans[i]->nrow; k++) + printf("%.10lf ", trans[i]->mat[k][j]); + printf("\n"); + } + printf("v %d", bp[i]->ncol); + for (j = 0; j < bp[i]->ncol; j++) + printf(" %.10lf", bp[i]->mat[0][j]); + printf("\n"); + if (i != layers - 1 && trans[i+1]->ncol < trans[i+1]->nrow) + printf(" %d %d\n", bp[i]->ncol, bp[i]->ncol); + } + printf(" %d %d\n", bp[layers - 1]->ncol, bp[layers - 1]->ncol); + perror("done!\n"); + + return 0; +} + -- cgit v1.2.3