Commit 13053d6b authored by Vlad Shakhuro's avatar Vlad Shakhuro

Interface and naming fixes

parent 09286322
......@@ -56,21 +56,23 @@ namespace xobjdetect
class CV_EXPORTS WBDetector {
public:
WBDetector(const std::string& model_filename);
virtual void read(const FileNode &node) = 0;
virtual void write(FileStorage &fs) const = 0;
void train(
virtual void train(
const std::string& pos_samples,
const std::string& neg_imgs);
const std::string& neg_imgs) = 0;
void detect(
virtual void detect(
const Mat& img,
std::vector<Rect> &bboxes,
std::vector<double> &confidences);
std::vector<double> &confidences) = 0;
private:
std::string model_filename_;
virtual ~WBDetector(){}
};
CV_EXPORTS Ptr<WBDetector> create_wbdetector();
} /* namespace xobjdetect */
} /* namespace cv */
......
......@@ -45,9 +45,7 @@ the use of this software, even if advised of the possibility of such damage.
#ifndef _OPENCV_CASCADECLASSIFIER_H_
#define _OPENCV_CASCADECLASSIFIER_H_
#include <ctime>
#include "traincascade_features.h"
#include "lbpfeatures.h"
#include "precomp.hpp"
#define CC_CASCADE_FILENAME "cascade.xml"
#define CC_PARAMS_FILENAME "params.xml"
......
......@@ -45,7 +45,7 @@ the use of this software, even if advised of the possibility of such damage.
#ifndef _OPENCV_LBPFEATURES_H_
#define _OPENCV_LBPFEATURES_H_
#include "traincascade_features.h"
#include "precomp.hpp"
#define LBPF_NAME "lbpFeatureParams"
......
......@@ -72,8 +72,10 @@ the use of this software, even if advised of the possibility of such damage.
#include <cstdio>
#include "cascadeclassifier.h"
#include "traincascade_features.h"
#include "feature_evaluator.hpp"
#include "lbpfeatures.h"
#include "waldboost.hpp"
#include "wbdetector.hpp"
#include <opencv2/xobjdetect.hpp>
#endif /* __OPENCV_XOBJDETECT_PRECOMP_HPP__ */
......@@ -271,7 +271,6 @@ void WaldBoost::fit(Mat& data_pos, Mat& data_neg)
thresholds_.push_back(min_threshold);
polarities_.push_back(min_polarity);
feature_ignore[min_feature_ind] = true;
cascade_thresholds_.push_back(-1);
double loss = 0;
// Update positive weights
......@@ -291,13 +290,48 @@ void WaldBoost::fit(Mat& data_pos, Mat& data_neg)
neg_trace(0, j) += alpha * label;
loss += exp(+neg_trace(0, j)) / (2.0f * data_neg.cols);
}
double cascade_threshold = -1;
minMaxIdx(pos_trace, &cascade_threshold);
cascade_thresholds_.push_back(cascade_threshold);
std::cerr << "i=" << std::setw(4) << i;
std::cerr << " feat=" << std::setw(5) << min_feature_ind;
std::cerr << " thr=" << std::setw(3) << threshold_q;
std::cerr << " casthr=" << std::fixed << std::setprecision(3)
<< cascade_threshold;
std::cerr << " alpha=" << std::fixed << std::setprecision(3)
<< alpha << " err=" << std::fixed << std::setprecision(3) << min_err
<< " loss=" << std::scientific << loss << std::endl;
//int pos = 0;
//for (int j = 0; j < data_pos.cols; ++j) {
// if (pos_trace(0, j) > cascade_threshold - 0.5) {
// pos_trace(0, pos) = pos_trace(0, j);
// data_pos.col(j).copyTo(data_pos.col(pos));
// pos_weights(0, pos) = pos_weights(0, j);
// pos += 1;
// }
//}
//std::cerr << "pos " << data_pos.cols << "/" << pos << std::endl;
//pos_trace = pos_trace.colRange(0, pos);
//data_pos = data_pos.colRange(0, pos);
//pos_weights = pos_weights.colRange(0, pos);
int pos = 0;
for (int j = 0; j < data_neg.cols; ++j) {
if (neg_trace(0, j) > cascade_threshold - 0.5) {
neg_trace(0, pos) = neg_trace(0, j);
data_neg.col(j).copyTo(data_neg.col(pos));
neg_weights(0, pos) = neg_weights(0, j);
pos += 1;
}
}
std::cerr << "neg " << data_neg.cols << "/" << pos << std::endl;
neg_trace = neg_trace.colRange(0, pos);
data_neg = data_neg.colRange(0, pos);
neg_weights = neg_weights.colRange(0, pos);
if (loss < 1e-50 || min_err > 0.5) {
std::cerr << "Stopping early" << std::endl;
weak_count_ = i + 1;
......@@ -313,72 +347,86 @@ void WaldBoost::fit(Mat& data_pos, Mat& data_neg)
int WaldBoost::predict(Ptr<CvFeatureEvaluator> eval, float *h) const
{
double THR = -6;
assert(feature_indices_.size() == size_t(weak_count_));
assert(cascade_thresholds_.size() == size_t(weak_count_));
float res = 0;
for (int i = 0; i < weak_count_; ++i) {
int count = weak_count_;
for (int i = 0; i < count; ++i) {
float val = (*eval)(feature_indices_[i]);
int label = polarities_[i] * (val - thresholds_[i]) > 0 ? +1: -1;
res += alphas_[i] * label;
if (res < THR) {
if (res < cascade_thresholds_[i]) {
return -1;
}
}
*h = res;
return res > THR ? +1 : -1;
return res > cascade_thresholds_[count - 1] ? +1 : -1;
}
void WaldBoost::save(const std::string& filename)
void WaldBoost::write(FileStorage &fs) const
{
std::ofstream f(filename.c_str());
f << weak_count_ << std::endl;
for (size_t i = 0; i < thresholds_.size(); ++i) {
f << thresholds_[i] << " ";
}
f << std::endl;
for (size_t i = 0; i < alphas_.size(); ++i) {
f << alphas_[i] << " ";
}
f << std::endl;
for (size_t i = 0; i < polarities_.size(); ++i) {
f << polarities_[i] << " ";
}
f << std::endl;
for (size_t i = 0; i < cascade_thresholds_.size(); ++i) {
f << cascade_thresholds_[i] << " ";
}
f << std::endl;
for (size_t i = 0; i < feature_indices_.size(); ++i) {
f << feature_indices_[i] << " ";
}
f << std::endl;
fs << "waldboost" << "{";
fs << "waldboost_params"
<< "{" << "weak_count" << weak_count_ << "}";
fs << "thresholds" << "[";
for (size_t i = 0; i < thresholds_.size(); ++i)
fs << thresholds_[i];
fs << "]";
fs << "alphas" << "[";
for (size_t i = 0; i < alphas_.size(); ++i)
fs << alphas_[i];
fs << "]";
fs << "polarities" << "[";
for (size_t i = 0; i < polarities_.size(); ++i)
fs << polarities_[i];
fs << "]";
fs << "cascade_thresholds" << "[";
for (size_t i = 0; i < cascade_thresholds_.size(); ++i)
fs << cascade_thresholds_[i];
fs << "]";
fs << "feature_indices" << "[";
for (size_t i = 0; i < feature_indices_.size(); ++i)
fs << feature_indices_[i];
fs << "]";
fs << "}";
}
void WaldBoost::load(const std::string& filename)
void WaldBoost::read(const FileNode &node)
{
std::ifstream f(filename.c_str());
f >> weak_count_;
weak_count_ = (int)(node["waldboost_params"]["weak_count"]);
thresholds_.resize(weak_count_);
alphas_.resize(weak_count_);
polarities_.resize(weak_count_);
cascade_thresholds_.resize(weak_count_);
feature_indices_.resize(weak_count_);
for (int i = 0; i < weak_count_; ++i) {
f >> thresholds_[i];
}
for (int i = 0; i < weak_count_; ++i) {
f >> alphas_[i];
}
for (int i = 0; i < weak_count_; ++i) {
f >> polarities_[i];
}
for (int i = 0; i < weak_count_; ++i) {
f >> cascade_thresholds_[i];
}
for (int i = 0; i < weak_count_; ++i) {
f >> feature_indices_[i];
}
FileNodeIterator n;
n = node["thresholds"].begin();
for (int i = 0; i < weak_count_; ++i, ++n)
*n >> thresholds_[i];
n = node["alphas"].begin();
for (int i = 0; i < weak_count_; ++i, ++n)
*n >> alphas_[i];
n = node["polarities"].begin();
for (int i = 0; i < weak_count_; ++i, ++n)
*n >> polarities_[i];
n = node["cascade_thresholds"].begin();
for (int i = 0; i < weak_count_; ++i, ++n)
*n >> cascade_thresholds_[i];
n = node["feature_indices"].begin();
for (int i = 0; i < weak_count_; ++i, ++n)
*n >> feature_indices_[i];
}
void WaldBoost::reset(int weak_count)
......
......@@ -72,6 +72,10 @@ public:
int predict(Ptr<CvFeatureEvaluator> eval, float *h) const;
void save(const std::string& filename);
void load(const std::string& filename);
void read(const FileNode &node);
void write(FileStorage &fs) const;
void reset(int weak_count);
~WaldBoost();
......
......@@ -52,10 +52,6 @@ using std::string;
namespace cv {
namespace xobjdetect {
WBDetector::WBDetector(const string& model_filename):
model_filename_(model_filename)
{}
static vector<Mat> sample_patches(
const string& path,
int n_rows,
......@@ -93,13 +89,24 @@ static vector<Mat> read_imgs(const string& path)
return imgs;
}
void WBDetector::train(
void WBDetectorImpl::read(const FileNode& node)
{
boost_.read(node);
}
void WBDetectorImpl::write(FileStorage &fs) const
{
boost_.write(fs);
}
void WBDetectorImpl::train(
const string& pos_samples_path,
const string& neg_imgs_path)
{
vector<Mat> pos_imgs = read_imgs(pos_samples_path);
vector<Mat> neg_imgs = sample_patches(neg_imgs_path, 24, 24, pos_imgs.size());
vector<Mat> neg_imgs = sample_patches(neg_imgs_path, 24, 24, pos_imgs.size() * 10);
assert(pos_imgs.size());
assert(neg_imgs.size());
......@@ -111,16 +118,15 @@ void WBDetector::train(
eval->init(CvFeatureParams::create(), 1, Size(24, 24));
n_features = eval->getNumFeatures();
const int stages[] = {32, 64, 128, 256, 512, 1024, 2048, 4096};
const int stages[] = {64, 128, 256, 512, 1024};
const int stage_count = sizeof(stages) / sizeof(*stages);
const int stage_neg = 5000;
const int max_per_image = 25;
const int stage_neg = pos_imgs.size() * 5;
const int max_per_image = 100;
const float scales_arr[] = {.3f, .4f, .5f, .6f, .7f, .8f, .9f, 1.0f};
const vector<float> scales(scales_arr,
scales_arr + sizeof(scales_arr) / sizeof(*scales_arr));
WaldBoost boost;
vector<String> neg_filenames;
glob(neg_imgs_path, neg_filenames);
......@@ -133,22 +139,22 @@ void WBDetector::train(
neg_data = Mat1b(n_features, (int)neg_imgs.size());
for (size_t k = 0; k < pos_imgs.size(); ++k) {
eval->setImage(pos_imgs[k], +1, 0, boost.get_feature_indices());
eval->setImage(pos_imgs[k], +1, 0, boost_.get_feature_indices());
for (int j = 0; j < n_features; ++j) {
pos_data.at<uchar>(j, (int)k) = (uchar)(*eval)(j);
}
}
for (size_t k = 0; k < neg_imgs.size(); ++k) {
eval->setImage(neg_imgs[k], 0, 0, boost.get_feature_indices());
eval->setImage(neg_imgs[k], 0, 0, boost_.get_feature_indices());
for (int j = 0; j < n_features; ++j) {
neg_data.at<uchar>(j, (int)k) = (uchar)(*eval)(j);
}
}
boost.reset(stages[i]);
boost.fit(pos_data, neg_data);
boost_.reset(stages[i]);
boost_.fit(pos_data, neg_data);
if (i + 1 == stage_count) {
break;
......@@ -162,7 +168,7 @@ void WBDetector::train(
Mat img = imread(neg_filenames[img_i], CV_LOAD_IMAGE_GRAYSCALE);
vector<Rect> bboxes;
Mat1f confidences;
boost.detect(eval, img, scales, bboxes, confidences);
boost_.detect(eval, img, scales, bboxes, confidences);
if (confidences.rows > 0) {
Mat1i indices;
......@@ -185,18 +191,13 @@ void WBDetector::train(
cerr << "bootstrapped " << bootstrap_count << " windows from "
<< (img_i + 1) << " images" << endl;
}
boost.save(model_filename_);
}
void WBDetector::detect(
void WBDetectorImpl::detect(
const Mat& img,
vector<Rect> &bboxes,
vector<double> &confidences)
{
WaldBoost boost;
boost.load(model_filename_);
Mat test_img = img.clone();
bboxes.clear();
confidences.clear();
......@@ -207,9 +208,14 @@ void WBDetector::detect(
Ptr<CvFeatureParams> params = CvFeatureParams::create();
Ptr<CvFeatureEvaluator> eval = CvFeatureEvaluator::create();
eval->init(params, 1, Size(24, 24));
boost.detect(eval, img, scales, bboxes, confidences);
boost_.detect(eval, img, scales, bboxes, confidences);
assert(confidences.size() == bboxes.size());
}
Ptr<WBDetector> create_wbdetector()
{
return Ptr<WBDetector>(new WBDetectorImpl());
}
}
}
/*
By downloading, copying, installing or using the software you agree to this
license. If you do not agree to this license, do not download, install,
copy or use the software.
License Agreement
For Open Source Computer Vision Library
(3-clause BSD License)
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
Third party copyrights are property of their respective owners.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the names of the copyright holders nor the names of the contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall copyright holders or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
*/
#ifndef __OPENCV_XOBJDETECT_DETECTOR_HPP__
#define __OPENCV_XOBJDETECT_DETECTOR_HPP__
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <vector>
#include <string>
#include "precomp.hpp"
namespace cv
{
namespace xobjdetect
{
class WBDetectorImpl : public WBDetector {
public:
virtual void read(const FileNode &node);
virtual void write(FileStorage &fs) const;
virtual void train(
const std::string& pos_samples,
const std::string& neg_imgs);
virtual void detect(
const Mat& img,
std::vector<Rect> &bboxes,
std::vector<double> &confidences);
private:
WaldBoost boost_;
};
} /* namespace xobjdetect */
} /* namespace cv */
#endif /* __OPENCV_XOBJDETECT_DETECTOR_HPP__ */
......@@ -2,6 +2,7 @@
#include <opencv2/imgcodecs/imgcodecs_c.h>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <cstdio>
using namespace std;
using namespace cv;
using namespace cv::xobjdetect;
......@@ -10,20 +11,32 @@ int main(int argc, char **argv)
{
if (argc < 5) {
cerr << "Usage: " << argv[0] << " train <model_filename> <pos_path> <neg_path>" << endl;
cerr << " " << argv[0] << " detect <model_filename> <img_filename> <out_filename>" << endl;
cerr << " " << argv[0] << " detect <model_filename> <img_filename> <out_filename> <labelling_filename>" << endl;
return 0;
}
string mode = argv[1];
WBDetector detector(argv[2]);
Ptr<WBDetector> detector = create_wbdetector();
if (mode == "train") {
detector.train(argv[3], argv[4]);
assert(argc == 5);
detector->train(argv[3], argv[4]);
FileStorage fs(argv[2], FileStorage::WRITE);
detector->write(fs);
} else if (mode == "detect") {
cerr << "detect" << endl;
assert(argc == 6);
vector<Rect> bboxes;
vector<double> confidences;
Mat img = imread(argv[3], CV_LOAD_IMAGE_GRAYSCALE);
detector.detect(img, bboxes, confidences);
FileStorage fs(argv[2], FileStorage::READ);
detector->read(fs["waldboost"]);
detector->detect(img, bboxes, confidences);
FILE *fhandle = fopen(argv[5], "a");
for (size_t i = 0; i < bboxes.size(); ++i) {
Rect o = bboxes[i];
fprintf(fhandle, "%s;%u;%u;%u;%u;%lf\n",
argv[3], o.x, o.y, o.width, o.height, confidences[i]);
}
for (size_t i = 0; i < bboxes.size(); ++i) {
rectangle(img, bboxes[i], Scalar(255, 0, 0));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment