CVB++ 15.0
predictor_factory_base.hpp
1#pragma once
2
3#include "../_cexports/c_polimago.h"
4
5#include "../global.hpp"
6#include "../string.hpp"
7#include "../value_range.hpp"
8#include "predictor_base.hpp"
9#include "test_result_base.hpp"
10
11#include <algorithm>
12#include <iterator>
13#include <cctype>
14
15namespace Cvb
16{
17 CVB_BEGIN_INLINE_NS
18
20 namespace Polimago
21 {
23 namespace Training
24 {
25
27
29 class PredictorFactoryBase
30 {
31 public:
32 PredictorFactoryBase(const PredictorFactoryBase &other) = delete;
33 PredictorFactoryBase &operator=(const PredictorFactoryBase &other) = delete;
34 PredictorFactoryBase(PredictorFactoryBase &&other) = delete;
35 PredictorFactoryBase &operator=(PredictorFactoryBase &&other) = delete;
36 virtual ~PredictorFactoryBase() = default;
37
38 protected:
39 PredictorFactoryBase()
40 : preproCode_()
41 , lambda_(LambdaDefault)
42 {
43 }
44
45 public:
47 static constexpr double LambdaDefault = 0.1;
48
51 {
52 return ValueRange<double>(0.0, 10.0);
53 }
54
56 static constexpr int FeatureResolutionDefault = 1;
57
60 {
61 return ValueRange<int>(0, 10);
62 }
63
65 static constexpr int PreprocessingMaxLength = 15;
66
69 {
70 return {'p', 'a', 's', '+'};
71 }
72
74
78 double Lambda() const noexcept
79 {
80 return lambda_;
81 }
82
85
89 void SetLambda(double lambda)
90 {
91 if ((lambda < LambdaRange().Min()) || (lambda > LambdaRange().Max()))
92 {
93 throw std::out_of_range("lambda value out of range");
94 }
95
96 lambda_ = lambda;
97 }
98
100
105 {
106 return preproCode_;
107 }
108
110
114 void SetPreprocessing(const String &code)
115 {
116 if (code.length() > PreprocessingMaxLength)
117 {
118 throw std::invalid_argument("Preprocessing codes must not be longer than 15 characters");
119 }
120 std::string asciiCode(Internal::CastToAscii(code));
121 std::transform(asciiCode.begin(), asciiCode.end(), asciiCode.begin(),
122 [](char ch) { return static_cast<char>(std::tolower(ch)); });
123 if (asciiCode.find_first_not_of(PreprocessingValidCharacters().data()) != std::string::npos)
124 {
125 throw std::invalid_argument("Preprocessing code contains invalid characters");
126 }
127
128 preproCode_.assign(asciiCode.begin(), asciiCode.end());
129 }
130
133
139 {
140 std::string asciiCode;
141 std::transform(input.begin(), input.end(), std::back_inserter(asciiCode), [](const String::value_type ch) {
142 return static_cast<char>(std::tolower(static_cast<char>(ch)));
143 });
144
145 String retval;
146 for (auto c : asciiCode)
147 {
148 auto preprocessingValidCharacters = PreprocessingValidCharacters();
149 if (std::find(preprocessingValidCharacters.begin(), preprocessingValidCharacters.end(), c)
150 != preprocessingValidCharacters.end()
151 && retval.length() < PreprocessingMaxLength)
152 {
153 retval.push_back(c);
154 }
155 }
156 return retval;
157 }
158
159 protected:
160 void UseSettingsFromPredictor(const PredictorBase &clf)
161 {
162 preproCode_ = clf.Preprocessing();
163 lambda_ = clf.Lambda();
164 }
165
166 double Lamda() const noexcept
167 {
168 return lambda_;
169 }
170
171 void SetLamda(double lambda) noexcept
172 {
173 lambda_ = lambda;
174 }
175
176 String PreproCode() const
177 {
178 return preproCode_;
179 }
180
181 void SetPreproCode(const String &prepreCode)
182 {
183 preproCode_ = prepreCode;
184 }
185
186 private:
187 String preproCode_;
188 double lambda_;
189 };
190
193
195
197 class PredictorFactoryBaseEx : public PredictorFactoryBase
198 {
199 protected:
200 PredictorFactoryBaseEx()
201 : PredictorFactoryBase()
202 , featureResolution_(FeatureResolutionDefault)
203 , interpolation_(InterpolationDefault)
204 {
205 }
206
207 public:
210
212
217 {
218 return interpolation_;
219 }
220
223
227 void SetInterpolation(InterpolationType interpolation) noexcept
228 {
229 interpolation_ = interpolation;
230 }
231
233
237 int FeatureResolution() const noexcept
238 {
239 return featureResolution_;
240 }
241
243
247 void SetFeatureResolution(int featureResolution)
248 {
249 if ((featureResolution < FeatureResolutionRange().Min())
250 || (featureResolution > FeatureResolutionRange().Max()))
251 {
252 throw std::out_of_range("feature resolution value out of range");
253 }
254
255 featureResolution_ = featureResolution;
256 }
257
258 protected:
259 void UseSettingsFromPredictor(const PredictorBaseEx &clf)
260 {
261 PredictorFactoryBase::UseSettingsFromPredictor(clf);
262 featureResolution_ = clf.FeatureResolution();
263 interpolation_ = clf.Interpolation();
264 }
265
266 void UseSettingsFromTestResult(const Testing::TestResultBase &res)
267 {
268 interpolation_ = res.Interpolation();
269 SetPreproCode(res.Preprocessing());
270 SetLambda(res.Lambda());
271 featureResolution_ = res.FeatureResolution();
272 }
273
274 private:
275 int featureResolution_;
276 InterpolationType interpolation_;
277 };
278
281
282 } /* namespace Training */
283
288
289 } /* namespace Polimago */
290 CVB_END_INLINE_NS
291} /* namespace Cvb */
T back_inserter(T... args)
String Preprocessing() const
Preprocessing code with which this object was generated.
Definition predictor_base.hpp:181
double Lambda() const noexcept
Regularization value that has been used for generating this object.
Definition predictor_base.hpp:100
InterpolationType Interpolation() const noexcept
Interpolation setting used for generating this object.
Definition predictor_base.hpp:120
Base class for Polimago predictors.
Definition predictor_base.hpp:300
int FeatureResolution() const noexcept
Feature resolution value with which the classifier was trained.
Definition predictor_base.hpp:314
Base class for Polimago predictors.
Definition predictor_base.hpp:214
static constexpr InterpolationType InterpolationDefault
Default value for interpolation.
Definition predictor_factory_base.hpp:209
static constexpr double LambdaDefault
Default value for lambda.
Definition predictor_factory_base.hpp:47
Base class for all Polimago Test Results.
Definition test_result_base.hpp:26
int FeatureResolution() const noexcept
Feature resolution value with which the classifier was trained.
Definition test_result_base.hpp:92
Base class for classifier factory classes.
Definition predictor_factory_base.hpp:198
void SetInterpolation(InterpolationType interpolation) noexcept
Sets the interpolation setting to be used for generating this object. Using interpolation will genera...
Definition predictor_factory_base.hpp:227
int FeatureResolution() const noexcept
Gets the feature resolution (determines the size of the classification retina).
Definition predictor_factory_base.hpp:237
void SetFeatureResolution(int featureResolution)
Sets the feature resolution (determines the size of the classification retina).
Definition predictor_factory_base.hpp:247
InterpolationType Interpolation() const noexcept
Gets the interpolation setting to be used for generating this object.
Definition predictor_factory_base.hpp:216
static constexpr InterpolationType InterpolationDefault
Default value for interpolation.
Definition predictor_factory_base.hpp:209
Base class for classifier factory classes.
Definition predictor_factory_base.hpp:30
static constexpr std::array< char, 4 > PreprocessingValidCharacters()
Characters that a preprocessing string may contain.
Definition predictor_factory_base.hpp:68
String Preprocessing() const
Get preprocessing code with which the object is to be generated.
Definition predictor_factory_base.hpp:104
static ValueRange< double > LambdaRange()
Acceptable scale factor range for search classifier training.
Definition predictor_factory_base.hpp:50
void SetLambda(double lambda)
Sets the regularization value to be used for generating the object. Possible values range from 0 to 1...
Definition predictor_factory_base.hpp:89
static String FormatPreprocessingCode(const String &input)
Correct a preprocessing code to make sure that no invalid characters are in the code and the code doe...
Definition predictor_factory_base.hpp:138
static ValueRange< int > FeatureResolutionRange()
Valid range of feature resolution value.
Definition predictor_factory_base.hpp:59
double Lambda() const noexcept
Gets the regularization value to be used for generating the object.
Definition predictor_factory_base.hpp:78
static constexpr double LambdaDefault
Default value for lambda.
Definition predictor_factory_base.hpp:47
static constexpr int FeatureResolutionDefault
Default value for feature resolution.
Definition predictor_factory_base.hpp:56
void SetPreprocessing(const String &code)
Set preprocessing code with which the object is to be generated.
Definition predictor_factory_base.hpp:114
static constexpr int PreprocessingMaxLength
Maximum length of a preprocessing code (excluding the terminating zero).
Definition predictor_factory_base.hpp:65
Container for range definitions.
Definition value_range.hpp:17
T find(T... args)
Namespace for the Polimago package training functionality.
Definition classification_predictor_factory.hpp:23
std::shared_ptr< PredictorFactoryBaseEx > PredictorFactoryBaseExPtr
Convenience shared pointer for PredictorFactoryBaseEx.
Definition predictor_factory_base.hpp:280
std::shared_ptr< PredictorFactoryBase > PredictorFactoryBasePtr
Convenience shared pointer for PredictorFactoryBase.
Definition predictor_factory_base.hpp:192
Namespace for the Polimago package.
Definition classification_predictor.hpp:38
InterpolationType
Interpolation to be used when extracting image data for classifier generation.
Definition predictor_base.hpp:33
@ Linear
Image data is (if necessary) extracted with linear interpolation.
Definition predictor_base.hpp:37
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
Angle Max(Angle a, Angle b) noexcept
Returns the bigger of two angles.
Definition angle.hpp:495
Angle Min(Angle a, Angle b) noexcept
Returns the smaller of two angles.
Definition angle.hpp:512
T transform(T... args)