File: data_io.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (227 lines) | stat: -rw-r--r-- 7,336 bytes parent folder | download | duplicates (2)
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
// Copyright (C) 2011  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 <dlib/data_io.h>
#include <dlib/sparse_vector.h>
#include "create_iris_datafile.h"
#include <vector>
#include <sstream>

namespace  
{
    using namespace test;
    using namespace dlib;
    using namespace std;
    dlib::logger dlog("test.data_io");


    class test_data_io : 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_data_io (
        ) :
            tester (
                "test_data_io",       // the command line argument name for this test
                "Run tests on the data_io stuff.", // the command line argument description
                0                     // the number of command line arguments for this test
            )
        {
        }


        template <typename sample_type>
        void run_test()
        {
            print_spinner();

            typedef typename sample_type::value_type::second_type scalar_type;

            std::vector<sample_type> samples;
            std::vector<scalar_type> labels;

            load_libsvm_formatted_data("iris.scale",samples, labels);
            save_libsvm_formatted_data("iris.scale2", samples, labels);

            DLIB_TEST(samples.size() == 150);
            DLIB_TEST(labels.size() == 150);
            DLIB_TEST(max_index_plus_one(samples) == 5);
            fix_nonzero_indexing(samples);
            DLIB_TEST(max_index_plus_one(samples) == 4);

            load_libsvm_formatted_data("iris.scale2",samples, labels);

            DLIB_TEST(samples.size() == 150);
            DLIB_TEST(labels.size() == 150);

            DLIB_TEST(max_index_plus_one(samples) == 5);
            fix_nonzero_indexing(samples);
            DLIB_TEST(max_index_plus_one(samples) == 4);

            one_vs_one_trainer<any_trainer<sample_type,scalar_type>,scalar_type> trainer;

            typedef sparse_linear_kernel<sample_type> kernel_type;
            trainer.set_trainer(krr_trainer<kernel_type>());

            randomize_samples(samples, labels);
            matrix<double> cv = cross_validate_multiclass_trainer(trainer, samples, labels, 4);

            dlog << LINFO << "confusion matrix: \n" << cv;
            const scalar_type cv_accuracy = sum(diag(cv))/sum(cv);
            dlog << LINFO << "cv accuracy: " << cv_accuracy;
            DLIB_TEST(cv_accuracy > 0.97);




            {
                print_spinner();
                typedef matrix<scalar_type,0,1> dsample_type;
                std::vector<dsample_type> dsamples = sparse_to_dense(samples);
                DLIB_TEST(dsamples.size() == 150);
                DLIB_TEST(dsamples[0].size() == 4);
                DLIB_TEST(max_index_plus_one(dsamples) == 4);

                one_vs_one_trainer<any_trainer<dsample_type,scalar_type>,scalar_type> trainer;

                typedef linear_kernel<dsample_type> kernel_type;
                trainer.set_trainer(rr_trainer<kernel_type>());

                cv = cross_validate_multiclass_trainer(trainer, dsamples, labels, 4);

                dlog << LINFO << "dense confusion matrix: \n" << cv;
                const scalar_type cv_accuracy = sum(diag(cv))/sum(cv);
                dlog << LINFO << "dense cv accuracy: " << cv_accuracy;
                DLIB_TEST(cv_accuracy > 0.97);
            }

        }


        void test_sparse_to_dense()
        {
            {
                std::map<unsigned long, double> temp;

                matrix<double,0,1> m, m2;

                m = sparse_to_dense(m);
                DLIB_TEST(m.size() == 0);
                m.set_size(2,1);
                m = 1, 2;
                m2 = sparse_to_dense(m);
                DLIB_TEST(m == m2);
                m2 = sparse_to_dense(m,1);
                DLIB_TEST(m2.size() == 1);
                DLIB_TEST(m2(0,0) == 1);
                m2 = sparse_to_dense(m,0);
                DLIB_TEST(m2.size() == 0);

                temp[3] = 2;
                temp[5] = 4;
                m2 = sparse_to_dense(temp);
                m.set_size(6);
                m = 0,0,0,2,0,4;
                DLIB_TEST(m2 == m);

                m2 = sparse_to_dense(temp, 5);
                m.set_size(5);
                m = 0,0,0,2,0;
                DLIB_TEST(m2 == m);

                m2 = sparse_to_dense(temp, 7);
                m.set_size(7);
                m = 0,0,0,2,0,4,0;
                DLIB_TEST(m2 == m);

                std::vector<std::vector<std::pair<unsigned long,double> > > vects;

                std::vector<std::pair<unsigned long,double> > v;
                v.push_back(make_pair(5,2));
                v.push_back(make_pair(3,1));
                v.push_back(make_pair(5,2));
                v.push_back(make_pair(3,1));
                v = make_sparse_vector(v);
                vects.push_back(v);
                vects.push_back(v);
                vects.push_back(v);
                vects.push_back(v);
                DLIB_TEST(max_index_plus_one(v) == 6);
                m2 = sparse_to_dense(v);
                m.set_size(6);
                m = 0,0,0,2,0,4;
                DLIB_TEST_MSG(m2 == m, m2 << "\n\n" << m );

                m2 = sparse_to_dense(v,7);
                m.set_size(7);
                m = 0,0,0,2,0,4,0;
                DLIB_TEST(m2 == m);

                m2 = sparse_to_dense(v,5);
                m.set_size(5);
                m = 0,0,0,2,0;
                DLIB_TEST(m2 == m);

                v.clear();
                m2 = sparse_to_dense(v);
                DLIB_TEST(m2.size() == 0);


                std::vector<matrix<double,0,1> > mvects = sparse_to_dense(vects);
                DLIB_TEST(mvects.size() == 4);
                m.set_size(6);
                m = 0,0,0,2,0,4;
                DLIB_TEST(mvects[0] == m);
                DLIB_TEST(mvects[1] == m);
                DLIB_TEST(mvects[2] == m);
                DLIB_TEST(mvects[3] == m);


                mvects = sparse_to_dense(vects, 7);
                DLIB_TEST(mvects.size() == 4);
                m.set_size(7);
                m = 0,0,0,2,0,4,0;
                DLIB_TEST(mvects[0] == m);
                DLIB_TEST(mvects[1] == m);
                DLIB_TEST(mvects[2] == m);
                DLIB_TEST(mvects[3] == m);

                mvects = sparse_to_dense(vects, 5);
                DLIB_TEST(mvects.size() == 4);
                m.set_size(5);
                m = 0,0,0,2,0;
                DLIB_TEST(mvects[0] == m);
                DLIB_TEST(mvects[1] == m);
                DLIB_TEST(mvects[2] == m);
                DLIB_TEST(mvects[3] == m);

            }
        }


        void perform_test (
        )
        {
            print_spinner();
            create_iris_datafile();

            test_sparse_to_dense();

            run_test<std::map<unsigned int, double> >();
            run_test<std::map<unsigned int, float> >();
            run_test<std::vector<std::pair<unsigned int, float> > >();
            run_test<std::vector<std::pair<unsigned long, double> > >();
        }
    };

    test_data_io a;

}