deep_speech_model.py 6.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
15
"""Network structure for DeepSpeech2 model."""
16
17
18
19
20
21
22
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf

23
# Supported rnn cells.
24
SUPPORTED_RNNS = {
25
26
27
    "lstm": tf.keras.layers.LSTMCell,
    "rnn": tf.keras.layers.SimpleRNNCell,
    "gru": tf.keras.layers.GRUCell,
28
29
}

30
31
32
# Parameters for batch normalization.
_BATCH_NORM_EPSILON = 1e-5
_BATCH_NORM_DECAY = 0.997
33

34
35
# Filters of convolution layer
_CONV_FILTERS = 32
36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

def batch_norm(inputs, training):
  """Batch normalization layer.

  Note that the momentum to use will affect validation accuracy over time.
  Batch norm has different behaviors during training/evaluation. With a large
  momentum, the model takes longer to get a near-accurate estimation of the
  moving mean/variance over the entire training dataset, which means we need
  more iterations to see good evaluation results. If the training data is evenly
  distributed over the feature space, we can also try setting a smaller momentum
  (such as 0.1) to get good evaluation result sooner.

  Args:
    inputs: input data for batch norm layer.
    training: a boolean to indicate if it is in training stage.

  Returns:
    tensor output from batch norm layer.
  """
56
57
  return tf.keras.layers.BatchNormalization(
      momentum=_BATCH_NORM_DECAY, epsilon=_BATCH_NORM_EPSILON)(inputs, training=training)
58
59
60
61


def _conv_bn_layer(inputs, padding, filters, kernel_size, strides, layer_id,
                   training):
62
  """Defines 2D convolutional + batch normalization layer.
63
64

  Args:
65
66
    inputs: input data for convolution layer.
    padding: padding to be applied before convolution layer.
67
68
69
70
71
    filters: an integer, number of output filters in the convolution.
    kernel_size: a tuple specifying the height and width of the 2D convolution
      window.
    strides: a tuple specifying the stride length of the convolution.
    layer_id: an integer specifying the layer index.
72
    training: a boolean to indicate which stage we are in (training/eval).
73
74
75
76

  Returns:
    tensor output from the current layer.
  """
77
78
79
80
81
82
  # Perform symmetric padding on the feature dimension of time_step
  # This step is required to avoid issues when RNN output sequence is shorter
  # than the label length.
  inputs = tf.pad(
      inputs,
      [[0, 0], [padding[0], padding[0]], [padding[1], padding[1]], [0, 0]])
83
84
  inputs = tf.keras.layers.Conv2D(
      filters=filters, kernel_size=kernel_size, strides=strides,
85
      padding="valid", use_bias=False, activation=tf.nn.relu6,
86
      name="cnn_{}".format(layer_id))(inputs)
87
88
89
90
91
  return batch_norm(inputs, training)


def _rnn_layer(inputs, rnn_cell, rnn_hidden_size, layer_id, is_batch_norm,
               is_bidirectional, training):
92
93
94
  """Defines a batch normalization + rnn layer.

  Args:
95
    inputs: input tensors for the current layer.
96
97
98
99
100
101
102
    rnn_cell: RNN cell instance to use.
    rnn_hidden_size: an integer for the dimensionality of the rnn output space.
    layer_id: an integer for the index of current layer.
    is_batch_norm: a boolean specifying whether to perform batch normalization
      on input states.
    is_bidirectional: a boolean specifying whether the rnn layer is
      bi-directional.
103
    training: a boolean to indicate which stage we are in (training/eval).
104
105
106
107
108

  Returns:
    tensor output for the current layer.
  """
  if is_batch_norm:
109
    inputs = batch_norm(inputs, training)
110

111
  if is_bidirectional:
112
113
114
    rnn_outputs = tf.keras.layers.Bidirectional(
        tf.keras.layers.RNN(rnn_cell(rnn_hidden_size),
                            return_sequences=True))(inputs)
115
  else:
116
117
    rnn_outputs = tf.keras.layers.RNN(
        rnn_cell(rnn_hidden_size), return_sequences=True)(inputs)
118

119
  return rnn_outputs
120

121
122
class DeepSpeech2(object):
  """Define DeepSpeech2 model."""
123

124
125
126
  def __init__(self, num_rnn_layers, rnn_type, is_bidirectional,
               rnn_hidden_size, num_classes, use_bias):
    """Initialize DeepSpeech2 model.
127
128
129
130
131
132
133
134
135

    Args:
      num_rnn_layers: an integer, the number of rnn layers. By default, it's 5.
      rnn_type: a string, one of the supported rnn cells: gru, rnn and lstm.
      is_bidirectional: a boolean to indicate if the rnn layer is bidirectional.
      rnn_hidden_size: an integer for the number of hidden states in each unit.
      num_classes: an integer, the number of output classes/labels.
      use_bias: a boolean specifying whether to use bias in the last fc layer.
    """
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    self.num_rnn_layers = num_rnn_layers
    self.rnn_type = rnn_type
    self.is_bidirectional = is_bidirectional
    self.rnn_hidden_size = rnn_hidden_size
    self.num_classes = num_classes
    self.use_bias = use_bias

  def __call__(self, inputs, training):
    # Two cnn layers.
    inputs = _conv_bn_layer(
        inputs, padding=(20, 5), filters=_CONV_FILTERS, kernel_size=(41, 11),
        strides=(2, 2), layer_id=1, training=training)

    inputs = _conv_bn_layer(
        inputs, padding=(10, 5), filters=_CONV_FILTERS, kernel_size=(21, 11),
        strides=(2, 1), layer_id=2, training=training)

153
    # output of conv_layer2 with the shape of
154
155
156
157
158
159
160
    # [batch_size (N), times (T), features (F), channels (C)].
    # Convert the conv output to rnn input.
    batch_size = tf.shape(inputs)[0]
    feat_size = inputs.get_shape().as_list()[2]
    inputs = tf.reshape(
        inputs,
        [batch_size, -1, feat_size * _CONV_FILTERS])
161
162

    # RNN layers.
163
164
165
    rnn_cell = SUPPORTED_RNNS[self.rnn_type]
    for layer_counter in xrange(self.num_rnn_layers):
      # No batch normalization on the first layer.
166
      is_batch_norm = (layer_counter != 0)
167
168
169
170
171
172
      inputs = _rnn_layer(
          inputs, rnn_cell, self.rnn_hidden_size, layer_counter + 1,
          is_batch_norm, self.is_bidirectional, training)

    # FC layer with batch norm.
    inputs = batch_norm(inputs, training)
173
174
    logits = tf.keras.layers.Dense(
        self.num_classes, use_bias=self.use_bias, activation="softmax")(inputs)
175
176
177

    return logits