From a69c9c3522288ccd699b677f09ec02051443f9d0 Mon Sep 17 00:00:00 2001 From: M Taborsky Date: Thu, 10 Jan 2013 14:11:21 -0500 Subject: [PATCH] Attempting to squish... --- sdk/core/core.cpp | 10 +++++++++- sdk/openbr_plugin.cpp | 33 +++++++++++++++++++++++++++++++++ sdk/openbr_plugin.h | 15 ++++++++++++++- sdk/plugins/eigen3.cpp | 2 ++ sdk/plugins/meta.cpp | 37 +++++++++++++++++++++++++++++++++++++ sdk/plugins/pixel.cpp | 2 +- sdk/plugins/pp5.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 7 files changed, 148 insertions(+), 15 deletions(-) diff --git a/sdk/core/core.cpp b/sdk/core/core.cpp index 48dc432..1f791fd 100644 --- a/sdk/core/core.cpp +++ b/sdk/core/core.cpp @@ -133,7 +133,15 @@ struct AlgorithmCore if (data.isEmpty()) break; const int numFiles = data.size(); - data >> *transform; + if (!Globals->backProject){ + data >> *transform; + } else { + // I don't really know if this makes sense. + //TemplateList dst; + transform->backProject(data, data); + //data = dst; + } + g->writeBlock(data); const FileList newFiles = data.files(); fileList.append(newFiles); diff --git a/sdk/openbr_plugin.cpp b/sdk/openbr_plugin.cpp index 3941a46..a2b6895 100644 --- a/sdk/openbr_plugin.cpp +++ b/sdk/openbr_plugin.cpp @@ -637,6 +637,7 @@ br::Context::Context() quiet = verbose = false; currentStep = totalSteps = 0; enrollAll = false; + backProject = false; } int br::Context::blocks(int size) const @@ -1080,6 +1081,8 @@ private: } } + + void store(QDataStream &stream) const { const int size = transforms.size(); @@ -1175,6 +1178,18 @@ static void _project(const Transform *transform, const Template *src, Template * } } +static void _backProject(const Transform *transform, const Template *dst, Template *src) +{ + try { + transform->backProject(*dst, *src); + } catch (...) { + qWarning("Exception triggered when processing %s with transform %s", qPrintable(src->file.flat()), qPrintable(transform->name())); + *src = Template(dst->file); + src->file.setBool("FTE"); + } +} + + void Transform::project(const TemplateList &src, TemplateList &dst) const { dst.reserve(src.size()); @@ -1188,6 +1203,24 @@ void Transform::project(const TemplateList &src, TemplateList &dst) const if (Globals->parallelism) Globals->trackFutures(futures); } +void Transform::backProject(const Template &dst, Template &src) const +{ + src = dst; +} + +void Transform::backProject(const TemplateList &dst, TemplateList &src) const +{ + src.reserve(dst.size()); + for (int i=0; i > futures; + if (Globals->parallelism) futures.reserve(dst.size()); + for (int i=0; iparallelism) futures.append(QtConcurrent::run(_backProject, this, &dst[i], &src[i])); + else _backProject (this, &dst[i], &src[i]); + if (Globals->parallelism) Globals->trackFutures(futures); +} + /* Distance - public methods */ void Distance::train(const TemplateList &templates) { diff --git a/sdk/openbr_plugin.h b/sdk/openbr_plugin.h index 0a0bd66..d9ca718 100644 --- a/sdk/openbr_plugin.h +++ b/sdk/openbr_plugin.h @@ -450,6 +450,13 @@ public: BR_PROPERTY(int, parallelism, 0) /*! + * \brief true if backProject should be used instead of project (the algorithm should be inverted) + */ + Q_PROPERTY(bool backProject READ get_backProject WRITE set_backProject RESET reset_backProject) + BR_PROPERTY(bool, backProject, false) + + + /*! * \brief If \c true no messages will be sent to the terminal, \c false by default. */ Q_PROPERTY(bool quiet READ get_quiet WRITE set_quiet RESET reset_quiet) @@ -919,6 +926,10 @@ public: virtual void train(const TemplateList &data) = 0; /*!< \brief Train the transform. */ virtual void project(const Template &src, Template &dst) const = 0; /*!< \brief Apply the transform. */ virtual void project(const TemplateList &src, TemplateList &dst) const; /*!< \brief Apply the transform. */ + virtual void backProject(const Template &dst, Template &src) const; /*!< \brief Invert the transform. */ + virtual void backProject(const TemplateList &dst, TemplateList &src) const; /*!< \brief Invert the transform. */ + + /*! * \brief Convenience function equivalent to project(). @@ -944,6 +955,7 @@ public: protected: Transform(bool independent = true); /*!< \brief Construct a transform. */ inline Transform *make(const QString &description) { return make(description, this); } /*!< \brief Make a subtransform. */ + }; /*! @@ -1010,6 +1022,7 @@ private: void load(QDataStream &stream) { (void) stream; } }; + /*! * \brief A br::MetaTransform that does not require training data. */ @@ -1057,7 +1070,7 @@ private: * \brief Returns \c true if the algorithm is a classifier, \c false otherwise. * * Classifers have no br::Distance associated with their br::Transform. -* Instead they populate br::Template::file \c Label metadata fielf with the predicted class. +* Instead they populate br::Template::file \c Label metadata field with the predicted class. */ BR_EXPORT bool IsClassifier(const QString &algorithm); diff --git a/sdk/plugins/eigen3.cpp b/sdk/plugins/eigen3.cpp index e5a3154..15a0a90 100644 --- a/sdk/plugins/eigen3.cpp +++ b/sdk/plugins/eigen3.cpp @@ -52,6 +52,7 @@ public: PCA() : keep(0.95), drop(0), whiten(false) {} private: + /* void backProject(const Template &src, Template &dst) const { const cv::Mat &m = src; @@ -64,6 +65,7 @@ private: // Do projection outMap = (eVecs * inMap) + mean; } + */ double residualReconstructionError(const Template &src) const { diff --git a/sdk/plugins/meta.cpp b/sdk/plugins/meta.cpp index 84f95b8..ff9b486 100644 --- a/sdk/plugins/meta.cpp +++ b/sdk/plugins/meta.cpp @@ -136,6 +136,25 @@ class PipeTransform : public MetaTransform Transform::project(src, dst); } } + + void projectBack(const Template &dst, Template &src) const + { + src = dst; + //reverse order in which transforms are processed + int length = transforms.length(); + //foreach (const Transform *f, transforms) { + for (int i = 0; i < length; i++){ + Transform *f = transforms.at(length - i - 1); + try { + src >> *f; + } catch (...) { + qWarning("Exception triggered when processing %s with transform %s", qPrintable(dst.file.flat()), qPrintable(f->name())); + src = Template(src.file); + src.file.setBool("FTE"); + } + } + } + }; BR_REGISTER(Transform, PipeTransform) @@ -193,6 +212,24 @@ class ChainTransform : public MetaTransform dst = Simplified(dst); } } + + void projectBack(const Template &dst, Template &src) const + { + src = dst; + //reverse order in which transforms are processed + int length = transforms.length(); + //foreach (const Transform *f, transforms) { + for (int i = 0; i < length; i++){ + Transform *f = transforms.at(length - i - 1); + try { + src >> *f; + } catch (...) { + qWarning("Exception triggered when processing %s with transform %s", qPrintable(dst.file.flat()), qPrintable(f->name())); + src = Template(src.file); + src.file.setBool("FTE"); + } + } + } }; BR_REGISTER(Transform, ChainTransform) diff --git a/sdk/plugins/pixel.cpp b/sdk/plugins/pixel.cpp index ce01991..c86aa2e 100644 --- a/sdk/plugins/pixel.cpp +++ b/sdk/plugins/pixel.cpp @@ -52,7 +52,7 @@ class PerPixelClassifier : public MetaTransform void rotate(Template &src, Template &dst) const { - if (temp.m.cols()%9 != 0) qFatal("Rotation invariance can only be used after Neighbors"); + //if (src.m().cols%9 != 0) qFatal("Rotation invariance can only be used after Neighbors"); int images = (src.m().cols)/9; dst = src; for (int i = 0; i < images; i++){ diff --git a/sdk/plugins/pp5.cpp b/sdk/plugins/pp5.cpp index dc1110d..3bd577f 100644 --- a/sdk/plugins/pp5.cpp +++ b/sdk/plugins/pp5.cpp @@ -11,9 +11,9 @@ #include #include #include -#include +#include -#include "common/resource.h" +#include "core/resource.h" #define TRY(CC) \ { \ @@ -30,7 +30,15 @@ if ((CC) != PPR_RAW_IMAGE_SUCCESS) qFatal("%d error (%s, %d): %s.", CC, __FILE__, __LINE__, ppr_raw_image_error_message(CC)); \ } -using namespace mm; +using namespace br; + + +/*! + * \ingroup initializers + * \brief Initialize PP5 + * \author Josh Klontz \cite jklontz + * \author E. Taborsky \cite mmtaborsky + */ class PP5Initializer : public Initializer { @@ -38,8 +46,8 @@ class PP5Initializer : public Initializer void initialize() const { - TRY(ppr_initialize_sdk(qPrintable(Globals->SDKPath + "/models/pp5/"), my_license_id, my_license_key)) - Globals->Abbreviations.insert("PP5","Open+PP5Enroll!Identity:PP5Compare"); + TRY(ppr_initialize_sdk(qPrintable(Globals->sdkPath + "/share/openbr/models/pp5/"), my_license_id, my_license_key)) + Globals->abbreviations.insert("PP5","Open+PP5Enroll!Identity:PP5Compare"); } void finalize() const @@ -48,7 +56,13 @@ class PP5Initializer : public Initializer } }; -MM_REGISTER(Initializer, PP5Initializer, "") +BR_REGISTER(Initializer, PP5Initializer) + +/*! + * \brief PP5 context + * \author Josh Klontz \cite jklontz + * \author E. Taborsky \cite mmtaborsky + */ struct PP5Context { @@ -194,11 +208,19 @@ struct PP5Context } }; -class PP5Enroll : public UntrainableFeature + +/*! + * \ingroup transforms + * \brief Enroll faces in PP5 + * \author Josh Klontz \cite jklontz + * \author E. Taborsky \cite mmtaborsky + */ + +class PP5Enroll : public UntrainableTransform { Q_OBJECT Q_PROPERTY(bool detectOnly READ get_detectOnly WRITE set_detectOnly) - MM_MEMBER(bool, detectOnly) + BR_PROPERTY(bool, detectOnly, false) Resource contexts; void project(const Template &src, Template &dst) const @@ -245,13 +267,29 @@ class PP5Enroll : public UntrainableFeature } }; -MM_REGISTER(Feature, PP5Enroll, "bool detectOnly = 0") +BR_REGISTER(Transform, PP5Enroll) + + -class PP5Compare : public Comparer +/*! + * \ingroup distances + * \brief Compare templates with PP5 + * \author Josh Klontz \cite jklontz + * \author E. Taborsky \cite mmtaborsky + */ + +class PP5Compare : public Distance , public PP5Context { Q_OBJECT + + float _compare(const Template &target, const Template &query) const + { + qFatal("PP5Compare: _compare should never be called"); + return 0; + } + void compare(const TemplateList &target, const TemplateList &query, Output *output) const { const float DefaultNonMatchScore = -1.5; @@ -274,7 +312,7 @@ class PP5Compare : public Comparer if ((query_face_id != -1) && (target_face_id != -1)) { TRY(ppr_get_face_similarity_score(context, similarity_matrix, query_face_id, target_face_id, &score)) } - output->setData(score, i, j); + output->setRelative(score, i, j); } } @@ -301,6 +339,8 @@ class PP5Compare : public Comparer } }; -MM_REGISTER(Comparer, PP5Compare, "") +BR_REGISTER(Distance, PP5Compare) + + #include "plugins/pp5.moc" -- libgit2 0.21.4