File: prune_mixw.py

package info (click to toggle)
sphinxtrain 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,572 kB
  • sloc: ansic: 94,052; perl: 8,939; python: 6,702; cpp: 2,044; makefile: 6
file content (203 lines) | stat: -rw-r--r-- 5,752 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3

import sys
import numpy
import struct
from cmusphinx import s3mixw


def perplexity(dist):
    return numpy.exp(-(dist * numpy.log(dist)).sum())


def prune_mixw_entropy(mixw, avgn):
    # Calculate average entropy
    avgp = 0
    count = 0
    for m in mixw:
        for f in m:
            pplx = perplexity(f)
            avgp += pplx
            count += 1
    avgp /= count
    scale = float(avgn) / avgp
    avgtop = 0
    mintop = 999
    maxtop = 0
    histo = numpy.zeros(len(mixw[0, 0]), 'i')
    for m in mixw:
        for f in m:
            pplx = perplexity(f)
            top = round(pplx * scale)
            if top < len(f):
                histo[top] += 1
            avgtop += top
            if top < mintop:
                mintop = top
            if top > maxtop:
                maxtop = top
            f.put(f.argsort()[:-top], 0)
    print("Average #mixw: %.2f" % (float(avgtop) / count))
    print("Min #mixw: %d Max #mixw: %d" % (mintop, maxtop))
    return histo


def prune_mixw_entropy_min(mixw, avgn, minn):
    # Calculate average entropy
    avgp = 0
    count = 0
    for m in mixw:
        for f in m:
            pplx = perplexity(f)
            avgp += pplx
            count += 1
    avgp /= count
    scale = float(avgn) / avgp
    avgtop = 0
    mintop = 999
    maxtop = 0
    histo = numpy.zeros(len(mixw[0, 0]), 'i')
    for m in mixw:
        for f in m:
            pplx = perplexity(f)
            top = round(pplx * scale)
            if top < minn:
                top = minn
            elif top >= len(f):
                top = len(f) - 1
            else:
                histo[top] += 1
            avgtop += top
            if top < mintop:
                mintop = top
            if top > maxtop:
                maxtop = top
            f.put(f.argsort()[:-top], 0)
    print("Average #mixw: %.2f" % (float(avgtop) / count))
    print("Min #mixw: %d Max #mixw: %d" % (mintop, maxtop))
    return histo


def prune_mixw_pplx_hist(mixw):
    # Calculate perplexity histogram
    histo = numpy.zeros(len(mixw[0, 0]), 'i')
    for m in mixw:
        for f in m:
            pplx = perplexity(f)
            histo[round(pplx)] += 1
    # Floor number of mixture weights at the mode of perplexity
    minn = histo.argmax()
    avgtop = 0
    mintop = 999
    maxtop = 0
    for m in mixw:
        for f in m:
            top = round(perplexity(f))
            avgtop += top
            if top < minn:
                top = minn
            if top < mintop:
                mintop = top
            if top > maxtop:
                maxtop = top
            f.put(f.argsort()[:-top], 0)
    count = mixw.shape[0] * mixw.shape[1]
    print("Average #mixw: %.2f" % (float(avgtop) / count))
    print("Min #mixw: %d Max #mixw: %d" % (mintop, maxtop))
    return histo


def prune_mixw_topn(mixw, n):
    for m in mixw:
        for f in m:
            f.put(f.argsort()[:-n], 0)


def prune_mixw_thresh(mixw, thresh):
    avgtop = 0
    mintop = 999
    maxtop = 0
    histo = numpy.zeros(len(mixw[0, 0]), 'i')
    for m in mixw:
        for f in m:
            toprune = numpy.less(f, thresh).nonzero()[0]
            top = len(f) - len(toprune)
            histo[top] += 1
            avgtop += top
            if top < mintop:
                mintop = top
            if top > maxtop:
                maxtop = top
            f.put(toprune, 0)
    count = mixw.shape[0] * mixw.shape[1]
    print("Average #mixw: %.2f" % (float(avgtop) / count))
    print("Min #mixw: %d Max #mixw: %d" % (mintop, maxtop))
    return histo


def norm_floor_mixw(mixw, floor=1e-7):
    return (mixw.T / mixw.T.sum(0)).T.clip(floor, 1.0)


fmtdesc = \
"""BEGIN FILE FORMAT DESCRIPTION
(int32) <length(string)> (including trailing 0)
<string> (including trailing 0)
... preceding 2 items repeated any number of times
(int32) 0 (length(string)=0 terminates the header)
(int32) <#components>
(int32) <#gmms>
#gmms (unsigned char) quantized mixture weights for feature-0 gmm-0
preceding 2 items repeated feature_count times.
preceding 4 items repeated codebook_count times.
END FILE FORMAT DESCRIPTION
cluster_count 0
logbase 1.0001
codebook_count 1
feature_count %d"""


def write_sendump(mixw, outfile, floor=1e-7):
    n_sen, n_feat, n_gau = mixw.shape
    fh = open(outfile, "wb")
    # Write the header
    fmtdesc0 = fmtdesc % (n_feat)
    for line in fmtdesc0.split('\n'):
        fh.write(struct.pack('>I', len(line) + 1))
        fh.write(line)
        fh.write('\0')
    # Align to 4 bytes
    k = fh.tell() & 3
    if k > 0:
        k = 4 - k
        fh.write(struct.pack('>I', k))
        fh.write('!' * k)
    fh.write(struct.pack('>I', 0))
    # Align number of senones to 4 bytes
    aligned_n_sen = (n_sen + 3) & ~3
    fh.write(struct.pack('>I', n_gau))
    fh.write(struct.pack('>I', aligned_n_sen))
    # Write them out transposed and quantized (could be much faster)
    if floor == 0.0:
        # Assume they are already normalized and floored
        qmixw = (-numpy.log(mixw) / numpy.log(1.0001)).astype('i') >> 10
    else:
        qmixw = (-numpy.log(norm_floor_mixw(mixw, floor)) /
                 numpy.log(1.0001)).astype('i') >> 10
    qmixw = qmixw.clip(0, 159).astype('uint8')
    for f in range(0, n_feat):
        for d in range(0, n_gau):
            qmixw[:, f, d].tofile(fh)
            # Align it to 4 byte boundary (why?)
            if aligned_n_sen > n_sen:
                fh.write('\0' * (aligned_n_sen - n_sen))
    fh.close()


if __name__ == '__main__':
    ifn, ofn, tmw, mmw = sys.argv[1:]
    tmw = int(tmw)
    mmw = int(mmw)
    mixw = norm_floor_mixw(s3mixw.open(ifn).getall())
    prune_mixw_entropy_min(mixw, tmw, mmw)
    write_sendump(mixw, ofn)