CVB++ 14.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{
17CVB_BEGIN_INLINE_NS
18
20namespace Polimago
21{
23namespace Training
24{
25
27
30{
31protected:
33 : preproCode_(), lambda_(LambdaDefault)
34 {}
35
37
38 PredictorFactoryBase& operator=(const PredictorFactoryBase&) = default;
39
40 virtual ~PredictorFactoryBase () = default;
41
42public:
44 static constexpr double LambdaDefault = 0.1;
45
48 {
49 return ValueRange<double> (0.0, 10.0);
50 }
51
53 static constexpr int FeatureResolutionDefault = 1;
54
57 {
58 return ValueRange<int> (0, 10);
59 }
60
62 static constexpr int PreprocessingMaxLength = 15;
63
64
65
66
69 {
70 return{ 'p', 'a', 's', '+' };
71 }
72
74
78 double Lambda() const noexcept
79 {
80 return lambda_;
81 }
82
84
88 void SetLambda(double lambda)
89 {
90 if ((lambda < LambdaRange().Min ()) || (lambda > LambdaRange().Max ()))
91 {
92 throw std::out_of_range ("lambda value out of range");
93 }
94
95 lambda_ = lambda;
96 }
97
99
104 {
105 return preproCode_;
106 }
107
109
113 void SetPreprocessing(const String &code)
114 {
115 if (code.length () > PreprocessingMaxLength)
116 {
117 throw std::invalid_argument ("Preprocessing codes must not be longer than 15 characters");
118 }
119 std::string asciiCode (Internal::CastToAscii(code));
120 std::transform(asciiCode.begin(), asciiCode.end(), asciiCode.begin(), [](char ch)
121 {
122 return static_cast<char>(std::tolower(ch));
123 });
124 if (asciiCode.find_first_not_of (PreprocessingValidCharacters().data()) != std::string::npos)
125 {
126 throw std::invalid_argument ("Preprocessing code contains invalid characters");
127 }
128
129 preproCode_.assign (asciiCode.begin(), asciiCode.end());
130 }
131
133
139 {
140 std::string asciiCode;
141 std::transform(input.begin(), input.end(), std::back_inserter(asciiCode), [](const String::value_type ch)
142 {
143 return static_cast<char>(std::tolower(static_cast<char>(ch)));
144 });
145
146 String retval;
147 for (auto c : asciiCode)
148 {
149 auto preprocessingValidCharacters = PreprocessingValidCharacters();
150 if (std::find (preprocessingValidCharacters.begin(), preprocessingValidCharacters.end(), c) != preprocessingValidCharacters.end()
151 && retval.length () < PreprocessingMaxLength)
152 {
153 retval.push_back (c);
154 }
155 }
156 return retval;
157 }
158
159protected:
160 void UseSettingsFromPredictor (const PredictorBase &clf)
161 {
162 preproCode_ = clf.Preprocessing();
163 lambda_ = clf.Lambda();
164 }
165
166protected:
167 String preproCode_;
168 double lambda_;
169};
170
173
175
178{
179protected:
182 featureResolution_(FeatureResolutionDefault), interpolation_(InterpolationDefault)
183 {}
184
186
187 PredictorFactoryBaseEx& operator=(const PredictorFactoryBaseEx&) = default;
188
189 virtual ~PredictorFactoryBaseEx () = default;
190
191public:
194
196
201 {
202 return interpolation_;
203 }
204
206
210 void SetInterpolation(InterpolationType interpolation) noexcept
211 {
212 interpolation_ = interpolation;
213 }
214
216
220 int FeatureResolution() const noexcept
221 {
222 return featureResolution_;
223 }
224
226
230 void SetFeatureResolution(int featureResolution)
231 {
232 if ((featureResolution < FeatureResolutionRange().Min ()) || (featureResolution > FeatureResolutionRange().Max ()))
233 {
234 throw std::out_of_range ("feature resolution value out of range");
235 }
236
237 featureResolution_ = featureResolution;
238 }
239
240protected:
241 void UseSettingsFromPredictor (const PredictorBaseEx &clf)
242 {
243 PredictorFactoryBase::UseSettingsFromPredictor (clf);
244 featureResolution_ = clf.FeatureResolution();
245 interpolation_ = clf.Interpolation ();
246 }
247
248 void UseSettingsFromTestResult (const Testing::TestResultBase &res)
249 {
250 interpolation_ = res.Interpolation ();
251 preproCode_ = res.Preprocessing();
252 lambda_ = res.Lambda();
253 featureResolution_ = res.FeatureResolution();
254 }
255
256private:
257 int featureResolution_;
258 InterpolationType interpolation_;
259};
260
263
264
265} /* namespace Training */
266
271
272} /* namespace Polimago */
273CVB_END_INLINE_NS
274} /* namespace Cvb */
String Preprocessing() const
Preprocessing code with which this object was generated.
Definition: predictor_base.hpp:175
double Lambda() const noexcept
Regularization value that has been used for generating this object.
Definition: predictor_base.hpp:98
InterpolationType Interpolation() const noexcept
Interpolation setting used for generating this object.
Definition: predictor_base.hpp:118
Base class for Polimago predictors.
Definition: predictor_base.hpp:284
int FeatureResolution() const noexcept
Feature resolution value with which the classifier was trained.
Definition: predictor_base.hpp:304
Base class for Polimago predictors.
Definition: predictor_base.hpp:198
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:97
Base class for classifier factory classes.
Definition: predictor_factory_base.hpp:178
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:210
int FeatureResolution() const noexcept
Gets the feature resolution (determines the size of the classification retina).
Definition: predictor_factory_base.hpp:220
void SetFeatureResolution(int featureResolution)
Sets the feature resolution (determines the size of the classification retina).
Definition: predictor_factory_base.hpp:230
InterpolationType Interpolation() const noexcept
Gets the interpolation setting to be used for generating this object.
Definition: predictor_factory_base.hpp:200
static constexpr InterpolationType InterpolationDefault
Default value for interpolation.
Definition: predictor_factory_base.hpp:193
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:103
static ValueRange< double > LambdaRange()
Acceptable scale factor range for search classifier training.
Definition: predictor_factory_base.hpp:47
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:88
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:56
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:44
static constexpr int FeatureResolutionDefault
Default value for feature resolution.
Definition: predictor_factory_base.hpp:53
void SetPreprocessing(const String &code)
Set preprocessing code with which the object is to be generated.
Definition: predictor_factory_base.hpp:113
static constexpr int PreprocessingMaxLength
Maximum length of a preprocessing code (excluding the terminating zero).
Definition: predictor_factory_base.hpp:62
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.
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
Angle Max(Angle a, Angle b) noexcept
Returns the bigger of two angles.
Definition: angle.hpp:504
Angle Min(Angle a, Angle b) noexcept
Returns the smaller of two angles.
Definition: angle.hpp:521