-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_func.cpp
113 lines (95 loc) · 3.18 KB
/
io_func.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
#include "io_func.h"
#include <iomanip>
feature_point parse_line(const std::string& line)
{
using namespace std;
feature_point p;
string::size_type start = 0;
for (auto f = 0; f < DIMENTIONS - 1; ++f)
{
auto end = line.find_first_of(DELIMITER, start);
p(f) = atof(line.substr(start, end - start).c_str());
start = end + 1;
}
p(DIMENTIONS - 1) = 1.0;
auto end = line.find_first_of(DELIMITER, start);
if (end == std::string::npos)
{
end = line.size();
p(DIMENTIONS - 1) = atof(line.substr(start, end - start).c_str());
return p;
}
auto floor = atoi(line.substr(start, end - start).c_str());
if (floor == 1)
{
p(DIMENTIONS - 1) = 0.0;
}
else
{
start = end + 1;
end = line.size();
auto storeys = atoi(line.substr(start, end - start).c_str());
if (storeys == floor)
{
p(DIMENTIONS - 1) = 0.0;
}
}
return p;
}
void print_formatted(std::ostream& stream, const feature_point& point)
{
using namespace std;
const char separator = ';';
stream << setprecision(6) << point(0) << separator << point(1) << separator;
stream << setprecision(0) << point(2) << separator;
stream << setprecision(2) << fixed << point(3) << separator << point(4) << separator << point(5) << separator;
stream << setprecision(0) << point(6) << resetiosflags(std::ios_base::basefield) << endl;
}
void serialize_model(
const std::string& filename,
const dlib::kkmeans<kernel_type>& kmeans_model,
const dlib::vector_normalizer<feature_point>& normalizer,
const std::vector<feature_point>& dataset)
{
using namespace dlib;
auto serializer = serialize(filename);
serializer << kmeans_model;
serializer << normalizer;
std::vector<std::vector<int>> cluster_indexes(kmeans_model.number_of_centers());
for(size_t i = 0; i < dataset.size(); ++i)
{
auto cluster = kmeans_model(normalizer(dataset[i]));
cluster_indexes[cluster].push_back(i);
}
for (size_t cluster = 0; cluster < cluster_indexes.size(); ++cluster)
{
for(size_t index = 0; index < cluster_indexes[cluster].size(); ++index)
{
serializer << dataset[index];
}
}
}
void deserialize_model(const std::string& filename,
dlib::kkmeans<kernel_type>& kmeans_model,
dlib::vector_normalizer<feature_point>& normalizer,
std::vector<std::vector<feature_point>>& clusters)
{
using namespace dlib;
auto deserializer = deserialize(filename);
deserializer >> kmeans_model;
deserializer >> normalizer;
auto num_centers = kmeans_model.number_of_centers();
clusters.reserve(num_centers);
for(unsigned long cluster = 0; cluster < num_centers; ++cluster)
{
auto num_points = kmeans_model.get_kcentroid(cluster).samples_trained();
clusters[cluster] = std::vector<feature_point>();
clusters[cluster].reserve(num_points);
for(auto p = 0; p < num_points; ++p)
{
feature_point item;
deserializer >> item;
clusters[cluster].push_back(item);
}
}
}