| 40 | * \br_property enum termCrit Termination criteria for training the random forest. Options are Iter, EPS and Both. Iter terminates when the maximum number of trees is reached. EPS terminates when forestAccuracy is met. Both terminates when either is true. Default is Iter. |
| 41 | */ |
| 42 | class ForestTransform : public Transform |
| 43 | { |
| 44 | Q_OBJECT |
| 45 | |
| 46 | void train(const TemplateList &data) |
| 47 | { |
| 48 | trainForest(data); |
| 49 | } |
| 50 | |
| 51 | void project(const Template &src, Template &dst) const |
| 52 | { |
| 53 | dst = src; |
| 54 | |
| 55 | float response; |
| 56 | if (classification && returnConfidence) { |
| 57 | // Fuzzy class label |
| 58 | response = forest.predict_prob(src.m().reshape(1,1)); |
| 59 | } else { |
| 60 | response = forest.predict(src.m().reshape(1,1)); |
| 61 | } |
| 62 | |
| 63 | if (overwriteMat) { |
| 64 | dst.m() = Mat(1, 1, CV_32F); |
| 65 | dst.m().at<float>(0, 0) = response; |
| 66 | } else { |
| 67 | dst.file.set(outputVariable, response); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void load(QDataStream &stream) |
| 72 | { |
| 73 | OpenCVUtils::loadModel(forest,stream); |
| 74 | } |
| 75 | |
| 76 | void store(QDataStream &stream) const |
| 77 | { |
| 78 | OpenCVUtils::storeModel(forest,stream); |
| 79 | } |
| 80 | |
| 81 | void init() |
| 82 | { |
| 83 | if (outputVariable.isEmpty()) |
| 84 | outputVariable = inputVariable; |
| 85 | } |
| 86 | |
| 87 | protected: |
| 88 | Q_ENUMS(TerminationCriteria) |
| 89 | Q_PROPERTY(bool classification READ get_classification WRITE set_classification RESET reset_classification STORED false) |
| 90 | Q_PROPERTY(float splitPercentage READ get_splitPercentage WRITE set_splitPercentage RESET reset_splitPercentage STORED false) |
| 91 | Q_PROPERTY(int maxDepth READ get_maxDepth WRITE set_maxDepth RESET reset_maxDepth STORED false) |
| 92 | Q_PROPERTY(int maxTrees READ get_maxTrees WRITE set_maxTrees RESET reset_maxTrees STORED false) |
| 93 | Q_PROPERTY(float forestAccuracy READ get_forestAccuracy WRITE set_forestAccuracy RESET reset_forestAccuracy STORED false) |
| 94 | Q_PROPERTY(bool returnConfidence READ get_returnConfidence WRITE set_returnConfidence RESET reset_returnConfidence STORED false) |
| 95 | Q_PROPERTY(bool overwriteMat READ get_overwriteMat WRITE set_overwriteMat RESET reset_overwriteMat STORED false) |
| 96 | Q_PROPERTY(QString inputVariable READ get_inputVariable WRITE set_inputVariable RESET reset_inputVariable STORED false) |
| 97 | Q_PROPERTY(QString outputVariable READ get_outputVariable WRITE set_outputVariable RESET reset_outputVariable STORED false) |
| 98 | Q_PROPERTY(bool weight READ get_weight WRITE set_weight RESET reset_weight STORED false) |
| 99 | Q_PROPERTY(TerminationCriteria termCrit READ get_termCrit WRITE set_termCrit RESET reset_termCrit STORED false) |