00001 #ifndef Impala_Core_Training_Factory_h
00002 #define Impala_Core_Training_Factory_h
00003
00004 #include "Core/Training/ParameterSearcher.h"
00005 #include "Core/Training/PrecomputeKernelMatrix.h"
00006 #include "Core/Training/MakeEvaluation.h"
00007 #include "Core/Training/Svm.h"
00008 #include "Core/Training/ClassifierEvaluator.h"
00009 #include "Core/Training/TrainDataSrcFeature.h"
00010 #include "Core/Training/TrainDataSrcKernelTable.h"
00011 #include "Core/Training/TrainDataSrcKernelDistributed.h"
00012
00013 namespace Impala
00014 {
00015 namespace Core
00016 {
00017 namespace Training
00018 {
00019
00020 class Factory
00021 {
00022 public:
00023 Factory()
00024 {
00025 mProperties = new Util::PropertySet;
00026 }
00027
00028 Factory(Util::PropertySet* props)
00029 {
00030 mProperties = props;
00031 }
00032
00033 Classifier* MakeClassifier(String type)
00034 {
00035 if(type == "svm")
00036 return new Svm();
00037 ILOG_ERROR("unable to make classifier of type " << type);
00038 exit(1);
00039 }
00040
00041 ParameterSearcher* MakeSearcher(Table::AnnotationTable* annotation,
00042 DataFactory* data)
00043 {
00044 String evalDesc = mProperties->GetString("evaluator");
00045 ILOG_DEBUG_ONCE("ceating " << evalDesc << " evaluator");
00046 Evaluation* eval = MakeEvaluation(evalDesc, annotation);
00047 Classifier* svm = new Svm();
00048 ILOG_DEBUG_ONCE("creating TrainDataSrcFeature...");
00049 TrainDataSrc* src = MakeDataSrc(annotation, data);
00050 ILOG_DEBUG_ONCE("creating ClassifierEvaluator...");
00051 ParameterEvaluator* evaluator = new ClassifierEvaluator(svm, src, eval);
00052 ILOG_DEBUG_ONCE("creating ParameterSearcher...");
00053 Training::ParameterSearcher* searcher;
00054 searcher = new Training::ParameterSearcher(mProperties, evaluator);
00055 if(mProperties->GetString("kernel","rbf") == "dist-precomputed")
00056 searcher->OverrideParallelMode(false);
00057 return searcher;
00058 }
00059
00061 TrainDataSrc* MakeDataSrc(Table::AnnotationTable* anno, DataFactory* data)
00062 {
00063 String kernelName = mProperties->GetString("kernel", "rbf");
00064 ILOG_DEBUG_ONCE("MakeDataSrc with kernel "<<kernelName);
00065 if(kernelName == "precomputed")
00066 {
00067 Matrix::DistributedAccess* da = data->GetDistributedAccess();
00068 Feature::FeatureTable* f = LoadDistributedMatrix(*da);
00069 return new TrainDataSrcKernelTable(f, anno);
00070 }
00071 if(kernelName == "dist-precomputed")
00072 {
00073 Matrix::DistributedAccess* da = data->GetDistributedAccess();
00074 return new TrainDataSrcKernelDistributed(da, anno);
00075 }
00076 return new TrainDataSrcFeature(anno, data);
00077 }
00078
00079 Util::PropertySet GetBestFileParams(DataFactory* dataFactory, String concept)
00080 {
00081 Util::IOBuffer* buf = dataFactory->MakeBestFile(concept, false);
00082
00083 if(buf == 0 || !buf->Valid())
00084 {
00085 ILOG_ERROR("Could not open best file for concept "<<concept);
00086 return Util::PropertySet();
00087 }
00088 String line = buf->ReadLine();
00089 ILOG_DEBUG(line);
00090 line = buf->ReadLine();
00091 ILOG_DEBUG(line);
00092 delete buf;
00093 Util::PropertySet params(line);
00094 params.Add("probability", 1);
00095 params.Add("cache", mProperties->GetInt("cache"));
00096 return params;
00097 }
00098 private:
00099 Util::PropertySet* mProperties;
00100
00101 ILOG_VAR_DECL;
00102 };
00103
00104 ILOG_VAR_INIT(Factory, Impala.Core.Training);
00105
00106 }
00107 }
00108 }
00109
00110 #endif //Impala_Core_DataFactory_h