Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
bob.pad.base
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
5
Issues
5
List
Boards
Labels
Milestones
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bob
bob.pad.base
Commits
d104f866
Commit
d104f866
authored
Feb 07, 2020
by
Amir MOHAMMADI
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert "Merge branch 'scikit_wrapper' into 'master'"
This reverts merge request
!64
parent
4736950a
Pipeline
#36760
passed with stage
in 5 minutes and 17 seconds
Changes
5
Pipelines
1
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
337 additions
and
584 deletions
+337
-584
bob/pad/base/algorithm/OneClassGMM.py
bob/pad/base/algorithm/OneClassGMM.py
+321
-15
bob/pad/base/algorithm/OneClassGMM2.py
bob/pad/base/algorithm/OneClassGMM2.py
+5
-59
bob/pad/base/algorithm/ScikitClassifier.py
bob/pad/base/algorithm/ScikitClassifier.py
+0
-462
bob/pad/base/algorithm/__init__.py
bob/pad/base/algorithm/__init__.py
+1
-3
bob/pad/base/test/test_algorithms.py
bob/pad/base/test/test_algorithms.py
+10
-45
No files found.
bob/pad/base/algorithm/OneClassGMM.py
View file @
d104f866
This diff is collapsed.
Click to expand it.
bob/pad/base/algorithm/OneClassGMM2.py
View file @
d104f866
...
...
@@ -8,9 +8,6 @@ import logging
import
numpy
as
np
from
collections.abc
import
Iterable
from
multiprocessing
import
cpu_count
from
bob.bio.video.utils
import
FrameContainer
from
bob.pad.base.utils
import
convert_frame_cont_to_array
,
mean_std_normalize
,
convert_and_prepare_features
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -50,8 +47,7 @@ class OneClassGMM2(Algorithm):
update_weights
=
True
,
update_means
=
True
,
update_variances
=
True
,
n_threads
=
4
,
frame_level_scores_flag
=
True
,
n_threads
=
cpu_count
(),
**
kwargs
):
kwargs
.
setdefault
(
"performs_projection"
,
True
)
...
...
@@ -66,9 +62,9 @@ class OneClassGMM2(Algorithm):
update_weights
=
update_weights
,
update_means
=
update_means
,
update_variances
=
update_variances
,
n_threads
=
n_threads
,
)
self
.
number_of_gaussians
=
number_of_gaussians
self
.
frame_level_scores_flag
=
frame_level_scores_flag
def
train_projector
(
self
,
training_features
,
projector_file
):
del
training_features
[
1
]
...
...
@@ -107,59 +103,9 @@ class OneClassGMM2(Algorithm):
self
.
gmm_alg
.
load_ubm
(
projector_file
)
def
project
(
self
,
feature
):
feature
=
convert_and_prepare_features
([
feature
],
dtype
=
"float64"
)[
0
]
if
isinstance
(
feature
,
FrameContainer
):
# if FrameContainer convert to 2D numpy array
features_array
=
convert_frame_cont_to_array
(
feature
)
else
:
features_array
=
feature
print
(
'features_array'
,
features_array
.
shape
)
scores
=
[]
for
feat
in
features_array
:
score
=
self
.
gmm_alg
.
ubm
(
feat
)
scores
.
append
(
score
)
return
np
.
array
(
scores
)
return
self
.
gmm_alg
.
ubm
(
feature
)
def
score
(
self
,
toscore
):
"""
Returns a probability of a sample being a real class.
**Parameters:**
``toscore`` : 1D :py:class:`numpy.ndarray`
Vector with scores for each frame/sample defining the probability
of the frame being a sample of the real class.
**Returns:**
``score`` : [:py:class:`float`]
If ``frame_level_scores_flag = False`` a single score is returned.
One score per video. This score is placed into a list, because
the ``score`` must be an iterable.
Score is a probability of a sample being a real class.
If ``frame_level_scores_flag = True`` a list of scores is returned.
One score per frame/sample.
"""
print
(
'toscore'
,
toscore
.
shape
)
if
self
.
frame_level_scores_flag
:
score
=
list
(
toscore
)
else
:
score
=
[
np
.
mean
(
toscore
)]
# compute a single score per video
return
score
return
[
toscore
]
bob/pad/base/algorithm/ScikitClassifier.py
deleted
100644 → 0
View file @
4736950a
This diff is collapsed.
Click to expand it.
bob/pad/base/algorithm/__init__.py
View file @
d104f866
...
...
@@ -2,11 +2,10 @@ from .Algorithm import Algorithm
from
.SVM
import
SVM
from
.OneClassGMM
import
OneClassGMM
from
.OneClassGMM2
import
OneClassGMM2
from
.LogRegr
import
LogRegr
from
.SVMCascadePCA
import
SVMCascadePCA
from
.Predictions
import
Predictions
,
VideoPredictions
from
.ScikitClassifier
import
ScikitClassifier
from
.MLP
import
MLP
from
.PadLDA
import
PadLDA
...
...
@@ -38,7 +37,6 @@ __appropriate__(
SVMCascadePCA
,
Predictions
,
VideoPredictions
,
ScikitClassifier
,
MLP
,
PadLDA
)
...
...
bob/pad/base/test/test_algorithms.py
View file @
d104f866
...
...
@@ -14,8 +14,7 @@ from bob.pad.base.algorithm import SVM
from
bob.pad.base.algorithm
import
OneClassGMM
from
bob.pad.base.algorithm
import
MLP
from
bob.pad.base.algorithm
import
PadLDA
from
bob.pad.base.algorithm
import
ScikitClassifier
import
os
import
random
from
bob.pad.base.utils
import
(
...
...
@@ -123,8 +122,6 @@ def test_video_gmm_pad_algorithm():
real
=
convert_array_to_list_of_frame_cont
(
real_array
)
attack
=
convert_array_to_list_of_frame_cont
(
attack_array
)
N_COMPONENTS
=
1
RANDOM_STATE
=
3
FRAME_LEVEL_SCORES_FLAG
=
True
...
...
@@ -136,13 +133,18 @@ def test_video_gmm_pad_algorithm():
# training_features[0] - training features for the REAL class.
real_array_converted
=
convert_list_of_frame_cont_to_array
(
real
)
# output is array
attack_array_converted
=
convert_list_of_frame_cont_to_array
(
attack
)
# output is array
assert
(
real_array
==
real_array_converted
).
all
()
# Train the OneClassGMM machine and get normalizers:
status
=
algorithm
.
train_clf
(
real
=
real_array_converted
,
attack
=
attack_array_converted
)
machine
,
features_mean
,
features_std
=
algorithm
.
train_gmm
(
real
=
real_array_converted
)
algorithm
.
machine
=
machine
algorithm
.
features_mean
=
features_mean
algorithm
.
features_std
=
features_std
scores_real
=
algorithm
.
project
(
real_array_converted
)
...
...
@@ -153,6 +155,7 @@ def test_video_gmm_pad_algorithm():
assert
(
np
.
min
(
scores_attack
)
+
38.831260843070098
)
<
0.000001
assert
(
np
.
max
(
scores_attack
)
+
5.3633030621521272
)
<
0.000001
def
test_convert_list_of_frame_cont_to_array
():
N
=
1000
...
...
@@ -216,41 +219,3 @@ def test_LDA():
lda
=
PadLDA
()
lda
.
train_projector
(
training_features
,
'/tmp/lda.hdf5'
)
assert
lda
.
machine
.
shape
==
(
2
,
1
)
def
test_ScikitClassifier
():
random
.
seed
(
7
)
os
.
mkdir
(
'tmp'
)
N
=
20000
mu
=
1
sigma
=
1
real_array
=
np
.
transpose
(
np
.
vstack
([[
random
.
gauss
(
mu
,
sigma
)
for
_
in
range
(
N
)],
[
random
.
gauss
(
mu
,
sigma
)
for
_
in
range
(
N
)]]))
mu
=
5
sigma
=
1
attack_array
=
np
.
transpose
(
np
.
vstack
([[
random
.
gauss
(
mu
,
sigma
)
for
_
in
range
(
N
)],
[
random
.
gauss
(
mu
,
sigma
)
for
_
in
range
(
N
)]]))
training_features
=
[
real_array
,
attack_array
]
from
sklearn.preprocessing
import
StandardScaler
from
sklearn.mixture
import
GaussianMixture
_scaler
=
StandardScaler
()
_clf
=
GaussianMixture
(
n_components
=
10
,
covariance_type
=
'full'
)
sk
=
ScikitClassifier
(
clf
=
_clf
,
scaler
=
_scaler
,
frame_level_scores_flag
=
False
,
one_class
=
True
)
sk
.
train_projector
(
training_features
,
'tmp/sk.hdf5'
)
# Model path `tmp/sk_skmodel.obj`
# Scaler path `tmp/sk_scaler.obj`
assert
sk
.
clf
.
n_components
==
10
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