"git@developer.sourcefind.cn:OpenDAS/dlib.git" did not exist on "b41455fd26ea1753a84d3ed3f862637970eb0993"
dlib.cpp 4.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <boost/python.hpp>
#include <dlib/matrix.h>
#include <sstream>
#include <string>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/indexing_suite.hpp>
#include <boost/shared_ptr.hpp>

#include <dlib/string.h>
#include "serialize_pickle.h"

using namespace std;
using namespace dlib;
using namespace boost::python;


void bind_matrix();
void bind_vector();
void bind_svm_c_trainer();
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
void bind_decision_functions();

boost::shared_ptr<std::vector<double> > array_from_object(object obj)
{
    extract<long> thesize(obj);
    if (thesize.check())
    {
        long nr = thesize;
        boost::shared_ptr<std::vector<double> > temp(new std::vector<double>(nr));
        return temp;
    }
    else
    {
        const long nr = len(obj);
        boost::shared_ptr<std::vector<double> > temp(new std::vector<double>(nr));
        for ( long r = 0; r < nr; ++r)
        {
            (*temp)[r] = extract<double>(obj[r]);
        }
        return temp;
    }
}

string array__str__ (const std::vector<double>& v)
{
    std::ostringstream sout;
    for (unsigned long i = 0; i < v.size(); ++i)
    {
        sout << v[i];
        if (i+1 < v.size())
            sout << "\n";
    }
    return sout.str();
}

string array__repr__ (const std::vector<double>& v)
{
    std::ostringstream sout;
    sout << "dlib.array([";
    for (unsigned long i = 0; i < v.size(); ++i)
    {
        sout << v[i];
        if (i+1 < v.size())
            sout << ", ";
    }
    sout << "])";
    return sout.str();
}

string pair__str__ (const std::pair<unsigned long,double>& p)
{
    std::ostringstream sout;
    sout << p.first << ": " << p.second;
    return sout.str();
}

string pair__repr__ (const std::pair<unsigned long,double>& p)
{
    std::ostringstream sout;
    sout << "dlib.pair(" << p.first << ", " << p.second << ")";
    return sout.str();
}

string sparse_vector__str__ (const std::vector<std::pair<unsigned long,double> >& v)
{
    std::ostringstream sout;
    for (unsigned long i = 0; i < v.size(); ++i)
    {
        sout << v[i].first << ": " << v[i].second;
        if (i+1 < v.size())
            sout << "\n";
    }
    return sout.str();
}

string sparse_vector__repr__ (const std::vector<std::pair<unsigned long,double> >& v)
{
    std::ostringstream sout;
    sout << "< dlib.sparse_vector containing: \n" << sparse_vector__str__(v) << " >";
    return sout.str();
}

Davis King's avatar
Davis King committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
tuple get_training_data()
{
    typedef matrix<double,0,1> sample_type;
    std::vector<sample_type> samples;
    std::vector<double> labels;

    sample_type samp(3);
    samp = 1,2,3;
    samples.push_back(samp);
    labels.push_back(+1);
    samp = -1,-2,-3;
    samples.push_back(samp);
    labels.push_back(-1);

    return make_tuple(samples, labels);
}
119
120
121
122
123
124

BOOST_PYTHON_MODULE(dlib)
{
    bind_matrix();
    bind_vector();
    bind_svm_c_trainer();
125
    bind_decision_functions();
126

127
    class_<std::vector<double> >("array", init<>())
128
        .def(vector_indexing_suite<std::vector<double> >())
129
130
131
        .def("__init__", make_constructor(&array_from_object))
        .def("__str__", array__str__)
        .def("__repr__", array__repr__)
132
133
134
135
136
137
138
        .def_pickle(serialize_pickle<std::vector<double> >());

    class_<std::vector<matrix<double,0,1> > >("vectors")
        .def(vector_indexing_suite<std::vector<matrix<double,0,1> > >())
        .def_pickle(serialize_pickle<std::vector<matrix<double,0,1> > >());

    typedef pair<unsigned long,double> pair_type;
139
    class_<pair_type>("pair", "This object is used to represent the elements of a sparse_vector.", init<>() )
140
        .def(init<unsigned long,double>())
141
142
143
144
        .def_readwrite("first",&pair_type::first, "This field represents the index/dimension number.")
        .def_readwrite("second",&pair_type::second, "This field contains the value in a vector at dimension specified by the first field.")
        .def("__str__", pair__str__)
        .def("__repr__", pair__repr__)
145
146
147
148
        .def_pickle(serialize_pickle<pair_type>());

    class_<std::vector<pair_type> >("sparse_vector")
        .def(vector_indexing_suite<std::vector<pair_type> >())
149
150
        .def("__str__", sparse_vector__str__)
        .def("__repr__", sparse_vector__repr__)
151
152
153
154
155
156
        .def_pickle(serialize_pickle<std::vector<pair_type> >());

    class_<std::vector<std::vector<pair_type> > >("sparse_vectors")
        .def(vector_indexing_suite<std::vector<std::vector<pair_type> > >())
        .def_pickle(serialize_pickle<std::vector<std::vector<pair_type> > >());

Davis King's avatar
Davis King committed
157
    def("get_training_data",get_training_data);
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    /*
    def("tomat",tomat);
    def("add_to_map", add_to_map);
    def("getpair", getpair);
    def("getmatrix", getmatrix);
    def("yay", yay);
    def("sum", sum_mat);
    def("getmap", getmap);
    def("go", go);
    def("append_to_vector", append_to_vector);
    */




}