-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.cpp
More file actions
185 lines (141 loc) · 5.44 KB
/
classifier.cpp
File metadata and controls
185 lines (141 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "evaluator.h"
#include "data_retriever.h"
#include "image_reader.h"
#include "classifier.h"
#include <math.h>
#include <istream>
#include <sstream>
#include <ostream>
#include <iostream>
#include <fstream>
Classifier::Classifier() {}
Classifier::Classifier(string images_data_file, string labels_data_file) {
DataRetriever model = DataRetriever(move(images_data_file), move(labels_data_file));
CreateProbabilityModel(model);
CreatePriorsModel(model);
}
//generate prob matrix
void Classifier::CreateProbabilityModel(DataRetriever training_model) {
SetToZero();
SetLikelihoodToModel(training_model);
}
//helper to genereate prob matrix
void Classifier::SetToZero() {
for (int row = 0; row < kImageLength; row++) {
for (int col = 0; col < kImageLength; col++) {
for (int class_number = 0; class_number < kNumberOfClasses; class_number++) {
for (int feature = 0; feature < 2; feature++) {
probability_model[row][col][class_number][feature] = 0;
}
}
}
}
}
//helper to generate prob matrix
void Classifier::SetLikelihoodToModel(DataRetriever training_model) {
for (int row = 0; row < kImageLength; row++) {
for (int col = 0; col < kImageLength; col++) {
for (int class_number = 0; class_number < kNumberOfClasses; class_number++) {
for (int feature = 0; feature < 2; feature++) {
probability_model[row][col][class_number][feature] = training_model.CalculateLikelihoodForEachIndex(row, col, class_number, feature);
}
}
}
}
cout <<"test" << probability_model[15][15][3][1];
}
//Priors generator
void Classifier::CreatePriorsModel(DataRetriever training_model) {
std::vector<double> vector = training_model.CreateVectorOfPriorsProbability();
for (int i = 0; i < vector.size(); i++) {
priors_vector.push_back(vector.at(i));
}
}
//calculates the posterior probabilities and returns the maximum, which is the class of the image
int Classifier::CalculatePosteriorProbabilities(ImagesReader image) {
std::vector<double> vector_of_posteriors;
for (unsigned long long i = 0; i < kNumberOfClasses; i++) {
double posterior_probability = 0;
posterior_probability = posterior_probability + log(priors_vector.at(i));
for (int row = 0; row < kImageLength; row++) {
for (int col = 0; col < kImageLength; col++) {
posterior_probability += log(probability_model[row][col][i][image.actual_image[row][col]]);
}
}
vector_of_posteriors.push_back(posterior_probability);
}
double max = vector_of_posteriors.at(0);
int class_of_image = 0;
for (int i = 1; i < vector_of_posteriors.size(); i++) {
if (vector_of_posteriors.at(i) > max) {
max = vector_of_posteriors.at(i);
class_of_image = i;
}
}
return class_of_image;
}
//loads the probability model from a file
void Classifier::LoadModelFromFile(string probability_file) {
ifstream my_input_file(probability_file);
if (my_input_file.is_open()) {
for (int row = 0; row < kImageLength; row++) {
for (int col = 0; col < kImageLength; col++) {
for (int class_number = 0; class_number < kNumberOfClasses; class_number++) {
for (int feature = 0; feature < 2; feature++) {
double specific_likelihood_value;
my_input_file >> specific_likelihood_value;
probability_model[row][col][class_number][feature] = specific_likelihood_value;
}
}
}
}
my_input_file.close();
} else {
cout << "Invalid file";
}
}
//loads priors from a file
void Classifier::LoadPriorsModelFromFile(string priors_file) {
ifstream my_input_file(priors_file);
if (my_input_file.is_open()) {
double prior_probability;
while (my_input_file >> prior_probability) {
priors_vector.push_back(prior_probability);
}
my_input_file.close();
} else {
cout << "Invalid file";
}
}
//save likelihood model to a file
void Classifier::SaveModelToFile(string probability_file) {
ofstream my_output_file;
my_output_file.open(probability_file, std::ofstream::out | std::ofstream::trunc);
if (my_output_file.is_open()) {
for (int row = 0; row < kImageLength; row++) {
for (int col = 0; col < kImageLength; col++) {
for (int class_number = 0; class_number < kNumberOfClasses; class_number++) {
for (int feature = 0; feature < 2; feature++) {
my_output_file << probability_model[row][col][class_number][feature] << '\n';
}
}
}
}
my_output_file.close();
} else {
cout << "Invalid output file";
}
}
//saves priors model to a file
void Classifier::SavePriorsModelToFile(string priors_file) {
ofstream my_output_file;
my_output_file.open(priors_file, std::ofstream::out | std::ofstream::trunc);
if (my_output_file.is_open()) {
for (int i = 0; i < priors_vector.size(); i++) {
my_output_file << priors_vector.at(i) << endl;
}
my_output_file.close();
} else {
cout << "Invalid output file";
}
}