LSTMEval.py 6.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
#!/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 numpy


10
# import tensorflow as tf
11
12

import logging
13

14
15
16
logger = logging.getLogger("bob.pad.voice")


17
class LSTMEval(Algorithm):
18
19
20
    """This class is used to test all the possible functions of the tool chain, but it does basically nothing."""

    def __init__(self,
21
22
                 input_shape=[200, 81],  # [temporal_length, feature_size]
                 lstm_network_size=60,  # the output size of LSTM cell
23
24
25
26
27
28
29
30
                 **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,
31
            **kwargs
32
        )
33

34
35
36
37
        self.input_shape = input_shape
        self.num_time_steps = input_shape[0]
        self.lstm_network_size = lstm_network_size

38
        self.data_reader = None
39
        self.session = None
40
        self.dnn_model = None
41
        self.data_placeholder = None
42

43
44
#    def __del__(self):
#        self.session.close()
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    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):
        import tensorflow as tf
        from bob.learn.tensorflow.layers import lstm
        slim = tf.contrib.slim

        if isinstance(train_data_shuffler, tf.Tensor):
            inputs = train_data_shuffler
        else:
            inputs = train_data_shuffler("data", from_queue=False)

        initializer = tf.contrib.layers.xavier_initializer(seed=seed)

        # Creating an LSTM network
        graph = lstm(inputs, lstm_cell_size, num_time_steps=num_time_steps, batch_size=batch_size,
61
62
                     output_activation_size=num_classes, scope='lstm',
                     weights_initializer=initializer, activation=tf.nn.sigmoid, reuse=reuse)
63
64

        # fully connect the LSTM output to the classes
65
66
#        graph = slim.fully_connected(graph, num_classes, activation_fn=None, scope='fc1',
#                                     weights_initializer=initializer, reuse=reuse)
67
68
69

        return graph

70
71
    def _check_feature(self, feature):
        """Checks that the features are appropriate."""
72
        if not isinstance(feature, numpy.ndarray) or feature.ndim != 2 or feature.dtype != numpy.float32:
73
74
            raise ValueError("The given feature is not appropriate", feature)
        return True
75

76
77
78
79
    def restore_trained_model(self, projector_file):
        import tensorflow as tf
        if self.session is None:
            self.session = tf.Session()
80
        data_pl = tf.placeholder(tf.float32, shape=(None, ) + tuple(self.input_shape))
81
        graph = self.simple_lstm_network(data_pl, batch_size=1,
82
                                         lstm_cell_size=self.lstm_network_size, num_time_steps=self.num_time_steps,
83
                                         num_classes=2, reuse=False)
84
85

        self.session.run(tf.global_variables_initializer())
86
87
        saver = tf.train.Saver()
#        saver = tf.train.import_meta_graph(projector_file + ".meta", clear_devices=True)
88
        saver.restore(self.session, projector_file)
89
        return tf.nn.softmax(graph, name="softmax"), data_pl
90

91
92
    def load_projector(self, projector_file):
        logger.info("Loading pretrained model from {0}".format(projector_file))
93
94

        self.dnn_model, self.data_placeholder = self.restore_trained_model(projector_file)
95

96
    def project_feature(self, feature):
97

98
        logger.info(" .... Projecting %d features vector" % feature.shape[0])
99
100
        from bob.learn.tensorflow.datashuffler import DiskAudio
        if not self.data_reader:
101
            self.data_reader = DiskAudio([0], [0], [1] + self.input_shape)
102
        # frames, labels = self.data_reader.extract_frames_from_wav(feature, 0)
103
104
        frames, labels = self.data_reader.split_features_in_windows(features=feature, label=1,
                                                                    win_size=self.num_time_steps)
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#        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)
        for frame, i in zip(frames, range(len(frames))):
            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))

            if self.session is not None:
                forward_output = self.session.run(self.dnn_model, feed_dict={self.data_placeholder: frame})
                projections[i]=forward_output[0]
            else:
                raise ValueError("Tensorflow session was not initialized, so cannot project on DNN model!")
        logger.info("Projected scores {0}".format(projections))
        return numpy.asarray(projections, dtype=numpy.float32)
120

121
122
    def project(self, feature):
        """project(feature) -> projected
123

124
125
        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.
126

127
        **Parameters:**
128

129
130
        feature : object
          The feature to be projected.
131

132
        **Returns:**
133

134
135
136
        projected : object
          The projected features.
          Must be writable with the :py:meth:`write_feature` function and readable with the :py:meth:`read_feature` function.
137

138
139
140
141
142
143
144
        """
        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)
145

146
147
    def score_for_multiple_projections(self, toscore):
        """scorescore_for_multiple_projections(toscore) -> score
148

149
        **Returns:**
150

151
152
153
154
        score : float
          A score value for the object ``toscore``.
        """
        scores = numpy.asarray(toscore, dtype=numpy.float32)
155
        real_scores = scores[:, 1]
156
157
        logger.debug("Mean score %f", numpy.mean(real_scores))
        return [numpy.mean(real_scores)]
158

159
160
161
162
163
    def score(self, toscore):
        """Returns the evarage value of the probe"""
        logger.debug("score() score %f", toscore)
        # return only real score
        return [toscore[0]]
164
165


166
algorithm = LSTMEval()