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 "Link/Svm/LinkSvm.h"
00008 #include "Util/Cache.h"
00009 #include "Util/TimeStats.h"
00010 #include "Persistency/FeatureTableRepository.h"
00011
00012 namespace Impala
00013 {
00014 namespace Core
00015 {
00016 namespace Training
00017 {
00018
00019
00020 class TrainDataSrcFeature : public TrainDataSrc
00021 {
00022 public:
00023
00024 typedef Core::Vector::VectorTem<Real64> VectorReal64;
00025
00026 TrainDataSrcFeature(Table::AnnotationTable* annotation,
00027 Database::RawDataSet* dataSet, CString feature)
00028 : TrainDataSrc(annotation),
00029 mFeatureTableCache(3)
00030 {
00031 mDataSet = dataSet;
00032 mFeature = feature;
00033 SelectValid();
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 }
00054
00055 virtual
00056 ~TrainDataSrcFeature()
00057 {
00058 }
00059
00060 virtual void
00061 SelectValid()
00062 {
00063 ILOG_DEBUG("SelectValid called, quids before: "<< mQuids->Size());
00064 Table::QuidTable* quids = MakeAllFeatureQuids();
00065 ILOG_DEBUG("number of quids in all feature quids = " << quids->Size());
00066 Table::CriterionElement1InSet<Table::AnnotationTable> eq(quids);
00067 SetAnnotation(Select(mAnnotation, eq));
00068 delete quids;
00069 ILOG_DEBUG("quids after: "<< mQuids->Size());
00070 }
00071
00072 virtual svm_problem*
00073 MakeSvmProblem()
00074 {
00075 SvmProblemBuilder pb(mAnnotation, mSelection);
00076 for(int i=0 ; i<FeatureTableCount() ; ++i)
00077 {
00078 Feature::FeatureTable* f = GetFeatureTableWithCache(i);
00079 pb.AddFeatureTable(f);
00080 }
00081 mFeatureTableCache.Empty();
00082 return pb.MakeProblem();
00083 }
00084
00085 virtual svm_problem*
00086 MakeSvmProblem(int i)
00087 {
00088 Quid q = GetQuid(i);
00089
00090 int videoId = QuidObject(q);
00091
00092 Feature::FeatureTable* table = GetFeatureTableWithCache(videoId);
00093 if (table == 0)
00094 {
00095 ILOG_ERROR("could not open features for video #" << videoId);
00096 return MakeEmptyProblem();
00097 }
00098 VectorReal64 vector = table->FindFeature(q);
00099
00100 SvmProblemBuilder pb(mAnnotation, mSelection);
00101 svm_problem* ret = pb.MakeProblem(&vector);
00102 return ret;
00103 }
00104
00105 void
00106 PrintProblem(svm_problem* p)
00107 {
00108 for(int i=0 ; p->x[0][i].index != -1 ; ++i)
00109 {
00110 std::cout << p->x[0][i].index << ":" << p->x[0][i].value << " ";
00111 }
00112 }
00113
00114 virtual void
00115 FreeProblem(svm_problem* p)
00116 {
00117 if(p->l > 0)
00118 {
00119 delete[] p->y;
00120 for(int i=0 ; i<p->l ; ++i)
00121 delete[] p->x[i];
00122 delete[] p->x;
00123 }
00124 delete p;
00125 }
00126
00127 virtual int
00128 GetVectorLength()
00129 {
00130 return 1;
00131 }
00132
00133 private:
00134
00135 int
00136 FeatureTableCount() const
00137 {
00138 return mDataSet->GetNrContainers();
00139 }
00140
00141 Feature::FeatureTable*
00142 MakeFeatureTable(int containerId)
00143 {
00144 typedef Persistency::FeatureLocator FeatureLocator;
00145 String walkType = (mDataSet->IsImageSet()) ? "" : "Keyframes";
00146 String container = mDataSet->GetContainer(containerId);
00147 FeatureLocator featLoc(mDataSet->GetLocator(), false, false, walkType,
00148 mFeature, container);
00149 return Persistency::FeatureTableRepository().Get(featLoc);
00150 }
00151
00152 Table::QuidTable*
00153 MakeAllFeatureQuids()
00154 {
00155 Table::QuidTable * quids = new Table::QuidTable();
00156 for (int i=0 ; i<FeatureTableCount() ; i++)
00157 {
00158 Feature::FeatureTable* f = MakeFeatureTable(i);
00159 quids->Append(f->GetQuidTable());
00160 delete f;
00161 }
00162 ILOG_INFO("[MakeAllFeatureQuids] Got "<< quids->Size() <<" quids");
00163 return quids;
00164 }
00165
00166 Feature::FeatureTable*
00167 GetFeatureTableWithCache(int videoId)
00168 {
00169 Feature::FeatureTable* table = 0;
00170 if (!mFeatureTableCache.GetElement(videoId, table))
00171 {
00172 table = MakeFeatureTable(videoId);
00173 mFeatureTableCache.AddElement(videoId, table);
00174 }
00175 return table;
00176 }
00177
00178 Database::RawDataSet* mDataSet;
00179 String mFeature;
00180 Util::Cache<int, Feature::FeatureTable*> mFeatureTableCache;
00181
00182 ILOG_VAR_DECL;
00183 };
00184
00185 ILOG_VAR_INIT(TrainDataSrcFeature, Impala.Core.Training);
00186
00187 }
00188 }
00189 }
00190
00191 #endif