CVB++ 15.0
classification_test_result.hpp
1#pragma once
2
3#include "../_cexports/c_polimago.h"
4
5#include "../global.hpp"
6#include "../string.hpp"
7#include "test_result_base.hpp"
8
9#include <vector>
10#include <memory>
11
12namespace Cvb
13{
14 CVB_BEGIN_INLINE_NS
15
17 namespace Polimago
18 {
20 namespace Testing
21 {
22
24
26 class ClassificationTestResult : public TestResultBase
27 {
28 private:
29 explicit ClassificationTestResult(ReleaseObjectGuard &&guard)
30 : TestResultBase(std::move(guard))
31 {
32 auto numClasses = NumClasses();
33 for (decltype(numClasses) i = 0; i < numClasses; ++i)
34 {
35 std::array<Char, 256> lbl = {};
36 CVB_CALL_CAPI_CHECKED(PMGetTestResultClassLabelTyped(Handle(), i, lbl.data()));
37 classes_.push_back(lbl.data());
38 }
39
40 auto numExamples = NumExamples();
41 for (decltype(numExamples) i = 0; i < numExamples; ++i)
42 {
43 auto confidence = CVB_CALL_CAPI(PMGetTestResultConfidence(Handle(), i));
44 exampleConfidences_.push_back(confidence);
45 }
46
47 for (decltype(numExamples) i = 0; i < numExamples; ++i)
48 {
49 std::vector<double> confidenceDistribution;
50 for (decltype(numClasses) j = 0; j < numClasses; ++j)
51 {
52 auto confidenceDistributionValue = CVB_CALL_CAPI(PMGetTestResultConfidenceDistribution(Handle(), i, j));
53 confidenceDistribution.push_back(confidenceDistributionValue);
54 }
55 exampleConfidenceDistributions_.push_back(confidenceDistribution);
56 }
57
58 for (decltype(numExamples) i = 0; i < numExamples; ++i)
59 {
60 auto index = CVB_CALL_CAPI(PMGetTestResultTrueClassIndex(Handle(), i));
61 trueClassIndices_.push_back(index);
62 }
63
64 for (decltype(numExamples) i = 0; i < numExamples; ++i)
65 {
66 auto index = CVB_CALL_CAPI(PMGetTestResultPredictedClass(Handle(), i));
67 predictedClassIndices_.push_back(index);
68 }
69 }
70
71 std::string ObjectName() const override
72 {
73 return thisObjectName_;
74 }
75
76 public:
78
82 explicit ClassificationTestResult(const String &fileName)
83 : ClassificationTestResult(ReleaseObjectGuard(LoadInternal(fileName)))
84 {
85 if (NumClasses() == 0)
86 {
87 throw std::runtime_error("Loading the ClassificationTestResult failed");
88 }
89 }
90
92
99 static std::unique_ptr<ClassificationTestResult> FromHandle(ReleaseObjectGuard &&guard)
100 {
101 if (!guard.Handle())
102 {
103 throw std::invalid_argument("invalid classification test result native handle");
104 }
105 return std::unique_ptr<ClassificationTestResult>(new ClassificationTestResult(std::move(guard)));
106 }
107
109
115 {
116 return std::make_unique<ClassificationTestResult>(fileName);
117 }
118
120
124 int NumClasses() const
125 {
126 return static_cast<int>(CVB_CALL_CAPI(PMGetTestResultNumClasses(Handle())));
127 }
128
132
136 int OutputDimension() const
137 {
138 return static_cast<int>(CVB_CALL_CAPI(PMGetTestResultOutputDimension(Handle())));
139 }
140
142
146 int NumErrors() const
147 {
148 return static_cast<int>(CVB_CALL_CAPI(PMGetTestResultNumErrors(Handle())));
149 }
150
152
156 double ErrorRate() const
157 {
158 return static_cast<int>(CVB_CALL_CAPI(PMGetTestResultErrorRate(Handle())));
159 }
160
162
167 {
168 return classes_;
169 }
170
172
177 {
178 return exampleConfidences_;
179 }
180
182
187 {
188 return exampleConfidenceDistributions_;
189 }
190
192
197 {
198 return trueClassIndices_;
199 }
200
202
207 {
208 return predictedClassIndices_;
209 }
210
211 private:
212 std::vector<String> classes_;
213 std::vector<double> exampleConfidences_;
214 std::vector<std::vector<double>> exampleConfidenceDistributions_;
215 std::vector<int> trueClassIndices_;
216 std::vector<int> predictedClassIndices_;
217 const std::string thisObjectName_ = "Polimago Classification Test Resul";
218 };
219
222
223 } /* namespace Testing */
224
227
228 } /* namespace Polimago */
229 CVB_END_INLINE_NS
230} /* namespace Cvb */
void * Handle() const noexcept
Classic API Polimago handle.
Definition predictor_base.hpp:68
Classification test result object.
Definition classification_test_result.hpp:27
int NumClasses() const
Number of classes in the sample database on which the test result was calculated.
Definition classification_test_result.hpp:124
std::vector< double > ExampleConfidences() const
Confidences in the classification decision for each sample.
Definition classification_test_result.hpp:176
static std::unique_ptr< ClassificationTestResult > Load(const String &fileName)
Load a saved test result from a file.
Definition classification_test_result.hpp:114
int NumErrors() const
Total number of errors made during classification testing.
Definition classification_test_result.hpp:146
double ErrorRate() const
Total number of errors divided by the total number of examples.
Definition classification_test_result.hpp:156
int OutputDimension() const
For tests with usage OneVersusAll the output dimension will equal the number of classes in the traini...
Definition classification_test_result.hpp:136
std::vector< std::vector< double > > ExampleConfidenceDistributions() const
Confidence distributions for each example.
Definition classification_test_result.hpp:186
std::vector< String > Classes() const
Class labels available in this test result.
Definition classification_test_result.hpp:166
std::vector< int > TrueClassIndices() const
The real class index for each example.
Definition classification_test_result.hpp:196
std::vector< int > PredictedClassIndices() const
Prediction results for all indices.
Definition classification_test_result.hpp:206
ClassificationTestResult(const String &fileName)
Load a saved classification test result from a file.
Definition classification_test_result.hpp:82
static std::unique_ptr< ClassificationTestResult > FromHandle(ReleaseObjectGuard &&guard)
Creates test result from a classic API handle.
Definition classification_test_result.hpp:99
int NumExamples() const
Number of examples that contributed to this test result.
Definition test_result_base.hpp:61
T move(T... args)
Namespace for the Polimago package testing functionality.
Definition classification_test_result.hpp:21
std::shared_ptr< ClassificationTestResult > ClassificationTestResultPtr
Convenience shared pointer for ClassificationTestResult.
Definition classification_test_result.hpp:221
Namespace for the Polimago package.
Definition classification_predictor.hpp:38
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