Commit fcb1bcac0dd72c4c1d63e70838f4ed5e156f81d8
1 parent
f76fe526
removed adaboost
Showing
1 changed file
with
0 additions
and
135 deletions
openbr/plugins/classification/adaboost.cpp deleted
| 1 | -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
| 2 | - * Copyright 2012 The MITRE Corporation * | ||
| 3 | - * * | ||
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); * | ||
| 5 | - * you may not use this file except in compliance with the License. * | ||
| 6 | - * You may obtain a copy of the License at * | ||
| 7 | - * * | ||
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 * | ||
| 9 | - * * | ||
| 10 | - * Unless required by applicable law or agreed to in writing, software * | ||
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, * | ||
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
| 13 | - * See the License for the specific language governing permissions and * | ||
| 14 | - * limitations under the License. * | ||
| 15 | - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
| 16 | - | ||
| 17 | -#include <openbr/plugins/openbr_internal.h> | ||
| 18 | -#include <openbr/core/opencvutils.h> | ||
| 19 | - | ||
| 20 | -using namespace cv; | ||
| 21 | - | ||
| 22 | -namespace br | ||
| 23 | -{ | ||
| 24 | - | ||
| 25 | -/*! | ||
| 26 | - * \ingroup transforms | ||
| 27 | - * \brief Wraps OpenCV's Ada Boost framework | ||
| 28 | - * \author Scott Klum \cite sklum | ||
| 29 | - * \br_link http://docs.opencv.org/modules/ml/doc/boosting.html | ||
| 30 | - * \br_property type enum Type of Adaboost to perform. Options are: [Discrete, Real, Logit, Gentle] Default is Real. | ||
| 31 | - * \br_property splitCriteria enum Splitting criteria used to choose optimal splits during a weak tree construction. Options are: [Default, Gini, Misclass, Sqerr] Default is Default. | ||
| 32 | - * \br_property weakCount int Maximum number of weak classifiers per stage. Default is 100. | ||
| 33 | - * \br_property trimRate float A threshold between 0 and 1 used to save computational time. Samples with summary weight \leq 1 - weight\_trim\_rate do not participate in the next iteration of training. Set this parameter to 0 to turn off this functionality. Default is 0.95. | ||
| 34 | - * \br_property folds int OpenCV parameter variable. Default value is 0. | ||
| 35 | - * \br_property maxDepth int Maximum height of each weak classifier tree. Default is 1 (stumps). | ||
| 36 | - * \br_property returnConfidence bool Return the confidence value of the classification or the class value of the classification. Default is true (return confidence value). | ||
| 37 | - * \br_property overwriteMat bool If true, the output template will be a 1x1 matrix with value equal to the confidence or classification (depending on returnConfidence). If false the output template will be the same as the input template. Default is true. | ||
| 38 | - * \br_property inputVariable QString Metadata variable storing the label for each template. Default is "Label". | ||
| 39 | - * \br_property outputVariable QString Metadata variable to store the confidence or classification of each template (depending on returnConfidence). If overwriteMat is true nothing will be written here. Default is "". | ||
| 40 | - */ | ||
| 41 | -class AdaBoostTransform : public Transform | ||
| 42 | -{ | ||
| 43 | - Q_OBJECT | ||
| 44 | - Q_ENUMS(Type) | ||
| 45 | - Q_ENUMS(SplitCriteria) | ||
| 46 | - | ||
| 47 | - Q_PROPERTY(Type type READ get_type WRITE set_type RESET reset_type STORED false) | ||
| 48 | - Q_PROPERTY(int weakCount READ get_weakCount WRITE set_weakCount RESET reset_weakCount STORED false) | ||
| 49 | - Q_PROPERTY(float trimRate READ get_trimRate WRITE set_trimRate RESET reset_trimRate STORED false) | ||
| 50 | - Q_PROPERTY(int folds READ get_folds WRITE set_folds RESET reset_folds STORED false) | ||
| 51 | - Q_PROPERTY(int maxDepth READ get_maxDepth WRITE set_maxDepth RESET reset_maxDepth STORED false) | ||
| 52 | - Q_PROPERTY(bool returnConfidence READ get_returnConfidence WRITE set_returnConfidence RESET reset_returnConfidence STORED false) | ||
| 53 | - Q_PROPERTY(bool overwriteMat READ get_overwriteMat WRITE set_overwriteMat RESET reset_overwriteMat STORED false) | ||
| 54 | - Q_PROPERTY(QString inputVariable READ get_inputVariable WRITE set_inputVariable RESET reset_inputVariable STORED false) | ||
| 55 | - Q_PROPERTY(QString outputVariable READ get_outputVariable WRITE set_outputVariable RESET reset_outputVariable STORED false) | ||
| 56 | - | ||
| 57 | -public: | ||
| 58 | - enum Type { Discrete = ml::Boost::DISCRETE, | ||
| 59 | - Real = ml::Boost::REAL, | ||
| 60 | - Logit = ml::Boost::LOGIT, | ||
| 61 | - Gentle = ml::Boost::GENTLE}; | ||
| 62 | - | ||
| 63 | -private: | ||
| 64 | - BR_PROPERTY(Type, type, Real) | ||
| 65 | - BR_PROPERTY(int, weakCount, 100) | ||
| 66 | - BR_PROPERTY(float, trimRate, .95) | ||
| 67 | - BR_PROPERTY(int, folds, 0) | ||
| 68 | - BR_PROPERTY(int, maxDepth, 1) | ||
| 69 | - BR_PROPERTY(bool, returnConfidence, true) | ||
| 70 | - BR_PROPERTY(bool, overwriteMat, true) | ||
| 71 | - BR_PROPERTY(QString, inputVariable, "Label") | ||
| 72 | - BR_PROPERTY(QString, outputVariable, "") | ||
| 73 | - | ||
| 74 | - Ptr<ml::Boost> boost; | ||
| 75 | - | ||
| 76 | - void init() | ||
| 77 | - { | ||
| 78 | - boost = ml::Boost::create(); | ||
| 79 | - | ||
| 80 | - if (outputVariable.isEmpty()) | ||
| 81 | - outputVariable = inputVariable; | ||
| 82 | - } | ||
| 83 | - | ||
| 84 | - void train(const TemplateList &data) | ||
| 85 | - { | ||
| 86 | - Mat samples = OpenCVUtils::toMat(data.data()); | ||
| 87 | - Mat labels = OpenCVUtils::toMat(File::get<float>(data, inputVariable)); | ||
| 88 | - | ||
| 89 | - Mat types = Mat(samples.cols + 1, 1, CV_8U); | ||
| 90 | - types.setTo(Scalar(ml::VAR_NUMERICAL)); | ||
| 91 | - types.at<char>(samples.cols, 0) = ml::VAR_CATEGORICAL; | ||
| 92 | - | ||
| 93 | - boost->setBoostType(type); | ||
| 94 | - boost->setWeakCount(weakCount); | ||
| 95 | - boost->setWeightTrimRate(trimRate); | ||
| 96 | - boost->setCVFolds(folds); | ||
| 97 | - boost->setMaxDepth(maxDepth); | ||
| 98 | - | ||
| 99 | - boost->train(ml::TrainData::create(samples, ml::ROW_SAMPLE, labels, noArray(), noArray(), types, noArray())); | ||
| 100 | - } | ||
| 101 | - | ||
| 102 | - void project(const Template &src, Template &dst) const | ||
| 103 | - { | ||
| 104 | - dst = src; | ||
| 105 | - float response; | ||
| 106 | - if (returnConfidence) { | ||
| 107 | - response = boost->predict(src.m().reshape(1,1), noArray(), ml::StatModel::RAW_OUTPUT) / weakCount; | ||
| 108 | - } else { | ||
| 109 | - response = boost->predict(src.m().reshape(1,1)); | ||
| 110 | - } | ||
| 111 | - | ||
| 112 | - if (overwriteMat) { | ||
| 113 | - dst.m() = Mat(1, 1, CV_32F); | ||
| 114 | - dst.m().at<float>(0, 0) = response; | ||
| 115 | - } else { | ||
| 116 | - dst.file.set(outputVariable, response); | ||
| 117 | - } | ||
| 118 | - } | ||
| 119 | - | ||
| 120 | - void load(QDataStream &stream) | ||
| 121 | - { | ||
| 122 | - OpenCVUtils::loadModel(boost,stream); | ||
| 123 | - } | ||
| 124 | - | ||
| 125 | - void store(QDataStream &stream) const | ||
| 126 | - { | ||
| 127 | - OpenCVUtils::storeModel(boost,stream); | ||
| 128 | - } | ||
| 129 | -}; | ||
| 130 | - | ||
| 131 | -BR_REGISTER(Transform, AdaBoostTransform) | ||
| 132 | - | ||
| 133 | -} // namespace br | ||
| 134 | - | ||
| 135 | -#include "classification/adaboost.moc" |