00001 #include "Link/Mpi/MpiFuncs.h"
00002 #include "Core/ApplicationFactory.h"
00003
00004
00005 #include "Link/Svm/LinkSvm.cpp"
00006
00007 namespace Impala
00008 {
00009 namespace Application
00010 {
00011
00012 using namespace Core;
00013 using namespace Core::Training;
00014
00015 void WriteResults(Util::PropertySet *properties, DataFactory *dataFactory,
00016 String concept, ParameterSearcher *searcher)
00017 {
00018 if (Link::Mpi::MyId() == 0)
00019 {
00020 Util::IOBuffer* buf = dataFactory->MakeBestFileWrite(concept);
00021 buf->Puts("value:" + MakeString(searcher->GetBestScore()));
00022 properties->Print(buf);
00023 delete buf;
00024
00025
00026 buf = dataFactory->MakeAllParametersFile(concept);
00027 searcher->PrintAllScores(buf);
00028 delete buf;
00029 }
00030 }
00031
00032 void CrossValidate(Util::PropertySet* properties,
00033 Training::Factory* trainFactory,
00034 DataFactory* dataFactory)
00035 {
00036 ILOG_VAR(main);
00037 ILOG_DEBUG_ONCE("start application");
00038 std::vector<String> conceptList = dataFactory->MakeConceptList();
00039
00040 for (int i=0 ; i<conceptList.size(); ++i)
00041 {
00042 String concept = conceptList[i];
00043 properties->Add("concept", concept);
00044 ILOG_INFO_ONCE("starting on concept " << i << ": " << concept);
00045
00046 if(!dataFactory->CanMakeBestFile(concept))
00047 {
00048 ILOG_INFO_ONCE("bestfile already exists: skipping...");
00049 continue;
00050 }
00051
00052 ILOG_DEBUG_ONCE("loading annotation...");
00053 Table::AnnotationTable* annotation = dataFactory->MakeAnnotation(concept);
00054 if(annotation == 0)
00055 {
00056 ILOG_INFO("no annotation found for " << concept << ", skipping...");
00057 continue;
00058 }
00059 ILOG_DEBUG("anno size: "<< annotation->Size());
00060 ILOG_DEBUG_ONCE("creating searcher...");
00061 ParameterSearcher* searcher =
00062 trainFactory->MakeSearcher(annotation, dataFactory);
00063 ILOG_DEBUG_ONCE("starting search...");
00064 Util::PropertySet results = searcher->Search();
00065
00066 ILOG_INFO_ONCE("saving results for " << concept);
00067 WriteResults(&results, dataFactory, concept, searcher);
00068 delete searcher;
00069 ILOG_DEBUG_ONCE("deleted searcher");
00070 delete annotation;
00071 ILOG_DEBUG_ONCE("deleted annotation");
00072 }
00073
00074 return;
00075 }
00076
00077 int mainCrossValidate(int argc, char** argv)
00078 {
00079 ILOG_VAR(main);
00080 Link::Mpi::Init(&argc, &argv);
00081 int retval = 0;
00082 CmdOptions& options = CmdOptions::GetInstance();
00083 options.Initialise(false, false, true);
00084 options.AddOption(0, "start", "index of concept to start with", "0");
00085 options.AddOption(0, "number", "number of concepts", "-1");
00086 options.AddOption(0, "concept", "name", "");
00087 options.AddOption(0, "w1", "number or range", "[log-3:3/10]");
00088 options.AddOption(0, "w2", "number or range", "[log-3:3/10]");
00089 options.AddOption(0, "autoweight", "bool", "0");
00090 options.AddOption(0, "C", "number or range", "1");
00091 options.AddOption(0, "gamma", "number or range (-1 for 1/feat length)", "-1");
00092 options.AddOption('r', "repetitions", "number", "2");
00093 options.AddOption(0, "episode-constrained", "bool", "1");
00094 options.AddOption(0, "assume-shotid", "bool", "0");
00095 options.AddOption
00096 (0, "evaluator",
00097 "choose from {AP, BAP, AUC, P@N, R@N} where N is number (precision @ n)",
00098 "AP");
00099 options.AddOption('f', "folds", "number", "3");
00100 options.AddOption('m', "cache", "megabytes", "500");
00101 options.AddOption('p', "probability", "bool", "0");
00102 options.AddOption
00103 (0, "kernel",
00104 "string: [linear,poly,rbf,sigmoid,precomputed,hist,dist-precomputed]",
00105 "rbf");
00106 options.AddOption(0, "precompute-kernel", "string: [chi2]", "chi2");
00107 options.AddOption(0, "maxVideoId", "index", "-1");
00108 options.AddOption(0, "maxPosPerVideo", "number", "-1");
00109 options.AddOption(0, "maxNegPerVideo", "number", "-1");
00110 options.AddOption(0, "dumpFolds", "", "0");
00111
00112 options.AddOption(0, "imCacheSize", "size", "1");
00113 if (options.ParseArgs(argc, argv, "dataSet concepts model featureDef", 4))
00114 {
00115 ApplicationFactory factory(&options);
00116 Util::PropertySet* properties = factory.MakeClassifierProperties();
00117 Training::Factory* trainFactory = factory.MakeTrainFactory(properties);
00118 DataFactory* dataFactory = factory.MakeDataFactory();
00119
00120
00121 if(options.GetString("kernel") == "dist-precomputed"
00122 && Link::Mpi::MyId() != 0)
00123 {
00124 dataFactory->ServeDistributedAccess();
00125 }
00126 else
00127 {
00128
00129
00130 if(options.GetString("kernel") == "dist-precomputed")
00131 dataFactory->GetDistributedAccess();
00132 std::cout <<"id: "<< Link::Mpi::MyId() << " ,kernel = "<<
00133 options.GetString("kernel") << std::endl;
00134 CrossValidate(properties, trainFactory, dataFactory);
00135 }
00136 ILOG_DEBUG_ONCE("deleting dataFactory");
00137 delete dataFactory;
00138 ILOG_DEBUG_ONCE("deleting trainFactory");
00139 delete trainFactory;
00140 ILOG_DEBUG_ONCE("deleting properties");
00141 delete properties;
00142 }
00143
00144 Link::Mpi::Finalize();
00145 return retval;
00146 }
00147
00148 }
00149 }
00150
00151 int
00152 main(int argc, char* argv[])
00153 {
00154 return Impala::Application::mainCrossValidate(argc, argv);
00155 }