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("Doing the obvious...");
00024 mVidSet = Core::VideoSet::MakeVideoSet("trec2009test.txt");
00025 mKeyframes = new Core::VideoSet::Keyframes(mVidSet, "keyframes");
00026 mSegmentation = new Core::VideoSet::Segmentation(mVidSet, "segmentation");
00027
00028 ILOG_INFO("Starting online learning...");
00029 DoActiveLearning();
00030
00031
00032 }
00033
00034 Quid
00035 ParseQuid(std::string line)
00036 {
00037 ILOG_DEBUG("parsing Q = " << line);
00038 char *cline = (char*)line.c_str();
00039 char * pch;
00040 pch = strtok (cline,"(,)");
00041
00042 pch = strtok (NULL, "(,)");
00043
00044 int qclass = atoi(pch);
00045 pch = strtok (NULL, "(,)");
00046 int qset = atoi(pch);
00047 pch = strtok (NULL, "(,)");
00048 int qobject = atoi(pch);
00049 pch = strtok (NULL, "(,)");
00050 int qid = atoi(pch);
00051 pch = strtok (NULL, "(,)");
00052
00053 return (((Quid) qclass) << QUID_CLASS_SHIFT) |
00054 (((Quid) qset) << QUID_SET_SHIFT) |
00055 (((Quid) qobject) << QUID_OBJECT_SHIFT) |
00056 (((Quid) qid) << QUID_ID_SHIFT);
00057 }
00058
00059 void
00060 DoActiveLearning()
00061 {
00062 Timer tim;
00063 int ctim = tim.SplitTime();
00064 int manReset;
00065
00066 manReset = 1;
00067
00068 bool done = false;
00069
00070 while (!done)
00071 {
00072
00073 if (!mALE->RequestSubmitted())
00074 {
00075 ILOG_INFO("Sending data to ALE...");
00076
00077
00078 mALE->PrepRequest();
00079
00080
00081
00082 std::ifstream f("quids_positive.txt");
00083 while (! f.eof() )
00084 {
00085 std::string line;
00086 f >> line;
00087 if (line == "") break;
00088 Quid q = ParseQuid(line);
00089 if (q<0) {
00090 continue;
00091 }
00092
00093 ILOG_DEBUG("Positive:" << QuidObj(q));
00094 mALE->AddPositive(q);
00095 }
00096 f.close();
00097
00098
00099
00100
00101 std::ifstream fneg("quids_negative.txt");
00102 bool nolines = TRUE;
00103 while (! fneg.eof() )
00104 {
00105 std::string line;
00106 fneg >> line;
00107 if (line == "") break;
00108 Quid q = ParseQuid(line);
00109 if (q<0) {
00110 continue;
00111 }
00112 nolines = FALSE;
00113 ILOG_DEBUG("Negative:" << QuidObj(q));
00114 mALE->AddNegative(q);
00115 }
00116 fneg.close();
00117
00118 if (nolines)
00119 {
00120 ILOG_DEBUG("Adding random negatives...");
00121 mALE->AddRandomNegatives(50);
00122 }
00123
00124 if (!mALE->Ready())
00125 {
00126 ILOG_ERROR("ALE not ready. Aborting.");
00127 return;
00128 }
00129
00130
00131 mALE->SubmitRequest();
00132
00133
00134 while (ctim+4 > tim.SplitTime());
00135 ctim = tim.SplitTime();
00136
00137 } else {
00138 if (mALE->HasResults())
00139 {
00140
00141 ILOG_INFO("Receiving data...");
00142 std::list<Core::Trec::QuidResult> quids = mALE->RetrieveResults();
00143
00144 if (quids.size() > 0 && mALE->GetErrCount() < 200)
00145 {
00146 std::ostringstream b;
00147 b << "result.txt";
00148 std::string ofname = b.str();
00149 std::ofstream out(ofname.c_str(), std::ios::out);
00150 std::list<Core::Trec::QuidResult>::iterator i = quids.begin();
00151
00152 while (i != quids.end())
00153 {
00154 if (QuidSet(i->quid) == 14)
00155 {
00156 int keyid = mKeyframes->GetFrameId(i->quid);
00157 if (keyid > -1)
00158 {
00159 int shotid = mKeyframes->GetShotId(keyid);
00160 std::string shotname = mSegmentation->GetName(shotid);
00161 out << shotname << " " << i->rank << " " << i->score << std::endl;
00162 }
00163 }
00164
00165 i++;
00166 }
00167 out.close();
00168 return;
00169 manReset = 1;
00170 } else {
00171 ILOG_INFO("Results not yet valid, retrying...");
00172
00173 while (ctim+3 > tim.SplitTime());
00174 ctim = tim.SplitTime();
00175 }
00176
00177 manReset++;
00178 if (manReset >3)
00179 {
00180 ILOG_ERROR("We have waited too long, resetting...");
00181 mALE->PrepareNewID();
00182 manReset = 1;
00183 }
00184
00185
00186
00187
00188 } else {
00189
00190 while (ctim+3 > tim.SplitTime());
00191 ctim = tim.SplitTime();
00192 }
00193 }
00194
00195 }
00196
00197 }
00198
00199
00200
00201 private:
00202
00203 ActiveLearnEngineQuids* mALE;
00204 Core::VideoSet::Keyframes *mKeyframes;
00205 Core::VideoSet::VideoSet *mVidSet;
00206 Core::VideoSet::Segmentation *mSegmentation;
00207
00208 ILOG_VAR_DEC;
00209 };
00210
00211 ILOG_VAR_INIT(ConceptLearnClient, Application);
00212
00213 int
00214 mainConceptLearnClient(int argc, char* argv[])
00215 {
00216 CmdOptions& options = CmdOptions::GetInstance();
00217 options.Initialise(true, false, true);
00218
00219
00220 if (!options.ParseArgs(argc, argv, "", 0))
00221 return 1;
00222
00223 std::cout << "starting ConceptLearnClient..." << std::endl;
00224 ConceptLearnClient* c = new ConceptLearnClient();
00225
00226 return 1;
00227 }
00228
00229 }
00230 }
00231
00232 int
00233 main(int argc, char* argv[])
00234 {
00235 return Impala::Application::mainConceptLearnClient(argc, argv);
00236 }