train.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: utility main for training codebooks
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <math.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <signal.h>
  21. #include "vqgen.h"
  22. #include "vqext.h"
  23. #include "bookutil.h"
  24. static char *rline(FILE *in,FILE *out,int pass){
  25. while(1){
  26. char *line=get_line(in);
  27. if(line && line[0]=='#'){
  28. if(pass)fprintf(out,"%s\n",line);
  29. }else{
  30. return(line);
  31. }
  32. }
  33. }
  34. /* command line:
  35. trainvq vqfile [options] trainfile [trainfile]
  36. options: -params entries,dim,quant
  37. -subvector start[,num]
  38. -error desired_error
  39. -iterations iterations
  40. */
  41. static void usage(void){
  42. fprintf(stderr, "\nOggVorbis %s VQ codebook trainer\n\n"
  43. "<foo>vqtrain vqfile [options] [datasetfile] [datasetfile]\n"
  44. "options: -p[arams] <entries,dim,quant>\n"
  45. " -s[ubvector] <start[,num]>\n"
  46. " -e[rror] <desired_error>\n"
  47. " -i[terations] <maxiterations>\n"
  48. " -d[istance] quantization mesh spacing for density limitation\n"
  49. " -b <dummy> eliminate cell size biasing; use normal LBG\n\n"
  50. " -c <dummy> Use centroid (not median) midpoints\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. float desired=.05f,mindist=0.f;
  71. int iter=1000;
  72. int biasp=1;
  73. int centroid=0;
  74. FILE *out=NULL;
  75. char *line;
  76. long i,j,k;
  77. int init=0;
  78. q.quant=-1;
  79. argv++;
  80. if(!*argv){
  81. usage();
  82. exit(0);
  83. }
  84. /* get the book name, a preexisting book to continue training */
  85. {
  86. FILE *in=NULL;
  87. char *filename=alloca(strlen(*argv)+30),*ptr;
  88. strcpy(filename,*argv);
  89. in=fopen(filename,"r");
  90. ptr=strrchr(filename,'-');
  91. if(ptr){
  92. int num;
  93. ptr++;
  94. num=atoi(ptr);
  95. sprintf(ptr,"%d.vqi",num+1);
  96. }else
  97. strcat(filename,"-0.vqi");
  98. out=fopen(filename,"w");
  99. if(out==NULL){
  100. fprintf(stderr,"Unable to open %s for writing\n",filename);
  101. exit(1);
  102. }
  103. if(in){
  104. /* we wish to suck in a preexisting book and continue to train it */
  105. float a;
  106. line=rline(in,out,1);
  107. if(strcmp(line,vqext_booktype)){
  108. fprintf(stderr,"wrong book type; %s!=%s\n",line,vqext_booktype);
  109. exit(1);
  110. }
  111. line=rline(in,out,1);
  112. if(sscanf(line,"%d %d %d",&entries,&dim,&vqext_aux)!=3){
  113. fprintf(stderr,"Syntax error reading book file\n");
  114. exit(1);
  115. }
  116. vqgen_init(&v,dim,vqext_aux,entries,mindist,
  117. vqext_metric,vqext_weight,centroid);
  118. init=1;
  119. /* quant setup */
  120. line=rline(in,out,1);
  121. if(sscanf(line,"%ld %ld %d %d",&q.min,&q.delta,
  122. &q.quant,&q.sequencep)!=4){
  123. fprintf(stderr,"Syntax error reading book file\n");
  124. exit(1);
  125. }
  126. /* quantized entries */
  127. i=0;
  128. for(j=0;j<entries;j++){
  129. for(k=0;k<dim;k++){
  130. line=rline(in,out,0);
  131. sscanf(line,"%f",&a);
  132. v.entrylist[i++]=a;
  133. }
  134. }
  135. vqgen_unquantize(&v,&q);
  136. /* bias */
  137. i=0;
  138. for(j=0;j<entries;j++){
  139. line=rline(in,out,0);
  140. sscanf(line,"%f",&a);
  141. v.bias[i++]=a;
  142. }
  143. v.seeded=1;
  144. {
  145. float *b=alloca((dim+vqext_aux)*sizeof(float));
  146. i=0;
  147. while(1){
  148. for(k=0;k<dim+vqext_aux;k++){
  149. line=rline(in,out,0);
  150. if(!line)break;
  151. sscanf(line,"%f",b+k);
  152. }
  153. if(feof(in))break;
  154. vqgen_addpoint(&v,b,b+dim);
  155. }
  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],"%f",&desired)!=1)
  183. goto syner;
  184. break;
  185. case 'd':
  186. if(sscanf(argv[1],"%f",&mindist)!=1)
  187. goto syner;
  188. if(init)v.mindist=mindist;
  189. break;
  190. case 'i':
  191. if(sscanf(argv[1],"%d",&iter)!=1)
  192. goto syner;
  193. break;
  194. case 'b':
  195. biasp=0;
  196. break;
  197. case 'c':
  198. centroid=1;
  199. break;
  200. default:
  201. fprintf(stderr,"Unknown option %s\n",argv[0]);
  202. exit(1);
  203. }
  204. argv+=2;
  205. }else{
  206. /* it's an input file */
  207. char *file=strdup(*argv++);
  208. FILE *in;
  209. int cols=-1;
  210. if(!init){
  211. if(dim==-1 || entries==-1 || q.quant==-1){
  212. fprintf(stderr,"-p required when training a new set\n");
  213. exit(1);
  214. }
  215. vqgen_init(&v,dim,vqext_aux,entries,mindist,
  216. vqext_metric,vqext_weight,centroid);
  217. init=1;
  218. }
  219. in=fopen(file,"r");
  220. if(in==NULL){
  221. fprintf(stderr,"Could not open input file %s\n",file);
  222. exit(1);
  223. }
  224. fprintf(out,"# training file entry: %s\n",file);
  225. while((line=rline(in,out,0))){
  226. if(cols==-1){
  227. char *temp=line;
  228. while(*temp==' ')temp++;
  229. for(cols=0;*temp;cols++){
  230. while(*temp>32)temp++;
  231. while(*temp==' ')temp++;
  232. }
  233. fprintf(stderr,"%d colums per line in file %s\n",cols,file);
  234. }
  235. {
  236. int i;
  237. float b[cols];
  238. if(start+num*dim>cols){
  239. fprintf(stderr,"ran out of columns reading %s\n",file);
  240. exit(1);
  241. }
  242. while(*line==' ')line++;
  243. for(i=0;i<cols;i++){
  244. /* static length buffer bug workaround */
  245. char *temp=line;
  246. char old;
  247. while(*temp>32)temp++;
  248. old=temp[0];
  249. temp[0]='\0';
  250. b[i]=atof(line);
  251. temp[0]=old;
  252. while(*line>32)line++;
  253. while(*line==' ')line++;
  254. }
  255. if(num<=0)num=(cols-start)/dim;
  256. for(i=0;i<num;i++)
  257. vqext_addpoint_adj(&v,b,start+i*dim,dim,cols,num);
  258. }
  259. }
  260. fclose(in);
  261. }
  262. }
  263. if(!init){
  264. fprintf(stderr,"No input files!\n");
  265. exit(1);
  266. }
  267. vqext_preprocess(&v);
  268. /* train the book */
  269. signal(SIGTERM,setexit);
  270. signal(SIGINT,setexit);
  271. for(i=0;i<iter && !exiting;i++){
  272. float result;
  273. if(i!=0){
  274. vqgen_unquantize(&v,&q);
  275. vqgen_cellmetric(&v);
  276. }
  277. result=vqgen_iterate(&v,biasp);
  278. vqext_quantize(&v,&q);
  279. if(result<desired)break;
  280. }
  281. /* save the book */
  282. fprintf(out,"# OggVorbis VQ codebook trainer, intermediate file\n");
  283. fprintf(out,"%s\n",vqext_booktype);
  284. fprintf(out,"%d %d %d\n",entries,dim,vqext_aux);
  285. fprintf(out,"%ld %ld %d %d\n",
  286. q.min,q.delta,q.quant,q.sequencep);
  287. /* quantized entries */
  288. fprintf(out,"# quantized entries---\n");
  289. i=0;
  290. for(j=0;j<entries;j++)
  291. for(k=0;k<dim;k++)
  292. fprintf(out,"%d\n",(int)(rint(v.entrylist[i++])));
  293. fprintf(out,"# biases---\n");
  294. i=0;
  295. for(j=0;j<entries;j++)
  296. fprintf(out,"%f\n",v.bias[i++]);
  297. /* we may have done the density limiting mesh trick; refetch the
  298. training points from the temp file */
  299. rewind(v.asciipoints);
  300. fprintf(out,"# points---\n");
  301. {
  302. /* sloppy, no error handling */
  303. long bytes;
  304. char buff[4096];
  305. while((bytes=fread(buff,1,4096,v.asciipoints)))
  306. while(bytes)bytes-=fwrite(buff,1,bytes,out);
  307. }
  308. fclose(out);
  309. fclose(v.asciipoints);
  310. vqgen_unquantize(&v,&q);
  311. vqgen_cellmetric(&v);
  312. exit(0);
  313. syner:
  314. fprintf(stderr,"Syntax error in argument '%s'\n",*argv);
  315. exit(1);
  316. }