File: quantize_params

package info (click to toggle)
flite 2.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 83,796 kB
  • sloc: ansic: 693,645; sh: 5,000; lisp: 3,057; cpp: 994; makefile: 893; xml: 52; perl: 33
file content (274 lines) | stat: -rwxr-xr-x 10,340 bytes parent folder | download | duplicates (4)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/bin/sh
###########################################################################
##                                                                       ##
##                  Language Technologies Institute                      ##
##                     Carnegie Mellon University                        ##
##                         Copyright (c) 2017                            ##
##                        All Rights Reserved.                           ##
##                                                                       ##
##  Permission is hereby granted, free of charge, to use and distribute  ##
##  this software and its documentation without restriction, including   ##
##  without limitation the rights to use, copy, modify, merge, publish,  ##
##  distribute, sublicense, and/or sell copies of this work, and to      ##
##  permit persons to whom this work is furnished to do so, subject to   ##
##  the following conditions:                                            ##
##   1. The code must retain the above copyright notice, this list of    ##
##      conditions and the following disclaimer.                         ##
##   2. Any modifications must be clearly marked as such.                ##
##   3. Original authors' names are not deleted.                         ##
##   4. The authors' names are not used to endorse or promote products   ##
##      derived from this software without specific prior written        ##
##      permission.                                                      ##
##                                                                       ##
##  CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK         ##
##  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      ##
##  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   ##
##  SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE      ##
##  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    ##
##  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   ##
##  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          ##
##  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       ##
##  THIS SOFTWARE.                                                       ##
##                                                                       ##
###########################################################################
##             Author:  Alan W Black (awb@cs.cmu.edu)                    ##
##               Date:  June 2017                                        ##
###########################################################################
##  Quantize values in params file for smaller models in flite           ##
###########################################################################

# Compress (and decompress) a params file
. ./etc/voice.defs

if [ "$1" = "find_segments_wagon" ]
then
    # Note used anymore, but was used to test to find the "best" partition
    # of the range of values, i.e. minimize mean error.
    if [ ! -d ch ]
    then
        mkdir ch
    fi

    for i in `seq 0 103`
    do
       $ESTDIR/bin/ch_track -c $i festival/trees/old_cmu_us_slt_mcep.params |
           awk '{print $1,$1}' >ch/$i.out
       # sss=`echo $i | awk '{if (($1 % 2) == 0) print 15; else print 250}'`
       sss=250
       $ESTDIR/bin/wagon -stop $sss -desc x.desc -data ch/$i.out -o x.tree
       $ESTDIR/bin/wagon_test -desc x.desc -data ch/$i.out -tree x.tree -predict_val >ch/$i.outn
    done

    paste `seq 0 103 | awk '{for(i=1;i<=NF;i++) printf("ch/%d.outn ",$i)}'` |
        $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s 0.005 -o new.params
    
    exit 0
fi

if [ "$1" = "find_segments_quant" ]
then
    if [ ! -d ch ]
    then
        mkdir ch
    fi

    basemodel=festival/trees/old_cmu_us_slt_mcep.params
    if [ $# = 2 ]
    then
        basemodel=$2
    fi
    numquants=255
#    numquants=16
    numchannels=`$ESTDIR/bin/ch_track -info $basemodel | grep "Number of channels" | awk '{print $NF-1}'`

    for i in `seq 0 $numchannels`
    do
       echo Quantizing q$numquants channel $i of $numchannels
       $ESTDIR/bin/ch_track -c $i $basemodel |
           awk '{print $1}' >ch/$i.out
       ns=`cat ch/$i.out | awk 'END {print NR}'`
       sort -g ch/$i.out |
           awk 'BEGIN {numlines='$ns'; segsize=numlines/'$numquants'.0; q=0; seg=0;}
                { #print NR,(segsize*(1+seg)),t,q,$1
                  if (NR < (segsize*(1+seg)))
                  {
                     t+=$1;
                     q+=1;
                  }
                  else
                  {
                     printf("seg[ %d ]= %f \n",seg,t/q);
                     seg+=1;
                     q=0;
                     t=0;
                  }}' >ch/$i.segs
       echo "BEGIN {" >x.awk
       cat ch/$i.segs >>x.awk
       echo "}
             { b=0; bs=(\$1-seg[0])*(\$1-seg[0]);
               for (i=0; i<$numquants; i++)
               {
                  d=\$1-seg[i]
                  if (d*d < bs)
                  {
                     bs=d*d; b=i;
                  }
                  else if (d*d > bs)
                     i=$numquants
               }
               print b}" >>x.awk
       awk -f x.awk ch/$i.out >ch/$i.outn
    done

    # Make the q_table for this params file
    for i in `seq 0 $numchannels`
    do
        awk '{printf("%s ",$NF)} END {printf("\n")}' ch/$i.segs
    done |
        $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s 0.005 -o new.q_table        

    paste `seq 0 $numchannels | awk '{for(i=1;i<=NF;i++) printf("ch/%d.outn ",$i)}'` |
        awk '{ if (NF == 114)
               {
                   # This drops vector values that arent actually used to get
                   # better reduction -- its very specific to current rf3 builds
                   # no f0, statics, delta stddev, me means and voicing
                   for (i=3; i<3+50; i+=2)
                       printf("%d ",($i*256)+$(i+1)); # statics mean/stddev
                   for (i=53; i<53+48; i+=4)
                       printf("%d ",($(i+1)*256)+$(i+3));# deltas only stddev +1
                   printf("%d ",($102*256)+$(103)); # last delta stdev + me[0]
                   printf("%d ",($105*256)+$(107)); # me[1], me[2]
                   printf("%d ",($109*256)+$(111)); # me[3], me[4]
                   printf("%d ",($113*256)+$(114));  # voicing mean,stddev
                   printf("\n"); 
                }
                else
                {
                   # A more simple generic 8 bit compression
                   for (i=1; i<=NF; i+=2)
                     printf("%d ",($i*256)+$(i+1));
                   printf("\n");
               }}' |
           $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s 0.005 -o new.params

    mv new.params $basemodel.q_params
    mv new.q_table $basemodel.q_table
    
    exit 0

