forked from chong-z/tree-ensemble-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.cc
184 lines (159 loc) · 5.29 KB
/
utility.cc
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
#include "utility.h"
#include <math.h>
#include <fstream>
namespace cz {
FeatureDir& operator|=(FeatureDir& lhs, FeatureDir rhs) {
lhs = static_cast<FeatureDir>(
static_cast<std::underlying_type<FeatureDir>::type>(lhs) |
static_cast<std::underlying_type<FeatureDir>::type>(rhs));
return lhs;
}
bool operator&(FeatureDir lhs, FeatureDir rhs) {
return static_cast<bool>(
static_cast<std::underlying_type<FeatureDir>::type>(lhs) &
static_cast<std::underlying_type<FeatureDir>::type>(rhs));
}
bool IsEq(double a, double b) {
return fabs(a - b) < eps;
}
bool IsEq(const std::optional<double>& a, const std::optional<double>& b) {
if (a.has_value() != b.has_value())
return false;
if (!a.has_value())
return true;
return IsEq(a.value(), b.value());
}
bool IsEq(const std::string& a, const std::string& b) {
return a == b;
}
std::vector<std::pair<int, Point>> LoadSVMFile(const char* path,
int feature_dim,
int start_idx) {
std::vector<std::pair<int, Point>> parsed_data;
std::ifstream fin(path);
std::string line;
while (std::getline(fin, line)) {
if (line.size() <= 2)
break;
auto* s = line.c_str();
int label;
int pos;
sscanf(s, "%d%n", &label, &pos);
s += pos;
Point p(feature_dim + start_idx);
int axis;
double value;
while (sscanf(s, "%d:%lf%n", &axis, &value, &pos) == 2) {
assert(axis < p.Size());
// assert(value >= 0 && value <= 1.0);
value = std::max(0.0, std::min(1.0, value));
p[axis] = value;
s += pos;
}
parsed_data.emplace_back(std::make_pair(label, std::move(p)));
}
return std::move(parsed_data);
}
//basically the inverse of LoadSVMFile
void WriteSVMFile(const std::string path, std::vector<std::pair<int, cz::Point>> const * data){
std::ofstream fout(path, std::ios::binary);
std::string features;
std::for_each(data->begin(), data->end(), [&fout, &features](std::pair<int, cz::Point> label_sample){
//write line
u_int i = 0; //we probably will not run out of space here
for (const auto& iter : label_sample.second) {
features.append(" " + std::to_string(i) + ":" + std::to_string(iter));
i++;
}
//fout << Y 0:X_0 1:X_1 2:X_2 ... \n
fout << label_sample.first << features << endl;
features.clear();
}
);
};
double Clip(double v, double min_v, double max_v) {
assert(min_v <= max_v);
return fmax(fmin(v, max_v), min_v);
}
int MaxIndex(const std::vector<double>& v, int except) {
assert(v.size() > 0);
int max_index = (except == 0) ? 1 : 0;
double max_score = v[max_index];
for (int i = 0; i < v.size(); ++i) {
if (i == except)
continue;
if (v[i] > max_score) {
max_score = v[i];
max_index = i;
}
}
return max_index;
}
int MaxIndexBetween(const std::vector<double>& v, int class1, int class2) {
if (v[class2] > v[class1])
return class2;
return class1;
}
Direction ToTrimmedDirection(const Patch& patch, const Point& ref_point) {
Direction dir;
for (const auto& iter : patch) {
if (iter.second == ref_point[iter.first])
continue;
if (iter.second > ref_point[iter.first]) {
dir.push_back(iter.first);
} else {
dir.push_back(-iter.first);
}
}
return std::move(dir);
}
double NormFast(double old_norm,
const Point& old_point,
const Point& ref_point,
const Patch& new_patch,
int norm_type) {
if (norm_type == 1) {
for (const auto& feature_value : new_patch) {
const int feature_id = feature_value.first;
const double value = feature_value.second;
old_norm += fabs(value - ref_point[feature_id]) -
fabs(old_point[feature_id] - ref_point[feature_id]);
}
return old_norm;
} else if (norm_type == 2) {
old_norm = std::pow(old_norm, 2);
for (const auto& feature_value : new_patch) {
const int feature_id = feature_value.first;
const double value = feature_value.second;
old_norm += std::pow(value - ref_point[feature_id], 2) -
std::pow(old_point[feature_id] - ref_point[feature_id], 2);
}
return sqrt(old_norm);
} else if (norm_type == -1) {
double new_diff_max = -1;
double old_diff_max = -1;
for (const auto& feature_value : new_patch) {
const int feature_id = feature_value.first;
const double value = feature_value.second;
new_diff_max =
std::max(new_diff_max, fabs(value - ref_point[feature_id]));
old_diff_max = std::max(
old_diff_max, fabs(old_point[feature_id] - ref_point[feature_id]));
}
if (new_diff_max >= old_norm)
return new_diff_max;
if (old_diff_max < old_norm)
return old_norm;
int len = old_point.Size();
for (int i = 0; i < len; ++i) {
const double t = fabs(old_point[i] - ref_point[i]);
if (t > new_diff_max && new_patch.find(i) == new_patch.end())
new_diff_max = t;
}
return new_diff_max;
}
cout << "Unsupported norm_type:" << norm_type << endl;
assert(false);
return -1;
}
} // namespace cz