00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include <iostream>
00056 #include "alize.h"
00057 #include "liatools.h"
00058 #include <cmath>
00059 using namespace std;
00060 using namespace alize;
00061
00062
00063
00064
00065 void computeExpansion(Feature & f,Feature & expF) {
00066 expF[0]=1;
00067 unsigned long size=f.getVectSize();
00068 for (unsigned long i=0;i<size;i++)
00069 expF[i+1]=f[i];
00070
00071 unsigned long idx=0;
00072
00073 for (unsigned long i=0;i<size+1;i++){
00074 for (unsigned long j=i;j<size+1;j++){
00075 for (unsigned long k=j;k<size+1;k++){
00076 expF[idx] = expF[i]*expF[j]*expF[k] ;
00077 idx++;
00078 }
00079 }
00080 }
00081
00082 }
00083
00084
00085 void computeAndAccumulateExpansion(FeatureServer & fs,FrameAccGD & avgExp,unsigned long idxBeginFrame,unsigned long nbFrames,Config & config) {
00086
00087 fs.seekFeature(idxBeginFrame);
00088 unsigned long vectSize=fs.getVectSize();
00089 unsigned long expSize = (vectSize + 3) * (vectSize + 2) * (vectSize + 1) / 6 ;
00090 if (debug) cout << "Expension size: "<<expSize<<endl;
00091 Feature expF(expSize);
00092 for (unsigned long n=0;n<nbFrames;n++){
00093 Feature f;
00094 fs.readFeature(f);
00095
00096 computeExpansion(f,expF);
00097 avgExp.accumulate(expF);
00098 }
00099 }
00100
00101
00102 void computeAndAccumulateExpansion(FeatureServer & fs, FrameAccGD & avgExp, Seg * seg,Config & config) {
00103 unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
00104 computeAndAccumulateExpansion(fs,avgExp,begin,seg->length(),config);
00105 }
00106
00107
00108
00109 void computeAndAccumulateExpansion(FeatureServer & fs, FrameAccGD & avgExp, SegCluster & selectedSegments,Config & config) {
00110 Seg* seg;
00111 selectedSegments.rewind();
00112 while((seg=selectedSegments.getSeg())!=NULL)
00113 computeAndAccumulateExpansion(fs,avgExp,seg,config);
00114 }
00115
00116
00117
00118 void multiplyByR(DoubleVector & avgExp, XList & rMat) {
00119 for (unsigned long i=0;i<avgExp.size();i++) {
00120 avgExp[i]*=rMat.getLine(i).getElement(0).toDouble();
00121 }
00122 }
00123
00124 void zNorm(DoubleVector & avgExp, XList & rMat) {
00125 for (unsigned long i=0;i<avgExp.size();i++) {
00126 avgExp[i]-=rMat.getLine(i).getElement(1).toDouble();
00127 avgExp[i]/=rMat.getLine(i).getElement(0).toDouble();
00128 }
00129 }
00130
00131 void computeRSqrt(DoubleVector & R,unsigned long count) {
00132 for (unsigned long i=0;i<R.size();i++) {
00133 R[i]/=count;
00134 R[i]=1/sqrt(R[i]);
00135 }
00136 }
00137
00138 void outputR(const DoubleVector & v1,const DoubleVector & v2,Config &config) {
00139 String outRFile=config.getParam("computeR");
00140 ofstream out(outRFile.c_str());
00141 for (unsigned long i=0;i<v1.size();i++) {
00142 out << v1[i] << " " << v2[i] << endl;
00143 }
00144 out << endl;
00145 out.close();
00146 }
00147 void outputInstanceSVMLight(const DoubleVector & v, String & filename,Config & config) {
00148 String exType=config.getParam("exType");
00149 ofstream out(filename.c_str());
00150 out << exType << " ";
00151 for (unsigned long i=0;i<v.size();i++) {
00152 out << i+1 << ":" << v[i] << " ";
00153 }
00154 out << endl;
00155 out.close();
00156 }
00157 void outputInstance(const DoubleVector & v, String & filename,Config & config) {
00158 String format=config.getParam("format");
00159 if (format=="SVMLight") outputInstanceSVMLight(v,filename,config);
00160 else {cerr << "E: Format unknown" << endl;}
00161 }
00162
00163
00164 int PolyExpand(Config & config){
00165 try {
00166 XList inputList(config.getParam("inputFeatureFilename"),config);
00167 XLine * pLine;
00168 bool computeR=config.existsParam("computeR");
00169 bool normalize=config.existsParam("normalize");
00170 String labelSelectedFrames=config.getParam("labelSelectedFrames");
00171 bool verbose=false;
00172 if (config.existsParam("verbose")) verbose=config.getParam("verbose").toBool();
00173 XList xMat;
00174 if (normalize) {
00175 xMat.load(config.getParam("normalize"),config);
00176 }
00177
00178 FrameAccGD avgExp;
00179 while((pLine=inputList.getLine())!=NULL) {
00180 String filename=pLine->getElement(0);
00181 if (verbose) cout << "Processing file: ["<<filename<<"] ... ";
00182 FeatureServer fs(config,filename);
00183 SegServer segmentsServer;
00184 LabelServer labelServer;
00185 initializeClusters(filename,segmentsServer,labelServer,config);
00186 verifyClusterFile(segmentsServer,fs,config);
00187 unsigned long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);
00188 SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);
00189
00190 computeAndAccumulateExpansion(fs,avgExp,selectedSegments,config);
00191 if (verbose) cout << "Done" <<endl;
00192
00193 if (!computeR) {
00194 DoubleVector avgExpVect=avgExp.getMeanVect();
00195 if (normalize) multiplyByR(avgExpVect,xMat);
00196 String outFile=config.getParam("vectorFilesPath")+filename+config.getParam("vectorFilesExtension");
00197 outputInstance(avgExpVect,outFile,config);
00198 avgExp.reset();
00199 }
00200 }
00201 if (computeR) {
00202 const DoubleVector & meanR=avgExp.getMeanVect();
00203 DoubleVector R=avgExp.getxAccVect();
00204 computeRSqrt(R,avgExp.getCount());
00205
00206 outputR(R,meanR,config);
00207 }
00208 }
00209 catch (Exception& e) {cout << e.toString() << endl;}
00210 return 0;
00211 }