Example #1
0
def reduce_to_final(images, num_filters_out, nhidden=None, scope=None):
    """Reduce an image to a final state by running two LSTMs.

  Args:
    images: (num_images, height, width, depth) tensor
    num_filters_out: output layer depth
    nhidden: hidden layer depth (defaults to num_filters_out)
    scope: optional scope name

  Returns:
    A (num_images, num_filters_out) batch.
  """
    with variable_scope.variable_scope(scope, "ReduceToFinal", [images]):
        nhidden = nhidden or num_filters_out
        batch_size, height, width, depth = _shape(images)
        transposed = array_ops.transpose(images, [1, 0, 2, 3])
        reshaped = array_ops.reshape(transposed,
                                     [height, batch_size * width, depth])
        with variable_scope.variable_scope("reduce1"):
            reduced = lstm1d.sequence_to_final(reshaped, nhidden)
            transposed_hidden = array_ops.reshape(reduced,
                                                  [batch_size, width, nhidden])
            hidden = array_ops.transpose(transposed_hidden, [1, 0, 2])
        with variable_scope.variable_scope("reduce2"):
            output = lstm1d.sequence_to_final(hidden, num_filters_out)
        return output
Example #2
0
 def testSequenceToFinalDims(self):
     with self.test_session():
         inputs = tf.constant(_rand(17, 6, 5))
         outputs = lstm1d.sequence_to_final(inputs, 8)
         tf.global_variables_initializer().run()
         names = [v.name for v in tf.trainable_variables()]
         self.assertEqual(len(names), 2)
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (6, 8))
Example #3
0
def reduce_to_sequence(images, num_filters_out, scope=None):
  """Reduce an image to a sequence by scanning an LSTM vertically.

  Args:
    images: (num_images, height, width, depth) tensor
    num_filters_out: output layer depth
    scope: optional scope name

  Returns:
    A (width, num_images, num_filters_out) sequence.
  """
  with tf.variable_scope(scope, "ReduceToSequence", [images]):
    batch_size, height, width, depth = _shape(images)
    transposed = tf.transpose(images, [1, 0, 2, 3])
    reshaped = tf.reshape(transposed, [height, batch_size * width, depth])
    reduced = lstm1d.sequence_to_final(reshaped, num_filters_out)
    output = tf.reshape(reduced, [batch_size, width, num_filters_out])
    return output