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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
/*
* ranksvm: main program
* usage: ./ranksvm -h to see all options
* support:
* training
* validating
* predicting
* model:
* TN RankSVM(truncated newton, conjugate gradient, various opt)
* BH bhat-dist
* HE Hell-dist(but output chance instead?!)
* out features:
* cmc
* Cumulative Matching Characteristic
* avg
* Normalized avg rank
* predict
* image pair relevance value
*/
#include <iostream>
#include <Eigen/Dense>
#include <boost/program_options.hpp>
#include <list>
#include "tools/easylogging++.h"
#include "model/ranksvmtn.h"
#include "tools/fileDataProvider.h"
#include "model/rankaccu.h"
INITIALIZE_EASYLOGGINGPP
using namespace Eigen;
using namespace std;
namespace po = boost::program_options;
po::variables_map vm;
typedef int (*mainFunc)(DataProvider &dp);
int train(DataProvider &dp) {
RSVM *rsvm;
rsvm = RSVM::loadModel(vm["model"].as<string>());
dp.open();
RidList D;
LOG(INFO)<<"Training started";
dp.getAllDataSet(D);
LOG(INFO)<<"Read "<<D.getSize()<<" entries with "<< D.getfSize()<<" features";
LOG(INFO)<<"C: "<<C<<" ,iter: "<<maxiter<<" ,prec: "<<prec;
LOG(INFO)<<"cg_maxiter: "<<cg_maxiter<<" ,cg_prec: "<<cg_prec<<" ,ls_maxiter: "<<ls_maxiter<<" ,ls_prec: "<<ls_prec;
rsvm->train(D);
LOG(INFO)<<"Training finished,saving model";
dp.close();
rsvm->saveModel(vm["output"].as<string>().c_str());
delete rsvm;
return 0;
}
int predict(DataProvider &dp) {
RSVM *rsvm;
rsvm = RSVM::loadModel(vm["model"].as<string>().c_str());
dp.open();
RidList D;
vector<double> L;
CMC cmc;
Fscore f;
LOG(INFO)<<"Prediction started";
ofstream fout;
ostream* ot;
if (vm.count("output")) {
fout.open(vm["output"].as<string>().c_str());
ot=&fout;
}
else
ot=&cout;
dp.getAllDataSet(D);
LOG(INFO)<<"Read "<<D.getSize()<<" entries with "<< D.getfSize()<<" features";
rsvm->predict(D,L);
if (vm.count("validate"))
{
rank_accu(D,L);
if (vm.count("cmc"))
rank_CMC(D,L,cmc);
}
if (vm.count("predict"))
{
if (vm.count("pair"))
{
vector<double> pair;
rank_pair(D,L,pair);
for (int i=0;i<pair.size();++i)
*ot<<pair[i]<<endl;
}
else
if (vm.count("fscore"))
{
vector<double> pair;
f.audit(D);
pair=f.getFscore();
for (int i=0;i<D.getfSize();++i)
*ot<<pair[i]<<endl;
}
else
for (int i=0; i<L.size();++i)
*ot<<L[i]<<endl;
}
LOG(INFO)<<"Finished";
if (vm.count("cmc"))
{
LOG(INFO)<< "CMC accounted over " <<cmc.getCount() << " queries";
*ot << "CMC"<<endl;
vector<double> cur = cmc.getAcc();
for (int i = 0;i<CMC_MAX;++i)
*ot << cur[i]<<endl;
*ot << "AVG"<<endl;
*ot << cmc.getAvg()/D.getqSize() <<endl;
}
if (vm.count("output"))
fout.close();
dp.close();
delete rsvm;
return 0;
}
void getmask(string fname,vector<double> &msk)
{
ifstream fin;
int fsize;
fin.open(fname.c_str());
fin>>fsize;
msk.resize(fsize);
for (int i=0;i<fsize;++i)
fin>>msk[i];
fin.close();
}
int main(int argc, char **argv) {
el::Configurations defaultConf;
defaultConf.setToDefault();
// Values are always std::string
defaultConf.setGlobally(el::ConfigurationType::Format, "%datetime %level %msg");
// Defining program options
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("train,T", "training model")
("validate,V", "validate model")
("predict,P", "use model for prediction")
("cmc,C", "enable cmc auditing")
("debug,d", "show debug messages")
("single,s", "one from a pair")
("pair,p","get pair result")
("fscore,f","get F-score")
("mask,M", po::value<string>(), "set feature mask")
("model,m", po::value<string>(), "set input model file")
("output,o", po::value<string>(), "set output model/prediction file")
("feature,i", po::value<string>(), "set input feature file")
("c,c",po::value<double>(),"trades margin size against training error")
("iter",po::value<int>(),"iter main")
("prec",po::value<double>(),"prec main")
("cg_iter",po::value<int>(),"iter conjugate gradient")
("cg_prec",po::value<double>(),"prec conjugate gradient")
("ls_iter",po::value<int>(),"iter line search")
("ls_prec",po::value<double>(),"prec line search");
// Parsing program options
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
// Print help if necessary
if (vm.count("help") || !(vm.count("train") || vm.count("validate") || vm.count("predict"))) {
cout << desc;
return 0;
}
if (!vm.count("debug"))
defaultConf.setGlobally(el::ConfigurationType::Enabled, "false");
// default logger uses default configurations
el::Loggers::reconfigureLogger("default", defaultConf);
mainFunc mainf;
RidList::single=vm.count("single")>0;
if (vm.count("train")) {
if (vm.count("c")) { C=vm["c"].as<double>(); }
if (vm.count("iter")) { maxiter=vm["iter"].as<int>(); }
if (vm.count("prec")) { prec=vm["prec"].as<double>(); }
if (vm.count("cg_iter")) { cg_maxiter=vm["cg_iter"].as<int>(); }
if (vm.count("cg_prec")) { cg_prec=vm["cg_prec"].as<double>(); }
if (vm.count("ls_iter")) { ls_maxiter=vm["ls_iter"].as<int>(); }
if (vm.count("ls_prec")) { ls_prec=vm["ls_prec"].as<double>(); }
mainf = &train;
}
else if (vm.count("validate")||vm.count("predict")) {
mainf = &predict;
}
else return 0;
DataProvider* dp;
if (vm["feature"].as<string>().find(".rid") == string::npos)
LOG(FATAL)<<"Format no longer supported";
else
{
RidFileDP* tmpdp = new RidFileDP(vm["feature"].as<string>());
if (vm.count("mask"))
{
vector<double> msk;
getmask(vm["mask"].as<string>(),msk);
tmpdp->datmask(msk);
}
dp = tmpdp;
}
mainf(*dp);
delete dp;
return 0;
}
|