-
Notifications
You must be signed in to change notification settings - Fork 0
/
hopfieldimagerecognition.cpp
145 lines (133 loc) · 3.92 KB
/
hopfieldimagerecognition.cpp
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
#include <iostream>
#include <fstream>
#include <vector>
#include <armadillo>
#include "hopfieldimagerecognition.h"
using namespace std;
using namespace arma;
HopfieldImageRecognition::HopfieldImageRecognition(){
iteration = 0;
change = true;
}
void HopfieldImageRecognition::setImages(char *fileName){
ifstream myfile(fileName);
if (myfile.is_open()){
myfile >> height;
myfile >> width;
pixels = height * width;
myfile >> size;
for (int indexImage = 0; indexImage < size; indexImage++){
vector<double> vector;
for (int indexPixel = 0; indexPixel < pixels; indexPixel++){
double pixel;
myfile >> pixel;
vector.push_back(pixel);
}
images.push_back(mat(vector).t());
}
} else{
cout << "Error open file" << endl;
}
}
void HopfieldImageRecognition::showImages(){
cout << "Images:" << endl;
for (int indexImage = 0; indexImage < size; indexImage++){
cout << "Image [" << indexImage + 1 << "]:" << endl;
for (int indexPixel = 0; indexPixel < pixels; indexPixel++){
cout << ((images.at(indexImage)(0,indexPixel) == 1) ? "#" : ".");
if ((indexPixel + 1) % width == 0) {
cout << endl;
}
}
cout << endl;
}
}
void HopfieldImageRecognition::setImage(char *fileName){
int h;
int w;
ifstream myfile(fileName);
if (myfile.is_open()){
myfile >> h;
myfile >> w;
if (h != height && w != width){
cout << "Wrong height or width in image for recognition";
exit(1);
}
vector<double> vector;
for (int indexPixel = 0; indexPixel < pixels; indexPixel++){
double pixel;
myfile >> pixel;
vector.push_back(pixel);
}
image = mat(vector).t();
} else{
cout << "Error open file for recognition" << endl;
}
}
void HopfieldImageRecognition::showImage(){
cout << endl;
for (int indexPixel = 0; indexPixel < pixels; indexPixel++){
cout << ((image(0,indexPixel) == 1) ? "#" : ".");
if ((indexPixel + 1) % width == 0) {
cout << endl;
}
}
cout << endl;
}
void HopfieldImageRecognition::nextStep(){
srand (time(NULL));
int random = getHopfielRandomOrExit();
mat S = (image * weight.col(random));
if (S(0,0) < 0){
if (image(0, random) != -1){
image(0,random) = -1;
change = true;
}
} else if (S(0,0) > 0){
if (image(0, random) != 1){
image(0,random) = 1;
change = true;
}
} else {
// image(0,random) /*(t)*/ = image(0,random) /*(t-1)*/
}
iteration++;
}
int HopfieldImageRecognition::getHopfielRandomOrExit(){
srand (time(NULL));
if (hopfieldRandom.empty()){
if (change == false){
cout << "Education completed! Iteration: " << iteration << endl;
showImage();
exit(0);
} else {
change = false;
}
for(int i = 0; i < pixels; i++){
hopfieldRandom.push_back(i);
}
random_shuffle(hopfieldRandom.begin(), hopfieldRandom.end());
}
int random = hopfieldRandom.back();;
hopfieldRandom.pop_back();
return random;
}
int HopfieldImageRecognition::getIteration(){
return iteration;
}
void HopfieldImageRecognition::createWeightMatrix(){
double FACTOR = 0.8; // 0.7..0.9
double E = 0.0000001;
int N = pixels + 1;
double change;
weight = mat(pixels,pixels,fill::zeros);
do{
change = 0;
for (int indexImage = 0; indexImage < size; indexImage++){
mat Xi = images.at(indexImage).t();
mat deltaW = ((FACTOR / N) * (Xi - weight * Xi) * Xi.t());
change += accu(abs(deltaW));
weight = weight + deltaW;
}
} while (change > E);
}