Commit d11eb451 authored by Pavel KORSHUNOV's avatar Pavel KORSHUNOV

Scoring using pre-trained tensorflow model

parent a9f615ea
Pipeline #5320 failed with stages
in 3 minutes and 32 seconds
from .gmm_algorithm import GmmAlgorithm
from .logregr_algorithm import LogRegrAlgorithm
# to fix sphinx warnings of not able to find classes, when path is shortened
GmmAlgorithm.__module__ = "bob.pad.voice.algorithm"
LogRegrAlgorithm.__module__ = "bob.pad.voice.algorithm"
from .tensorflow_algorithm import TensorflowAlgo
# gets sphinx autodoc done right - don't remove it
def __appropriate__(*args):
......@@ -22,5 +19,6 @@ def __appropriate__(*args):
__all__ = [_ for _ in dir() if not _.startswith('_')]
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Pavel Korshunov <>
# @date: Wed 19 Oct 23:43:22 2016
from bob.pad.base.algorithm import Algorithm
import numpy
from import SequenceNetwork
from bob.learn.tensorflow.datashuffler import DiskAudio
import tensorflow as tf
import logging
logger = logging.getLogger("bob.pad.voice")
class TensorflowAlgo (Algorithm):
"""This class is used to test all the possible functions of the tool chain, but it does basically nothing."""
def __init__(self,
"""Generates a test value that is read and written"""
# call base class constructor registering that this tool performs everything.
self.data_reader = DiskAudio([0], [0])
self.session = tf.Session()
self.dnn_model = None
def _check_feature(self, feature):
"""Checks that the features are appropriate."""
if not isinstance(feature, numpy.ndarray) or feature.ndim != 1 or feature.dtype != numpy.float32:
raise ValueError("The given feature is not appropriate", feature)
return True
def load_projector(self, projector_file):"Loading pretrained model from {0}".format(projector_file))
self.dnn_model = SequenceNetwork()
self.dnn_model.load(, session=self.session)
def project_feature(self, feature):
logger.debug(" .... Projecting %d features vector" % feature.shape[0])
frames, labels = self.data_reader.extract_frames_from_wav(feature, 0)
frames = numpy.asarray(frames)
frames = numpy.reshape(frames, (frames.shape[0], -1, 1))
forward_output = self.dnn_model(frames, session=self.session)
return tf.nn.log_softmax(tf.nn.log_softmax(forward_output)).eval(session=self.session)
def project(self, feature):
"""project(feature) -> projected
This function will project the given feature.
It is assured that the :py:meth:`load_projector` was called once before the ``project`` function is executed.
feature : object
The feature to be projected.
projected : object
The projected features.
Must be writable with the :py:meth:`write_feature` function and readable with the :py:meth:`read_feature` function.
if len(feature) > 0:
feature = numpy.cast['float32'](feature)
return self.project_feature(feature)
return numpy.zeros(1, dtype=numpy.float64)
def score_for_multiple_projections(self, toscore):
"""scorescore_for_multiple_projections(toscore) -> score
score : float
A score value for the object ``toscore``.
scores = numpy.asarray(toscore, dtype=numpy.float32)
real_scores = scores[:, 0]
return numpy.mean(real_scores)
def score(self, toscore):
"""Returns the evarage value of the probe""""score() score %f", toscore)
# return only real score
return toscore[0]
algorithm = TensorflowAlgo()
......@@ -4,6 +4,7 @@ from .vectors_ratios import VectorsRatios
from .glcms import GLCMs
from .spectrogram_extended import SpectrogramExtended
from .lbp_histograms import LBPHistograms
from .dummy_tensorflow import DummyTF
# gets sphinx autodoc done right - don't remove it
def __appropriate__(*args):
......@@ -26,5 +27,6 @@ __appropriate__(
__all__ = [_ for _ in dir() if not _.startswith('_')]
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Pavel Korshunov <>
# Tue 15 Nov 15:43:22 CEST 2016
from import Extractor
from import Base
import numpy
import logging
logger = logging.getLogger("bob.pad.voice")
class DummyTF(Base, Extractor):
This class can be used as a simple preprocessor (reads data only) and a dummy extractor (reads saved data)
def __init__(self, **kwargs):
Base.__init__(self, **kwargs)
Extractor.__init__(self, requires_training=False, split_training_data_by_client=False, **kwargs)
def __call__(self, input_data, annotations):
We assume here that this will be called only once in the capacity of Preprocessor.
If it is called as an Extractor, it will break. To avoid it, make sure path to preprocessing directory
and extraction directory are the same. It will ensure that both preprocessing and extraction files are
exactly the same, and this called will be executed only the first time (when preprocessing)
# create empty labels array, since this what read/write function of Base accepts
labels = numpy.ones(len(input_data[1]))
return input_data[0], input_data[1], labels
dummytf = DummyTF()
......@@ -4,7 +4,6 @@
parts = scripts
eggs = gridtk
eggs = bob.pad.voice
......@@ -18,6 +17,7 @@ eggs = bob.pad.voice
extensions = bob.buildout
......@@ -34,6 +34,7 @@ develop = src/
; options for bob.buildout
......@@ -52,6 +53,7 @@ bob.pad.base = git
bob.ap = git
bob.db.voicepa = git
bob.extension = git
bob.learn.tensorflow = git branch=audio-support
recipe = bob.buildout:scripts
......@@ -115,6 +115,7 @@ setup(
'bob.pad.algorithm': [
'tensorflow = bob.pad.voice.algorithm.tensorflow_algorithm:algorithm',
'dummy-algo = bob.pad.voice.algorithm.dummy_algorithm:algorithm',
# compute scores based on different energy bands
'logregr = bob.pad.voice.algorithm.logregr_algorithm:algorithm',
......@@ -128,6 +129,7 @@ setup(
'bob.pad.preprocessor': [
'dummytfp = bob.pad.voice.extractor.dummy_tensorflow:dummytf', # For tensorflow
'energy-2gauss =', # two Gauss energy
'energy-thr =',
# thresholded energy
......@@ -136,6 +138,7 @@ setup(
'bob.pad.extractor': [
'dummytfe = bob.pad.voice.extractor.dummy_tensorflow:dummytf', # For tensorflow
'glcms = bob.pad.voice.extractor.glcms:extractor',
'lbp-hist = bob.pad.voice.extractor.lbp_histograms:extractor',
# LBP-based features as per the paper from Eurecom
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment