train.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: utility main for training codebooks
  14. last mod: $Id: train.c,v 1.16.4.4 2000/04/21 16:35:40 xiphmont Exp $
  15. ********************************************************************/
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <signal.h>
  22. #include "vqgen.h"
  23. #include "vqext.h"
  24. #include "bookutil.h"
  25. static char *rline(FILE *in,FILE *out,int pass){
  26. while(1){
  27. char *line=get_line(in);
  28. if(line && line[0]=='#'){
  29. if(pass)fprintf(out,"%s\n",line);
  30. }else{
  31. return(line);
  32. }
  33. }
  34. }
  35. /* command line:
  36. trainvq vqfile [options] trainfile [trainfile]
  37. options: -params entries,dim,quant
  38. -subvector start[,num]
  39. -error desired_error
  40. -iterations iterations
  41. */
  42. static void usage(void){
  43. fprintf(stderr, "\nOggVorbis %s VQ codebook trainer\n\n"
  44. "<foo>vqtrain vqfile [options] [datasetfile] [datasetfile]\n"
  45. "options: -p[arams] <entries,dim,quant>\n"
  46. " -s[ubvector] <start[,num]>\n"
  47. " -e[rror] <desired_error>\n"
  48. " -i[terations] <maxiterations>\n"
  49. " -d[istance] desired minimum cell radius from midpoint\n"
  50. " -b <dummy> eliminate cell size biasing; use normal LBG\n\n"
  51. "examples:\n"
  52. " train a new codebook to 1%% tolerance on datafile 'foo':\n"
  53. " xxxvqtrain book -p 256,6,8 -e .01 foo\n"
  54. " (produces a trained set in book-0.vqi)\n\n"
  55. " continue training 'book-0.vqi' (produces book-1.vqi):\n"
  56. " xxxvqtrain book-0.vqi\n\n"
  57. " add subvector from element 1 to <dimension> from files\n"
  58. " data*.m to the training in progress, prodicing book-1.vqi:\n"
  59. " xxxvqtrain book-0.vqi -s 1,1 data*.m\n\n",vqext_booktype);
  60. }
  61. int exiting=0;
  62. void setexit(int dummy){
  63. fprintf(stderr,"\nexiting... please wait to finish this iteration\n");
  64. exiting=1;
  65. }
  66. int main(int argc,char *argv[]){
  67. vqgen v;
  68. int entries=-1,dim=-1;
  69. int start=0,num=-1;
  70. double desired=.05,mindist=0.;
  71. int iter=1000;
  72. int biasp=1;
  73. FILE *out=NULL;
  74. char *line;
  75. long i,j,k;
  76. int init=0;
  77. q.quant=-1;
  78. argv++;
  79. if(!*argv){
  80. usage();
  81. exit(0);
  82. }
  83. /* get the book name, a preexisting book to continue training */
  84. {
  85. FILE *in=NULL;
  86. char *filename=alloca(strlen(*argv)+30),*ptr;
  87. strcpy(filename,*argv);
  88. in=fopen(filename,"r");
  89. ptr=strrchr(filename,'-');
  90. if(ptr){
  91. int num;
  92. ptr++;
  93. num=atoi(ptr);
  94. sprintf(ptr,"%d.vqi",num+1);
  95. }else
  96. strcat(filename,"-0.vqi");
  97. out=fopen(filename,"w");
  98. if(out==NULL){
  99. fprintf(stderr,"Unable to open %s for writing\n",filename);
  100. exit(1);
  101. }
  102. if(in){
  103. /* we wish to suck in a preexisting book and continue to train it */
  104. double a;
  105. line=rline(in,out,1);
  106. if(strcmp(line,vqext_booktype)){
  107. fprintf(stderr,"wrong book type; %s!=%s\n",line,vqext_booktype);
  108. exit(1);
  109. }
  110. line=rline(in,out,1);
  111. if(sscanf(line,"%d %d %d",&entries,&dim,&vqext_aux)!=3){
  112. fprintf(stderr,"Syntax error reading book file\n");
  113. exit(1);
  114. }
  115. vqgen_init(&v,dim,vqext_aux,entries,mindist,
  116. vqext_metric,vqext_weight);
  117. init=1;
  118. /* quant setup */
  119. line=rline(in,out,1);
  120. if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
  121. &q.quant,&q.sequencep)!=4){
  122. fprintf(stderr,"Syntax error reading book file\n");
  123. exit(1);
  124. }
  125. /* quantized entries */
  126. i=0;
  127. for(j=0;j<entries;j++){
  128. for(k=0;k<dim;k++){
  129. line=rline(in,out,0);
  130. sscanf(line,"%lf",&a);
  131. v.entrylist[i++]=a;
  132. }
  133. }
  134. vqgen_unquantize(&v,&q);
  135. /* bias, points */
  136. i=0;
  137. for(j=0;j<entries;j++){
  138. line=rline(in,out,0);
  139. sscanf(line,"%lf",&a);
  140. v.bias[i++]=a;
  141. }
  142. {
  143. double *b=alloca((dim+vqext_aux)*sizeof(double));
  144. i=0;
  145. v.entries=0; /* hack to avoid reseeding */
  146. while(1){
  147. for(k=0;k<dim+vqext_aux;k++){
  148. line=rline(in,out,0);
  149. if(!line)break;
  150. sscanf(line,"%lf",b+k);
  151. }
  152. if(feof(in))break;
  153. vqgen_addpoint(&v,b,b+dim);
  154. }
  155. v.entries=entries;
  156. }
  157. fclose(in);
  158. }
  159. }
  160. /* get the rest... */
  161. argv=argv++;
  162. while(*argv){
  163. if(argv[0][0]=='-'){
  164. /* it's an option */
  165. if(!argv[1]){
  166. fprintf(stderr,"Option %s missing argument.\n",argv[0]);
  167. exit(1);
  168. }
  169. switch(argv[0][1]){
  170. case 'p':
  171. if(sscanf(argv[1],"%d,%d,%d",&entries,&dim,&q.quant)!=3)
  172. goto syner;
  173. break;
  174. case 's':
  175. if(sscanf(argv[1],"%d,%d",&start,&num)!=2){
  176. num= -1;
  177. if(sscanf(argv[1],"%d",&start)!=1)
  178. goto syner;
  179. }
  180. break;
  181. case 'e':
  182. if(sscanf(argv[1],"%lf",&desired)!=1)
  183. goto syner;
  184. break;
  185. case 'd':
  186. if(sscanf(argv[1],"%lf",&mindist)!=1)
  187. goto syner;
  188. break;
  189. case 'i':
  190. if(sscanf(argv[1],"%d",&iter)!=1)
  191. goto syner;
  192. break;
  193. case 'b':
  194. biasp=0;
  195. break;
  196. default:
  197. fprintf(stderr,"Unknown option %s\n",argv[0]);
  198. exit(1);
  199. }
  200. argv+=2;
  201. }else{
  202. /* it's an input file */
  203. char *file=strdup(*argv++);
  204. FILE *in;
  205. int cols=-1;
  206. if(!init){
  207. if(dim==-1 || entries==-1 || q.quant==-1){
  208. fprintf(stderr,"-p required when training a new set\n");
  209. exit(1);
  210. }
  211. vqgen_init(&v,dim,vqext_aux,entries,mindist,
  212. vqext_metric,vqext_weight);
  213. init=1;
  214. }
  215. in=fopen(file,"r");
  216. if(in==NULL){
  217. fprintf(stderr,"Could not open input file %s\n",file);
  218. exit(1);
  219. }
  220. fprintf(out,"# training file entry: %s\n",file);
  221. while((line=rline(in,out,0))){
  222. if(cols==-1){
  223. char *temp=line;
  224. while(*temp==' ')temp++;
  225. for(cols=0;*temp;cols++){
  226. while(*temp>32)temp++;
  227. while(*temp==' ')temp++;
  228. }
  229. }
  230. {
  231. int i;
  232. double b[cols];
  233. if(start+num*dim>cols){
  234. fprintf(stderr,"ran out of columns reading %s\n",file);
  235. exit(1);
  236. }
  237. while(*line==' ')line++;
  238. for(i=0;i<cols;i++){
  239. /* static length buffer bug workaround */
  240. char *temp=line;
  241. char old;
  242. while(*temp>32)temp++;
  243. old=temp[0];
  244. temp[0]='\0';
  245. b[i]=atof(line);
  246. temp[0]=old;
  247. while(*line>32)line++;
  248. while(*line==' ')line++;
  249. }
  250. if(num<=0)num=(cols-start)/dim;
  251. for(i=0;i<num;i++)
  252. vqext_addpoint_adj(&v,b,start+i*dim,dim,cols,num);
  253. }
  254. }
  255. fclose(in);
  256. }
  257. }
  258. if(!init){
  259. fprintf(stderr,"No input files!\n");
  260. exit(1);
  261. }
  262. vqext_preprocess(&v);
  263. /* train the book */
  264. signal(SIGTERM,setexit);
  265. signal(SIGINT,setexit);
  266. for(i=0;i<iter && !exiting;i++){
  267. double result;
  268. if(i!=0){
  269. vqgen_unquantize(&v,&q);
  270. vqgen_cellmetric(&v);
  271. }
  272. result=vqgen_iterate(&v,biasp);
  273. vqext_quantize(&v,&q);
  274. if(result<desired)break;
  275. }
  276. /* save the book */
  277. fprintf(out,"# OggVorbis VQ codebook trainer, intermediate file\n");
  278. fprintf(out,"%s\n",vqext_booktype);
  279. fprintf(out,"%d %d %d\n",entries,dim,vqext_aux);
  280. fprintf(out,"%ld %ld %d %d\n",
  281. q.min,q.delta,q.quant,q.sequencep);
  282. /* quantized entries */
  283. fprintf(out,"# quantized entries---\n");
  284. i=0;
  285. for(j=0;j<entries;j++)
  286. for(k=0;k<dim;k++)
  287. fprintf(out,"%d\n",(int)(rint(v.entrylist[i++])));
  288. fprintf(out,"# biases---\n");
  289. i=0;
  290. for(j=0;j<entries;j++)
  291. fprintf(out,"%f\n",v.bias[i++]);
  292. fprintf(out,"# points---\n");
  293. i=0;
  294. for(j=0;j<v.points;j++)
  295. for(k=0;k<dim+vqext_aux;k++)
  296. fprintf(out,"%f\n",v.pointlist[i++]);
  297. fclose(out);
  298. vqgen_unquantize(&v,&q);
  299. vqgen_cellmetric(&v);
  300. exit(0);
  301. syner:
  302. fprintf(stderr,"Syntax error in argument '%s'\n",*argv);
  303. exit(1);
  304. }