-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhammingnetwork.cpp
159 lines (124 loc) · 3.88 KB
/
hammingnetwork.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "hammingnetwork.h"
#include <QColor>
#include <QDebug>
#include <algorithm>
HammingNetwork::HammingNetwork(int width, int height) :img_height_(height), img_width_(width), size_layer_0_(img_width_ * img_height_), size_layer_1_(0) {
}
bool HammingNetwork::LoadInputFromFile(QString filePath) {
QImage img(filePath);
return LoadInputToLayer(img);
}
bool HammingNetwork::LoadInputToLayer(QImage img) {
if(img.width() != img_width_ || img.height() != img_height_)
return false;
int k = 0;
for(int i = 0; i < img_height_; i++)
for(int j = 0; j < img_width_; j++) {
if(k > size_layer_0_ - 1)
return false;
QColor col = img.pixel(j, i);
// determine whether it is black or white
layer_0_[k++] = col.red() ? HI : LO;
}
return true;
}
bool HammingNetwork::Train() {
size_layer_0_ = img_width_ * img_height_;
// allocate memory
layer_0_.assign(size_layer_0_, 0);
layer_1_.assign(size_layer_1_, 0);
layer_1_prev_.assign(size_layer_1_, 0);
weight_hamming_.assign(size_layer_1_, std::vector<double>(size_layer_0_, 0));
// define braking coefficient
braking_ = 1.0 / (size_layer_1_ * 2.0);
for(int fi = 0; fi < patterns_images_.size(); fi++) {
if(!LoadInputToLayer(patterns_images_.at(fi)))
return false;
// hamming layer traning rule
for(int i = 0; i < size_layer_0_; i++)
weight_hamming_[fi][i] = layer_0_[i] * 0.5;
}
return true;
}
double HammingNetwork::StateHamming(int num) {
double s = 0.0;
for(int i = 0; i < size_layer_0_; i++)
s += layer_0_[i] * weight_hamming_[num][i];
return s;
}
void HammingNetwork::StepHamming() {
for(int i = 0; i < size_layer_1_; i++)
layer_1_[i] = StateHamming(i);
}
bool HammingNetwork::StableHopfield() {
return layer_1_ == layer_1_prev_;
}
double HammingNetwork::HopfieldActivateFunction(double x) {
return x > T ? x : T;
}
double HammingNetwork::StateHopfield(int num) {
double s = 0.0;
for(int i = 0; i < size_layer_1_; i++)
if(i != num)
s += layer_1_prev_[i];
return layer_1_prev_[num] - braking_ * s;
}
void HammingNetwork::StepHopfield() {
layer_1_prev_ = layer_1_;
for(int i = 0; i < size_layer_1_; i++)
layer_1_[i] = HopfieldActivateFunction(StateHopfield(i));
}
int HammingNetwork::TestPattern(QImage img) {
if(!LoadInputToLayer(img))
return -1;
layer_1_.assign(size_layer_1_, 0);
StepHamming();
for(int i = 0; i < MAX_ITER; i++) {
StepHopfield();
if(StableHopfield())
break;
}
return std::distance(layer_1_.begin(), std::max_element(layer_1_.begin(), layer_1_.end()));
}
int HammingNetwork::TestPattern(QString filePath) {
QImage img(filePath);
return TestPattern(img);
}
bool HammingNetwork::AddPattern(QString filePath) {
QImage img(filePath);
return AddPattern(img);
}
bool HammingNetwork::AddPattern(QImage img) {
if(img.width() != img_width_ || img.height() != img_height_)
return false;
patterns_images_.push_back(img);
size_layer_1_++;
return true;
}
int HammingNetwork::GetZeroLayerSize() {
return size_layer_0_;
}
int HammingNetwork::GetFirstLayerSize() {
return size_layer_1_;
}
int HammingNetwork::GetCountOfPatterns() {
return size_layer_1_;
}
QImage HammingNetwork::GetPattern(int index) {
if(index > patterns_images_.size() || index < 0)
return QImage();
return patterns_images_.at(index);
}
int HammingNetwork::GetImageWidth() {
return img_width_;
}
int HammingNetwork::GetImageHeight() {
return img_height_;
}
NetworkInformation HammingNetwork::GetInformation() {
return NetworkInformation(weight_hamming_, size_layer_0_, size_layer_1_);
}
void HammingNetwork::Resize(int width, int height) {
img_width_ = width;
img_height_ = height;
}