Commit b7c60410 authored by Pavel KORSHUNOV's avatar Pavel KORSHUNOV

updated tf algorithm

parent 49aaac1a
Pipeline #12923 passed with stages
in 13 minutes and 39 seconds
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
from bob.pad.base.algorithm import Algorithm from bob.pad.base.algorithm import Algorithm
import numpy import numpy
# import tensorflow as tf # import tensorflow as tf
import os import os
...@@ -16,7 +15,7 @@ logger = logging.getLogger("bob.pad.voice") ...@@ -16,7 +15,7 @@ logger = logging.getLogger("bob.pad.voice")
class LSTMEval(Algorithm): class LSTMEval(Algorithm):
"""This class is used to test all the possible functions of the tool chain, but it does basically nothing.""" """This class is for evaluating data stored in tensorflow tfrecord format using a pre-trained LSTM model."""
def __init__(self, def __init__(self,
input_shape=[200, 81], # [temporal_length, feature_size] input_shape=[200, 81], # [temporal_length, feature_size]
...@@ -50,14 +49,11 @@ class LSTMEval(Algorithm): ...@@ -50,14 +49,11 @@ class LSTMEval(Algorithm):
self.dnn_model = None self.dnn_model = None
self.data_placeholder = None self.data_placeholder = None
# def __del__(self):
# self.session.close()
def simple_lstm_network(self, train_data_shuffler, batch_size=10, lstm_cell_size=64, def simple_lstm_network(self, train_data_shuffler, batch_size=10, lstm_cell_size=64,
num_time_steps=28, num_classes=10, seed=10, reuse=False): num_time_steps=28, num_classes=10, seed=10, reuse=False):
import tensorflow as tf import tensorflow as tf
from bob.learn.tensorflow.layers import lstm from bob.learn.tensorflow.layers import lstm
# slim = tf.contrib.slim slim = tf.contrib.slim
if isinstance(train_data_shuffler, tf.Tensor): if isinstance(train_data_shuffler, tf.Tensor):
inputs = train_data_shuffler inputs = train_data_shuffler
...@@ -69,11 +65,11 @@ class LSTMEval(Algorithm): ...@@ -69,11 +65,11 @@ class LSTMEval(Algorithm):
# Creating an LSTM network # Creating an LSTM network
graph = lstm(inputs, lstm_cell_size, num_time_steps=num_time_steps, batch_size=batch_size, graph = lstm(inputs, lstm_cell_size, num_time_steps=num_time_steps, batch_size=batch_size,
output_activation_size=num_classes, scope='lstm', output_activation_size=num_classes, scope='lstm',
weights_initializer=initializer, activation=tf.nn.sigmoid, reuse=reuse) weights_initializer=initializer, activation=tf.nn.relu, reuse=reuse)
# fully connect the LSTM output to the classes # fully connect the LSTM output to the classes
# graph = slim.fully_connected(graph, num_classes, activation_fn=None, scope='fc1', graph = slim.fully_connected(graph, num_classes, activation_fn=None, scope='fc1',
# weights_initializer=initializer, reuse=reuse) weights_initializer=initializer, reuse=reuse)
return graph return graph
...@@ -92,14 +88,14 @@ class LSTMEval(Algorithm): ...@@ -92,14 +88,14 @@ class LSTMEval(Algorithm):
import tensorflow as tf import tensorflow as tf
if self.session is None: if self.session is None:
self.session = tf.Session() self.session = tf.Session()
data_pl = tf.placeholder(tf.float32, shape=(None, ) + tuple(self.input_shape)) data_pl = tf.placeholder(tf.float32, shape=(None,) + tuple(self.input_shape))
graph = self.simple_lstm_network(data_pl, batch_size=1, graph = self.simple_lstm_network(data_pl, batch_size=1,
lstm_cell_size=self.lstm_network_size, num_time_steps=self.num_time_steps, lstm_cell_size=self.lstm_network_size, num_time_steps=self.num_time_steps,
num_classes=2, reuse=False) num_classes=2, reuse=False)
self.session.run(tf.global_variables_initializer()) self.session.run(tf.global_variables_initializer())
saver = tf.train.Saver() saver = tf.train.Saver()
# saver = tf.train.import_meta_graph(projector_file + ".meta", clear_devices=True) # saver = tf.train.import_meta_graph(projector_file + ".meta", clear_devices=True)
saver.restore(self.session, projector_file) saver.restore(self.session, projector_file)
return tf.nn.softmax(graph, name="softmax"), data_pl return tf.nn.softmax(graph, name="softmax"), data_pl
...@@ -114,32 +110,28 @@ class LSTMEval(Algorithm): ...@@ -114,32 +110,28 @@ class LSTMEval(Algorithm):
from bob.learn.tensorflow.datashuffler import DiskAudio from bob.learn.tensorflow.datashuffler import DiskAudio
if not self.data_reader: if not self.data_reader:
self.data_reader = DiskAudio([0], [0], [1] + self.input_shape) 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(features=feature, label=1,
win_size=self.num_time_steps,
sliding_step=5)
# frames = numpy.asarray(frames) # normalize the feature using pre-loaded normalization parameters
# logger.info(" .... And frames of shape {0} are extracted to pass into DNN model".format(frames.shape)) if self.data_std and feature is not None:
feature = numpy.divide(feature - self.data_mean, self.data_std)
# split the feature in the sliding window frames
frames, _ = self.data_reader.split_features_in_windows(features=feature, label=1,
win_size=self.num_time_steps,
sliding_step=5)
logger.info(" .... And frames of shape {0} are extracted to pass into DNN model".format(frames.shape))
if frames is None: if frames is None:
return None return None
if self.data_std:
frames = numpy.divide(frames - self.data_mean, self.data_std)
projections = numpy.zeros((len(frames), 2), dtype=numpy.float32) projections = numpy.zeros((len(frames), 2), dtype=numpy.float32)
for i in range(frames.shape[0]): for i in range(frames.shape[0]):
# normalize frame using pre-loaded normalization parameters
# if self.data_std:
# frame = numpy.divide(frame - self.data_mean, self.data_std)
frame = frames[i] frame = frames[i]
frame = numpy.reshape(frame, ([1] + list(frames[0].shape))) frame = numpy.reshape(frame, ([1] + list(frames[0].shape)))
# frames = numpy.reshape(frames, (frames.shape[0], -1, 1))
logger.info(" .... projecting frame of shape {0} onto DNN model".format(frame.shape)) logger.info(" .... projecting frame of shape {0} onto DNN model".format(frame.shape))
if self.session is not None: if self.session is not None:
forward_output = self.session.run(self.dnn_model, feed_dict={self.data_placeholder: frame}) forward_output = self.session.run(self.dnn_model, feed_dict={self.data_placeholder: frame})
projections[i]=forward_output[0] projections[i] = forward_output[0]
else: else:
raise ValueError("Tensorflow session was not initialized, so cannot project on DNN model!") raise ValueError("Tensorflow session was not initialized, so cannot project on DNN model!")
logger.info("Projected scores {0}".format(projections)) logger.info("Projected scores {0}".format(projections))
......
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