Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
affd197e
Commit
affd197e
authored
Apr 27, 2013
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refined the python bindings more. Still lots to do though.
parent
1a64bcc3
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
202 additions
and
16 deletions
+202
-16
CMakeLists.txt
tools/python/CMakeLists.txt
+3
-1
decision_funcions.cpp
tools/python/src/decision_funcions.cpp
+55
-0
dlib.cpp
tools/python/src/dlib.cpp
+94
-4
matrix.cpp
tools/python/src/matrix.cpp
+25
-3
serialize_pickle.h
tools/python/src/serialize_pickle.h
+3
-2
svm_c_trainer.cpp
tools/python/src/svm_c_trainer.cpp
+0
-3
vector.cpp
tools/python/src/vector.cpp
+22
-3
No files found.
tools/python/CMakeLists.txt
View file @
affd197e
...
@@ -7,4 +7,6 @@ add_python_module(dlib
...
@@ -7,4 +7,6 @@ add_python_module(dlib
src/dlib.cpp
src/dlib.cpp
src/matrix.cpp
src/matrix.cpp
src/vector.cpp
src/vector.cpp
src/svm_c_trainer.cpp
)
src/svm_c_trainer.cpp
src/decision_funcions.cpp
)
tools/python/src/decision_funcions.cpp
0 → 100644
View file @
affd197e
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include "serialize_pickle.h"
#include <dlib/svm.h>
using
namespace
dlib
;
using
namespace
std
;
using
namespace
boost
::
python
;
typedef
matrix
<
double
,
0
,
1
>
sample_type
;
typedef
std
::
vector
<
std
::
pair
<
unsigned
long
,
double
>
>
sparse_vect
;
template
<
typename
decision_function
>
double
predict
(
const
decision_function
&
df
,
const
typename
decision_function
::
kernel_type
::
sample_type
&
samp
)
{
if
(
df
.
basis_vectors
.
size
()
==
0
)
{
return
0
;
}
else
if
(
df
.
basis_vectors
(
0
).
size
()
!=
samp
.
size
())
{
std
::
ostringstream
sout
;
sout
<<
"Input vector should have "
<<
df
.
basis_vectors
(
0
).
size
()
<<
" dimensions, not "
<<
samp
.
size
()
<<
"."
;
PyErr_SetString
(
PyExc_IndexError
,
sout
.
str
().
c_str
()
);
boost
::
python
::
throw_error_already_set
();
}
return
df
(
samp
);
}
template
<
typename
kernel_type
>
void
add_df
(
const
std
::
string
name
)
{
typedef
decision_function
<
kernel_type
>
df_type
;
class_
<
df_type
>
(
name
.
c_str
())
.
def
(
"predict"
,
&
predict
<
df_type
>
)
.
def_pickle
(
serialize_pickle
<
df_type
>
());
}
void
bind_decision_functions
()
{
add_df
<
linear_kernel
<
sample_type
>
>
(
"_decision_function_linear"
);
add_df
<
sparse_linear_kernel
<
sparse_vect
>
>
(
"_decision_function_sparse_linear"
);
add_df
<
radial_basis_kernel
<
sample_type
>
>
(
"_decision_function_radial_basis"
);
add_df
<
sparse_radial_basis_kernel
<
sparse_vect
>
>
(
"_decision_function_sparse_radial_basis"
);
}
tools/python/src/dlib.cpp
View file @
affd197e
...
@@ -18,15 +18,101 @@ using namespace boost::python;
...
@@ -18,15 +18,101 @@ using namespace boost::python;
void
bind_matrix
();
void
bind_matrix
();
void
bind_vector
();
void
bind_vector
();
void
bind_svm_c_trainer
();
void
bind_svm_c_trainer
();
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
();
}
BOOST_PYTHON_MODULE
(
dlib
)
BOOST_PYTHON_MODULE
(
dlib
)
{
{
bind_matrix
();
bind_matrix
();
bind_vector
();
bind_vector
();
bind_svm_c_trainer
();
bind_svm_c_trainer
();
bind_decision_functions
();
class_
<
std
::
vector
<
double
>
>
(
"array"
)
class_
<
std
::
vector
<
double
>
>
(
"array"
,
init
<>
()
)
.
def
(
vector_indexing_suite
<
std
::
vector
<
double
>
>
())
.
def
(
vector_indexing_suite
<
std
::
vector
<
double
>
>
())
.
def
(
"__init__"
,
make_constructor
(
&
array_from_object
))
.
def
(
"__str__"
,
array__str__
)
.
def
(
"__repr__"
,
array__repr__
)
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
double
>
>
());
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
double
>
>
());
class_
<
std
::
vector
<
matrix
<
double
,
0
,
1
>
>
>
(
"vectors"
)
class_
<
std
::
vector
<
matrix
<
double
,
0
,
1
>
>
>
(
"vectors"
)
...
@@ -34,14 +120,18 @@ BOOST_PYTHON_MODULE(dlib)
...
@@ -34,14 +120,18 @@ BOOST_PYTHON_MODULE(dlib)
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
matrix
<
double
,
0
,
1
>
>
>
());
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
matrix
<
double
,
0
,
1
>
>
>
());
typedef
pair
<
unsigned
long
,
double
>
pair_type
;
typedef
pair
<
unsigned
long
,
double
>
pair_type
;
class_
<
pair_type
>
(
"pair"
,
"
help message
"
,
init
<>
()
)
class_
<
pair_type
>
(
"pair"
,
"
This object is used to represent the elements of a sparse_vector.
"
,
init
<>
()
)
.
def
(
init
<
unsigned
long
,
double
>
())
.
def
(
init
<
unsigned
long
,
double
>
())
.
def_readwrite
(
"first"
,
&
pair_type
::
first
,
"THE FIRST, LOVE IT!"
)
.
def_readwrite
(
"first"
,
&
pair_type
::
first
,
"This field represents the index/dimension number."
)
.
def_readwrite
(
"second"
,
&
pair_type
::
second
)
.
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__
)
.
def_pickle
(
serialize_pickle
<
pair_type
>
());
.
def_pickle
(
serialize_pickle
<
pair_type
>
());
class_
<
std
::
vector
<
pair_type
>
>
(
"sparse_vector"
)
class_
<
std
::
vector
<
pair_type
>
>
(
"sparse_vector"
)
.
def
(
vector_indexing_suite
<
std
::
vector
<
pair_type
>
>
())
.
def
(
vector_indexing_suite
<
std
::
vector
<
pair_type
>
>
())
.
def
(
"__str__"
,
sparse_vector__str__
)
.
def
(
"__repr__"
,
sparse_vector__repr__
)
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
pair_type
>
>
());
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
pair_type
>
>
());
class_
<
std
::
vector
<
std
::
vector
<
pair_type
>
>
>
(
"sparse_vectors"
)
class_
<
std
::
vector
<
std
::
vector
<
pair_type
>
>
>
(
"sparse_vectors"
)
...
...
tools/python/src/matrix.cpp
View file @
affd197e
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
#include <boost/python.hpp>
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <dlib/matrix.h>
#include <dlib/matrix.h>
#include <dlib/string.h>
#include "serialize_pickle.h"
#include "serialize_pickle.h"
...
@@ -16,15 +17,29 @@ void matrix_set_size(matrix<double>& m, long nr, long nc)
...
@@ -16,15 +17,29 @@ void matrix_set_size(matrix<double>& m, long nr, long nc)
m
=
0
;
m
=
0
;
}
}
string
matrix_double__repr__
(
matrix
<
double
>&
c
)
{
ostringstream
sout
;
sout
<<
"< dlib.matrix containing:
\n
"
;
sout
<<
c
;
return
trim
(
sout
.
str
())
+
" >"
;
}
string
matrix_double__str__
(
matrix
<
double
>&
c
)
string
matrix_double__str__
(
matrix
<
double
>&
c
)
{
{
ostringstream
sout
;
ostringstream
sout
;
sout
<<
c
;
sout
<<
c
;
return
sout
.
str
(
);
return
trim
(
sout
.
str
()
);
}
}
boost
::
shared_ptr
<
matrix
<
double
>
>
make_matrix_from_size
(
long
nr
,
long
nc
)
boost
::
shared_ptr
<
matrix
<
double
>
>
make_matrix_from_size
(
long
nr
,
long
nc
)
{
{
if
(
nr
<
0
||
nc
<
0
)
{
PyErr_SetString
(
PyExc_IndexError
,
"Input dimensions can't be negative."
);
boost
::
python
::
throw_error_already_set
();
}
boost
::
shared_ptr
<
matrix
<
double
>
>
temp
(
new
matrix
<
double
>
(
nr
,
nc
));
boost
::
shared_ptr
<
matrix
<
double
>
>
temp
(
new
matrix
<
double
>
(
nr
,
nc
));
*
temp
=
0
;
*
temp
=
0
;
return
temp
;
return
temp
;
...
@@ -89,6 +104,13 @@ string mat_row__str__(mat_row& c)
...
@@ -89,6 +104,13 @@ string mat_row__str__(mat_row& c)
return
sout
.
str
();
return
sout
.
str
();
}
}
string
mat_row__repr__
(
mat_row
&
c
)
{
ostringstream
sout
;
sout
<<
"< matrix row: "
<<
mat
(
c
.
data
,
1
,
c
.
size
);
return
trim
(
sout
.
str
())
+
" >"
;
}
long
mat_row__len__
(
mat_row
&
m
)
long
mat_row__len__
(
mat_row
&
m
)
{
{
return
m
.
size
;
return
m
.
size
;
...
@@ -130,7 +152,7 @@ void bind_matrix()
...
@@ -130,7 +152,7 @@ void bind_matrix()
{
{
class_
<
mat_row
>
(
"_row"
)
class_
<
mat_row
>
(
"_row"
)
.
def
(
"__len__"
,
&
mat_row__len__
)
.
def
(
"__len__"
,
&
mat_row__len__
)
.
def
(
"__repr__"
,
&
mat_row__
st
r__
)
.
def
(
"__repr__"
,
&
mat_row__
rep
r__
)
.
def
(
"__str__"
,
&
mat_row__str__
)
.
def
(
"__str__"
,
&
mat_row__str__
)
.
def
(
"__setitem__"
,
&
mat_row__setitem__
)
.
def
(
"__setitem__"
,
&
mat_row__setitem__
)
.
def
(
"__getitem__"
,
&
mat_row__getitem__
);
.
def
(
"__getitem__"
,
&
mat_row__getitem__
);
...
@@ -139,7 +161,7 @@ void bind_matrix()
...
@@ -139,7 +161,7 @@ void bind_matrix()
.
def
(
"__init__"
,
make_constructor
(
&
make_matrix_from_size
))
.
def
(
"__init__"
,
make_constructor
(
&
make_matrix_from_size
))
.
def
(
"set_size"
,
&
matrix_set_size
)
.
def
(
"set_size"
,
&
matrix_set_size
)
.
def
(
"__init__"
,
make_constructor
(
&
from_object
))
.
def
(
"__init__"
,
make_constructor
(
&
from_object
))
.
def
(
"__repr__"
,
&
matrix_double__
st
r__
)
.
def
(
"__repr__"
,
&
matrix_double__
rep
r__
)
.
def
(
"__str__"
,
&
matrix_double__str__
)
.
def
(
"__str__"
,
&
matrix_double__str__
)
.
def
(
"__len__"
,
&
matrix_double__len__
)
.
def
(
"__len__"
,
&
matrix_double__len__
)
.
def
(
"__getitem__"
,
&
matrix_double__getitem__
,
with_custodian_and_ward_postcall
<
0
,
1
>
())
.
def
(
"__getitem__"
,
&
matrix_double__getitem__
,
with_custodian_and_ward_postcall
<
0
,
1
>
())
...
...
tools/python/src/serialize_pickle.h
View file @
affd197e
...
@@ -34,8 +34,9 @@ struct serialize_pickle : boost::python::pickle_suite
...
@@ -34,8 +34,9 @@ struct serialize_pickle : boost::python::pickle_suite
throw_error_already_set
();
throw_error_already_set
();
}
}
std
::
string
&
data
=
extract
<
std
::
string
&>
(
state
[
0
]);
str
data
=
extract
<
str
>
(
state
[
0
]);
std
::
istringstream
sin
(
data
);
std
::
string
temp
(
extract
<
const
char
*>
(
data
),
len
(
data
));
std
::
istringstream
sin
(
temp
);
deserialize
(
item
,
sin
);
deserialize
(
item
,
sin
);
}
}
};
};
...
...
tools/python/src/svm_c_trainer.cpp
View file @
affd197e
...
@@ -21,9 +21,6 @@ void bind_kernel(
...
@@ -21,9 +21,6 @@ void bind_kernel(
class_
<
trainer
>
(
"svm_c_trainer"
)
class_
<
trainer
>
(
"svm_c_trainer"
)
.
def
(
"train"
,
&
trainer
::
template
train
<
std
::
vector
<
sample_type
>
,
std
::
vector
<
double
>
>
);
.
def
(
"train"
,
&
trainer
::
template
train
<
std
::
vector
<
sample_type
>
,
std
::
vector
<
double
>
>
);
typedef
decision_function
<
kernel_type
>
df
;
class_
<
df
>
(
"df"
)
.
def
(
"predict"
,
&
df
::
operator
());
}
}
...
...
tools/python/src/vector.cpp
View file @
affd197e
...
@@ -17,10 +17,29 @@ void cv_set_size(cv& m, long s)
...
@@ -17,10 +17,29 @@ void cv_set_size(cv& m, long s)
m
=
0
;
m
=
0
;
}
}
string
cv__str__
(
c
v
&
c
)
string
cv__str__
(
c
onst
cv
&
v
)
{
{
ostringstream
sout
;
ostringstream
sout
;
sout
<<
c
;
for
(
long
i
=
0
;
i
<
v
.
size
();
++
i
)
{
sout
<<
v
(
i
);
if
(
i
+
1
<
v
.
size
())
sout
<<
"
\n
"
;
}
return
sout
.
str
();
}
string
cv__repr__
(
const
cv
&
v
)
{
std
::
ostringstream
sout
;
sout
<<
"dlib.vector(["
;
for
(
unsigned
long
i
=
0
;
i
<
v
.
size
();
++
i
)
{
sout
<<
v
(
i
);
if
(
i
+
1
<
v
.
size
())
sout
<<
", "
;
}
sout
<<
"])"
;
return
sout
.
str
();
return
sout
.
str
();
}
}
...
@@ -89,7 +108,7 @@ void bind_vector()
...
@@ -89,7 +108,7 @@ void bind_vector()
class_
<
cv
>
(
"vector"
,
init
<>
())
class_
<
cv
>
(
"vector"
,
init
<>
())
.
def
(
"set_size"
,
&
cv_set_size
)
.
def
(
"set_size"
,
&
cv_set_size
)
.
def
(
"__init__"
,
make_constructor
(
&
cv_from_object
))
.
def
(
"__init__"
,
make_constructor
(
&
cv_from_object
))
.
def
(
"__repr__"
,
&
cv__
st
r__
)
.
def
(
"__repr__"
,
&
cv__
rep
r__
)
.
def
(
"__str__"
,
&
cv__str__
)
.
def
(
"__str__"
,
&
cv__str__
)
.
def
(
"__len__"
,
&
cv__len__
)
.
def
(
"__len__"
,
&
cv__len__
)
.
def
(
"__getitem__"
,
&
cv__getitem__
)
.
def
(
"__getitem__"
,
&
cv__getitem__
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment