Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
bob
bob.pad.base
Commits
054414a5
Commit
054414a5
authored
Jun 13, 2018
by
Guillaume HEUSCH
Browse files
[algorithm] added a minimal SVM version
parent
e85eb10b
Pipeline
#21035
failed with stage
in 16 minutes and 31 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
bob/pad/base/algorithm/__init__.py
View file @
054414a5
...
...
@@ -6,6 +6,9 @@ from .SVMCascadePCA import SVMCascadePCA
from
.MLP
import
MLP
from
.PadLDA
import
PadLDA
from
.OCSVM
import
OCSVM
from
.OCGMM
import
OCGMM
from
.SKLGMM
import
SKLGMM
from
.mySVM
import
mySVM
def
__appropriate__
(
*
args
):
"""Says object was actually declared here, and not in the import module.
...
...
bob/pad/base/algorithm/mySVM.py
0 → 100644
View file @
054414a5
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
import
numpy
from
bob.pad.base.algorithm
import
Algorithm
import
bob.learn.libsvm
import
bob.io.base
class
mySVM
(
Algorithm
):
"""
This class interfaces a One Class SVM classifier used for PAD
"""
def
__init__
(
self
,
rescale
=
True
,
gamma
=
0.1
,
**
kwargs
):
Algorithm
.
__init__
(
self
,
performs_projection
=
True
,
requires_projector_training
=
True
,
**
kwargs
)
self
.
rescale
=
rescale
self
.
gamma
=
gamma
self
.
machine
=
None
self
.
trainer
=
bob
.
learn
.
libsvm
.
Trainer
(
machine_type
=
'C_SVC'
,
kernel_type
=
'RBF'
,
probability
=
True
)
setattr
(
self
.
trainer
,
'gamma'
,
self
.
gamma
)
def
train_projector
(
self
,
training_features
,
projector_file
):
"""
Trains a SVM
**Parameters**
training_features:
"""
# training_features[0] - training features for the REAL class.
# training_features[1] - training features for the ATTACK class.
# The data - "positive class only"
pos
=
numpy
.
array
(
training_features
[
0
])
neg
=
numpy
.
array
(
training_features
[
1
])
if
self
.
rescale
:
for
i
in
range
(
pos
.
shape
[
0
]):
min_value
=
numpy
.
min
(
pos
[
i
])
max_value
=
numpy
.
max
(
pos
[
i
])
pos
[
i
]
=
((
2
*
(
pos
[
i
]
-
min_value
))
/
(
max_value
-
min_value
))
-
1
for
i
in
range
(
neg
.
shape
[
0
]):
min_value
=
numpy
.
min
(
neg
[
i
])
max_value
=
numpy
.
max
(
neg
[
i
])
neg
[
i
]
=
((
2
*
(
neg
[
i
]
-
min_value
))
/
(
max_value
-
min_value
))
-
1
data
=
[
pos
,
neg
]
# train
self
.
machine
=
self
.
trainer
.
train
(
data
)
f
=
bob
.
io
.
base
.
HDF5File
(
projector_file
,
'w'
)
self
.
machine
.
save
(
f
)
def
project
(
self
,
feature
):
"""
Project the given feature
"""
if
self
.
rescale
:
min_value
=
numpy
.
min
(
feature
)
max_value
=
numpy
.
max
(
feature
)
feature
=
((
2
*
(
feature
-
min_value
))
/
(
max_value
-
min_value
))
-
1
return
self
.
machine
(
feature
)
def
score
(
self
,
toscore
):
return
[
toscore
[
0
]]
Guillaume HEUSCH
@heusch
mentioned in issue
bob.learn.libsvm#11 (closed)
·
Jun 13, 2018
mentioned in issue
bob.learn.libsvm#11 (closed)
mentioned in issue bob.learn.libsvm#11
Toggle commit list
Write
Preview
Supports
Markdown
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