CVB++ 15.0
classifier_factory.hpp
1#pragma once
2
3#include "../_cexports/c_minos.h"
4
5#include "../global.hpp"
6
7#include "classifier.hpp"
8#include "training_set.hpp"
9
10#include <memory>
11#include <utility>
12#include <cmath>
13
14namespace Cvb
15{
16 CVB_BEGIN_INLINE_NS
17
19 namespace Minos
20 {
21
23
25 class ClassifierFactory
26 {
27 public:
28 ClassifierFactory() noexcept
29 : indifferenceRadius_(IndifferenceRadiusDefault())
30 , negativeDensity_(NegativeDensityDefault())
31 , contrastTrigger_(ContrastTriggerDefault())
32 , ensembleSize_(EnsembleSizeDefault())
33 , polydromy_(PolydromyDefault())
34 , minPairFeatures_(MinPairFeaturesDefault())
35 {
36 }
37
38 public:
41
47 {
48 CExports::TLearnControlStructure lcs = {};
49 lcs.Param1 = static_cast<CExports::cvbval_t>(indifferenceRadius_);
50 lcs.Param2 = static_cast<CExports::cvbval_t>(lround(negativeDensity_ * 1000.0));
51 lcs.Param3 = static_cast<CExports::cvbval_t>(contrastTrigger_);
52 lcs.Param4 = static_cast<CExports::cvbval_t>(ensembleSize_);
53 lcs.Param5 = static_cast<CExports::cvbval_t>(polydromy_);
54 lcs.Param6 = static_cast<CExports::cvbval_t>(minPairFeatures_);
55 LearnProgressData progressData = {trainingSet, false};
56 lcs.Data = &progressData;
57 lcs.ShowProgress = NativeLearnProgress;
58
59 CExports::CLF clf = nullptr;
60 CVB_CALL_CAPI_CHECKED(LearnCLFFromMTSTyped(trainingSet.Handle(), trainingSet.FileName().c_str(), lcs, clf));
61 if (progressData.wasInterrupted)
62 {
63 CVB_CALL_CAPI(ReleaseObject(clf));
64 return nullptr;
65 }
66 HandleGuard<Classifier> clfGuard(clf);
67
68 CVB_CALL_CAPI(SetCLFQualityType(clf, CExports::Quality_ReturnCorrelation));
69 CVB_CALL_CAPI(SetCLFThreshold(clf, 0.6));
70 return Classifier::FromHandle(std::move(clfGuard));
71 }
72
74
78 static int IndifferenceRadiusDefault() noexcept
79 {
80 return 6;
81 }
82
84
88 static double NegativeDensityDefault() noexcept
89 {
90 return 1.0;
91 }
92
94
98 static int ContrastTriggerDefault() noexcept
99 {
100 return 8;
101 }
102
104
108 static int EnsembleSizeDefault() noexcept
109 {
110 return 15;
111 }
112
114
118 static int PolydromyDefault() noexcept
119 {
120 return 2;
121 }
122
124
128 static int MinPairFeaturesDefault() noexcept
129 {
130 return 20;
131 }
132
133 public:
136
140 int IndifferenceRadius() const noexcept
141 {
142 return indifferenceRadius_;
143 }
144
147
151 void SetIndifferenceRadius(int indifferenceRadius)
152 {
153 if (indifferenceRadius < 0)
154 {
155 throw std::invalid_argument("indifference radius value must be >=0");
156 }
157 indifferenceRadius_ = indifferenceRadius;
158 }
159
161
165 double NegativeDensity() const noexcept
166 {
167 return negativeDensity_;
168 }
169
173
177 void SetNegativeDensity(double negativeDensity)
178 {
179 if (negativeDensity < 0.0 || negativeDensity > 1.0)
180 {
181 throw std::invalid_argument("negative density value must be in [0.0, 1.0]");
182 }
183 negativeDensity_ = negativeDensity;
184 }
185
188
192 int ContrastTrigger() const noexcept
193 {
194 return contrastTrigger_;
195 }
196
201
205 void SetContrastTrigger(int contrastTrigger)
206 {
207 if (contrastTrigger < 1 || contrastTrigger > 254)
208 {
209 throw std::invalid_argument("contrast trigger value must be in [1, 254]");
210 }
211 contrastTrigger_ = contrastTrigger;
212 }
213
216
220 int EnsembleSize() const noexcept
221 {
222 return ensembleSize_;
223 }
224
227
231 void SetEnsembleSize(int ensembleSize)
232 {
233 if (ensembleSize < 1)
234 {
235 throw std::invalid_argument("ensemble size value must be >0");
236 }
237 ensembleSize_ = ensembleSize;
238 }
239
241
245 int Polydromy() const noexcept
246 {
247 return polydromy_;
248 }
249
252
256 void SetPolydromy(int polydromy)
257 {
258 if (polydromy < 1)
259 {
260 throw std::invalid_argument("polydromy value must be >0");
261 }
262 polydromy_ = polydromy;
263 }
264
266
270 int MinPairFeatures() const noexcept
271 {
272 return minPairFeatures_;
273 }
274
276
280 void SetMinPairFeatures(int minPairFeatures)
281 {
282 if (minPairFeatures < 1)
283 {
284 throw std::invalid_argument("minimum feature count value must be >0");
285 }
286 minPairFeatures_ = minPairFeatures;
287 }
288
289 private:
290 struct LearnProgressData
291 {
292 const TrainingSet &currentlyProcessed;
293 bool wasInterrupted;
294 };
295
296 static CExports::cvbbool_t __stdcall NativeLearnProgress(void *pPrivate, CExports::MTSINSTANCE instance,
297 CExports::cvbval_t total, CExports::cvbval_t correct,
298 CExports::cvbval_t current)
299 {
300 LearnProgressData *pdata = reinterpret_cast<LearnProgressData *>(pPrivate);
301 if (pdata)
302 {
303 }
304 (void)instance;
305 (void)total;
306 (void)correct;
307 (void)current;
308 return false;
309 }
310
311 private:
312 int indifferenceRadius_;
313 double negativeDensity_;
314 int contrastTrigger_;
315 int ensembleSize_;
316 int polydromy_;
317 int minPairFeatures_;
318 };
319
320 } /* namespace Minos */
321 CVB_END_INLINE_NS
322} /* namespace Cvb */
static int ContrastTriggerDefault() noexcept
Default value for the ContrastTrigger.
Definition classifier_factory.hpp:98
std::unique_ptr< Classifier > Learn(const TrainingSet &trainingSet)
Learn a new classifier from the trainingSet using the parameters stored in the properties of this obj...
Definition classifier_factory.hpp:46
int MinPairFeatures() const noexcept
Get the minimum number of features to extract for each model when building a classifier.
Definition classifier_factory.hpp:270
void SetEnsembleSize(int ensembleSize)
Set the maximum size of the Ensembles of similar instance images to be used for pair feature calculat...
Definition classifier_factory.hpp:231
static int PolydromyDefault() noexcept
Default value for the Polydromy.
Definition classifier_factory.hpp:118
int IndifferenceRadius() const noexcept
Get the minimum distance to be assumed between a (labeled) positive sample and a counter sample in a ...
Definition classifier_factory.hpp:140
void SetIndifferenceRadius(int indifferenceRadius)
Set the minimum distance to be assumed between a (labeled) positive sample and a counter sample in a ...
Definition classifier_factory.hpp:151
int ContrastTrigger() const noexcept
Get the minimum contrast a Minos feature must achieve before it is eligible to become part of the cla...
Definition classifier_factory.hpp:192
void SetMinPairFeatures(int minPairFeatures)
Set the minimum number of features to extract for each model when building a classifier.
Definition classifier_factory.hpp:280
static double NegativeDensityDefault() noexcept
Default value for the NegativeDensity.
Definition classifier_factory.hpp:88
int Polydromy() const noexcept
Get the polydromy value controlling the complexity of the feature search tree in the classifier.
Definition classifier_factory.hpp:245
static int EnsembleSizeDefault() noexcept
Default value for the EnsembleSize.
Definition classifier_factory.hpp:108
int EnsembleSize() const noexcept
Get the maximum size of the Ensembles of similar instance images to be used for pair feature calculat...
Definition classifier_factory.hpp:220
void SetContrastTrigger(int contrastTrigger)
Set the minimum contrast a Minos feature must achieve before it is eligible to become part of the cla...
Definition classifier_factory.hpp:205
static int MinPairFeaturesDefault() noexcept
Default value for the MinPairFeatures.
Definition classifier_factory.hpp:128
void SetNegativeDensity(double negativeDensity)
Set the scan density with which to extract counter samples from the training set images....
Definition classifier_factory.hpp:177
static int IndifferenceRadiusDefault() noexcept
Default value for the IndifferenceRadius.
Definition classifier_factory.hpp:78
double NegativeDensity() const noexcept
Get the scan density with which to extract counter samples from the training set images.
Definition classifier_factory.hpp:165
void SetPolydromy(int polydromy)
Set the polydromy value controlling the complexity of the feature search tree in the classifier....
Definition classifier_factory.hpp:256
static std::unique_ptr< Classifier > FromHandle(HandleGuard< Classifier > &&guard)
Creates classifier from a classic API handle.
Definition classifier.hpp:390
A Minos Training Set from which a classifier can be generated.
Definition training_set.hpp:1188
String FileName() const
Name of the file, from which this training set was loaded (empty string if this image list was neithe...
Definition training_set.hpp:1448
void * Handle() const noexcept
Classic API CLF handle.
Definition training_set.hpp:1295
cvbbool_t ReleaseObject(OBJ &Object)
T move(T... args)
Namespace for the Minos package.
Definition classifier.hpp:29
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17