Commit 952f60e635d270b138a31061a48e2f3d104e0698

Authored by Josh Klontz
1 parent 521999f4

refactored future waiting

app/examples/face_recognition_evaluation.cpp
... ... @@ -41,21 +41,21 @@ int main(int argc, char *argv[])
41 41 br_set_property("algorithm", "FaceRecognition");
42 42  
43 43 // Equivalent to 'Globals->path = "../data/MEDS/img/";' in C++ API
44   - br_set_property("path", "../data/MEDS/img/");
  44 + //br_set_property("path", "../data/MEDS/img/");
45 45  
46 46 // Enroll galleries, don't re-enroll if they already exist (cache)
47   - br_enroll("../data/MEDS/sigset/MEDS_frontal_target.xml", "target.gal[cache]");
48   - br_enroll("../data/MEDS/sigset/MEDS_frontal_query.xml", "query.gal[cache]");
  47 + //br_enroll("../data/MEDS/sigset/MEDS_frontal_target.xml", "target.gal[cache]");
  48 + //br_enroll("../data/MEDS/sigset/MEDS_frontal_query.xml", "query.gal[cache]");
49 49  
50 50 // Compare galleries and store result in a binary similarity matrix
51 51 br_compare("target.gal", "query.gal", "FaceRecognition_MEDS.mtx");
52 52  
53 53 // Make a ground truth "mask" matrix from the sigsets.
54   - br_make_mask("../data/MEDS/sigset/MEDS_frontal_target.xml", "../data/MEDS/sigset/MEDS_frontal_query.xml", "MEDS.mask");
  54 + //br_make_mask("../data/MEDS/sigset/MEDS_frontal_target.xml", "../data/MEDS/sigset/MEDS_frontal_query.xml", "MEDS.mask");
55 55  
56 56 // Evaluate the performance of OpenBR's FaceRecognition and a COTS face recognition system.
57   - br_eval("FaceRecognition_MEDS.mtx", "MEDS.mask", "Algorithm_Dataset/FaceRecognition_MEDS.csv");
58   - br_eval("../data/MEDS/simmat/COTS_MEDS.mtx", "MEDS.mask", "Algorithm_Dataset/COTS_MEDS.csv");
  57 + //br_eval("FaceRecognition_MEDS.mtx", "MEDS.mask", "Algorithm_Dataset/FaceRecognition_MEDS.csv");
  58 + //br_eval("../data/MEDS/simmat/COTS_MEDS.mtx", "MEDS.mask", "Algorithm_Dataset/COTS_MEDS.csv");
59 59  
60 60 // The '_' character has special significance and is used to populate plot legends.
61 61 // Requires R installation, see documentation of br_plot for details.
... ... @@ -63,6 +63,9 @@ int main(int argc, char *argv[])
63 63 files[0] = "Algorithm_Dataset/FaceRecognition_MEDS.csv";
64 64 files[1] = "Algorithm_Dataset/COTS_MEDS.csv";
65 65 br_plot(2, files, "MEDS", true);
  66 + br_plot(2, files, "MEDS", true);
  67 + br_plot(2, files, "MEDS", true);
  68 + br_plot(2, files, "MEDS", true);
66 69  
67 70 br_finalize();
68 71 return 0;
... ...
sdk/core/qtutils.h
... ... @@ -21,6 +21,7 @@
21 21 #include <QDir>
22 22 #include <QFile>
23 23 #include <QFileInfo>
  24 +#include <QFuture>
24 25 #include <QMap>
25 26 #include <QString>
26 27 #include <QStringList>
... ... @@ -62,6 +63,14 @@ namespace QtUtils
62 63 bool runRScript(const QString &file);
63 64 bool runDot(const QString &file);
64 65 void showFile(const QString &file);
  66 +
  67 + /**** Thread Utilities ****/
  68 + template <typename T>
  69 + static inline void waitForFinished(const QList< QFuture<T> > &futures)
  70 + {
  71 + foreach (QFuture<T> future, futures)
  72 + future.waitForFinished();
  73 + }
