-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
206 lines (194 loc) · 6.35 KB
/
main.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
// #pragma GCC optimize("section-anchors")
// #pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
// #pragma GCC optimize("vpt")
// #pragma GCC optimize("rename-registers")
// #pragma GCC optimize("move-loop-invariants")
// #pragma GCC optimize("unswitch-loops")
// #pragma GCC optimize("function-sections")
// #pragma GCC optimize("data-sections")
// #pragma GCC optimize("branch-target-load-optimize")
// #pragma GCC optimize("branch-target-load-optimize2")
// #pragma GCC optimize("btr-bb-exclusive")
#include <bits/stdc++.h>
#include "CNNTool.h"
using namespace std;
vector<pair<Image, vector<double> > > vec;
vector<int> who;
inline void get(string s) {
for (int test = 0;; ++test) {
ifstream check(string("tests/") + s + "/" + to_string(test) + ".jpg");
if (!check) {
check.close();
break;
}
check.close();
cerr << s << ": " << test << endl;
Image im(string("tests/") + s + "/" + to_string(test) + ".jpg");
vec.push_back(
pair<Image, vector<double> > {
im,
vector<double> {
double(int(s == "zero")),
double(int(s == "one"))
}
}
);
who.push_back(who.size());
}
}
void put() {
int zero, one, none;
zero = one = none = 0;
for (int i = 0; i < int(vec.size()); ++i) {
string s;
if (vec[i].second[0] > 0.5) {
vec[i].first.write(string("tests/zero/") +
to_string(zero++) + ".jpg");
} else if (vec[i].second[1] > 0.5) {
vec[i].first.write(string("tests/one/" ) +
to_string(one++ ) + ".jpg");
} else {
vec[i].first.write(string("tests/none/") +
to_string(none++) + ".jpg");
}
}
}
inline int teach(string netfile) {
srand(time(NULL));
CNN net(vector<int>{4, 8, 16, 8, 4, 2}, vector<int>{8, 2});
net.write("CNNtemp");
cerr << net.arch() << endl;
get("one");
get("zero");
cout << "number of tests: " << vec.size() << endl;
// for (int i = 0; i < int(vec.size()); ++i) {
// vec[i].first.show();
// }
random_shuffle(who.begin(), who.end());
pair<Image, vector<double> > last;
int loop = 0;
while (true) {
vector<double> res;
int cnt = 0;
while (true) {
cnt++;
double mx = 0, sum = 0;
random_shuffle(who.begin(), who.begin() + loop);
for (int i = 0; i < loop; ++i) {
res = net.run(vec[who[i]].first);
double err = abs(res[0] - vec[who[i]].second[0]) +
abs(res[1] - vec[who[i]].second[1]);
mx = max(mx, err);
sum += err;
}
if (mx > 0.1) {
for (int i = 0; i < loop; ++i) {
if (i % 10 == 0) {
net.teach(last.first, last.second, 0.01);
}
res = net.run(vec[who[i]].first);
double err = abs(res[0] - vec[who[i]].second[0]) +
abs(res[1] - vec[who[i]].second[1]);
if (err > 0.1) {
net.teach(vec[who[i]].first, vec[who[i]].second, 0.01);
}
}
if (cnt % 10 == 0) {
cerr << fixed << setprecision(7)
<< "\n#" << loop << "." << cnt
<< "\nmax error : " << mx
<< "\naverage error: " << sum / (loop) << endl;
}
} else {
break;
}
}
net.write("CNNtemp");
if (loop == int(vec.size())) {
Image im;
im.paint();
res = net.run(im);
cout << int(res[1] > res[0])
<< " precision: " << abs(res[0] - res[1]) << endl;
int got;
cin >> got;
if (got == -2) {
break;
} else if (got == -1) {
continue;
}
vec.push_back(
pair<Image, vector<double> > {
im,
vector<double> {
double(int(got == 0)),
double(int(got == 1))
}
}
);
who.push_back(who.size());
put();
}
last = vec[who[loop]];
loop = min(loop + 1, int(vec.size()));
}
net.write(netfile);
return 0;
}
inline int teach2(string netfile) {
srand(time(NULL));
CNN net({4, 8, 16, 16, 10, 8}, {4, 2});
// net.write("CNN_02");
cerr << net.arch() << endl;
vector<pair<Image, vector<double> > > all;
while (true) {
Image cur;
cur.paint();
int ans;
cin >> ans;
vector<double> need(2);
if (ans == 1) {
need[1] = 1;
} else if (ans == 0) {
need[0] = 1;
}
all.push_back(pair<Image, vector<double> >{cur, need});
double error = 2;
while (error > 0.1) {
for (int j = 0; j < 10; ++j) {
error = 0;
random_shuffle(all.begin(), all.end());
for (int i = 0; i < all.size(); ++i) {
net.teach(all[i].first, all[i].second, 0.5);
vector<double> res = net.run(all[i].first);
error = max(error, abs(all[i].second[0] - res[0]) +
abs(all[i].second[1] - res[1]));
}
cout << "error : " << error << endl;
}
net.write("CNN_02");
}
net.write("CNN_02");
}
return 0;
}
void run(string netfile) {
CNN net(netfile);
cout << "0-1 DETECTOR\narch: " << net.arch() << endl;
while (true) {
Image im;
im.paint();
vector<double> res = net.run(im);
cout << int(res[1] > res[0])
<< " precision: " << abs(res[0] - res[1]) << endl;
}
}
int main() {
// teach2("CNN_02");
run("CNN_01_boosted");
return 0;
}