22 #include "MAWeakModel.hpp" 24 #include "core/MACoreTypes.hpp" 26 #include <MCContainers.hpp> 29 MAWeakModel::MAWeakModel(
MAModel& classifier)
31 AddClassifier(classifier);
40 unsigned int MAWeakModel::GetFeatureCount()
const 42 unsigned int Size = 0;
44 for (
unsigned int i = 0; i <
Classifiers.size(); ++i)
52 MC::FloatList& MAWeakModel::GetModelLabels()
58 void MAWeakModel::AddClassifier(
MAModel& classifier)
64 MC::FloatList MAWeakModel::Predict(
const MC::FloatTable& input_vectors, MC::FloatList& confidences)
66 if (input_vectors.empty())
68 MC_WARNING(
"No data in the input table for classifier prediction.");
69 return MC::FloatList();
72 int RowSize = input_vectors[0].size();
76 MC_WARNING(
"No data in the input table for classifier prediction.");
77 return MC::FloatList();
79 for (
unsigned int i = 1; i < input_vectors.size(); ++i)
81 if ((
int)input_vectors[i].size() != RowSize)
83 MC_WARNING(
"Row counts are inconsistent in the input table for classifier prediction.");
84 return MC::FloatList();
87 if (RowSize != (
int)GetFeatureCount())
89 MC_WARNING(
"Feature count does not match with the expected (%d != %d).", GetFeatureCount(), RowSize);
90 return MC::FloatList();
94 for (
unsigned int i = 0; i < input_vectors.size(); ++i)
96 Labels.push_back(Predict(input_vectors[i], confidences));
102 float MAWeakModel::Predict(
const MC::FloatList& input_vector, MC::FloatList& confidence)
104 if (input_vector.empty())
106 MC_WARNING(
"No data in the input vector for classifier prediction.");
109 if (input_vector.size() != GetFeatureCount())
111 MC_WARNING(
"Feature count does not match with the expected test vector (%d != %d).",
112 GetFeatureCount(), input_vector.size());
116 MA::FloatFloatSortedMap LabelCounts;
118 for (
unsigned int i = 0; i <
Classifiers.size(); ++i)
120 int FeatureCount =
Classifiers[i]->GetFeatureCount();
121 MC::FloatList VectorPart(input_vector.begin()+StartIndex, input_vector.begin()+StartIndex+FeatureCount);
122 MC::FloatList Confidence;
123 float Label =
Classifiers[i]->Predict(VectorPart, Confidence);
125 if (Confidence.empty())
127 LabelCounts[Label]++;
129 LabelCounts[Label] += Confidence[0];
131 StartIndex += FeatureCount;
134 float MaxLabel = LabelCounts.begin()->first;
135 float MaxValue = LabelCounts.begin()->second;
137 for (
auto& value : LabelCounts)
139 MaxAmount += value.second;
140 if (MaxValue < value.second)
142 MaxLabel = value.first;
143 MaxValue = value.second;
146 confidence.push_back(MaxValue / MaxAmount);
151 MC::FloatTable MAWeakModel::Predict(
const MC::FloatTable& input_vectors)
153 if (input_vectors.empty())
155 MC_WARNING(
"No data in the input table for classifier prediction.");
156 return MC::FloatTable();
159 int RowSize = input_vectors[0].size();
163 MC_WARNING(
"No data in the input table for classifier prediction.");
164 return MC::FloatTable();
166 for (
unsigned int i = 1; i < input_vectors.size(); ++i)
168 if ((
int)input_vectors[i].size() != RowSize)
170 MC_WARNING(
"Row counts are inconsistent in the input table for classifier prediction.");
171 return MC::FloatTable();
174 if (RowSize != (
int)GetFeatureCount())
176 MC_WARNING(
"Feature count does not match with the expected (%d != %d).", GetFeatureCount(), RowSize);
177 return MC::FloatTable();
179 MC::FloatTable LabelTable;
181 for (
unsigned int i = 0; i < input_vectors.size(); ++i)
183 LabelTable.push_back(Predict(input_vectors[i]));
189 MC::FloatList MAWeakModel::Predict(
const MC::FloatList& input_vector)
191 if (input_vector.empty())
193 MC_WARNING(
"No data in the input vector for classifier prediction.");
194 return MC::FloatList();
196 if (input_vector.size() != GetFeatureCount())
198 MC_WARNING(
"Feature count does not match with the expected test vector (%d != %d).",
199 GetFeatureCount(), input_vector.size());
200 return MC::FloatList();
202 MC::FloatList Labels;
204 MC::FloatList Confidence;
206 for (
unsigned int i = 0; i <
Classifiers.size(); ++i)
208 int FeatureCount =
Classifiers[i]->GetFeatureCount();
209 MC::FloatList VectorPart(input_vector.begin()+StartIndex, input_vector.begin()+StartIndex+FeatureCount);
211 Labels.push_back(
Classifiers[i]->Predict(VectorPart, Confidence));
212 StartIndex += FeatureCount;
221 MA::BinaryDataSPtrList DataList;
224 for (
unsigned int i = 0; i <
Classifiers.size(); ++i)
226 DataList.push_back(MC::BinaryDataSPtr(
Classifiers[i]->Encode()));
227 FileSize += DataList[i]->GetSize();
230 for (
unsigned int i = 0; i <
Classifiers.size(); ++i)
232 FileData->
AddInt32(DataList[i]->GetSize());
233 DataList[i]->ResetPosition();
234 DataList[i]->WriteData(*FileData, DataList[i]->GetSize());
252 ReadData->Allocate(Data.
GetInt32());
253 Data.
WriteData(*ReadData, ReadData->GetSize());
254 Model = MAModel::Decode(*ReadData);
262 WeakModel->AddClassifier(*Model);
~MAWeakModel()
Class destructor.
Classifier model based on OpenCV classifiers.
#define MC_WARNING(...)
Warning macro.
MA::OpenCVModelPtrList Classifiers
OpenCV classifiers.
void AddInt32(int32_t new_int, bool reverse_order=false)
Add a 32 bit integer at the current position.
Weak model based on OpenCV classifiers.
bool WriteData(MCBinaryData &target, int capacity=-1)
Write data into a target instance.
void SetPosition(unsigned int position)
Set the cursor position.
float MCFloatInfinity()
Get float infinity.
int32_t GetInt32(bool reverse_order=false)
Get a 32 bit integer from the current position.
int GetPosition() const
Get the current position in the binary data.
bool IsPositionAtEnd() const
Check if the current position is at the end of the binary data.