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
a0fe7efc
Commit
a0fe7efc
authored
Jun 27, 2013
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added initial version of structural svm python bindings
parent
421e5bcd
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
150 additions
and
0 deletions
+150
-0
CMakeLists.txt
tools/python/CMakeLists.txt
+1
-0
boost_python_utils.h
tools/python/src/boost_python_utils.h
+24
-0
dlib.cpp
tools/python/src/dlib.cpp
+2
-0
svm_struct.cpp
tools/python/src/svm_struct.cpp
+123
-0
No files found.
tools/python/CMakeLists.txt
View file @
a0fe7efc
...
...
@@ -15,6 +15,7 @@ add_python_module(dlib
src/basic.cpp
src/cca.cpp
src/sequence_segmenter.cpp
src/svm_struct.cpp
)
# When you run "make install" we will copy the compiled dlib.so (or dlib.pyd)
...
...
tools/python/src/boost_python_utils.h
0 → 100644
View file @
a0fe7efc
// Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_BOOST_PYTHON_UtILS_H__
#define DLIB_BOOST_PYTHON_UtILS_H__
#include <boost/python.hpp>
inline
bool
hasattr
(
boost
::
python
::
object
obj
,
const
std
::
string
&
attr_name
)
/*!
ensures
- if (obj has an attribute named attr_name) then
- returns true
- else
- returns false
!*/
{
return
PyObject_HasAttrString
(
obj
.
ptr
(),
attr_name
.
c_str
());
}
#endif // DLIB_BOOST_PYTHON_UtILS_H__
tools/python/src/dlib.cpp
View file @
a0fe7efc
...
...
@@ -12,6 +12,7 @@ void bind_other();
void
bind_svm_rank_trainer
();
void
bind_cca
();
void
bind_sequence_segmenter
();
void
bind_svm_struct
();
BOOST_PYTHON_MODULE
(
dlib
)
...
...
@@ -29,5 +30,6 @@ BOOST_PYTHON_MODULE(dlib)
bind_svm_rank_trainer
();
bind_cca
();
bind_sequence_segmenter
();
bind_svm_struct
();
}
tools/python/src/svm_struct.cpp
0 → 100644
View file @
a0fe7efc
// Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include <boost/python.hpp>
#include <dlib/matrix.h>
#include <boost/python/args.hpp>
#include "pyassert.h"
#include "boost_python_utils.h"
#include <dlib/svm.h>
using
namespace
dlib
;
using
namespace
std
;
using
namespace
boost
::
python
;
class
svm_struct_dense
:
public
structural_svm_problem
<
matrix
<
double
,
0
,
1
>
>
{
public
:
svm_struct_dense
(
object
&
problem_
,
long
num_dimensions_
,
long
num_samples_
)
:
num_dimensions
(
num_dimensions_
),
num_samples
(
num_samples_
),
problem
(
problem_
)
{}
virtual
long
get_num_dimensions
(
)
const
{
return
num_dimensions
;
}
virtual
long
get_num_samples
(
)
const
{
return
num_samples
;
}
virtual
void
get_truth_joint_feature_vector
(
long
idx
,
feature_vector_type
&
psi
)
const
{
problem
.
attr
(
"get_truth_joint_feature_vector"
)(
idx
,
boost
::
ref
(
psi
));
}
virtual
void
separation_oracle
(
const
long
idx
,
const
matrix_type
&
current_solution
,
scalar_type
&
loss
,
feature_vector_type
&
psi
)
const
{
loss
=
extract
<
double
>
(
problem
.
attr
(
"separation_oracle"
)(
idx
,
boost
::
ref
(
current_solution
),
boost
::
ref
(
psi
)));
}
private
:
const
long
num_dimensions
;
const
long
num_samples
;
object
&
problem
;
};
// ----------------------------------------------------------------------------------------
/*
class svm_struct_sparse : public structural_svm_problem<matrix<double,0,1>,
std::vector<std::pair<unsigned long,double> >
{
};
*/
// ----------------------------------------------------------------------------------------
matrix
<
double
,
0
,
1
>
solve_structural_svm_problem
(
object
problem
)
{
const
double
C
=
extract
<
double
>
(
problem
.
attr
(
"C"
));
const
bool
be_verbose
=
hasattr
(
problem
,
"be_verbose"
)
&&
extract
<
bool
>
(
problem
.
attr
(
"be_verbose"
));
const
bool
use_sparse_feature_vectors
=
hasattr
(
problem
,
"use_sparse_feature_vectors"
)
&&
extract
<
bool
>
(
problem
.
attr
(
"use_sparse_feature_vectors"
));
double
eps
=
0.001
;
unsigned
long
max_cache_size
=
10
;
if
(
hasattr
(
problem
,
"epsilon"
))
eps
=
extract
<
double
>
(
problem
.
attr
(
"epsilon"
));
if
(
hasattr
(
problem
,
"max_cache_size"
))
eps
=
extract
<
double
>
(
problem
.
attr
(
"max_cache_size"
));
const
long
num_samples
=
extract
<
long
>
(
problem
.
attr
(
"num_samples"
));
const
long
num_dimensions
=
extract
<
long
>
(
problem
.
attr
(
"num_dimensions"
));
if
(
be_verbose
)
{
cout
<<
"C: "
<<
C
<<
endl
;
cout
<<
"epsilon: "
<<
eps
<<
endl
;
cout
<<
"max_cache_size: "
<<
max_cache_size
<<
endl
;
cout
<<
"num_samples: "
<<
num_samples
<<
endl
;
cout
<<
"num_dimensions: "
<<
num_dimensions
<<
endl
;
cout
<<
"use_sparse_feature_vectors: "
<<
std
::
boolalpha
<<
use_sparse_feature_vectors
<<
endl
;
cout
<<
endl
;
}
svm_struct_dense
prob
(
problem
,
num_dimensions
,
num_samples
);
prob
.
set_c
(
C
);
prob
.
set_epsilon
(
eps
);
prob
.
set_max_cache_size
(
max_cache_size
);
if
(
be_verbose
)
prob
.
be_verbose
();
oca
solver
;
matrix
<
double
,
0
,
1
>
w
;
solver
(
prob
,
w
);
return
w
;
}
// ----------------------------------------------------------------------------------------
void
bind_svm_struct
()
{
def
(
"solve_structural_svm_problem"
,
solve_structural_svm_problem
);
}
// ----------------------------------------------------------------------------------------
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