00001 #ifndef Impala_Core_Training_TrainDataSrcFeature_h
00002 #define Impala_Core_Training_TrainDataSrcFeature_h
00003
00004 #include "Core/VideoSet/VideoSet.h"
00005 #include "Core/Training/TrainDataSrc.h"
00006 #include "Core/Training/SvmProblemBuilder.h"
00007 #include "Core/DataFactory.h"
00008 #include "Link/Svm/LinkSvm.h"
00009 #include "Util/Cache.h"
00010 #include "Util/TimeStats.h"
00011
00012 namespace Impala
00013 {
00014 namespace Core
00015 {
00016 namespace Training
00017 {
00018
00019 class TrainDataSrcFeature : public TrainDataSrc
00020 {
00021 public:
00022 typedef Core::Vector::VectorTem<Real64> VectorReal64;
00023
00024 TrainDataSrcFeature(Table::AnnotationTable* annotation,
00025 DataFactory* factory)
00026 : TrainDataSrc(annotation),
00027 mFeatureTableCache(3)
00028 {
00029 mDataFactory = factory;
00030 SelectValid();
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 }
00051
00052 virtual
00053 ~TrainDataSrcFeature()
00054 {
00055 }
00056
00057 virtual void
00058 SelectValid()
00059 {
00060 ILOG_DEBUG("SelectValid called, quids before: "<< mQuids->Size());
00061 Table::QuidTable* quids = mDataFactory->MakeTrainFeatureQuids();
00062 ILOG_DEBUG("number of quids in all feature quids = " << quids->Size());
00063 Table::CriterionElement1InSet<Table::AnnotationTable> eq(quids);
00064 SetAnnotation(Select(mAnnotation, eq));
00065 delete quids;
00066 ILOG_DEBUG("quids after: "<< mQuids->Size());
00067 }
00068
00073 virtual svm_problem*
00074 MakeSvmProblem()
00075 {
00076 SvmProblemBuilder pb(mAnnotation, mSelection);
00077 for(int i=0 ; i<mDataFactory->FeatureTableCount() ; ++i)
00078 {
00079 ILOG_DEBUG_NODE("getting feature table...");
00080 Feature::FeatureTable* f = GetFeatureTableWithCache(i);
00081 ILOG_DEBUG_NODE("got feature table");
00082 pb.AddFeatureTable(f);
00083 }
00084 mFeatureTableCache.Empty();
00085 return pb.MakeProblem();
00086 }
00087
00088 virtual svm_problem*
00089 MakeSvmProblem(int i)
00090 {
00091 Quid q = GetQuid(i);
00092
00093 int videoId = QuidObject(q);
00094
00095 Feature::FeatureTable* table = GetFeatureTableWithCache(videoId);
00096 if(table == 0)
00097 {
00098 ILOG_ERROR("could not open features for video #" << videoId);
00099 return MakeEmptyProblem();
00100 }
00101 VectorReal64 vector = table->FindFeature(q);
00102
00103 SvmProblemBuilder pb(mAnnotation, mSelection);
00104 svm_problem* ret = pb.MakeProblem(&vector);
00105 return ret;
00106 }
00107
00108 void
00109 PrintProblem(svm_problem* p)
00110 {
00111 for(int i=0 ; p->x[0][i].index != -1 ; ++i)
00112 {
00113 std::cout << p->x[0][i].index << ":" << p->x[0][i].value << " ";
00114 }
00115 }
00116
00117 virtual void
00118 FreeProblem(svm_problem* p)
00119 {
00120 if(p->l > 0)
00121 {
00122 delete[] p->y;
00123 for(int i=0 ; i<p->l ; ++i)
00124 delete[] p->x[i];
00125 delete[] p->x;
00126 }
00127 delete p;
00128 }
00129
00130 virtual int
00131 GetVectorLength()
00132 {
00133 return 1;
00134 }
00135
00136 private:
00137 Feature::FeatureTable*
00138 GetFeatureTableWithCache(int videoId)
00139 {
00140 Feature::FeatureTable* table=0;
00141
00142 if(!mFeatureTableCache.GetElement(videoId, table))
00143 {
00144 ILOG_DEBUG_NODE("not found, making new table");
00145 table = mDataFactory->MakeFeatureTable(videoId);
00146 ILOG_DEBUG_NODE("adding to cache...");
00147 mFeatureTableCache.AddElement(videoId, table);
00148 }
00149
00150 return table;
00151 }
00152
00153 Database::RawDataSet* mVideoSet;
00154 Core::DataFactory* mDataFactory;
00155 Util::Cache<int, Feature::FeatureTable*> mFeatureTableCache;
00156 ILOG_VAR_DECL;
00157 };
00158
00159 ILOG_VAR_INIT(TrainDataSrcFeature, Impala.Core.Training);
00160
00161 }
00162 }
00163 }
00164
00165 #endif