mirror of
https://github.com/opencv/opencv.git
synced 2025-01-18 22:44:02 +08:00
fix floating point precision
This commit is contained in:
parent
ef4b18f341
commit
23d0e36167
@ -147,7 +147,7 @@ public:
|
|||||||
virtual void setRejectThresholds(cv::Mat& thresholds);
|
virtual void setRejectThresholds(cv::Mat& thresholds);
|
||||||
virtual void write( CvFileStorage* fs, string name) const;
|
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;
|
int logScale;
|
||||||
|
|
||||||
@ -160,7 +160,7 @@ protected:
|
|||||||
|
|
||||||
float predict( const Mat& _sample, const cv::Range range) const;
|
float predict( const Mat& _sample, const cv::Range range) const;
|
||||||
private:
|
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]);
|
virtual void initial_weights(double (&p)[2]);
|
||||||
|
|
||||||
cv::Rect boundingBox;
|
cv::Rect boundingBox;
|
||||||
|
@ -116,7 +116,7 @@ void sft::Octave::setRejectThresholds(cv::Mat& thresholds)
|
|||||||
for (int si = 0; si < nsamples; ++si)
|
for (int si = 0; si < nsamples; ++si)
|
||||||
{
|
{
|
||||||
float decision = dptr[si] = predict(trainData.col(si), stab, false, false);
|
float decision = dptr[si] = predict(trainData.col(si), stab, false, false);
|
||||||
mptr[si] = cv::saturate_cast<uchar>((uint)(responses.ptr<float>(si)[0] == 1.f && decision == 1.f));
|
mptr[si] = cv::saturate_cast<uchar>((uint)( (responses.ptr<float>(si)[0] == 1.f) && (decision == 1.f)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// std::cout << "WARNING: responses " << responses << std::endl;
|
// std::cout << "WARNING: responses " << responses << std::endl;
|
||||||
@ -144,8 +144,10 @@ void sft::Octave::setRejectThresholds(cv::Mat& thresholds)
|
|||||||
double mintrace = 0.;
|
double mintrace = 0.;
|
||||||
cv::minMaxLoc(traces.row(w), &mintrace);
|
cv::minMaxLoc(traces.row(w), &mintrace);
|
||||||
thptr[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 {
|
namespace {
|
||||||
@ -300,7 +302,7 @@ template <typename T> int sgn(T val) {
|
|||||||
return (T(0) < val) - (val < T(0));
|
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<const CvDTreeNode*> nodes;
|
std::queue<const CvDTreeNode*> nodes;
|
||||||
nodes.push( tree->get_root());
|
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)];
|
float* leafs = new float[(int)pow(2.f, get_params().max_depth)];
|
||||||
|
|
||||||
fs << "{";
|
fs << "{";
|
||||||
|
fs << "treeThreshold" << *th;
|
||||||
fs << "internalNodes" << "[";
|
fs << "internalNodes" << "[";
|
||||||
while (!nodes.empty())
|
while (!nodes.empty())
|
||||||
{
|
{
|
||||||
@ -349,14 +352,16 @@ void sft::Octave::traverse(const CvBoostTree* tree, cv::FileStorage& fs, int& nf
|
|||||||
|
|
||||||
fs << "leafValues" << "[";
|
fs << "leafValues" << "[";
|
||||||
for (int ni = 0; ni < -leafValIdx; ni++)
|
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 << "]";
|
||||||
|
|
||||||
|
|
||||||
fs << "}";
|
fs << "}";
|
||||||
}
|
}
|
||||||
|
|
||||||
void sft::Octave::write( cv::FileStorage &fso, const FeaturePool& pool, const Mat& thresholds) const
|
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);
|
cv::Mat used( 1, weak->total * (pow(2, params.max_depth) - 1), CV_32SC1);
|
||||||
int* usedPtr = used.ptr<int>(0);
|
int* usedPtr = used.ptr<int>(0);
|
||||||
int nfeatures = 0;
|
int nfeatures = 0;
|
||||||
@ -373,19 +378,13 @@ void sft::Octave::write( cv::FileStorage &fso, const FeaturePool& pool, const Ma
|
|||||||
CvBoostTree* tree;
|
CvBoostTree* tree;
|
||||||
CV_READ_SEQ_ELEM( tree, reader );
|
CV_READ_SEQ_ELEM( tree, reader );
|
||||||
|
|
||||||
if (!thresholds.empty())
|
traverse(tree, fso, nfeatures, usedPtr, thresholds.ptr<double>(0) + i);
|
||||||
traverse(tree, fso, nfeatures, usedPtr, thresholds.ptr<float>(0)+ i);
|
|
||||||
else
|
|
||||||
traverse(tree, fso, nfeatures, usedPtr);
|
|
||||||
}
|
}
|
||||||
//
|
|
||||||
|
|
||||||
fso << "]";
|
fso << "]";
|
||||||
// features
|
// features
|
||||||
|
|
||||||
fso << "features" << "[";
|
fso << "features" << "[";
|
||||||
for (int i = 0; i < nfeatures; ++i)
|
for (int i = 0; i < nfeatures; ++i)
|
||||||
// fso << usedPtr[i];
|
|
||||||
pool.write(fso, usedPtr[i]);
|
pool.write(fso, usedPtr[i]);
|
||||||
fso << "]"
|
fso << "]"
|
||||||
<< "}";
|
<< "}";
|
||||||
@ -409,7 +408,6 @@ bool sft::Octave::train(const Dataset& dataset, const FeaturePool& pool, int wea
|
|||||||
// 1. fill integrals and classes
|
// 1. fill integrals and classes
|
||||||
processPositives(dataset, pool);
|
processPositives(dataset, pool);
|
||||||
generateNegatives(dataset);
|
generateNegatives(dataset);
|
||||||
// exit(0);
|
|
||||||
|
|
||||||
// 2. only sumple case (all features used)
|
// 2. only sumple case (all features used)
|
||||||
int nfeatures = pool.size();
|
int nfeatures = pool.size();
|
||||||
@ -550,7 +548,6 @@ void sft::FeaturePool::fill(int desired)
|
|||||||
|
|
||||||
if (std::find(pool.begin(), pool.end(),f) == pool.end())
|
if (std::find(pool.begin(), pool.end(),f) == pool.end())
|
||||||
{
|
{
|
||||||
// std::cout << f << std::endl;
|
|
||||||
pool.push_back(f);
|
pool.push_back(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,16 +100,6 @@ int main(int argc, char** argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
cv::FileStorage fsr(cfg.outXmlPath + ".raw.xml" , cv::FileStorage::WRITE);
|
|
||||||
if(!fsr.isOpened())
|
|
||||||
{
|
|
||||||
std::cout << "Training stopped. Output classifier Xml file " <<cfg.outXmlPath + ".raw.xml" << " can't be opened." << std::endl << std::flush;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ovector strong;
|
|
||||||
// strong.reserve(cfg.octaves.size());
|
|
||||||
|
|
||||||
fso << cfg.cascadeName
|
fso << cfg.cascadeName
|
||||||
<< "{"
|
<< "{"
|
||||||
<< "stageType" << "BOOST"
|
<< "stageType" << "BOOST"
|
||||||
@ -120,16 +110,6 @@ int main(int argc, char** argv)
|
|||||||
<< "shrinkage" << cfg.shrinkage
|
<< "shrinkage" << cfg.shrinkage
|
||||||
<< "octaves" << "[";
|
<< "octaves" << "[";
|
||||||
|
|
||||||
fsr << cfg.cascadeName
|
|
||||||
<< "{"
|
|
||||||
<< "stageType" << "BOOST"
|
|
||||||
<< "featureType" << "ICF"
|
|
||||||
<< "octavesNum" << (int)cfg.octaves.size()
|
|
||||||
<< "width" << cfg.modelWinSize.width
|
|
||||||
<< "height" << cfg.modelWinSize.height
|
|
||||||
<< "shrinkage" << cfg.shrinkage
|
|
||||||
<< "octaves" << "[";
|
|
||||||
|
|
||||||
// 3. Train all octaves
|
// 3. Train all octaves
|
||||||
for (ivector::const_iterator it = cfg.octaves.begin(); it != cfg.octaves.end(); ++it)
|
for (ivector::const_iterator it = cfg.octaves.begin(); it != cfg.octaves.end(); ++it)
|
||||||
{
|
{
|
||||||
@ -163,8 +143,6 @@ int main(int argc, char** argv)
|
|||||||
boost.setRejectThresholds(thresholds);
|
boost.setRejectThresholds(thresholds);
|
||||||
|
|
||||||
boost.write(fso, pool, thresholds);
|
boost.write(fso, pool, thresholds);
|
||||||
boost.write(fsr, pool);
|
|
||||||
// std::cout << "thresholds " << thresholds << std::endl;
|
|
||||||
|
|
||||||
cv::FileStorage tfs(("thresholds." + cfg.resPath(it)).c_str(), cv::FileStorage::WRITE);
|
cv::FileStorage tfs(("thresholds." + cfg.resPath(it)).c_str(), cv::FileStorage::WRITE);
|
||||||
tfs << "thresholds" << thresholds;
|
tfs << "thresholds" << thresholds;
|
||||||
@ -174,18 +152,6 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fso << "]" << "}";
|
fso << "]" << "}";
|
||||||
fsr << "]" << "}";
|
|
||||||
|
|
||||||
// // // 6. Set thresolds
|
|
||||||
// // cascade.prune();
|
|
||||||
|
|
||||||
// // // 7. Postprocess
|
|
||||||
// // cascade.normolize();
|
|
||||||
|
|
||||||
// // // 8. Write result xml
|
|
||||||
// // cv::FileStorage ofs(cfg.outXmlPath, cv::FileStorage::WRITE);
|
|
||||||
// // ofs << cfg.cascadeName << cascade;
|
|
||||||
|
|
||||||
std::cout << "Training complete..." << std::endl;
|
std::cout << "Training complete..." << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user