Debian Codegraph @

tools/quantize_params

raw
1 #!/bin/sh
2 ###########################################################################
3 ## ##
4 ## Language Technologies Institute ##
5 ## Carnegie Mellon University ##
6 ## Copyright (c) 2017 ##
7 ## All Rights Reserved. ##
8 ## ##
9 ## Permission is hereby granted, free of charge, to use and distribute ##
10 ## this software and its documentation without restriction, including ##
11 ## without limitation the rights to use, copy, modify, merge, publish, ##
12 ## distribute, sublicense, and/or sell copies of this work, and to ##
13 ## permit persons to whom this work is furnished to do so, subject to ##
14 ## the following conditions: ##
15 ## 1. The code must retain the above copyright notice, this list of ##
16 ## conditions and the following disclaimer. ##
17 ## 2. Any modifications must be clearly marked as such. ##
18 ## 3. Original authors' names are not deleted. ##
19 ## 4. The authors' names are not used to endorse or promote products ##
20 ## derived from this software without specific prior written ##
21 ## permission. ##
22 ## ##
23 ## CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK ##
24 ## DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ##
25 ## ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT ##
26 ## SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE ##
27 ## FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ##
28 ## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN ##
29 ## AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ##
30 ## ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF ##
31 ## THIS SOFTWARE. ##
32 ## ##
33 ###########################################################################
34 ## Author: Alan W Black ([email protected]) ##
35 ## Date: June 2017 ##
36 ###########################################################################
37 ## Quantize values in params file for smaller models in flite ##
38 ###########################################################################
39
40 # Compress (and decompress) a params file
41 . ./etc/voice.defs
42
43 if [ "$1" = "find_segments_wagon" ]
44 then
45 # Note used anymore, but was used to test to find the "best" partition
46 # of the range of values, i.e. minimize mean error.
47 if [ ! -d ch ]
48 then
49 mkdir ch
50 fi
51
52 for i in `seq 0 103`
53 do
54 $ESTDIR/bin/ch_track -c $i festival/trees/old_cmu_us_slt_mcep.params |
55 awk '{print $1,$1}' >ch/$i.out
56 # sss=`echo $i | awk '{if (($1 % 2) == 0) print 15; else print 250}'`
57 sss=250
58 $ESTDIR/bin/wagon -stop $sss -desc x.desc -data ch/$i.out -o x.tree
59 $ESTDIR/bin/wagon_test -desc x.desc -data ch/$i.out -tree x.tree -predict_val >ch/$i.outn
60 done
61
62 paste `seq 0 103 | awk '{for(i=1;i<=NF;i++) printf("ch/%d.outn ",$i)}'` |
63 $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s 0.005 -o new.params
64
65 exit 0
66 fi
67
68 if [ "$1" = "find_segments_quant" ]
69 then
70 if [ ! -d ch ]
71 then
72 mkdir ch
73 fi
74
75 basemodel=festival/trees/old_cmu_us_slt_mcep.params
76 if [ $# = 2 ]
77 then
78 basemodel=$2
79 fi
80 numquants=255
81 # numquants=16
82 numchannels=`$ESTDIR/bin/ch_track -info $basemodel | grep "Number of channels" | awk '{print $NF-1}'`
83
84 for i in `seq 0 $numchannels`
85 do
86 echo Quantizing q$numquants channel $i of $numchannels
87 $ESTDIR/bin/ch_track -c $i $basemodel |
88 awk '{print $1}' >ch/$i.out
89 ns=`cat ch/$i.out | awk 'END {print NR}'`
90 sort -g ch/$i.out |
91 awk 'BEGIN {numlines='$ns'; segsize=numlines/'$numquants'.0; q=0; seg=0;}
92 { #print NR,(segsize*(1+seg)),t,q,$1
93 if (NR < (segsize*(1+seg)))
94 {
95 t+=$1;
96 q+=1;
97 }
98 else
99 {
100 printf("seg[ %d ]= %f \n",seg,t/q);
101 seg+=1;
102 q=0;
103 t=0;
104 }}' >ch/$i.segs
105 echo "BEGIN {" >x.awk
106 cat ch/$i.segs >>x.awk
107 echo "}
108 { b=0; bs=(\$1-seg[0])*(\$1-seg[0]);
109 for (i=0; i<$numquants; i++)
110 {
111 d=\$1-seg[i]
112 if (d*d < bs)
113 {
114 bs=d*d; b=i;
115 }
116 else if (d*d > bs)
117 i=$numquants
118 }
119 print b}" >>x.awk
120 awk -f x.awk ch/$i.out >ch/$i.outn
121 done
122
123 # Make the q_table for this params file
124 for i in `seq 0 $numchannels`
125 do
126 awk '{printf("%s ",$NF)} END {printf("\n")}' ch/$i.segs
127 done |
128 $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s 0.005 -o new.q_table
129
130 paste `seq 0 $numchannels | awk '{for(i=1;i<=NF;i++) printf("ch/%d.outn ",$i)}'` |
131 awk '{ if (NF == 114)
132 {
133 # This drops vector values that arent actually used to get
134 # better reduction -- its very specific to current rf3 builds
135 # no f0, statics, delta stddev, me means and voicing
136 for (i=3; i<3+50; i+=2)
137 printf("%d ",($i*256)+$(i+1)); # statics mean/stddev
138 for (i=53; i<53+48; i+=4)
139 printf("%d ",($(i+1)*256)+$(i+3));# deltas only stddev +1
140 printf("%d ",($102*256)+$(103)); # last delta stdev + me[0]
141 printf("%d ",($105*256)+$(107)); # me[1], me[2]
142 printf("%d ",($109*256)+$(111)); # me[3], me[4]
143 printf("%d ",($113*256)+$(114)); # voicing mean,stddev
144 printf("\n");
145 }
146 else
147 {
148 # A more simple generic 8 bit compression
149 for (i=1; i<=NF; i+=2)
150 printf("%d ",($i*256)+$(i+1));
151 printf("\n");
152 }}' |
153 $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s 0.005 -o new.params
154
155 mv new.params $basemodel.q_params
156 mv new.q_table $basemodel.q_table
157
158 exit 0
159
160 fi
161
162 if [ "$1" = "find_segments2" ]
163 then
164 # This is used for debugging the params in festival space
165 # it quantizes the values, rather than gives you the index to the values
166 # so you can test the quality of quantization
167 if [ ! -d ch ]
168 then
169 mkdir ch
170 fi
171
172 basemodel=festival/trees/old_cmu_us_slt_mcep.params
173 if [ $# = 2 ]
174 then
175 basemodel=$2
176 fi
177 numquants=255
178 numquants=16
179 numchannels=`$ESTDIR/bin/ch_track -info $basemodel | grep "Number of channels" | awk '{print $NF-1}'`
180
181 for i in `seq 0 $numchannels`
182 do
183 $ESTDIR/bin/ch_track -c $i $basemodel |
184 awk '{print $1}' >ch/$i.out
185 ns=`cat ch/$i.out | awk 'END {print NR}'`
186 sort -g ch/$i.out |
187 awk 'BEGIN {numlines='$ns'; segsize=numlines/'$numquants'.0; q=0; seg=0;}
188 { #print NR,(segsize*(1+seg)),t,q,$1
189 if (NR < (segsize*(1+seg)))
190 {
191 t+=$1;
192 q+=1;
193 }
194 else
195 {
196 printf("seg[ %d ]= %f \n",seg,t/q);
197 seg+=1;
198 q=0;
199 t=0;
200 }}' >ch/$i.segs
201 echo "BEGIN {" >x.awk
202 cat ch/$i.segs >>x.awk
203 echo "}
204 { b=0; bs=(\$1-seg[0])*(\$1-seg[0]);
205 for (i=0; i<$numquants; i++)
206 {
207 d=\$1-seg[i]
208 if (d*d < bs)
209 {
210 bs=d*d; b=i;
211 }
212 }
213 print seg[b]}" >>x.awk
214 awk -f x.awk ch/$i.out >ch/$i.outn
215 done
216
217 paste `seq 0 $numchannels | awk '{for(i=1;i<=NF;i++) printf("ch/%d.outn ",$i)}'` |
218 $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s 0.005 -o new.params
219
220 exit 0
221 fi
222
223 if [ "$1" = "quantize_rf_models" ]
224 then
225 for i in `cat rf_models/mlist`
226 do
227 echo Quantizing model $i
228 $0 find_segments_quant rf_models/trees_$i/${FV_VOICENAME}_mcep.params
229 done
230
231 exit
232 fi
233
234 if [ "$1" = "find_segments3" ]
235 then
236 # Split into equal ranges, ignoring distribution of the ranges (i.e. bad)
237 if [ ! -d ch ]
238 then
239 mkdir ch
240 fi
241
242 for i in `seq 0 103`
243 do
244 $ESTDIR/bin/ch_track -c $i festival/trees/old_cmu_us_slt_mcep.params |
245 awk '{print $1,$1}' >ch/$i.out
246 nmax=`cat ch/$i.out | awk '{if (NR == 1) t=$1; if ($1 > t) t=$1} END {print t}'`
247 nmin=`cat ch/$i.out | awk '{if (NR == 1) t=$1; if ($1 < t) t=$1} END {print t}'`
248 echo $nmin $nmax |
249 awk '{ segsize = ($2-$1)/254.0;
250 for (i=0; i<254; i++)
251 {
252 printf("seg[%d]=%f\n",i,(0.5+i)*segsize);
253 }}' >ch/$i.segs
254 echo "BEGIN {" >x.awk
255 cat ch/$i.segs >>x.awk
256 echo "}
257 { b=0; bs=(\$1-seg[0])*(\$1-seg[0]);
258 for (i=0; i<254; i++)
259 {
260 d=\$1-seg[i]
261 if (d*d < bs)
262 {
263 bs=d*d; b=i;
264 }
265 }
266 print seg[b]}" >>x.awk
267 awk -f x.awk ch/$i.out >ch/$i.outn
268 done
269
270 paste `seq 0 103 | awk '{for(i=1;i<=NF;i++) printf("ch/%d.outn ",$i)}'` |
271 $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s 0.005 -o new.params
272
273 exit 0
274 fi