65 74 }
66 75  
67 76 #endif // __QTUTILS_H
... ...
sdk/openbr_plugin.cpp
... ... @@ -781,14 +781,6 @@ int br::Context::timeRemaining() const
781 781 return std::max(0, int((1 - p) / p * startTime.elapsed())) / 1000;
782 782 }
783 783  
784   -void br::Context::trackFutures(QList< QFuture<void> > &futures)
785   -{
786   - foreach (QFuture<void> future, futures) {
787   - QCoreApplication::processEvents();
788   - future.waitForFinished();
789   - }
790   -}
791   -
792 784 bool br::Context::checkSDKPath(const QString &sdkPath)
793 785 {
794 786 return QFileInfo(sdkPath + "/share/openbr/openbr.bib").exists();
... ... @@ -1171,13 +1163,11 @@ private:
1171 1163 templatesList[i] = Downsample(templatesList[i], transforms[i]);
1172 1164  
1173 1165 QList< QFuture<void> > futures;
1174   - const bool threaded = Globals->parallelism && (templatesList.size() > 1);
1175 1166 for (int i=0; i<templatesList.size(); i++) {
1176   - if (threaded) futures.append(QtConcurrent::run(_train, transforms[i], &templatesList[i]));
1177   - else _train (transforms[i], &templatesList[i]);
  1167 + if (Globals->parallelism) futures.append(QtConcurrent::run(_train, transforms[i], &templatesList[i]));
  1168 + else _train (transforms[i], &templatesList[i]);
1178 1169 }
1179   -
1180   - if (threaded) Globals->trackFutures(futures);
  1170 + QtUtils::waitForFinished(futures);
1181 1171 }
1182 1172  
1183 1173 void project(const Template &src, Template &dst) const
... ... @@ -1317,11 +1307,10 @@ void Transform::backProject(const TemplateList &amp;dst, TemplateList &amp;src) const
1317 1307 for (int i=0; i<dst.size(); i++) src.append(Template());
1318 1308  
1319 1309 QList< QFuture<void> > futures;
1320   - if (Globals->parallelism) futures.reserve(dst.size());
1321 1310 for (int i=0; i<dst.size(); i++)
1322 1311 if (Globals->parallelism) futures.append(QtConcurrent::run(_backProject, this, &dst[i], &src[i]));
1323   - else _backProject (this, &dst[i], &src[i]);
1324   - if (Globals->parallelism) Globals->trackFutures(futures);
  1312 + else _backProject (this, &dst[i], &src[i]);
  1313 + QtUtils::waitForFinished(futures);
1325 1314 }
1326 1315  
1327 1316 /* Distance - public methods */
... ... @@ -1358,7 +1347,7 @@ void Distance::compare(const TemplateList &amp;target, const TemplateList &amp;query, Ou
1358 1347 if (Globals->parallelism) futures.append(QtConcurrent::run(this, &Distance::compareBlock, targets, queries, output, targetOffset, queryOffset));
1359 1348 else compareBlock (targets, queries, output, targetOffset, queryOffset);
1360 1349 }
1361   - if (Globals->parallelism) Globals->trackFutures(futures);
  1350 + QtUtils::waitForFinished(futures);
1362 1351 }
1363 1352  
1364 1353 QList<float> Distance::compare(const TemplateList &targets, const Template &query) const
... ...
sdk/openbr_plugin.h
... ... @@ -622,12 +622,6 @@ public:
622 622 int timeRemaining() const;
623 623  
624 624 /*!
625   - * \brief Continues to print the progress of the futures until they are completed.
626   - * \param futures The list of futures to track.
627   - */
628   - void trackFutures(QList< QFuture<void> > &futures);
629   -
630   - /*!
631 625 * \brief Returns \c true if \em sdkPath is valid, \c false otherwise.
632 626 * \param sdkPath The path to <tt>share/openbr/openbr.bib</tt>
633 627 */
... ...
sdk/plugins/distance.cpp
... ... @@ -19,6 +19,7 @@
19 19 #include <openbr_plugin.h>
20 20  
21 21 #include "core/distance_sse.h"
  22 +#include "core/qtutils.h"
