From 23d0e36167f0bc18325f8cc31bac5213433713cc Mon Sep 17 00:00:00 2001 From: "marina.kolpakova" Date: Mon, 17 Dec 2012 15:13:03 +0400 Subject: [PATCH] fix floating point precision --- apps/sft/include/sft/octave.hpp | 4 ++-- apps/sft/octave.cpp | 23 ++++++++++------------- apps/sft/sft.cpp | 34 ---------------------------------- 3 files changed, 12 insertions(+), 49 deletions(-) diff --git a/apps/sft/include/sft/octave.hpp b/apps/sft/include/sft/octave.hpp index 6544272..0a5fc17 100644 --- a/apps/sft/include/sft/octave.hpp +++ b/apps/sft/include/sft/octave.hpp @@ -147,7 +147,7 @@ public: virtual void setRejectThresholds(cv::Mat& thresholds); virtual void write( CvFileStorage* fs, string name) const; - virtual void write( cv::FileStorage &fs, const FeaturePool& pool, const Mat& thresholds = Mat()) const; + virtual void write( cv::FileStorage &fs, const FeaturePool& pool, const Mat& thresholds) const; int logScale; @@ -160,7 +160,7 @@ protected: float predict( const Mat& _sample, const cv::Range range) const; private: - void traverse(const CvBoostTree* tree, cv::FileStorage& fs, int& nfeatures, int* used, const float* th = 0) const; + void traverse(const CvBoostTree* tree, cv::FileStorage& fs, int& nfeatures, int* used, const double* th) const; virtual void initial_weights(double (&p)[2]); cv::Rect boundingBox; diff --git a/apps/sft/octave.cpp b/apps/sft/octave.cpp index bc8ea80..4e26ad9 100644 --- a/apps/sft/octave.cpp +++ b/apps/sft/octave.cpp @@ -116,7 +116,7 @@ void sft::Octave::setRejectThresholds(cv::Mat& thresholds) for (int si = 0; si < nsamples; ++si) { float decision = dptr[si] = predict(trainData.col(si), stab, false, false); - mptr[si] = cv::saturate_cast((uint)(responses.ptr(si)[0] == 1.f && decision == 1.f)); + mptr[si] = cv::saturate_cast((uint)( (responses.ptr(si)[0] == 1.f) && (decision == 1.f))); } // std::cout << "WARNING: responses " << responses << std::endl; @@ -144,8 +144,10 @@ void sft::Octave::setRejectThresholds(cv::Mat& thresholds) double mintrace = 0.; cv::minMaxLoc(traces.row(w), &mintrace); thptr[w] = mintrace; - // std::cout << "mintrace " << mintrace << std::endl << traces.colRange(0, npositives) << std::endl; + // std::cout << "mintrace " << mintrace << std::endl << traces.colRange(0, npositives).rowRange(w, w + 1) << std::endl << std::endl << std::endl << std::endl; } + + std::cout << "WARNING: thresholds " << thresholds << std::endl; } namespace { @@ -300,7 +302,7 @@ template int sgn(T val) { return (T(0) < val) - (val < T(0)); } -void sft::Octave::traverse(const CvBoostTree* tree, cv::FileStorage& fs, int& nfeatures, int* used, const float* th) const +void sft::Octave::traverse(const CvBoostTree* tree, cv::FileStorage& fs, int& nfeatures, int* used, const double* th) const { std::queue nodes; nodes.push( tree->get_root()); @@ -310,6 +312,7 @@ void sft::Octave::traverse(const CvBoostTree* tree, cv::FileStorage& fs, int& nf float* leafs = new float[(int)pow(2.f, get_params().max_depth)]; fs << "{"; + fs << "treeThreshold" << *th; fs << "internalNodes" << "["; while (!nodes.empty()) { @@ -349,14 +352,16 @@ void sft::Octave::traverse(const CvBoostTree* tree, cv::FileStorage& fs, int& nf fs << "leafValues" << "["; for (int ni = 0; ni < -leafValIdx; ni++) - fs << ( (!th) ? leafs[ni] : (sgn(leafs[ni]) * *th)); + fs << leafs[ni];//( (!th) ? leafs[ni] : (sgn(leafs[ni]) * *th)); fs << "]"; + fs << "}"; } void sft::Octave::write( cv::FileStorage &fso, const FeaturePool& pool, const Mat& thresholds) const { + CV_Assert(!thresholds.empty()); cv::Mat used( 1, weak->total * (pow(2, params.max_depth) - 1), CV_32SC1); int* usedPtr = used.ptr(0); int nfeatures = 0; @@ -373,19 +378,13 @@ void sft::Octave::write( cv::FileStorage &fso, const FeaturePool& pool, const Ma CvBoostTree* tree; CV_READ_SEQ_ELEM( tree, reader ); - if (!thresholds.empty()) - traverse(tree, fso, nfeatures, usedPtr, thresholds.ptr(0)+ i); - else - traverse(tree, fso, nfeatures, usedPtr); + traverse(tree, fso, nfeatures, usedPtr, thresholds.ptr(0) + i); } - // - fso << "]"; // features fso << "features" << "["; for (int i = 0; i < nfeatures; ++i) - // fso << usedPtr[i]; pool.write(fso, usedPtr[i]); fso << "]" << "}"; @@ -409,7 +408,6 @@ bool sft::Octave::train(const Dataset& dataset, const FeaturePool& pool, int wea // 1. fill integrals and classes processPositives(dataset, pool); generateNegatives(dataset); - // exit(0); // 2. only sumple case (all features used) int nfeatures = pool.size(); @@ -550,7 +548,6 @@ void sft::FeaturePool::fill(int desired) if (std::find(pool.begin(), pool.end(),f) == pool.end()) { - // std::cout << f << std::endl; pool.push_back(f); } } diff --git a/apps/sft/sft.cpp b/apps/sft/sft.cpp index 61f9267..c4435fb 100644 --- a/apps/sft/sft.cpp +++ b/apps/sft/sft.cpp @@ -100,16 +100,6 @@ int main(int argc, char** argv) return 1; } - cv::FileStorage fsr(cfg.outXmlPath + ".raw.xml" , cv::FileStorage::WRITE); - if(!fsr.isOpened()) - { - std::cout << "Training stopped. Output classifier Xml file " <