convert_states.pl
1.62 KB
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
#!/usr/bin/perl
@ARGV ==1 || die "Usage: convert_states.pl dimension < states.txt > kaldi.am_gmm";
$dim = shift @ARGV;
$maxpdf = 0;
while(<>) {
m/\<STATE\> (\d+) (\d+)/ || die "bad line $_";
$pdf = $1;
if($pdf > $maxpdf) { $maxpdf = $pdf; }
$nummix{$pdf} = $2;
for($mix = 0; $mix < $nummix{$pdf}; $mix++) {
$l = <>;
chop $l;
$weight{$pdf,$mix} = $l;
$l = <>;
@A = split(" ", $l);
@A == $dim || die "Dimension mismatch.\n";
for($d = 0; $d < $dim; $d++) {
$mean{$pdf,$mix,$d} = $A[$d];
}
$l = <>;
@A = split(" ", $l);
@A == $dim || die "Dimension mismatch.\n";
for($d = 0; $d < $dim; $d++) {
$var{$pdf,$mix,$d} = $A[$d];
}
}
}
$numpdfs = $maxpdf+1;
print "<DIMENSION> 39 \n";
print "<NUMPDFS> $numpdfs\n";
for($pdf = 0; $pdf < $numpdfs; $pdf++) {
defined $nummix{$pdf} || die "No nummix defined for pdf = $pdf\n";
print " <DiagGMMBegin>\n";
$nm = $nummix{$pdf};
print " <WEIGHTS> [ ";
for($n = 0; $n < $nm; $n++) { print "$weight{$pdf,$n} "; }
print "]\n";
print " <MEANS_INVVARS> [\n";
for($n = 0; $n < $nm; $n++) {
for($d = 0; $d < $dim; $d++) {
$val = $mean{$pdf,$n,$d} / $var{$pdf,$n,$d};
print "$val ";
}
print "\n";
}
print " ]\n";
print " <INV_VARS> [\n";
for($n = 0; $n < $nm; $n++) {
for($d = 0; $d < $dim; $d++) {
$val = 1.0 / $var{$pdf,$n,$d};
print "$val ";
}
print "\n";
}
print " ]\n";
print " <DiagGMMEnd>\n";
}