OTB  6.7.0
Orfeo Toolbox
otbTrainDecisionTree.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbTrainDecisionTree_hxx
22 #define otbTrainDecisionTree_hxx
25 
26 namespace otb
27 {
28 namespace Wrapper
29 {
30 
31 template <class TInputValue, class TOutputValue>
32 void
33 LearningApplicationBase<TInputValue,TOutputValue>
34 ::InitDecisionTreeParams()
35 {
36  AddChoice("classifier.dt", "Decision Tree classifier");
37  SetParameterDescription("classifier.dt",
38  "http://docs.opencv.org/modules/ml/doc/decision_trees.html");
39  //MaxDepth
40  AddParameter(ParameterType_Int, "classifier.dt.max", "Maximum depth of the tree");
41 #ifdef OTB_OPENCV_3
42  SetParameterInt("classifier.dt.max",10);
43 #else
44  SetParameterInt("classifier.dt.max",65535);
45 #endif
46  SetParameterDescription("classifier.dt.max",
47  "The training algorithm attempts to split each node while its depth is smaller "
48  "than the maximum possible depth of the tree. The actual depth may be smaller "
49  "if the other termination criteria are met, and/or if the tree is pruned.");
50 
51  //MinSampleCount
52  AddParameter(ParameterType_Int, "classifier.dt.min", "Minimum number of samples in each node");
53  SetParameterInt("classifier.dt.min",10);
54  SetParameterDescription("classifier.dt.min",
55  "If the number of samples in a node is smaller "
56  "than this parameter, then this node will not be split.");
57 
58  //RegressionAccuracy
59  AddParameter(ParameterType_Float, "classifier.dt.ra", "Termination criteria for regression tree");
60  SetParameterFloat("classifier.dt.ra",0.01);
61  SetParameterDescription("classifier.dt.ra",
62  "If all absolute differences between an estimated value in a node "
63  "and the values of the train samples in this node are smaller than this "
64  "regression accuracy parameter, then the node will not be split further.");
65 
66  //UseSurrogates : don't need to be exposed !
67  //SetParameterDescription("classifier.dt.sur","These splits allow working with missing data and compute variable importance correctly.");
68 
69  //MaxCategories
70  AddParameter(ParameterType_Int, "classifier.dt.cat",
71  "Cluster possible values of a categorical variable into K <= cat clusters to find a "
72  "suboptimal split");
73  SetParameterInt("classifier.dt.cat",10);
74  SetParameterDescription("classifier.dt.cat",
75  "Cluster possible values of a categorical variable into K <= cat clusters to find a "
76  "suboptimal split.");
77 
78  //CVFolds
79  AddParameter(ParameterType_Int, "classifier.dt.f", "K-fold cross-validations");
80 #ifdef OTB_OPENCV_3
81  // disable cross validation by default (crash in opencv 3.2)
82  SetParameterInt("classifier.dt.f",0);
83 #else
84  SetParameterInt("classifier.dt.f",10);
85 #endif
86  SetParameterDescription("classifier.dt.f",
87  "If cv_folds > 1, then it prunes a tree with K-fold cross-validation where K "
88  "is equal to cv_folds.");
89 
90  //Use1seRule
91  AddParameter(ParameterType_Bool, "classifier.dt.r", "Set Use1seRule flag to false");
92  SetParameterDescription("classifier.dt.r",
93  "If true, then a pruning will be harsher. This will make a tree more compact and more "
94  "resistant to the training data noise but a bit less accurate.");
95 
96  //TruncatePrunedTree
97  AddParameter(ParameterType_Bool, "classifier.dt.t", "Set TruncatePrunedTree flag to false");
98  SetParameterDescription("classifier.dt.t",
99  "If true, then pruned branches are physically removed from the tree.");
100 
101  //Priors are not exposed.
102 
103 }
104 
105 template <class TInputValue, class TOutputValue>
106 void
107 LearningApplicationBase<TInputValue,TOutputValue>
108 ::TrainDecisionTree(typename ListSampleType::Pointer trainingListSample,
109  typename TargetListSampleType::Pointer trainingLabeledListSample,
110  std::string modelPath)
111 {
113  typename DecisionTreeType::Pointer classifier = DecisionTreeType::New();
114  classifier->SetRegressionMode(this->m_RegressionFlag);
115  classifier->SetInputListSample(trainingListSample);
116  classifier->SetTargetListSample(trainingLabeledListSample);
117  classifier->SetMaxDepth(GetParameterInt("classifier.dt.max"));
118  classifier->SetMinSampleCount(GetParameterInt("classifier.dt.min"));
119  classifier->SetRegressionAccuracy(GetParameterFloat("classifier.dt.ra"));
120  classifier->SetMaxCategories(GetParameterInt("classifier.dt.cat"));
121  classifier->SetCVFolds(GetParameterInt("classifier.dt.f"));
122  if (GetParameterInt("classifier.dt.r"))
123  {
124  classifier->SetUse1seRule(false);
125  }
126  if (GetParameterInt("classifier.dt.t"))
127  {
128  classifier->SetTruncatePrunedTree(false);
129  }
130  classifier->Train();
131  classifier->Save(modelPath);
132 }
133 
134 } //end namespace wrapper
135 } //end namespace otb
136 
137 #endif