Commit 17beaa9f authored by Pavel KORSHUNOV's avatar Pavel KORSHUNOV

Scoring using pre-trained tensorflow model

parent d11eb451
Pipeline #5325 failed with stages
in 3 minutes and 27 seconds
......@@ -13,88 +13,95 @@ import bob.io.base
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,
**kwargs):
"""Generates a test value that is read and written"""
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,
**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=True,
requires_projector_training=False,
)
# call base class constructor registering that this tool performs everything.
Algorithm.__init__(
self,
performs_projection=True,
requires_projector_training=False,
)
self.data_reader = DiskAudio([0], [0])
self.session = tf.Session()
self.dnn_model = None
self.data_reader = DiskAudio([0], [0])
self.session = tf.Session()
self.dnn_model = None
def __del__(self):
self.session.close()
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 _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):
logger.info("Loading pretrained model from {0}".format(projector_file))
self.dnn_model = SequenceNetwork()
self.dnn_model.load(bob.io.base.HDF5File(projector_file), session=self.session)
def load_projector(self, projector_file):
logger.info("Loading pretrained model from {0}".format(projector_file))
self.dnn_model = SequenceNetwork()
self.dnn_model.load(bob.io.base.HDF5File(projector_file), session=self.session)
def project_feature(self, feature):
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)
logger.debug(" .... Projecting %d features vector" % feature.shape[0])
frames, labels = self.data_reader.extract_frames_from_wav(feature, 0)
frames = numpy.asarray(frames)
logger.debug(" .... And %d frames are extracted to pass into DNN model" % frames.shape[0])
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
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.
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.
**Parameters:**
**Parameters:**
feature : object
The feature to be projected.
feature : object
The feature to be projected.
**Returns:**
**Returns:**
projected : object
The projected features.
Must be writable with the :py:meth:`write_feature` function and readable with the :py:meth:`read_feature` function.
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)
self._check_feature(feature)
return self.project_feature(feature)
else:
return numpy.zeros(1, dtype=numpy.float64)
"""
if len(feature) > 0:
feature = numpy.cast['float32'](feature)
self._check_feature(feature)
return self.project_feature(feature)
else:
return numpy.zeros(1, dtype=numpy.float64)
def score_for_multiple_projections(self, toscore):
"""scorescore_for_multiple_projections(toscore) -> score
def score_for_multiple_projections(self, toscore):
"""scorescore_for_multiple_projections(toscore) -> score
**Returns:**
**Returns:**
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)
score : float
A score value for the object ``toscore``.
"""
scores = numpy.asarray(toscore, dtype=numpy.float32)
real_scores = scores[:, 0]
logger.debug("Mean score %f", numpy.mean(real_scores))
return [numpy.mean(real_scores)]
def score(self, toscore):
"""Returns the evarage value of the probe"""
logger.debug("score() score %f", toscore)
# return only real score
return [toscore[0]]
def score(self, toscore):
"""Returns the evarage value of the probe"""
logger.info("score() score %f", toscore)
# return only real score
return toscore[0]
algorithm = TensorflowAlgo()
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