forked from bes-dev/SOINN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
143 lines (126 loc) · 4.3 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
#include <iostream>
#include <vector>
#include <string>
#include "SOINN.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <random>
#include <ctime>
#include <fstream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
using namespace boost::numeric;
using namespace soinn;
struct Options
{
std::string filename_load;
std::string filename_save;
std::string img;
bool save_file;
bool load_file;
bool visible;
int sl_iterations;
};
int options(int argc, char ** argv, Options& opts)
{
po::options_description desc("General options");
desc.add_options()("help,H", "Produce help message.");
desc.add_options()("load,L", po::value<std::string>(&opts.filename_load)->default_value("soinn.xml"), "file name with a model for load");
desc.add_options()("image,I", po::value<std::string>(&opts.img)->default_value("img.png"), "the name of the image file to the classes");
desc.add_options()("save,S", po::value<std::string>(&opts.filename_save)->default_value("soinn.xml"), "file name with a model for save");
desc.add_options()("fload,O", po::value<bool>(&opts.load_file)->default_value(0), "load model?(flag)");
desc.add_options()("fsave,C", po::value<bool>(&opts.save_file)->default_value(1), "save model?(flag)");
desc.add_options()("iteration,T", po::value<int>(&opts.sl_iterations)->default_value(3000), "the number of iterations for training the second layer");
desc.add_options()("visible,V", po::value<bool>(&opts.visible)->default_value(1), "visible mode(1 - on; 0 - off)");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help"))
{
std::cout << desc << "\n";
return 1;
}
return 0;
}
void draw(cv::Mat &image, Graph graph)
{
const cv::Scalar colors[6] = {cv::Scalar(255, 0, 0), cv::Scalar(0, 255, 0), cv::Scalar(0, 0, 255), cv::Scalar(255, 255, 0), cv::Scalar(0, 255, 255), cv::Scalar(255, 0, 255)};
EdgeIterator edge_begin, edge_end;
boost::tie(edge_begin, edge_end) = boost::edges(graph);
for(EdgeIterator i = edge_begin; i != edge_end; i++)
{
Vertex vertex_s = boost::source(*i, graph);
Vertex vertex_t = boost::target(*i, graph);
cv::Point p1(graph[vertex_s].weight[0], graph[vertex_s].weight[1]);
cv::Point p2(graph[vertex_t].weight[0], graph[vertex_t].weight[1]);
cv::line(image, p1, p2, colors[graph[vertex_s].class_id%6]);
}
VertexIterator vertex_begin, vertex_end;
boost::tie(vertex_begin, vertex_end) = boost::vertices(graph);
for(VertexIterator i = vertex_begin; i != vertex_end; i++)
{
cv::circle(image, cv::Point(graph[*i].weight[0], graph[*i].weight[1]), 3, colors[graph[*i].class_id%6]);
}
}
int main(int argc, char **argv)
{
Options opts;
if( options(argc, argv, opts))
{
return 1;
}
std::srand(std::time(0));
soinn::SOINN model;
ublas::vector<double> a(2);
ublas::vector<double> x1(2), x2(2);
x1(0) = 320;
x1(1) = 220;
x2(0) = 320;
x2(1) = 240;
model.init(x1, x2);
cv::Mat img = cv::imread(opts.img, 0);
if(!opts.load_file)
{
std::vector<cv::Point> points;
for(int i = 0; i < img.size().width; ++i)
{
for(int j = 0; j < img.size().height; ++j)
{
if(img.at<char>(cv::Point(i, j)) != 0)
{
points.push_back(cv::Point(i, j));
}
}
}
int size = points.size();
for(int i = 0; i < 10000; ++i)
{
std::cout<<i<<"\n";
int id = std::rand() % size;
a(0) = points[id].x;
a(1) = points[id].y;
model.addSignal(a);
}
model.classify(opts.sl_iterations);
}
else
{
model.load(opts.filename_load);
}
if(opts.visible)
{
cv::imshow("classes", img);
cv::Mat img1(img.size(), CV_32FC3);
draw(img1, model.getFirstLayer());
cv::imshow("first_layer", img1);
cv::Mat img2(img.size(), CV_32FC3);
draw(img2, model.getSecondLayer());
cv::imshow("second_layer", img2);
cv::waitKey();
}
if(opts.save_file)
{
model.save(opts.filename_save);
}
return 0;
}