22 23  
23 24 using namespace cv;
24 25  
... ... @@ -158,7 +159,7 @@ class PipeDistance : public Distance
158 159 foreach (br::Distance *distance, distances)
159 160 if (Globals->parallelism) futures.append(QtConcurrent::run(distance, &Distance::train, data));
160 161 else distance->train(data);
161   - Globals->trackFutures(futures);
  162 + QtUtils::waitForFinished(futures);
162 163 }
163 164  
164 165 float compare(const Template &a, const Template &b) const
... ...
sdk/plugins/meta.cpp
... ... @@ -285,12 +285,11 @@ class ForkTransform : public CompositeTransform
285 285 void train(const TemplateList &data)
286 286 {
287 287 QList< QFuture<void> > futures;
288   - const bool threaded = Globals->parallelism && (transforms.size() > 1);
289 288 for (int i=0; i<transforms.size(); i++) {
290   - if (threaded) futures.append(QtConcurrent::run(_train, transforms[i], &data));
291   - else _train (transforms[i], &data);
  289 + if (Globals->parallelism) futures.append(QtConcurrent::run(_train, transforms[i], &data));
  290 + else _train (transforms[i], &data);
292 291 }
293   - if (threaded) Globals->trackFutures(futures);
  292 + QtUtils::waitForFinished(futures);
294 293 }
295 294  
296 295 void backProject(const Template &dst, Template &src) const {Transform::backProject(dst, src);}
... ... @@ -644,9 +643,7 @@ public:
644 643 else
645 644 _projectList(transform, &input_buffer[i], &output_buffer[i]);
646 645 }
647   -
648   - if (Globals->parallelism)
649   - Globals->trackFutures(futures);
  646 + QtUtils::waitForFinished(futures);
650 647  
651 648 for (int i=0; i<src.size(); i++) dst.append(output_buffer[i]);
652 649 }
... ...
sdk/plugins/normalize.cpp
... ... @@ -22,6 +22,7 @@
22 22  
23 23 #include "core/common.h"
24 24 #include "core/opencvutils.h"
  25 +#include "core/qtutils.h"
25 26  
26 27 using namespace cv;
27 28  
... ... @@ -119,9 +120,8 @@ private:
119 120 bv.push_back(Mat(1, dims, CV_64FC1));
120 121 }
121 122  
122   - QList< QFuture<void> > futures; futures.reserve(dims);
  123 + QList< QFuture<void> > futures;
123 124 const bool parallel = (data.size() > 1000) && Globals->parallelism;
124   -
125 125 for (size_t c = 0; c < mv.size(); c++) {
126 126 for (int i=0; i<dims; i++)
127 127 if (parallel) futures.append(QtConcurrent::run(_train, method, mv[c], &av[c], &bv[c], i));
... ... @@ -129,8 +129,7 @@ private:
129 129 av[c] = av[c].reshape(1, data.first().m().rows);
130 130 bv[c] = bv[c].reshape(1, data.first().m().rows);
131 131 }
132   -
133   - if (parallel) Globals->trackFutures(futures);
  132 + QtUtils::waitForFinished(futures);
134 133  
135 134 merge(av, a);
136 135 merge(bv, b);
... ...
sdk/plugins/validate.cpp
1 1 #include <QtConcurrentRun>
2 2 #include <openbr_plugin.h>
3 3  
  4 +#include "core/qtutils.h"
  5 +
4 6 namespace br
5 7 {
6 8  
... ... @@ -41,9 +43,9 @@ class CrossValidateTransform : public MetaTransform
41 43 if (partitions[j] == i)
42 44 partitionedData.removeAt(j);
43 45 if (Globals->parallelism) futures.append(QtConcurrent::run(transforms[i], &Transform::train, partitionedData));
44   - else transforms[i]->train(partitionedData);
  46 + else transforms[i]->train(partitionedData);
45 47 }
46   - Globals->trackFutures(futures);
  48 + QtUtils::waitForFinished(futures);
47 49 }
48 50  
49 51 void project(const Template &src, Template &dst) const
... ...