-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_method.cc
87 lines (74 loc) · 2.4 KB
/
simple_method.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
#include "simple_method.h"
#include "hog.h"
#include "simple_method.h"
#include "svm.h"
#include "util.h"
#include "constant.h"
#include "random_function.h"
#include "search_database.h"
#include "timer.h"
#include <algorithm>
#include <fstream>
void SimpleMethod::set_svm_problem_impl(const std::vector<std::string>& files,
int& idx, const int flag_value) {
for (unsigned i = 0; i < files.size(); ++i) {
// Hog hog(files[i].c_str(), kCellX, kBlockX, kResizeX, kResizeY, kOrientation);
std::vector<double> hog;
Util::read_vector_data(files[i], hog);
svm.problem.x[idx] = new svm_node[kTotalDim + 1];
for (int i = 0; i < kTotalDim; ++i) {
svm.problem.x[idx][i].index = i + 1;
svm.problem.x[idx][i].value = flag_value * hog[i];
}
svm.problem.x[idx][kTotalDim].index = -1;
svm.problem.y[idx] = flag_value;
++idx;
}
}
void SimpleMethod::set_positive_to_svm_problem(int& idx) {
std::vector<std::string> vs;
query_ = kFeatureVectorDir + query_.substr(query_.find('/'));
query_.erase(query_.size() - 4);
if (!Util::is_exist(query_.c_str())) assert(false);
vs.push_back(query_);
set_svm_problem_impl(vs, idx, 1);
}
void SimpleMethod::set_negative_to_svm_problem(int& idx) {
std::vector<std::string> vs;
Util::get_file_list(kFeatureVectorDir + "/negativeImages", vs, true);
RandomFunction r;
std::random_shuffle(vs.begin(), vs.end(), r);
vs.erase(vs.begin() + k_, vs.end());
set_svm_problem_impl(vs, idx, -1);
}
void SimpleMethod::init_svm_problem() {
// 計算時間が少ないであろうサンプル数(ネガティブのみ計上)を設定
const int kSampleL = k_ + 1;
svm.problem.l = kSampleL;
svm.problem.y = new double[kSampleL];
svm.problem.x = new svm_node * [kSampleL];
// ポジティブサンプルの設定
int idx = 0;
set_positive_to_svm_problem(idx);
// ネガティブサンプルの設定
set_negative_to_svm_problem(idx);
}
void SimpleMethod::run() {
std::vector<double> wq;
{
Timer timer("simple method");
this->init_svm_problem();
wq = svm.get_weight_vector();
}
SearchDatabase::search(wq);
}
void SimpleMethod::run(std::vector<std::string>& ranking) {
std::vector<double> wq;
{
Timer timer("simple");
this->init_svm_problem();
wq = svm.get_weight_vector();
}
// SearchDatabase::search(wq, ranking, 10);
SearchDatabase::search(wq, ranking, 0);
}