Commit 97eef037 authored by Pavel KORSHUNOV's avatar Pavel KORSHUNOV

refactoring and extending LSTM tf algo

parent 8afa34ab
Pipeline #12780 passed with stages
in 29 minutes and 37 seconds
......@@ -17,7 +17,7 @@ import logging
logger = logging.getLogger("bob.pad.voice")
class GmmAlgorithm(Algorithm):
class GMM(Algorithm):
"""Trains Logistical Regression classifier and projects testing dat on it."""
def __init__(self,
......@@ -156,8 +156,8 @@ class GmmAlgorithm(Algorithm):
real_features = numpy.vstack(training_features[0])
attack_features = numpy.vstack(training_features[1])
print ("GmmAlgorithm:train_projector(), real_features shape:", real_features.shape)
print ("GmmAlgorithm:train_projector(), attack_features shape:", attack_features.shape)
print ("GMM:train_projector(), real_features shape:", real_features.shape)
print ("GMM:train_projector(), attack_features shape:", attack_features.shape)
print ("Min real ", numpy.min(real_features))
print ("Max real ", numpy.max(real_features))
print ("Min attack ", numpy.min(attack_features))
......@@ -244,4 +244,4 @@ class GmmAlgorithm(Algorithm):
self.score(toscore)
algorithm = GmmAlgorithm()
algorithm = GMM()
......@@ -18,7 +18,7 @@ import logging
logger = logging.getLogger("bob.pad.voice")
class HistDistanceAlgorithm(Algorithm):
class HistDistance(Algorithm):
"""This class is used to test all the possible functions of the tool chain, but it does basically nothing."""
def __init__(self, chi_square=False, hist_intersection=True, probab_dist=False, normalize_features=True, **kwargs):
......@@ -53,10 +53,10 @@ class HistDistanceAlgorithm(Algorithm):
raise ValueError("Training projector: features should contain two lists: real and attack!")
# the format is specified in FileSelector.py:training_list() of bob.spoof.base
# print ("HistDistanceAlgorithm:train_projector(), training_features", type(training_features[0][0]))
# print ("HistDistance:train_projector(), training_features", type(training_features[0][0]))
if isinstance(training_features[0][0][0], numpy.ndarray):
print ("HistDistanceAlgorithm:train_projector(), features are set of arrays of length: ",
print ("HistDistance:train_projector(), features are set of arrays of length: ",
len(training_features[0][0][0]))
real_features = numpy.array([row for feat in training_features[0] for row in feat], dtype=numpy.float64)
attack_features = numpy.array([row for feat in training_features[1] for row in feat], dtype=numpy.float64)
......@@ -64,10 +64,10 @@ class HistDistanceAlgorithm(Algorithm):
real_features = numpy.array(training_features[0], dtype=numpy.float64)
attack_features = numpy.array(training_features[1], dtype=numpy.float64)
# # print ("HistDistanceAlgorithm:train_projector(), real_features", real_features)
# # print ("HistDistanceAlgorithm:train_projector(), attack_features", attack_features)
# print ("HistDistanceAlgorithm:train_projector(), real_features shape:", real_features.shape)
# print ("HistDistanceAlgorithm:train_projector(), attack_features shape:", attack_features.shape)
# # print ("HistDistance:train_projector(), real_features", real_features)
# # print ("HistDistance:train_projector(), attack_features", attack_features)
# print ("HistDistance:train_projector(), real_features shape:", real_features.shape)
# print ("HistDistance:train_projector(), attack_features shape:", attack_features.shape)
# # real_features[real_features<-1024] = -1024
# # attack_features[attack_features<-1024] = -1024
# print ("Min real ", numpy.min(real_features))
......@@ -135,9 +135,9 @@ class HistDistanceAlgorithm(Algorithm):
dist_real = bob.math.histogram_intersection(self.real_mean, feature)
dist_attack = bob.math.histogram_intersection(self.attack_mean, feature)
else:
raise ValueError("HistDistanceAlgorithm: please specify the metric for histogram distance")
raise ValueError("HistDistance: please specify the metric for histogram distance")
# print ("HistDistanceAlgorithm:project(), projection: ", projection)
# print ("HistDistance:project(), projection: ", projection)
return numpy.array([dist_real, dist_attack], dtype=numpy.float64)
# return self.machine(feature)
return numpy.zeros(2, dtype=numpy.float64)
......@@ -158,7 +158,7 @@ class HistDistanceAlgorithm(Algorithm):
The ``feature`` projected into Fisher space.
"""
print ("HistDistanceAlgorithm:project(), feature shape: ", feature.shape)
print ("HistDistance:project(), feature shape: ", feature.shape)
if len(feature) > 0:
if isinstance(feature[0], numpy.ndarray) or isinstance(feature[0], list):
......@@ -170,7 +170,7 @@ class HistDistanceAlgorithm(Algorithm):
def score(self, toscore):
"""Returns the evarage value of the probe"""
print("HistDistanceAlgorithm:score() the score: ", toscore)
print("HistDistance:score() the score: ", toscore)
# projection is already the score in this case
if self.probab_dist:
......@@ -190,12 +190,12 @@ class HistDistanceAlgorithm(Algorithm):
return [dist_real - dist_attack]
else:
raise ValueError("HistDistanceAlgorithm:scoring() please specify the metric for histogram distance")
raise ValueError("HistDistance:scoring() please specify the metric for histogram distance")
def score_for_multiple_projections(self, toscore):
print("HistDistanceAlgorithm:score_for_multiple_projections() the score: ", len(toscore))
print("HistDistance:score_for_multiple_projections() the score: ", len(toscore))
return numpy.array([self.score(score) for score in toscore], dtype=numpy.float64)
algorithm = HistDistanceAlgorithm()
algorithm = HistDistance()
......@@ -14,10 +14,12 @@ import logging
logger = logging.getLogger("bob.pad.voice")
class TensorflowAlgo(Algorithm):
class LSTMEval(Algorithm):
"""This class is used to test all the possible functions of the tool chain, but it does basically nothing."""
def __init__(self,
input_shape=[200, 81], # [temporal_length, feature_size]
lstm_network_size=60, # the output size of LSTM cell
**kwargs):
"""Generates a test value that is read and written"""
......@@ -26,8 +28,13 @@ class TensorflowAlgo(Algorithm):
self,
performs_projection=True,
requires_projector_training=False,
**kwargs
)
self.input_shape = input_shape
self.num_time_steps = input_shape[0]
self.lstm_network_size = lstm_network_size
self.data_reader = None
self.session = None
self.dnn_model = None
......@@ -70,9 +77,9 @@ class TensorflowAlgo(Algorithm):
import tensorflow as tf
if self.session is None:
self.session = tf.Session()
data_pl = tf.placeholder(tf.float32, shape=(None, 60, 102))
data_pl = tf.placeholder(tf.float32, shape=(None, ) + tuple(self.input_shape))
graph = self.simple_lstm_network(data_pl, batch_size=1,
lstm_cell_size=60, num_time_steps=60,
lstm_cell_size=self.lstm_network_size, num_time_steps=self.num_time_steps,
num_classes=2, reuse=False)
self.session.run(tf.global_variables_initializer())
......@@ -91,9 +98,10 @@ class TensorflowAlgo(Algorithm):
logger.info(" .... Projecting %d features vector" % feature.shape[0])
from bob.learn.tensorflow.datashuffler import DiskAudio
if not self.data_reader:
self.data_reader = DiskAudio([0], [0], [1, 60, 102])
self.data_reader = DiskAudio([0], [0], [1] + self.input_shape)
# frames, labels = self.data_reader.extract_frames_from_wav(feature, 0)
frames, labels = self.data_reader.split_features_in_windows(feature, 0, 60)
frames, labels = self.data_reader.split_features_in_windows(features=feature, label=1,
win_size=self.num_time_steps)
# frames = numpy.asarray(frames)
# logger.info(" .... And frames of shape {0} are extracted to pass into DNN model".format(frames.shape))
projections = numpy.zeros((len(frames), 2), dtype=numpy.float32)
......@@ -144,7 +152,7 @@ class TensorflowAlgo(Algorithm):
A score value for the object ``toscore``.
"""
scores = numpy.asarray(toscore, dtype=numpy.float32)
real_scores = scores[:, 0]
real_scores = scores[:, 1]
logger.debug("Mean score %f", numpy.mean(real_scores))
return [numpy.mean(real_scores)]
......@@ -155,4 +163,4 @@ class TensorflowAlgo(Algorithm):
return [toscore[0]]
algorithm = TensorflowAlgo()
algorithm = LSTMEval()
......@@ -16,7 +16,7 @@ import logging
logger = logging.getLogger("bob.pad.voice")
class LogRegrAlgorithm(Algorithm):
class LogRegr(Algorithm):
"""Trains Logistical Regression classifier and projects testing dat on it."""
def __init__(self, use_PCA_training=False, normalize_features=False, **kwargs):
......@@ -69,8 +69,8 @@ class LogRegrAlgorithm(Algorithm):
[feat if self._check_feature(feat) else numpy.nan for feat in training_features[1]],
dtype=numpy.float64)
# print ("LogRegrAlgorithm:train_projector(), real_features shape:", real_features.shape)
# print ("LogRegrAlgorithm:train_projector(), attack_features shape:", attack_features.shape)
# print ("LogRegr:train_projector(), real_features shape:", real_features.shape)
# print ("LogRegr:train_projector(), attack_features shape:", attack_features.shape)
# print ("Min real ", numpy.min(real_features))
# print ("Max real ", numpy.max(real_features))
# print ("Min attack ", numpy.min(attack_features))
......@@ -130,8 +130,8 @@ class LogRegrAlgorithm(Algorithm):
self.machine.input_subtract = mean
self.machine.input_divide = std
# print ("LogRegrAlgorithm:train_projector(), machine shape: ", self.machine.shape)
# print ("LogRegrAlgorithm:train_projector(), machine weights: ", self.machine.weights)
# print ("LogRegr:train_projector(), machine shape: ", self.machine.shape)
# print ("LogRegr:train_projector(), machine weights: ", self.machine.weights)
hdf5file.cd('/')
hdf5file.create_group('LogRegProjector')
......@@ -203,4 +203,4 @@ class LogRegrAlgorithm(Algorithm):
return toscore
algorithm = LogRegrAlgorithm()
algorithm = LogRegr()
from .gmm_algorithm import GmmAlgorithm
from .logregr_algorithm import LogRegrAlgorithm
from .tensorflow_algorithm import TensorflowAlgo
from .GMM import GMM
from .LogRegr import LogRegr
from .LSTMEval import LSTMEval
# gets sphinx autodoc done right - don't remove it
def __appropriate__(*args):
......@@ -17,8 +17,8 @@ def __appropriate__(*args):
for obj in args: obj.__module__ = __name__
__appropriate__(
GmmAlgorithm,
LogRegrAlgorithm,
TensorflowAlgo,
GMM,
LogRegr,
LSTMEval,
)
__all__ = [_ for _ in dir() if not _.startswith('_')]
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Pavel Korshunov <pavel.korshunov@idiap.ch>
# @date: Wed 19 Oct 23:43:22 2016
from bob.pad.base.algorithm import Algorithm
import logging
logger = logging.getLogger("bob.pad.voice")
class DummyAlgorithm(Algorithm):
"""This class is used to test all the possible functions of the tool chain,
but it does basically nothing."""
def __init__(self, **kwargs):
"""Generates a test value that is read and written"""
# call base class constructor registering that this tool performs everything.
Algorithm.__init__(
self,
performs_projection=False,
requires_projector_training=False,
**kwargs
)
def score(self, toscore):
"""Returns the evarage value of the probe"""
logger.info("score() score %f", toscore)
return toscore
algorithm = DummyAlgorithm()
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Pavel Korshunov <pavel.korshunov@idiap.ch>
# @date: Wed 19 Oct 23:43:22 2016
from bob.pad.base.algorithm import Algorithm
import logging
logger = logging.getLogger("bob.pad.voice")
class DummyAlgorithm (Algorithm):
"""This class is used to test all the possible functions of the tool chain, but it does basically nothing."""
def __init__(self, **kwargs):
"""Generates a test value that is read and written"""
# call base class constructor registering that this tool performs everything.
Algorithm.__init__(
self,
performs_projection = False,
requires_projector_training = False,
)
def score(self, toscore):
"""Returns the evarage value of the probe"""
logger.info("score() score %f", toscore)
return toscore
algorithm = DummyAlgorithm()
import bob.pad.voice
algorithm = bob.pad.voice.algorithm.LogRegrAlgorithm(
algorithm = bob.pad.voice.algorithm.LogRegr(
# use PCA to reduce dimension of features
use_PCA_training = False,
normalize_features = True,
......
import bob.pad.voice
algorithm = bob.pad.voice.algorithm.LogRegrAlgorithm(
algorithm = bob.pad.voice.algorithm.LogRegr(
# use PCA to reduce dimension of features
use_PCA_training = True,
)
......
import bob.pad.voice
algorithm = bob.pad.voice.algorithm.GmmAlgorithm(
algorithm = bob.pad.voice.algorithm.GMM(
number_of_gaussians = 512,
kmeans_training_iterations = 10, # Maximum number of iterations for K-Means
gmm_training_iterations = 10, # Maximum number of iterations for ML GMM Training
......
......@@ -90,7 +90,7 @@ def test_spoof_EnergyLBP2():
'-d', 'bob.pad.voice.test.dummy.database.DummyDatabaseSpeechSpoof()',
'-p', 'bob.bio.spear.preprocessor.Mod_4Hz()',
'-e', 'bob.pad.voice.extractor.LBPHistograms(features_processor=bob.pad.voice.extractor.SpectrogramExtended())',
'-a', 'bob.pad.voice.algorithm.LogRegrAlgorithm()',
'-a', 'bob.pad.voice.algorithm.LogRegr()',
'-vs', 'test_energylbp',
'--temp-directory', test_dir,
'--result-directory', test_dir
......
......@@ -116,15 +116,15 @@ setup(
],
'bob.pad.algorithm': [
'tensorflow = bob.pad.voice.algorithm.tensorflow_algorithm:algorithm',
'dummy-algo = bob.pad.voice.algorithm.dummy_algorithm:algorithm',
'tensorflow = bob.pad.voice.algorithm.LSTMEval:algorithm',
'dummy-algo = bob.pad.voice.algorithm.dummy:algorithm',
# compute scores based on different energy bands
'logregr = bob.pad.voice.algorithm.logregr_algorithm:algorithm',
'pcalogregr = bob.pad.voice.config.algorithm.pcalogregr_algorithm:algorithm',
'logregr = bob.pad.voice.algorithm.LogRegr:algorithm',
'pcalogregr = bob.pad.voice.config.algorithm.PCALogRegr:algorithm',
# the best performing LR classifier:
'normlogregr = bob.pad.voice.config.algorithm.normlogregr_algorithm:algorithm',
'histdistance = bob.pad.voice.algorithm.histdistance_algorithm:algorithm',
'gmm = bob.pad.voice.algorithm.gmm_algorithm:algorithm',
'normlogregr = bob.pad.voice.config.algorithm.NormLogRegr:algorithm',
'histdistance = bob.pad.voice.algorithm.HistDistance:algorithm',
'gmm = bob.pad.voice.algorithm.GMM:algorithm',
# the same as above but with smaller thresholds
'gmm-scfc = bob.pad.voice.config.algorithm.gmm_scfc:algorithm',
],
......
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