00001 #ifndef Impala_Core_Training_ApplyConcepts_h
00002 #define Impala_Core_Training_ApplyConcepts_h
00003
00004
00005 #include "Core/Database/MakeRawDataSet.h"
00006 #include "Core/Database/PathCreator.h"
00007 #include "Core/Feature/FeatureTable.h"
00008 #include "Core/Feature/ConceptSet.h"
00009 #include "Core/Training/Classifier.h"
00010 #include "Core/Training/load.h"
00011 #include "Util/MemReport.h"
00012 #include "Core/Training/ApplyConceptsHelperFeatures.h"
00013 #include "Core/Training/ApplyConceptsHelperKernels.h"
00014
00015 namespace Impala
00016 {
00017 namespace Core
00018 {
00019 namespace Training
00020 {
00021
00022
00030 class ApplyConcepts
00031 {
00032 public:
00033 typedef Table::ScoreTable ScoreTable;
00034 typedef Feature::FeatureDefinition FeatureDefinition;
00035 typedef Feature::FeatureTable FeatureTable;
00036 typedef Feature::ConceptSet ConceptSet;
00037 public:
00038
00049 ApplyConcepts(CmdOptions& options, Database::PathCreator* pathCreator)
00050 {
00051 if (options.GetNrArg() < 6)
00052 {
00053 ILOG_ERROR("Missing argument");
00054 return;
00055 }
00056
00057
00058
00059
00060 mAnnoSet = Database::MakeRawDataSet(options.GetArg(2));
00061 String conceptFileName = options.GetArg(3);
00062 String modelType = options.GetArg(4);
00063 String featureName = options.GetArg(5);
00064 mConceptSet = ConceptSet::MakeFromFile(mAnnoSet, conceptFileName, modelType);
00065
00066 mHelper = MakeHelper(options.GetBool("precomputed"));
00067 mHelper->Initialise(mAnnoSet, featureName);
00068
00069 pathCreator->SetConceptSet(conceptFileName);
00070 pathCreator->SetFeature(featureName);
00071 pathCreator->SetModel(modelType);
00072 }
00073
00074 virtual
00075 ~ApplyConcepts()
00076 {
00077 delete mConceptSet;
00078 delete mHelper;
00079 delete mAnnoSet;
00080 }
00081
00085 virtual void
00086 NextPath(Database::PathCreator* pathCreator)
00087 {
00088 if((!CmdOptions::GetInstance().GetBool("override")) &&
00089 Table::SimilarityTableSet::Exists(pathCreator))
00090 {
00091
00092 ILOG_INFO("Table already exsists, skipping");
00093 return;
00094 }
00095 Util::PrintMemUsage("loading features...");
00096 FeatureTable* features = mHelper->LoadFeatures(pathCreator);
00097 Util::PrintMemUsage("loading features: done");
00098 std::vector<String> names = mConceptSet->GetNames();
00099 Util::PrintMemUsage("got names");
00100 Table::SimilarityTableSet simSet(names, features->Size());
00101 Core::Table::Copy(simSet.GetQuidTable(), features);
00102
00103 Timer timerTotal;
00104 for (int i=0 ; i<mConceptSet->GetNrConcepts() ; i++)
00105 {
00106 ILOG_INFO("concept " << i << " of " << mConceptSet->GetNrConcepts());
00107 Timer timerConcept;
00108 Feature::Concept* concept = mConceptSet->GetConcept(i);
00109 if(!concept)
00110 {
00111 ILOG_ERROR("concept " << i << " of " <<
00112 mConceptSet->GetNrConcepts() << " is NULL!");
00113 }
00114 String modelName = concept->GetModelName(mHelper->GetModelName());
00115 if (modelName.empty())
00116 {
00117 ILOG_ERROR("Unable to find model, skipping");
00118 continue;
00119 }
00120
00121 ILOG_INFO("model " << modelName);
00122 Util::PrintMemUsage("pre-predict");
00123 ILOG_DEBUG("predicting...");
00124 ScoreTable* scores = mHelper->Predict(modelName);
00125 ILOG_DEBUG("setting sim table...");
00126 simSet.SetSimTable(i, scores);
00127 ILOG_DEBUG("deleting...");
00128 delete scores;
00129 Util::PrintMemUsage("post-predict");
00130 ILOG_DEBUG("computing rank...");
00131 simSet.ComputeRank(i, true);
00132 ILOG_INFO("time this concept = " << timerConcept.SplitTimeStr()
00133 << ", total = " << timerTotal.SplitTimeStr());
00134 }
00135 Util::PrintMemUsage("presave");
00136 pathCreator->SetFeature(mHelper->GetModelName());
00137 simSet.Save(pathCreator);
00138 Util::PrintMemUsage("pre-unload");
00139 mHelper->UnloadFeatures();
00140 Util::PrintMemUsage("post-unload");
00141 }
00142
00143 private:
00144 ApplyConceptsHelper* mHelper;
00145 ConceptSet* mConceptSet;
00146 Database::RawDataSet* mAnnoSet;
00147 ILOG_VAR_DEC;
00148
00149 ApplyConceptsHelper* MakeHelper(bool isPrecomputed)
00150 {
00151 if(isPrecomputed)
00152 return new ApplyConceptsHelperKernels;
00153 return new ApplyConceptsHelperFeatures;
00154 }
00155
00156 };
00157
00158 ILOG_VAR_INIT(ApplyConcepts, Impala.Core.Training);
00159
00160 }
00161 }
00162 }
00163
00164 #endif