Commit 1be3b62e7cc243e2532d35ea86a560b7564f6b7b
Committed by
GitHub
Merge pull request #491 from DepthDeluxe/better-stopwatch
added timing report for training with StopWatch
Showing
1 changed file
with
43 additions
and
15 deletions
openbr/plugins/metadata/stopwatch.cpp
| @@ -14,6 +14,11 @@ | @@ -14,6 +14,11 @@ | ||
| 14 | * limitations under the License. * | 14 | * limitations under the License. * |
| 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 16 | 16 | ||
| 17 | +#include <opencv2/opencv.hpp> | ||
| 18 | +using namespace cv; | ||
| 19 | + | ||
| 20 | +#include <QAtomicInt> | ||
| 21 | + | ||
| 17 | #include <openbr/plugins/openbr_internal.h> | 22 | #include <openbr/plugins/openbr_internal.h> |
| 18 | 23 | ||
| 19 | namespace br | 24 | namespace br |
| @@ -32,10 +37,9 @@ class StopWatchTransform : public MetaTransform | @@ -32,10 +37,9 @@ class StopWatchTransform : public MetaTransform | ||
| 32 | BR_PROPERTY(QString, description, "Identity") | 37 | BR_PROPERTY(QString, description, "Identity") |
| 33 | 38 | ||
| 34 | br::Transform *transform; | 39 | br::Transform *transform; |
| 35 | - mutable QMutex mutex; | ||
| 36 | - mutable long miliseconds; | ||
| 37 | - mutable long images; | ||
| 38 | - mutable long pixels; | 40 | + mutable QAtomicInt milliseconds; |
| 41 | + mutable QAtomicInt images; | ||
| 42 | + mutable QAtomicInt pixels; | ||
| 39 | 43 | ||
| 40 | public: | 44 | public: |
| 41 | StopWatchTransform() | 45 | StopWatchTransform() |
| @@ -46,9 +50,9 @@ public: | @@ -46,9 +50,9 @@ public: | ||
| 46 | private: | 50 | private: |
| 47 | void reset() | 51 | void reset() |
| 48 | { | 52 | { |
| 49 | - miliseconds = 0; | ||
| 50 | - images = 0; | ||
| 51 | - pixels = 0; | 53 | + milliseconds.fetchAndStoreOrdered(0); |
| 54 | + images.fetchAndStoreOrdered(0); | ||
| 55 | + pixels.fetchAndStoreOrdered(0); | ||
| 52 | } | 56 | } |
| 53 | 57 | ||
| 54 | void init() | 58 | void init() |
| @@ -58,7 +62,22 @@ private: | @@ -58,7 +62,22 @@ private: | ||
| 58 | 62 | ||
| 59 | void train(const QList<TemplateList> &data) | 63 | void train(const QList<TemplateList> &data) |
| 60 | { | 64 | { |
| 65 | + QTime watch; | ||
| 66 | + watch.start(); | ||
| 67 | + | ||
| 61 | transform->train(data); | 68 | transform->train(data); |
| 69 | + | ||
| 70 | + milliseconds.fetchAndAddOrdered((int)watch.elapsed()); | ||
| 71 | + images.fetchAndAddOrdered(data.size()); | ||
| 72 | + | ||
| 73 | + // compute the total number of pixels we processed | ||
| 74 | + foreach(const TemplateList &list, data) { | ||
| 75 | + foreach(const Template &t, list) { | ||
| 76 | + foreach(const cv::Mat &m, t) { | ||
| 77 | + pixels.fetchAndAddOrdered(m.rows*m.cols); | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | + } | ||
| 62 | } | 81 | } |
| 63 | 82 | ||
| 64 | void project(const Template &src, Template &dst) const | 83 | void project(const Template &src, Template &dst) const |
| @@ -67,11 +86,11 @@ private: | @@ -67,11 +86,11 @@ private: | ||
| 67 | watch.start(); | 86 | watch.start(); |
| 68 | transform->project(src, dst); | 87 | transform->project(src, dst); |
| 69 | 88 | ||
| 70 | - QMutexLocker locker(&mutex); | ||
| 71 | - miliseconds += watch.elapsed(); | ||
| 72 | - images++; | ||
| 73 | - foreach (const cv::Mat &m, src) | ||
| 74 | - pixels += (m.rows * m.cols); | 89 | + milliseconds.fetchAndAddOrdered(watch.elapsed()); |
| 90 | + images.fetchAndAddOrdered(1); | ||
| 91 | + foreach (const cv::Mat &m, src) { | ||
| 92 | + pixels.fetchAndAddOrdered(m.rows * m.cols); | ||
| 93 | + } | ||
| 75 | } | 94 | } |
| 76 | 95 | ||
| 77 | void finalize(TemplateList &) | 96 | void finalize(TemplateList &) |
| @@ -81,15 +100,24 @@ private: | @@ -81,15 +100,24 @@ private: | ||
| 81 | "\tTemplates/s: %g\n" | 100 | "\tTemplates/s: %g\n" |
| 82 | "\tPixels/s: %g\n", | 101 | "\tPixels/s: %g\n", |
| 83 | qPrintable(description), | 102 | qPrintable(description), |
| 84 | - miliseconds / 1000.0, | ||
| 85 | - images * 1000.0 / miliseconds, | ||
| 86 | - pixels * 1000.0 / miliseconds); | 103 | + milliseconds / 1000.0, |
| 104 | + images * 1000.0 / milliseconds, | ||
| 105 | + pixels * 1000.0 / milliseconds); | ||
| 87 | reset(); | 106 | reset(); |
| 88 | } | 107 | } |
| 89 | 108 | ||
| 90 | void store(QDataStream &stream) const | 109 | void store(QDataStream &stream) const |
| 91 | { | 110 | { |
| 92 | transform->store(stream); | 111 | transform->store(stream); |
| 112 | + | ||
| 113 | + qDebug("\nTraining Profile for \"%s\"\n" | ||
| 114 | + "\tSeconds: %g\n" | ||
| 115 | + "\tTemplates/s: %g\n" | ||
| 116 | + "\tPixels/s: %g\n", | ||
| 117 | + qPrintable(description), | ||
| 118 | + milliseconds / 1000.0, | ||
| 119 | + images * 1000.0 / milliseconds, | ||
| 120 | + pixels * 1000.0 / milliseconds); | ||
| 93 | } | 121 | } |
| 94 | 122 | ||
| 95 | void load(QDataStream &stream) | 123 | void load(QDataStream &stream) |