Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
097b4eab
"tests/vscode:/vscode.git/clone" did not exist on "afca11142771a0d99c3fbdbe1c17ad4dc775b934"
Commit
097b4eab
authored
Jun 26, 2013
by
Davis King
Browse files
Added initial version of structural svm python example program.
parent
a0fe7efc
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
86 additions
and
0 deletions
+86
-0
python_examples/svm_struct.py
python_examples/svm_struct.py
+86
-0
No files found.
python_examples/svm_struct.py
0 → 100755
View file @
097b4eab
#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
#
#
# COMPILING THE DLIB PYTHON INTERFACE
# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If
# you are using another python version or operating system then you need to
# compile the dlib python interface before you can use this file. To do this,
# run compile_dlib_python_module.bat. This should work on any operating system
# so long as you have CMake and boost-python installed. On Ubuntu, this can be
# done easily by running the command: sudo apt-get install libboost-python-dev cmake
import
dlib
def
dot
(
a
,
b
):
return
sum
(
i
*
j
for
i
,
j
in
zip
(
a
,
b
))
class
three_class_classifier_problem
:
C
=
10
be_verbose
=
True
epsilon
=
0.0001
def
__init__
(
self
,
samples
,
labels
):
self
.
num_samples
=
len
(
samples
)
self
.
num_dimensions
=
len
(
samples
[
0
])
*
3
self
.
samples
=
samples
self
.
labels
=
labels
def
make_psi
(
self
,
psi
,
vector
,
label
):
psi
.
resize
(
self
.
num_dimensions
)
dims
=
len
(
vector
)
if
(
label
==
1
):
for
i
in
range
(
0
,
dims
):
psi
[
i
]
=
vector
[
i
]
elif
(
label
==
2
):
for
i
in
range
(
dims
,
2
*
dims
):
psi
[
i
]
=
vector
[
i
-
dims
]
else
:
for
i
in
range
(
2
*
dims
,
3
*
dims
):
psi
[
i
]
=
vector
[
i
-
2
*
dims
]
def
get_truth_joint_feature_vector
(
self
,
idx
,
psi
):
self
.
make_psi
(
psi
,
self
.
samples
[
idx
],
self
.
labels
[
idx
])
def
separation_oracle
(
self
,
idx
,
current_solution
,
psi
):
samp
=
samples
[
idx
]
dims
=
len
(
samp
)
scores
=
[
0
,
0
,
0
]
# compute scores for each of the three classifiers
scores
[
0
]
=
dot
(
current_solution
[
0
:
dims
],
samp
)
scores
[
1
]
=
dot
(
current_solution
[
dims
:
2
*
dims
],
samp
)
scores
[
2
]
=
dot
(
current_solution
[
2
*
dims
:
3
*
dims
],
samp
)
# Add in the loss-augmentation
if
(
labels
[
idx
]
!=
1
):
scores
[
0
]
+=
1
if
(
labels
[
idx
]
!=
2
):
scores
[
1
]
+=
1
if
(
labels
[
idx
]
!=
3
):
scores
[
2
]
+=
1
# Now figure out which classifier has the largest loss-augmented score.
max_scoring_label
=
scores
.
index
(
max
(
scores
))
+
1
if
(
max_scoring_label
==
labels
[
idx
]):
loss
=
0
else
:
loss
=
1
self
.
make_psi
(
psi
,
samp
,
max_scoring_label
)
return
loss
samples
=
[
[
0
,
0
,
1
],
[
0
,
1
,
0
],
[
1
,
0
,
0
]];
labels
=
[
1
,
2
,
3
]
problem
=
three_class_classifier_problem
(
samples
,
labels
)
weights
=
dlib
.
solve_structural_svm_problem
(
problem
)
print
weights
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