summaryrefslogtreecommitdiff
path: root/kaldi_seq/tools/transf_kaldi2nerv.cpp
blob: 525bcdaea08f8fc8dc025df8c3e43dce76c88772 (plain) (blame)
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
#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;
}