fi

if [ "$1" = "find_segments2" ]
then
    # This is used for debugging the params in festival space
    # it quantizes the values, rather than gives you the index to the values
    # so you can test the quality of quantization
    if [ ! -d ch ]
    then
        mkdir ch
    fi

    basemodel=festival/trees/old_cmu_us_slt_mcep.params
    if [ $# = 2 ]
    then
        basemodel=$2
    fi
    numquants=255
    numquants=16
    numchannels=`$ESTDIR/bin/ch_track -info $basemodel | grep "Number of channels" | awk '{print $NF-1}'`

    for i in `seq 0 $numchannels`
    do
       $ESTDIR/bin/ch_track -c $i $basemodel |
           awk '{print $1}' >ch/$i.out
       ns=`cat ch/$i.out | awk 'END {print NR}'`
       sort -g ch/$i.out |
           awk 'BEGIN {numlines='$ns'; segsize=numlines/'$numquants'.0; q=0; seg=0;}
                { #print NR,(segsize*(1+seg)),t,q,$1
                  if (NR < (segsize*(1+seg)))
                  {
                     t+=$1;
                     q+=1;
                  }
                  else
                  {
                     printf("seg[ %d ]= %f \n",seg,t/q);
                     seg+=1;
                     q=0;
                     t=0;
                  }}' >ch/$i.segs
       echo "BEGIN {" >x.awk
       cat ch/$i.segs >>x.awk
       echo "}
             { b=0; bs=(\$1-seg[0])*(\$1-seg[0]);
               for (i=0; i<$numquants; i++)
               {
                  d=\$1-seg[i]
                  if (d*d < bs)
                  {
                     bs=d*d; b=i;
                  }
               }
               print seg[b]}" >>x.awk
       awk -f x.awk ch/$i.out >ch/$i.outn
    done

    paste `seq 0 $numchannels | awk '{for(i=1;i<=NF;i++) printf("ch/%d.outn ",$i)}'` |
        $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s 0.005 -o new.params
    
    exit 0
fi

if [ "$1" = "quantize_rf_models" ]
then
    for i in `cat rf_models/mlist`
    do
        echo Quantizing model $i
        $0 find_segments_quant rf_models/trees_$i/${FV_VOICENAME}_mcep.params
    done

    exit
fi

if [ "$1" = "find_segments3" ]
then
    # Split into equal ranges, ignoring distribution of the ranges (i.e. bad)
    if [ ! -d ch ]
    then
        mkdir ch
    fi

    for i in `seq 0 103`
    do
       $ESTDIR/bin/ch_track -c $i festival/trees/old_cmu_us_slt_mcep.params |
           awk '{print $1,$1}' >ch/$i.out
       nmax=`cat ch/$i.out | awk '{if (NR == 1) t=$1; if ($1 > t) t=$1} END {print t}'`
       nmin=`cat ch/$i.out | awk '{if (NR == 1) t=$1; if ($1 < t) t=$1} END {print t}'`
       echo $nmin $nmax |
           awk '{ segsize = ($2-$1)/254.0;
                  for (i=0; i<254; i++)
                  {
                     printf("seg[%d]=%f\n",i,(0.5+i)*segsize);
                  }}' >ch/$i.segs
       echo "BEGIN {" >x.awk
       cat ch/$i.segs >>x.awk
       echo "}
             { b=0; bs=(\$1-seg[0])*(\$1-seg[0]);
               for (i=0; i<254; i++)
               {
                  d=\$1-seg[i]
                  if (d*d < bs)
                  {
                     bs=d*d; b=i;
                  }
               }
               print seg[b]}" >>x.awk
       awk -f x.awk ch/$i.out >ch/$i.outn
    done

    paste `seq 0 103 | awk '{for(i=1;i<=NF;i++) printf("ch/%d.outn ",$i)}'` |
        $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s 0.005 -o new.params
    
    exit 0
fi