00001
00002 #include "Link/ImpalaLib.cpp"
00003
00004 #include "Util/StringParser.h"
00005 #include "Core/VideoSet/MakeSegmentationDocument.h"
00006 #include "Core/Trec/ActiveLearnEngineQuids.h"
00007
00008 namespace Impala {
00009 namespace Application {
00010
00011
00012
00013 class ConceptLearnClient
00014 {
00015 public:
00016
00017 ConceptLearnClient()
00018 {
00019 CmdOptions& options = CmdOptions::GetInstance();
00020
00021 mALE = new ActiveLearnEngineQuids();
00022
00023 ILOG_INFO("Starting online learning...");
00024 DoActiveLearning();
00025 }
00026
00027 Quid
00028 ParseQuid(std::string line)
00029 {
00030 ILOG_DEBUG("parsing Q = " << line);
00031 char *cline = (char*)line.c_str();
00032 char * pch;
00033 pch = strtok (cline,"(,)");
00034
00035 pch = strtok (NULL, "(,)");
00036
00037 int qclass = atoi(pch);
00038 pch = strtok (NULL, "(,)");
00039 int qset = atoi(pch);
00040 pch = strtok (NULL, "(,)");
00041 int qobject = atoi(pch);
00042 pch = strtok (NULL, "(,)");
00043 int qid = atoi(pch);
00044 pch = strtok (NULL, "(,)");
00045
00046 return (((Quid) qclass) << QUID_CLASS_SHIFT) |
00047 (((Quid) qset) << QUID_SET_SHIFT) |
00048 (((Quid) qobject) << QUID_OBJECT_SHIFT) |
00049 (((Quid) qid) << QUID_ID_SHIFT);
00050 }
00051
00052 void
00053 DoActiveLearning()
00054 {
00055 Timer tim;
00056 int ctim = tim.SplitTime();
00057 int manReset;
00058
00059 manReset = 1;
00060
00061 bool done = false;
00062
00063 while (!done)
00064 {
00065
00066 if (!mALE->RequestSubmitted())
00067 {
00068 ILOG_INFO("Sending data to ALE...");
00069
00070
00071 mALE->PrepRequest();
00072
00073
00074
00075 std::ifstream f("quids_positive.txt");
00076 while (! f.eof() )
00077 {
00078 std::string line;
00079 f >> line;
00080 if (line == "") break;
00081 Quid q = ParseQuid(line);
00082 if (q<0) {
00083 continue;
00084 }
00085
00086 ILOG_DEBUG("Positive:" << QuidObj(q));
00087 mALE->AddPositive(q);
00088 }
00089 f.close();
00090
00091
00092
00093
00094 std::ifstream fneg("quids_negative.txt");
00095 while (! fneg.eof() )
00096 {
00097 std::string line;
00098 fneg >> line;
00099 if (line == "") break;
00100 Quid q = ParseQuid(line);
00101 if (q<0) {
00102 continue;
00103 }
00104 ILOG_DEBUG("Negative:" << QuidObj(q));
00105 mALE->AddNegative(q);
00106 }
00107 fneg.close();
00108
00109 if (!mALE->Ready())
00110 {
00111 ILOG_ERROR("ALE not ready. Aborting.");
00112 return;
00113 }
00114
00115
00116 mALE->SubmitRequest();
00117
00118
00119 while (ctim+4 > tim.SplitTime());
00120 ctim = tim.SplitTime();
00121
00122 } else {
00123 if (mALE->HasResults())
00124 {
00125
00126 ILOG_INFO("Receiving data...");
00127 std::list<Core::Trec::QuidResult> quids = mALE->RetrieveResults();
00128
00129 if (quids.size() > 0 && mALE->GetErrCount() < 200)
00130 {
00131 std::ostringstream b;
00132 b << "result.txt";
00133 std::string ofname = b.str();
00134 std::ofstream out(ofname.c_str(), std::ios::out);
00135 std::list<Core::Trec::QuidResult>::iterator i = quids.begin();
00136 while (i != quids.end())
00137 {
00138 out << "001 Q0 " << QuidObj(i->quid) << " " << i->rank << " " << i->score << std::endl;
00139
00140 i++;
00141 }
00142 out.close();
00143 manReset = 1;
00144 } else {
00145 ILOG_INFO("Results not yet valid, retrying...");
00146
00147 while (ctim+3 > tim.SplitTime());
00148 ctim = tim.SplitTime();
00149 }
00150
00151 manReset++;
00152 if (manReset >3)
00153 {
00154 ILOG_ERROR("We have waited too long, resetting...");
00155 mALE->PrepareNewID();
00156 manReset = 1;
00157 }
00158
00159
00160
00161
00162 } else {
00163
00164 while (ctim+3 > tim.SplitTime());
00165 ctim = tim.SplitTime();
00166 }
00167 }
00168
00169 }
00170
00171 }
00172
00173
00174
00175 private:
00176
00177 ActiveLearnEngineQuids* mALE;
00178
00179 ILOG_VAR_DEC;
00180 };
00181
00182 ILOG_VAR_INIT(ConceptLearnClient, Application);
00183
00184 int
00185 mainConceptLearnClient(int argc, char* argv[])
00186 {
00187 CmdOptions& options = CmdOptions::GetInstance();
00188 options.Initialise(true, false, true);
00189
00190
00191 if (!options.ParseArgs(argc, argv, "", 0))
00192 return 1;
00193
00194 std::cout << "starting ConceptLearnClient..." << std::endl;
00195 ConceptLearnClient* c = new ConceptLearnClient();
00196
00197 return 1;
00198 }
00199
00200 }
00201 }
00202
00203 int
00204 main(int argc, char* argv[])
00205 {
00206 return Impala::Application::mainConceptLearnClient(argc, argv);
00207 }