00001 #ifndef ActiveLearnEngineQuids_h
00002 #define ActiveLearnEngineQuids_h
00003
00004 #include "Basis/ILog.h"
00005 #include "Core/Table/SimilarityTableSet.h"
00006 #include "Util/Database.h"
00007 #include "Core/Trec/QuidResult.h"
00008
00009 #include "Basis/Timer.h"
00010
00011 namespace Impala {
00012
00013 namespace Application {
00014
00015
00016
00017
00018 typedef Core::Table::TableTem<Core::Column::ColumnTem<Int32>, Core::Column::ColumnTem<double> > ShotWithWeight;
00019
00020 class ActiveLearnEngineQuids
00021 {
00022 public:
00023 typedef std::list<Core::Trec::QuidResult> QuidResultList;
00024
00025 ActiveLearnEngineQuids()
00026 {
00027 mAnnotations = new Core::Table::AnnotationTable();
00028 mTimer = new Timer();
00029 mTimer->Start();
00030 srand( time(NULL) );
00031 mInfoSent = false;
00032 mActiveID = "start";
00033 mErrCount = 0;
00034
00035 mALDatabase = new Util::Database("activelearning");
00036 mALDatabase->SetOverride(true);
00037 }
00038
00039 void
00040 PrepareNewID()
00041 {
00042 mActiveID = "al" + mTimer->SplitTimeStr();
00043 mInfoSent = false;
00044 }
00045
00046 std::string
00047 GetID()
00048 {
00049 return mActiveID;
00050 }
00051
00052 bool
00053 RequestSubmitted()
00054 {
00055 return mInfoSent;
00056 }
00057
00058 int
00059 GetErrCount()
00060 {
00061 return mErrCount;
00062 }
00063
00064 void
00065 PrepRequest()
00066 {
00067 if (mInfoSent)
00068 return;
00069
00070 PrepareNewID();
00071
00072 ILOG_DEBUG("SubmitRequest(), ID for submit = " << GetID());
00073
00074 mAnnotations->SetEmpty();
00075
00076 }
00077
00078
00079 void
00080 AddPositive(Quid q)
00081 {
00082 mAnnotations->AddPositive(q);
00083 }
00084
00085 void
00086 AddNegative(Quid q)
00087 {
00088 mAnnotations->AddNegative(q);
00089 }
00090
00091 void
00092 AddRandomNegatives(int number)
00093 {
00094 ILOG_DEBUG("adding " << number << " random examples as negative...");
00095 Core::Table::QuidTable allquids;
00096 Core::Table::Read(&allquids, "unc-both2-2sift-p112213.columns.tab", &Util::Database::GetInstance() );
00097 ILOG_DEBUG("Found " << allquids.Size() << " quids.");
00098 for (int i=0; i<number; i++)
00099 {
00100 int rnd = ( (1.0 * rand()) / RAND_MAX) * allquids.Size();
00101 Quid randomquid = allquids.Get1(rnd);
00102 if (QuidClass(randomquid) != QUID_CLASS_IMAGE)
00103 AddNegative(randomquid);
00104
00105
00106
00107 }
00108 }
00109
00110 void
00111 SubmitRequest()
00112 {
00113 if (mInfoSent)
00114 return;
00115
00116 if (mAnnotations->GetNrPositive() == 0)
00117 {
00118 ILOG_WARN("No positive examples, ALE request not submitted.");
00119 return;
00120 }
00121
00122 if (mAnnotations->GetNrNegative() == 0)
00123 {
00124 ILOG_WARN("No negative examples and random selection disabled, ALE request not submitted.");
00125 return;
00126 }
00127
00128 ILOG_DEBUG("Using table with " << mAnnotations->GetNrPositive() << " positive shots and " << mAnnotations->GetNrNegative() << " negative shots.");
00129
00130 String dir = "Annotations/" + QuidClassToString(QUID_CLASS_FRAME) + "/" + "conceptsActiveLearn.txt";
00131 String filename = mALDatabase->GetFilePath(dir, GetID() + ".tab", true, false);
00132
00133 ILOG_DEBUG("Writing to " << filename << "...");
00134
00135 Core::Table::Write(mAnnotations, filename, mALDatabase, true);
00136
00137 ILOG_DEBUG("Signalling active learning engine...");
00138
00139 String startlearnfile = mALDatabase->GetFilePath(dir, "startlearner.txt", true, false);
00140
00141 Util::IOBuffer *slf = mALDatabase->GetIOBuffer(
00142 startlearnfile, false, false, "tmp");
00143 if (!slf)
00144 {
00145 ILOG_WARN("No startlearnfile, ALE request not submitted.");
00146 return;
00147 }
00148 slf->IOBuffer::Puts(GetID());
00149 delete slf;
00150
00151 ILOG_DEBUG("Signal sent.");
00152 mInfoSent = true;
00153 }
00154
00155 bool
00156 HasResults()
00157 {
00158 ILOG_DEBUG("Locating results...");
00159
00160 String dir =
00161 "SimilarityIndex/conceptsActiveLearn.txt/activelearn/activelearn";
00162
00163 String path = mALDatabase->GetFilePath(dir, "names.txt", false, false);
00164 Core::Table::SimilarityTableSet *conceptSet =
00165 Core::Table::SimilarityTableSet::MakeFromFile(path, mALDatabase);
00166
00167 Core::Table::SimilarityTableSet::RankTableType* rank = conceptSet->GetRankTable(GetID());
00168
00169
00170
00171 if (!rank)
00172 {
00173 ILOG_DEBUG("ID " << GetID() << " not (yet) found.");
00174 delete conceptSet;
00175 return false;
00176 }
00177 delete conceptSet;
00178 ILOG_DEBUG("ID " << GetID() << " found!");
00179 return true;
00180 }
00181
00182 QuidResultList
00183 RetrieveResults()
00184 {
00185 ILOG_DEBUG("Retrieving results...");
00186
00187 String dir =
00188 "SimilarityIndex/conceptsActiveLearn.txt/activelearn/activelearn";
00189
00190 String path = mALDatabase->GetFilePath(dir, "names.txt", false, false);
00191
00192 Core::Table::SimilarityTableSet *conceptSet =
00193 Core::Table::SimilarityTableSet::MakeFromFile(path, mALDatabase);
00194
00195 Core::Table::SimilarityTableSet::RankTableType* rank = conceptSet->GetRankTable(GetID());
00196
00197 QuidResultList out;
00198
00199 if (!rank)
00200 {
00201 ILOG_DEBUG("Could not find ID, no data present. :'(");
00202 delete conceptSet;
00203 return out;
00204 }
00205
00206 mInfoSent = false;
00207
00208 int results = 5000;
00209
00210 ILOG_DEBUG("Concept loaded, converting to list...");
00211
00212
00213 std::map<int,bool> noduplicates;
00214 int rrank = 1;
00215 mErrCount = 0;
00216 int dropcount = 0;
00217 for (int i=0; i<results; i++)
00218 {
00219
00220 Quid q = rank->Get1(i);
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 if (q != 0)
00242 {
00243 Core::Trec::QuidResult r;
00244 r.quid = q;
00245 r.rank = rrank++;
00246 r.score = conceptSet->GetSimTable(GetID())->Get1(conceptSet->FindQuid(q));
00247
00248 out.push_back(r);
00249 }
00250 }
00251
00252 if (dropcount > 0)
00253 ILOG_DEBUG("there were " << dropcount << " silent drops from results");
00254 if (mErrCount > 0)
00255 ILOG_DEBUG("there were " << mErrCount << " errors while receiving results");
00256
00257 delete conceptSet;
00258
00259 return out;
00260 }
00261
00262 bool
00263 Ready()
00264 {
00265
00266
00267 if (mAnnotations->GetNrPositive() == 0)
00268 {
00269 ILOG_WARN("No positive examples, ALE not ready.");
00270 return false;
00271 }
00272
00273 if (mAnnotations->GetNrNegative() == 0)
00274 {
00275 ILOG_WARN("No negative examples, ALE not ready.");
00276 return false;
00277 }
00278
00279
00280 return true;
00281 }
00282
00283 private:
00284
00285 std::string mActiveID;
00286 Core::Table::AnnotationTable *mAnnotations;
00287 Util::Database *mALDatabase;
00288 bool mInfoSent;
00289
00290 int mErrCount;
00291
00292 Timer* mTimer;
00293 ILOG_VAR_DEC;
00294
00295 };
00296 ILOG_VAR_INIT(ActiveLearnEngineQuids, Core.Trec);
00297
00298
00299 }
00300
00301 }
00302
00303
00304 #endif