Skip to content
GitLab
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.voice
Commits
97eef037
Commit
97eef037
authored
Sep 27, 2017
by
Pavel KORSHUNOV
Browse files
refactoring and extending LSTM tf algo
parent
8afa34ab
Pipeline
#12780
passed with stages
in 29 minutes and 37 seconds
Changes
12
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
bob/pad/voice/algorithm/
gmm_algorithm
.py
→
bob/pad/voice/algorithm/
GMM
.py
View file @
97eef037
...
...
@@ -17,7 +17,7 @@ import logging
logger
=
logging
.
getLogger
(
"bob.pad.voice"
)
class
G
mmAlgorithm
(
Algorithm
):
class
G
MM
(
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
(
"G
mmAlgorithm
:train_projector(), real_features shape:"
,
real_features
.
shape
)
print
(
"G
mmAlgorithm
:train_projector(), attack_features shape:"
,
attack_features
.
shape
)
print
(
"G
MM
:train_projector(), real_features shape:"
,
real_features
.
shape
)
print
(
"G
MM
: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
=
G
mmAlgorithm
()
algorithm
=
G
MM
()
bob/pad/voice/algorithm/
h
ist
d
istance
_algorithm
.py
→
bob/pad/voice/algorithm/
H
ist
D
istance.py
View file @
97eef037
...
...
@@ -18,7 +18,7 @@ import logging
logger
=
logging
.
getLogger
(
"bob.pad.voice"
)
class
HistDistance
Algorithm
(
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 ("HistDistance
Algorithm
: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
(
"HistDistance
Algorithm
: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 ("HistDistance
Algorithm
:train_projector(), real_features", real_features)
# # print ("HistDistance
Algorithm
:train_projector(), attack_features", attack_features)
# print ("HistDistance
Algorithm
:train_projector(), real_features shape:", real_features.shape)
# print ("HistDistance
Algorithm
: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
(
"HistDistance
Algorithm
: please specify the metric for histogram distance"
)
raise
ValueError
(
"HistDistance: please specify the metric for histogram distance"
)
# print ("HistDistance
Algorithm
: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
(
"HistDistance
Algorithm
: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
(
"HistDistance
Algorithm
: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
(
"HistDistance
Algorithm
: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
(
"HistDistance
Algorithm
: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
=
HistDistance
Algorithm
()
algorithm
=
HistDistance
()
bob/pad/voice/algorithm/
tensorflow_algorithm
.py
→
bob/pad/voice/algorithm/
LSTMEval
.py
View file @
97eef037
...
...
@@ -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
()
bob/pad/voice/algorithm/
l
og
r
egr
_algorithm
.py
→
bob/pad/voice/algorithm/
L
og
R
egr.py
View file @
97eef037
...
...
@@ -16,7 +16,7 @@ import logging
logger
=
logging
.
getLogger
(
"bob.pad.voice"
)
class
LogRegr
Algorithm
(
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 ("LogRegr
Algorithm
:train_projector(), real_features shape:", real_features.shape)
# print ("LogRegr
Algorithm
: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 ("LogRegr
Algorithm
:train_projector(), machine shape: ", self.machine.shape)
# print ("LogRegr
Algorithm
: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
=
LogRegr
Algorithm
()
algorithm
=
LogRegr
()
bob/pad/voice/algorithm/__init__.py
View file @
97eef037
from
.
gmm_algorithm
import
GmmAlgorithm
from
.
l
og
r
egr
_algorithm
import
LogRegr
Algorithm
from
.
tensorflow_algorithm
import
TensorflowAlgo
from
.
GMM
import
GMM
from
.
L
og
R
egr
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__
(
G
mmAlgorithm
,
LogRegr
Algorithm
,
TensorflowAlgo
,
G
MM
,
LogRegr
,
LSTMEval
,
)
__all__
=
[
_
for
_
in
dir
()
if
not
_
.
startswith
(
'_'
)]
bob/pad/voice/algorithm/dummy.py
0 → 100644
View file @
97eef037
#!/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
()
bob/pad/voice/algorithm/dummy_algorithm.py
deleted
100644 → 0
View file @
8afa34ab
#!/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
()
bob/pad/voice/config/algorithm/
n
orm
l
og
r
egr
_algorithm
.py
→
bob/pad/voice/config/algorithm/
N
orm
L
og
R
egr.py
View file @
97eef037
import
bob.pad.voice
algorithm
=
bob
.
pad
.
voice
.
algorithm
.
LogRegr
Algorithm
(
algorithm
=
bob
.
pad
.
voice
.
algorithm
.
LogRegr
(
# use PCA to reduce dimension of features
use_PCA_training
=
False
,
normalize_features
=
True
,
...
...
bob/pad/voice/config/algorithm/
pcal
og
r
egr
_algorithm
.py
→
bob/pad/voice/config/algorithm/
PCAL
og
R
egr.py
View file @
97eef037
import
bob.pad.voice
algorithm
=
bob
.
pad
.
voice
.
algorithm
.
LogRegr
Algorithm
(
algorithm
=
bob
.
pad
.
voice
.
algorithm
.
LogRegr
(
# use PCA to reduce dimension of features
use_PCA_training
=
True
,
)
...
...
bob/pad/voice/config/algorithm/gmm_scfc.py
View file @
97eef037
import
bob.pad.voice
algorithm
=
bob
.
pad
.
voice
.
algorithm
.
G
mmAlgorithm
(
algorithm
=
bob
.
pad
.
voice
.
algorithm
.
G
MM
(
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
...
...
bob/pad/voice/test/test_spoofspeech.py
View file @
97eef037
...
...
@@ -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.LogRegr
Algorithm
()'
,
'-a'
,
'bob.pad.voice.algorithm.LogRegr()'
,
'-vs'
,
'test_energylbp'
,
'--temp-directory'
,
test_dir
,
'--result-directory'
,
test_dir
...
...
setup.py
View file @
97eef037
...
...
@@ -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.
l
og
r
egr
_algorithm
:algorithm'
,
'pcalogregr = bob.pad.voice.config.algorithm.
pcal
og
r
egr
_algorithm
:algorithm'
,
'logregr = bob.pad.voice.algorithm.
L
og
R
egr:algorithm'
,
'pcalogregr = bob.pad.voice.config.algorithm.
PCAL
og
R
egr:algorithm'
,
# the best performing LR classifier:
'normlogregr = bob.pad.voice.config.algorithm.
n
orm
l
og
r
egr
_algorithm
:algorithm'
,
'histdistance = bob.pad.voice.algorithm.
h
ist
d
istance
_algorithm
:algorithm'
,
'gmm = bob.pad.voice.algorithm.
gmm_algorithm
:algorithm'
,
'normlogregr = bob.pad.voice.config.algorithm.
N
orm
L
og
R
egr:algorithm'
,
'histdistance = bob.pad.voice.algorithm.
H
ist
D
istance: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'
,
],
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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