Commit d104f866 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

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
This diff is collapsed.
...@@ -8,9 +8,6 @@ import logging ...@@ -8,9 +8,6 @@ import logging
import numpy as np import numpy as np
from collections.abc import Iterable from collections.abc import Iterable
from multiprocessing import cpu_count 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__) logger = logging.getLogger(__name__)
...@@ -50,8 +47,7 @@ class OneClassGMM2(Algorithm): ...@@ -50,8 +47,7 @@ class OneClassGMM2(Algorithm):
update_weights=True, update_weights=True,
update_means=True, update_means=True,
update_variances=True, update_variances=True,
n_threads=4, n_threads=cpu_count(),
frame_level_scores_flag=True,
**kwargs **kwargs
): ):
kwargs.setdefault("performs_projection", True) kwargs.setdefault("performs_projection", True)
...@@ -66,9 +62,9 @@ class OneClassGMM2(Algorithm): ...@@ -66,9 +62,9 @@ class OneClassGMM2(Algorithm):
update_weights=update_weights, update_weights=update_weights,
update_means=update_means, update_means=update_means,
update_variances=update_variances, update_variances=update_variances,
n_threads=n_threads,
) )
self.number_of_gaussians = number_of_gaussians self.number_of_gaussians = number_of_gaussians
self.frame_level_scores_flag =frame_level_scores_flag
def train_projector(self, training_features, projector_file): def train_projector(self, training_features, projector_file):
del training_features[1] del training_features[1]
...@@ -107,59 +103,9 @@ class OneClassGMM2(Algorithm): ...@@ -107,59 +103,9 @@ class OneClassGMM2(Algorithm):
self.gmm_alg.load_ubm(projector_file) self.gmm_alg.load_ubm(projector_file)
def project(self, feature): def project(self, feature):
feature = convert_and_prepare_features([feature], dtype="float64")[0]
return self.gmm_alg.ubm(feature)
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)
def score(self, toscore): def score(self, toscore):
""" return [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
This diff is collapsed.
...@@ -2,11 +2,10 @@ from .Algorithm import Algorithm ...@@ -2,11 +2,10 @@ from .Algorithm import Algorithm
from .SVM import SVM from .SVM import SVM
from .OneClassGMM import OneClassGMM from .OneClassGMM import OneClassGMM
from .OneClassGMM2 import OneClassGMM2 from .OneClassGMM2 import OneClassGMM2
from .LogRegr import LogRegr from .LogRegr import LogRegr
from .SVMCascadePCA import SVMCascadePCA from .SVMCascadePCA import SVMCascadePCA
from .Predictions import Predictions, VideoPredictions from .Predictions import Predictions, VideoPredictions
from .ScikitClassifier import ScikitClassifier
from .MLP import MLP from .MLP import MLP
from .PadLDA import PadLDA from .PadLDA import PadLDA
...@@ -38,7 +37,6 @@ __appropriate__( ...@@ -38,7 +37,6 @@ __appropriate__(
SVMCascadePCA, SVMCascadePCA,
Predictions, Predictions,
VideoPredictions, VideoPredictions,
ScikitClassifier,
MLP, MLP,
PadLDA PadLDA
) )
......
...@@ -14,8 +14,7 @@ from bob.pad.base.algorithm import SVM ...@@ -14,8 +14,7 @@ from bob.pad.base.algorithm import SVM
from bob.pad.base.algorithm import OneClassGMM from bob.pad.base.algorithm import OneClassGMM
from bob.pad.base.algorithm import MLP from bob.pad.base.algorithm import MLP
from bob.pad.base.algorithm import PadLDA from bob.pad.base.algorithm import PadLDA
from bob.pad.base.algorithm import ScikitClassifier
import os
import random import random
from bob.pad.base.utils import ( from bob.pad.base.utils import (
...@@ -123,8 +122,6 @@ def test_video_gmm_pad_algorithm(): ...@@ -123,8 +122,6 @@ def test_video_gmm_pad_algorithm():
real = convert_array_to_list_of_frame_cont(real_array) real = convert_array_to_list_of_frame_cont(real_array)
attack = convert_array_to_list_of_frame_cont(attack_array)
N_COMPONENTS = 1 N_COMPONENTS = 1
RANDOM_STATE = 3 RANDOM_STATE = 3
FRAME_LEVEL_SCORES_FLAG = True FRAME_LEVEL_SCORES_FLAG = True
...@@ -136,13 +133,18 @@ def test_video_gmm_pad_algorithm(): ...@@ -136,13 +133,18 @@ def test_video_gmm_pad_algorithm():
# training_features[0] - training features for the REAL class. # training_features[0] - training features for the REAL class.
real_array_converted = convert_list_of_frame_cont_to_array(real) # output is array 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() assert (real_array == real_array_converted).all()
# Train the OneClassGMM machine and get normalizers: # Train the OneClassGMM machine and get normalizers:
status = algorithm.train_clf( machine, features_mean, features_std = algorithm.train_gmm(
real=real_array_converted, attack=attack_array_converted) real=real_array_converted)
algorithm.machine = machine
algorithm.features_mean = features_mean
algorithm.features_std = features_std
scores_real = algorithm.project(real_array_converted) scores_real = algorithm.project(real_array_converted)
...@@ -153,6 +155,7 @@ def test_video_gmm_pad_algorithm(): ...@@ -153,6 +155,7 @@ def test_video_gmm_pad_algorithm():
assert (np.min(scores_attack) + 38.831260843070098) < 0.000001 assert (np.min(scores_attack) + 38.831260843070098) < 0.000001
assert (np.max(scores_attack) + 5.3633030621521272) < 0.000001 assert (np.max(scores_attack) + 5.3633030621521272) < 0.000001
def test_convert_list_of_frame_cont_to_array(): def test_convert_list_of_frame_cont_to_array():
N = 1000 N = 1000
...@@ -216,41 +219,3 @@ def test_LDA(): ...@@ -216,41 +219,3 @@ def test_LDA():
lda = PadLDA() lda = PadLDA()
lda.train_projector(training_features, '/tmp/lda.hdf5') lda.train_projector(training_features, '/tmp/lda.hdf5')
assert lda.machine.shape == (2, 1) 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment