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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include "tester.h"
#include <dlib/svm.h>
#include <vector>
#include <sstream>
namespace
{
using namespace test;
using namespace dlib;
using namespace std;
dlib::logger dlog("test.one_vs_all_trainer");
class test_one_vs_all_trainer : public tester
{
/*!
WHAT THIS OBJECT REPRESENTS
This object represents a unit test. When it is constructed
it adds itself into the testing framework.
!*/
public:
test_one_vs_all_trainer (
) :
tester (
"test_one_vs_all_trainer", // the command line argument name for this test
"Run tests on the one_vs_all_trainer stuff.", // the command line argument description
0 // the number of command line arguments for this test
)
{
}
template <typename sample_type, typename label_type>
void generate_data (
std::vector<sample_type>& samples,
std::vector<label_type>& labels
)
{
const long num = 50;
sample_type m;
dlib::rand rnd;
// make some samples near the origin
double radius = 0.5;
for (long i = 0; i < num+10; ++i)
{
double sign = 1;
if (rnd.get_random_double() < 0.5)
sign = -1;
m(0) = 2*radius*rnd.get_random_double()-radius;
m(1) = sign*sqrt(radius*radius - m(0)*m(0));
// add this sample to our set of samples we will run k-means
samples.push_back(m);
labels.push_back(1);
}
// make some samples in a circle around the origin but far away
radius = 10.0;
for (long i = 0; i < num+20; ++i)
{
double sign = 1;
if (rnd.get_random_double() < 0.5)
sign = -1;
m(0) = 2*radius*rnd.get_random_double()-radius;
m(1) = sign*sqrt(radius*radius - m(0)*m(0));
// add this sample to our set of samples we will run k-means
samples.push_back(m);
labels.push_back(2);
}
// make some samples in a circle around the point (25,25)
radius = 4.0;
for (long i = 0; i < num+30; ++i)
{
double sign = 1;
if (rnd.get_random_double() < 0.5)
sign = -1;
m(0) = 2*radius*rnd.get_random_double()-radius;
m(1) = sign*sqrt(radius*radius - m(0)*m(0));
// translate this point away from the origin
m(0) += 25;
m(1) += 25;
// add this sample to our set of samples we will run k-means
samples.push_back(m);
labels.push_back(3);
}
}
template <typename label_type, typename scalar_type>
void run_test (
)
{
print_spinner();
typedef matrix<scalar_type,2,1> sample_type;
std::vector<sample_type> samples, norm_samples;
std::vector<label_type> labels;
// First, get our labeled set of training data
generate_data(samples, labels);
typedef one_vs_all_trainer<any_trainer<sample_type,scalar_type>,label_type > ova_trainer;
ova_trainer trainer;
typedef polynomial_kernel<sample_type> poly_kernel;
typedef radial_basis_kernel<sample_type> rbf_kernel;
// make the binary trainers and set some parameters
krr_trainer<rbf_kernel> rbf_trainer;
svm_nu_trainer<poly_kernel> poly_trainer;
poly_trainer.set_kernel(poly_kernel(0.1, 1, 2));
rbf_trainer.set_kernel(rbf_kernel(0.1));
trainer.set_trainer(rbf_trainer);
trainer.set_trainer(poly_trainer, 1);
randomize_samples(samples, labels);
matrix<double> res = cross_validate_multiclass_trainer(trainer, samples, labels, 2);
print_spinner();
matrix<scalar_type> ans(3,3);
ans = 60, 0, 0,
0, 70, 0,
0, 0, 80;
DLIB_TEST_MSG(ans == res, "res: \n" << res);
// test using a normalized_function with a one_vs_all_decision_function
{
poly_trainer.set_kernel(poly_kernel(1.1, 1, 2));
trainer.set_trainer(poly_trainer, 1);
vector_normalizer<sample_type> normalizer;
normalizer.train(samples);
for (unsigned long i = 0; i < samples.size(); ++i)
norm_samples.push_back(normalizer(samples[i]));
normalized_function<one_vs_all_decision_function<ova_trainer> > ndf;
ndf.function = trainer.train(norm_samples, labels);
ndf.normalizer = normalizer;
DLIB_TEST(ndf(samples[0]) == labels[0]);
DLIB_TEST(ndf(samples[40]) == labels[40]);
DLIB_TEST(ndf(samples[90]) == labels[90]);
DLIB_TEST(ndf(samples[120]) == labels[120]);
poly_trainer.set_kernel(poly_kernel(0.1, 1, 2));
trainer.set_trainer(poly_trainer, 1);
print_spinner();
}
one_vs_all_decision_function<ova_trainer> df = trainer.train(samples, labels);
DLIB_TEST(df.number_of_classes() == 3);
DLIB_TEST(df(samples[0]) == labels[0])
DLIB_TEST(df(samples[90]) == labels[90])
one_vs_all_decision_function<ova_trainer,
decision_function<poly_kernel>, // This is the output of the poly_trainer
decision_function<rbf_kernel> // This is the output of the rbf_trainer
> df2, df3;
df2 = df;
ofstream fout("df.dat", ios::binary);
serialize(df2, fout);
fout.close();
// load the function back in from disk and store it in df3.
ifstream fin("df.dat", ios::binary);
deserialize(df3, fin);
DLIB_TEST(df3(samples[0]) == labels[0])
DLIB_TEST(df3(samples[90]) == labels[90])
res = test_multiclass_decision_function(df3, samples, labels);
DLIB_TEST(res == ans);
}
template <typename label_type, typename scalar_type>
void run_probabilistic_test (
)
{
print_spinner();
typedef matrix<scalar_type,2,1> sample_type;
std::vector<sample_type> samples;
std::vector<label_type> labels;
// First, get our labeled set of training data
generate_data(samples, labels);
typedef one_vs_all_trainer<any_trainer<sample_type,scalar_type>,label_type > ova_trainer;
ova_trainer trainer;
typedef polynomial_kernel<sample_type> poly_kernel;
typedef radial_basis_kernel<sample_type> rbf_kernel;
// make the binary trainers and set some parameters
krr_trainer<rbf_kernel> rbf_trainer;
svm_nu_trainer<poly_kernel> poly_trainer;
poly_trainer.set_kernel(poly_kernel(0.1, 1, 2));
rbf_trainer.set_kernel(rbf_kernel(0.1));
trainer.set_trainer(probabilistic(rbf_trainer, 3));
trainer.set_trainer(probabilistic(poly_trainer, 3), 1);
randomize_samples(samples, labels);
matrix<double> res = cross_validate_multiclass_trainer(trainer, samples, labels, 2);
print_spinner();
matrix<scalar_type> ans(3,3);
ans = 60, 0, 0,
0, 70, 0,
0, 0, 80;
DLIB_TEST_MSG(ans == res, "res: \n" << res);
one_vs_all_decision_function<ova_trainer> df = trainer.train(samples, labels);
DLIB_TEST(df.number_of_classes() == 3);
DLIB_TEST(df(samples[0]) == labels[0])
DLIB_TEST(df(samples[90]) == labels[90])
one_vs_all_decision_function<ova_trainer,
probabilistic_function<decision_function<poly_kernel> >, // This is the output of the poly_trainer
probabilistic_function<decision_function<rbf_kernel> > // This is the output of the rbf_trainer
> df2, df3;
df2 = df;
ofstream fout("df.dat", ios::binary);
serialize(df2, fout);
fout.close();
// load the function back in from disk and store it in df3.
ifstream fin("df.dat", ios::binary);
deserialize(df3, fin);
DLIB_TEST(df3(samples[0]) == labels[0])
DLIB_TEST(df3(samples[90]) == labels[90])
res = test_multiclass_decision_function(df3, samples, labels);
DLIB_TEST(res == ans);
}
void perform_test (
)
{
dlog << LINFO << "run_test<double,double>()";
run_test<double,double>();
dlog << LINFO << "run_test<int,double>()";
run_test<int,double>();
dlog << LINFO << "run_test<double,float>()";
run_test<double,float>();
dlog << LINFO << "run_test<int,float>()";
run_test<int,float>();
dlog << LINFO << "run_probabilistic_test<double,double>()";
run_probabilistic_test<double,double>();
dlog << LINFO << "run_probabilistic_test<int,double>()";
run_probabilistic_test<int,double>();
dlog << LINFO << "run_probabilistic_test<double,float>()";
run_probabilistic_test<double,float>();
dlog << LINFO << "run_probabilistic_test<int,float>()";
run_probabilistic_test<int,float>();
}
};
test_one_vs_all_trainer a;
}
|