"//tensorflow/contrib/model_pruning:all_files",
"//tensorflow/contrib/model_pruning/examples/cifar10:all_files",
"//tensorflow/contrib/nccl:all_files",
- "//tensorflow/contrib/ndlstm:all_files",
"//tensorflow/contrib/nearest_neighbor:all_files",
"//tensorflow/contrib/nn:all_files",
"//tensorflow/contrib/opt:all_files",
"//tensorflow/contrib/metrics:metrics_py",
"//tensorflow/contrib/model_pruning",
"//tensorflow/contrib/nccl:nccl_py",
- "//tensorflow/contrib/ndlstm",
"//tensorflow/contrib/nearest_neighbor:nearest_neighbor_py",
"//tensorflow/contrib/nn:nn_py",
"//tensorflow/contrib/opt:opt_py",
from tensorflow.contrib import util
from tensorflow.contrib.eager.python import tfe as eager
from tensorflow.contrib.lite.python import lite
-from tensorflow.contrib.ndlstm import python as ndlstm
from tensorflow.contrib.receptive_field import receptive_field_api as receptive_field
from tensorflow.contrib.remote_fused_graph import pylib as remote_fused_graph
from tensorflow.contrib.specs import python as specs
tensorflow/contrib/nccl/ops
tensorflow/contrib/nccl/python
tensorflow/contrib/nccl/python/ops
-tensorflow/contrib/ndlstm
-tensorflow/contrib/ndlstm/python
tensorflow/contrib/nearest_neighbor/kernels
tensorflow/contrib/nearest_neighbor/ops
tensorflow/contrib/nearest_neighbor/python
+++ /dev/null
-# Description:
-# Contains classes implementing 1D and 2D LSTMs for image and signal
-# processing problems.
-
-licenses(["notice"]) # Apache 2.0
-
-exports_files(["LICENSE"])
-
-package(default_visibility = ["//tensorflow:__subpackages__"])
-
-load("//tensorflow:tensorflow.bzl", "tf_py_test")
-
-py_library(
- name = "ndlstm",
- srcs = [
- "__init__.py",
- "python/__init__.py",
- "python/lstm1d.py",
- "python/lstm2d.py",
- "python/misc.py",
- ],
- srcs_version = "PY2AND3",
- deps = [
- "//tensorflow/contrib/framework:framework_py",
- "//tensorflow/contrib/layers:layers_py",
- "//tensorflow/contrib/rnn:rnn_py",
- "//tensorflow/python:array_ops",
- "//tensorflow/python:framework",
- "//tensorflow/python:framework_for_generated_wrappers",
- "//tensorflow/python:math_ops",
- "//tensorflow/python:nn_ops",
- "//tensorflow/python:ops",
- "//tensorflow/python:platform",
- "//tensorflow/python:random_ops",
- "//tensorflow/python:rnn",
- "//tensorflow/python:rnn_cell",
- "//tensorflow/python:sparse_ops",
- "//tensorflow/python:training",
- "//tensorflow/python:variable_scope",
- ],
-)
-
-tf_py_test(
- name = "lstm1d_test",
- srcs = ["python/lstm1d_test.py"],
- additional_deps = [
- ":ndlstm",
- "//third_party/py/numpy",
- "//tensorflow/python:client_testlib",
- "//tensorflow/python:framework_for_generated_wrappers",
- "//tensorflow/python:gradients",
- "//tensorflow/python:variables",
- ],
-)
-
-tf_py_test(
- name = "lstm2d_test",
- srcs = ["python/lstm2d_test.py"],
- additional_deps = [
- ":ndlstm",
- "//third_party/py/numpy",
- "//tensorflow/python:client_testlib",
- "//tensorflow/python:framework_for_generated_wrappers",
- "//tensorflow/python:framework_test_lib",
- "//tensorflow/python:variables",
- ],
-)
-
-tf_py_test(
- name = "misc_test",
- srcs = ["python/misc_test.py"],
- additional_deps = [
- ":ndlstm",
- "//third_party/py/numpy",
- "//tensorflow/python:client_testlib",
- "//tensorflow/python:framework_for_generated_wrappers",
- "//tensorflow/python:framework_test_lib",
- "//tensorflow/python:variables",
- ],
-)
-
-filegroup(
- name = "all_files",
- srcs = glob(
- ["**/*"],
- exclude = [
- "**/METADATA",
- "**/OWNERS",
- ],
- ),
- visibility = ["//tensorflow:__subpackages__"],
-)
+++ /dev/null
-Library of multidimensional LSTM models and related code.
-
-# 2D LSTM code
-
-The 2D LSTM layers take tensors of the form (batch_size, height, width,
-depth), compatible with convolutional layers, as inputs. The library
-transposes and reshapes these tensors in a way that allows batches of
-images to be processed by LSTMs.
-
-The library currently provides:
-
- - a separable 2D LSTM layer
- - a simple 2D convolutional layer that can be swapped out against 2D LSTM
- - layers to reduce images to sequences and images to final state vectors
- - layers for sequence classification, pixel-wise classification
-
-# Other Dimensions
-
-There is 1D LSTM code in `lstm1d.py`. This code implements 1D LSTM versions
-suitable as a basis for higher dimensional LSTMs. It is intended for constant
-batch size and uses a different layout. Although the code is perfectly fine for
-1D use, you may find other 1D LSTM implementations to be more convenient if you
-are interested in sequence problems.
-
-# Upcoming Changes
-
- - PyramidLSTM
- - support for 3D and 4D
- - optional use of native fused LSTM op
- - easy-to-use command line drivers and examples
- - operators for patch-wise processing
+++ /dev/null
-# Copyright 2017 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.
-# ==============================================================================
-"""Library of multidimensional LSTM models and related code."""
-
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
-from tensorflow.contrib.ndlstm.python import lstm1d
-from tensorflow.contrib.ndlstm.python import lstm2d
+++ /dev/null
-# Copyright 2016 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.
-# ==============================================================================
-"""Init file, giving convenient access to all ndlstm ops."""
-
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
-# pylint: disable=wildcard-import,g-importing-member
-from tensorflow.contrib.ndlstm.python.lstm1d import *
-from tensorflow.contrib.ndlstm.python.lstm2d import *
-from tensorflow.contrib.ndlstm.python.misc import *
-# pylint: enable=wildcard-import
+++ /dev/null
-# Copyright 2016 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.
-# ==============================================================================
-"""LSTM layers for sequences."""
-
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
-from six.moves import xrange # pylint: disable=redefined-builtin
-from tensorflow.contrib.framework.python.ops import variables
-from tensorflow.python.framework import constant_op
-from tensorflow.python.ops import array_ops
-from tensorflow.python.ops import nn_ops
-from tensorflow.python.ops import random_ops
-from tensorflow.python.ops import rnn
-from tensorflow.python.ops import rnn_cell
-from tensorflow.python.ops import variable_scope
-
-
-def _shape(tensor):
- return tensor.get_shape().as_list()
-
-
-def ndlstm_base_unrolled(inputs, noutput, scope=None, reverse=False):
- """Run an LSTM, either forward or backward.
-
- This is a 1D LSTM implementation using unrolling and the TensorFlow
- LSTM op.
-
- Args:
- inputs: input sequence (length, batch_size, ninput)
- noutput: depth of output
- scope: optional scope name
- reverse: run LSTM in reverse
-
- Returns:
- Output sequence (length, batch_size, noutput)
-
- """
- with variable_scope.variable_scope(scope, "SeqLstmUnrolled", [inputs]):
- length, batch_size, _ = _shape(inputs)
- lstm_cell = rnn_cell.BasicLSTMCell(noutput, state_is_tuple=False)
- state = array_ops.zeros([batch_size, lstm_cell.state_size])
- output_u = []
- inputs_u = array_ops.unstack(inputs)
- if reverse:
- inputs_u = list(reversed(inputs_u))
- for i in xrange(length):
- if i > 0:
- variable_scope.get_variable_scope().reuse_variables()
- output, state = lstm_cell(inputs_u[i], state)
- output_u += [output]
- if reverse:
- output_u = list(reversed(output_u))
- outputs = array_ops.stack(output_u)
- return outputs
-
-
-def ndlstm_base_dynamic(inputs, noutput, scope=None, reverse=False):
- """Run an LSTM, either forward or backward.
-
- This is a 1D LSTM implementation using dynamic_rnn and
- the TensorFlow LSTM op.
-
- Args:
- inputs: input sequence (length, batch_size, ninput)
- noutput: depth of output
- scope: optional scope name
- reverse: run LSTM in reverse
-
- Returns:
- Output sequence (length, batch_size, noutput)
- """
- with variable_scope.variable_scope(scope, "SeqLstm", [inputs]):
- lstm_cell = rnn_cell.BasicLSTMCell(noutput)
- if reverse:
- inputs = array_ops.reverse_v2(inputs, [0])
- outputs, _ = rnn.dynamic_rnn(
- lstm_cell, inputs, time_major=True, dtype=inputs.dtype)
- if reverse:
- outputs = array_ops.reverse_v2(outputs, [0])
- return outputs
-
-
-def ndlstm_base(inputs, noutput, scope=None, reverse=False, dynamic=True):
- """Implements a 1D LSTM, either forward or backward.
-
- This is a base case for multidimensional LSTM implementations, which
- tend to be used differently from sequence-to-sequence
- implementations. For general 1D sequence to sequence
- transformations, you may want to consider another implementation
- from TF slim.
-
- Args:
- inputs: input sequence (length, batch_size, ninput)
- noutput: depth of output
- scope: optional scope name
- reverse: run LSTM in reverse
- dynamic: use dynamic_rnn
-
- Returns:
- Output sequence (length, batch_size, noutput)
-
- """
- # TODO(tmb) maybe add option for other LSTM implementations, like
- # slim.rnn.basic_lstm_cell
- if dynamic:
- return ndlstm_base_dynamic(inputs, noutput, scope=scope, reverse=reverse)
- else:
- return ndlstm_base_unrolled(inputs, noutput, scope=scope, reverse=reverse)
-
-
-def sequence_to_final(inputs, noutput, scope=None, name=None, reverse=False):
- """Run an LSTM across all steps and returns only the final state.
-
- Args:
- inputs: (length, batch_size, depth) tensor
- noutput: size of output vector
- scope: optional scope name
- name: optional name for output tensor
- reverse: run in reverse
-
- Returns:
- Batch of size (batch_size, noutput).
- """
- with variable_scope.variable_scope(scope, "SequenceToFinal", [inputs]):
- length, batch_size, _ = _shape(inputs)
- lstm = rnn_cell.BasicLSTMCell(noutput, state_is_tuple=False)
- state = array_ops.zeros([batch_size, lstm.state_size])
- inputs_u = array_ops.unstack(inputs)
- if reverse:
- inputs_u = list(reversed(inputs_u))
- for i in xrange(length):
- if i > 0:
- variable_scope.get_variable_scope().reuse_variables()
- output, state = lstm(inputs_u[i], state)
- outputs = array_ops.reshape(output, [batch_size, noutput], name=name)
- return outputs
-
-
-def sequence_softmax(inputs, noutput, scope=None, name=None, linear_name=None):
- """Run a softmax layer over all the time steps of an input sequence.
-
- Args:
- inputs: (length, batch_size, depth) tensor
- noutput: output depth
- scope: optional scope name
- name: optional name for output tensor
- linear_name: name for linear (pre-softmax) output
-
- Returns:
- A tensor of size (length, batch_size, noutput).
-
- """
- length, _, ninputs = _shape(inputs)
- inputs_u = array_ops.unstack(inputs)
- output_u = []
- with variable_scope.variable_scope(scope, "SequenceSoftmax", [inputs]):
- initial_w = random_ops.truncated_normal([0 + ninputs, noutput], stddev=0.1)
- initial_b = constant_op.constant(0.1, shape=[noutput])
- w = variables.model_variable("weights", initializer=initial_w)
- b = variables.model_variable("biases", initializer=initial_b)
- for i in xrange(length):
- with variable_scope.variable_scope(scope, "SequenceSoftmaxStep",
- [inputs_u[i]]):
- # TODO(tmb) consider using slim.fully_connected(...,
- # activation_fn=tf.nn.softmax)
- linear = nn_ops.xw_plus_b(inputs_u[i], w, b, name=linear_name)
- output = nn_ops.softmax(linear)
- output_u += [output]
- outputs = array_ops.stack(output_u, name=name)
- return outputs
+++ /dev/null
-# Copyright 2016 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.
-# ==============================================================================
-"""Tests for 1D LSTM."""
-
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
-import numpy as np
-
-from tensorflow.contrib.ndlstm.python import lstm1d as lstm1d_lib
-from tensorflow.python.framework import constant_op
-from tensorflow.python.ops import gradient_checker
-from tensorflow.python.ops import gradients_impl
-from tensorflow.python.ops import variables
-from tensorflow.python.platform import test
-
-lstm1d = lstm1d_lib
-
-
-def _rand(*size):
- return np.random.uniform(size=size).astype("f")
-
-
-class Lstm1DTest(test.TestCase):
-
- def testSequenceToSequenceDims(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(17, 1, 5))
- outputs = lstm1d.ndlstm_base(inputs, 8)
- variables.global_variables_initializer().run()
- names = [v.name for v in variables.trainable_variables()]
- self.assertEqual(len(names), 2)
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (17, 1, 8))
-
- def testSequenceToSequenceGradient(self):
- with self.test_session():
- size = (17, 1, 15)
- output_size = (17, 1, 8)
- inputs = constant_op.constant(_rand(*size))
- outputs = lstm1d.ndlstm_base(inputs, 8, dynamic=False)
- variables.global_variables_initializer().run()
- gradients = gradients_impl.gradients(outputs, inputs)
- if 1: # pylint: disable=using-constant-test
- gradients = gradients_impl.gradients(outputs, inputs)[0].eval()
- self.assertEqual(gradients.shape, size)
- else:
- # TODO(tmb) tf.test.compute_gradient error is currently broken
- # with dynamic_rnn. Enable this test case eventually.
- err = gradient_checker.compute_gradient_error(
- inputs, size, outputs, output_size, delta=1e-4)
- self.assert_(not np.isnan(err))
- self.assert_(err < 0.1)
-
- def testSequenceToSequenceGradientReverse(self):
- with self.test_session():
- size = (17, 1, 15)
- output_size = (17, 1, 8)
- inputs = constant_op.constant(_rand(*size))
- outputs = lstm1d.ndlstm_base(inputs, 8, reverse=1, dynamic=False)
- variables.global_variables_initializer().run()
- if 1: # pylint: disable=using-constant-test
- gradients = gradients_impl.gradients(outputs, inputs)[0].eval()
- self.assertEqual(gradients.shape, size)
- else:
- # TODO(tmb) tf.test.compute_gradient error is currently broken
- # with dynamic_rnn. Enable this test case eventually.
- err = gradient_checker.compute_gradient_error(
- inputs, size, outputs, output_size, delta=1e-4)
- self.assert_(not np.isnan(err))
- self.assert_(err < 0.1)
-
- def testSequenceToFinalDims(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(17, 6, 5))
- outputs = lstm1d.sequence_to_final(inputs, 8)
- variables.global_variables_initializer().run()
- names = [v.name for v in variables.trainable_variables()]
- self.assertEqual(len(names), 2)
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (6, 8))
-
- def testSequenceSoftmaxDims(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(17, 1, 5))
- outputs = lstm1d.sequence_softmax(inputs, 8)
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (17, 1, 8))
-
-
-if __name__ == "__main__":
- test.main()
+++ /dev/null
-# Copyright 2016 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.
-# ==============================================================================
-"""A small library of functions dealing with LSTMs applied to images.
-
-Tensors in this library generally have the shape (num_images, height, width,
-depth).
-"""
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
-from tensorflow.contrib.ndlstm.python import lstm1d
-from tensorflow.python.ops import array_ops
-from tensorflow.python.ops import variable_scope
-
-
-def _shape(tensor):
- """Get the shape of a tensor as an int list."""
- return tensor.get_shape().as_list()
-
-
-def images_to_sequence(tensor):
- """Convert a batch of images into a batch of sequences.
-
- Args:
- tensor: a (num_images, height, width, depth) tensor
-
- Returns:
- (width, num_images*height, depth) sequence tensor
- """
-
- num_image_batches, height, width, depth = _shape(tensor)
- transposed = array_ops.transpose(tensor, [2, 0, 1, 3])
- return array_ops.reshape(transposed,
- [width, num_image_batches * height, depth])
-
-
-def sequence_to_images(tensor, num_image_batches):
- """Convert a batch of sequences into a batch of images.
-
- Args:
- tensor: (num_steps, num_batches, depth) sequence tensor
- num_image_batches: the number of image batches
-
- Returns:
- (num_images, height, width, depth) tensor
- """
-
- width, num_batches, depth = _shape(tensor)
- height = num_batches // num_image_batches
- reshaped = array_ops.reshape(tensor,
- [width, num_image_batches, height, depth])
- return array_ops.transpose(reshaped, [1, 2, 0, 3])
-
-
-def horizontal_lstm(images, num_filters_out, scope=None):
- """Run an LSTM bidirectionally over all the rows of each image.
-
- Args:
- images: (num_images, height, width, depth) tensor
- num_filters_out: output depth
- scope: optional scope name
-
- Returns:
- (num_images, height, width, num_filters_out) tensor, where
- num_steps is width and new num_batches is num_image_batches * height
- """
- with variable_scope.variable_scope(scope, "HorizontalLstm", [images]):
- batch_size, _, _, _ = _shape(images)
- sequence = images_to_sequence(images)
- with variable_scope.variable_scope("lr"):
- hidden_sequence_lr = lstm1d.ndlstm_base(sequence, num_filters_out // 2)
- with variable_scope.variable_scope("rl"):
- hidden_sequence_rl = (lstm1d.ndlstm_base(
- sequence, num_filters_out - num_filters_out // 2, reverse=1))
- output_sequence = array_ops.concat([hidden_sequence_lr, hidden_sequence_rl],
- 2)
- output = sequence_to_images(output_sequence, batch_size)
- return output
-
-
-def get_blocks(images, kernel_size):
- """Split images in blocks
-
- Args:
- images: (num_images, height, width, depth) tensor
- kernel_size: A list of length 2 holding the [kernel_height, kernel_width] of
- of the pooling. Can be an int if both values are the same.
-
- Returns:
- (num_images, height/kernel_height, width/kernel_width,
- depth*kernel_height*kernel_width) tensor
- """
- with variable_scope.variable_scope("image_blocks"):
- batch_size, height, width, chanels = _shape(images)
-
- if height % kernel_size[0] != 0:
- offset = array_ops.zeros([batch_size,
- kernel_size[0] - (height % kernel_size[0]),
- width,
- chanels])
- images = array_ops.concat([images, offset], 1)
- batch_size, height, width, chanels = _shape(images)
- if width % kernel_size[1] != 0:
- offset = array_ops.zeros([batch_size,
- height,
- kernel_size[1] - (width % kernel_size[1]),
- chanels])
- images = array_ops.concat([images, offset], 2)
- batch_size, height, width, chanels = _shape(images)
-
- h, w = int(height / kernel_size[0]), int(width / kernel_size[1])
- features = kernel_size[1] * kernel_size[0] * chanels
-
- lines = array_ops.split(images, h, axis=1)
- line_blocks = []
- for line in lines:
- line = array_ops.transpose(line, [0, 2, 3, 1])
- line = array_ops.reshape(line, [batch_size, w, features])
- line_blocks.append(line)
-
- return array_ops.stack(line_blocks, axis=1)
-
-
-def separable_lstm(images, num_filters_out,
- kernel_size=None, nhidden=None, scope=None):
- """Run bidirectional LSTMs first horizontally then vertically.
-
- Args:
- images: (num_images, height, width, depth) tensor
- num_filters_out: output layer depth
- kernel_size: A list of length 2 holding the [kernel_height, kernel_width] of
- of the pooling. Can be an int if both values are the same. Set to None for
- not using blocks
- nhidden: hidden layer depth
- scope: optional scope name
-
- Returns:
- (num_images, height/kernel_height, width/kernel_width,
- num_filters_out) tensor
- """
- with variable_scope.variable_scope(scope, "SeparableLstm", [images]):
- if nhidden is None:
- nhidden = num_filters_out
- if kernel_size is not None:
- images = get_blocks(images, kernel_size)
- hidden = horizontal_lstm(images, nhidden)
- with variable_scope.variable_scope("vertical"):
- transposed = array_ops.transpose(hidden, [0, 2, 1, 3])
- output_transposed = horizontal_lstm(transposed, num_filters_out)
- output = array_ops.transpose(output_transposed, [0, 2, 1, 3])
- return output
-
-
-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 variable_scope.variable_scope(scope, "ReduceToSequence", [images]):
- 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])
- reduced = lstm1d.sequence_to_final(reshaped, num_filters_out)
- output = array_ops.reshape(reduced, [batch_size, width, num_filters_out])
- return output
-
-
-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
+++ /dev/null
-# Copyright 2016 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.
-# ==============================================================================
-"""Tests for 2D LSTMs."""
-
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
-import numpy as np
-
-from tensorflow.contrib.ndlstm.python import lstm2d as lstm2d_lib
-from tensorflow.python.framework import constant_op
-from tensorflow.python.framework import test_util
-from tensorflow.python.ops import variables
-from tensorflow.python.platform import test
-
-lstm2d = lstm2d_lib
-
-
-def _rand(*size):
- return np.random.uniform(size=size).astype("f")
-
-
-class Lstm2DTest(test_util.TensorFlowTestCase):
-
- def testImagesToSequenceDims(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(2, 7, 11, 5))
- outputs = lstm2d.images_to_sequence(inputs)
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (11, 14, 5))
-
- def testSequenceToImagesDims(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(11, 14, 5))
- outputs = lstm2d.sequence_to_images(inputs, 2)
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (2, 7, 11, 5))
-
- def testImagesAndSequenceDims(self):
- with self.test_session():
- size = (2, 7, 11, 5)
- inputs = constant_op.constant(_rand(*size))
- sequence = lstm2d.images_to_sequence(inputs)
- outputs = lstm2d.sequence_to_images(sequence, size[0])
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), size)
-
- def testSeparableLstmDims(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(2, 7, 11, 5))
- outputs = lstm2d.separable_lstm(inputs, 8)
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (2, 7, 11, 8))
-
- def testSeparableLstmDimsBlocks(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(2, 7, 11, 5))
- outputs = lstm2d.separable_lstm(inputs, 8, kernel_size=[2, 2])
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (2, 4, 6, 8))
-
- def testReduceToSequenceDims(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(2, 7, 11, 5))
- outputs = lstm2d.reduce_to_sequence(inputs, 8)
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (2, 11, 8))
-
- def testReduceToFinalDims(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(2, 7, 11, 5))
- outputs = lstm2d.reduce_to_final(inputs, 8, 12)
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (2, 8))
-
-
-if __name__ == "__main__":
- test.main()
+++ /dev/null
-# Copyright 2016 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.
-# ==============================================================================
-"""Miscellaneous functions useful for nD-LSTM models.
-
-Some of these functions duplicate functionality in tfslim with
-slightly different interfaces.
-
-Tensors in this library generally have the shape (num_images, height, width,
-depth).
-"""
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
-from tensorflow.contrib.layers.python.layers import layers
-from tensorflow.python.framework import ops
-from tensorflow.python.ops import array_ops
-from tensorflow.python.ops import math_ops
-from tensorflow.python.ops import sparse_ops
-
-
-def _shape(tensor):
- """Get the shape of a tensor as an int list."""
- return tensor.get_shape().as_list()
-
-
-def pixels_as_vector(images, scope=None):
- """Reduce images to vectors by combining all pixels."""
- with ops.name_scope(scope, "PixelsAsVector", [images]):
- batch_size, height, width, depth = _shape(images)
- return array_ops.reshape(images, [batch_size, height * width * depth])
-
-
-def pool_as_vector(images, scope=None):
- """Reduce images to vectors by averaging all pixels."""
- with ops.name_scope(scope, "PoolAsVector", [images]):
- return math_ops.reduce_mean(images, [1, 2])
-
-
-def one_hot_planes(labels, num_classes, scope=None):
- """Compute 1-hot encodings for planes.
-
- Given a label, this computes a label image that contains
- 1 at all pixels in the plane corresponding to the target
- class and 0 in all other planes.
-
- Args:
- labels: (batch_size,) tensor
- num_classes: number of classes
- scope: optional scope name
-
- Returns:
- Tensor of shape (batch_size, 1, 1, num_classes) with a 1-hot encoding.
- """
- with ops.name_scope(scope, "OneHotPlanes", [labels]):
- batch_size, = _shape(labels)
- batched = layers.one_hot_encoding(labels, num_classes)
- return array_ops.reshape(batched, [batch_size, 1, 1, num_classes])
-
-
-def one_hot_mask(labels, num_classes, scope=None):
- """Compute 1-hot encodings for masks.
-
- Given a label image, this computes the one hot encoding at
- each pixel.
-
- Args:
- labels: (batch_size, width, height, 1) tensor containing labels.
- num_classes: number of classes
- scope: optional scope name
-
- Returns:
- Tensor of shape (batch_size, width, height, num_classes) with
- a 1-hot encoding.
- """
- with ops.name_scope(scope, "OneHotMask", [labels]):
- height, width, depth = _shape(labels)
- assert depth == 1
- sparse_labels = math_ops.to_int32(array_ops.reshape(labels, [-1, 1]))
- sparse_size, _ = _shape(sparse_labels)
- indices = array_ops.reshape(math_ops.range(0, sparse_size, 1), [-1, 1])
- concated = array_ops.concat([indices, sparse_labels], 1)
- dense_result = sparse_ops.sparse_to_dense(concated,
- [sparse_size, num_classes], 1.0,
- 0.0)
- result = array_ops.reshape(dense_result, [height, width, num_classes])
- return result
+++ /dev/null
-# Copyright 2016 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.
-# ==============================================================================
-"""Miscellaneous tests."""
-
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
-import numpy as np
-
-from tensorflow.contrib.ndlstm.python import misc as misc_lib
-from tensorflow.python.framework import constant_op
-from tensorflow.python.framework import test_util
-from tensorflow.python.ops import variables
-from tensorflow.python.platform import test
-
-misc = misc_lib
-
-
-def _rand(*size):
- return np.random.uniform(size=size).astype("f")
-
-
-class LstmMiscTest(test_util.TensorFlowTestCase):
-
- def testPixelsAsVectorDims(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(2, 7, 11, 5))
- outputs = misc.pixels_as_vector(inputs)
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (2, 7 * 11 * 5))
-
- def testPoolAsVectorDims(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(2, 7, 11, 5))
- outputs = misc.pool_as_vector(inputs)
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (2, 5))
-
- def testOneHotPlanes(self):
- with self.test_session():
- inputs = constant_op.constant([0, 1, 3])
- outputs = misc.one_hot_planes(inputs, 4)
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (3, 1, 1, 4))
- target = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
- self.assertAllClose(result.reshape(-1), target.reshape(-1))
-
- def testOneHotMask(self):
- with self.test_session():
- data = np.array([[0, 1, 2], [2, 0, 1]]).reshape(2, 3, 1)
- inputs = constant_op.constant(data)
- outputs = misc.one_hot_mask(inputs, 3)
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (2, 3, 3))
- target = np.array([[[1, 0, 0], [0, 1, 0]], [[0, 1, 0], [0, 0, 1]],
- [[0, 0, 1], [1, 0, 0]]]).transpose(1, 2, 0)
- self.assertAllClose(result.reshape(-1), target.reshape(-1))
-
-
-if __name__ == "__main__":
- test.main()
srcs_version = "PY2AND3",
deps = [
"//tensorflow/contrib/layers:layers_py",
- "//tensorflow/contrib/ndlstm",
"//tensorflow/python:array_ops",
"//tensorflow/python:framework_for_generated_wrappers",
"//tensorflow/python:logging_ops",
- `Squeeze` = tf.squeeze
- `Expand` = tf.expand_dims
-Multidimensional LSTM:
-
-These are intended as alternatives to 2D convolutions. For sequence models,
-there will be other modeling primitives.
-
- - `Lstm2` = Fun(lstm2d.separable_lstm) # 2D-to-2D
- - `Lstm2to1` = Fun(lstm2d.reduce_to_sequence) # 2D-to-1D
- - `Lstm2to0` = Fun(lstm2d.reduce_to_final) # 2D-to-vector
- - `Clstm2(n, m)` is a `Cl(n, [3,3])` followed by `Lstm2(m)`
- - `Dws(n)` is a depthwise convolution `Cs(n, [1, 1])`
-
Other:
- `Id` = identity
from __future__ import print_function
from tensorflow.contrib.layers.python.layers import layers
-from tensorflow.contrib.ndlstm.python import lstm1d
-from tensorflow.contrib.ndlstm.python import lstm2d
from tensorflow.contrib.specs.python import specs_lib
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import logging_ops
Tanh = Fun(math_ops.tanh)
Smax = Fun(nn_ops.softmax)
-# 2D LSTM
-
-Lstm2 = Fun(lstm2d.separable_lstm)
-Lstm2to1 = Fun(lstm2d.reduce_to_sequence) # 2D to 1D
-Lstm2to0 = Fun(lstm2d.reduce_to_final) # 2D to depth-only
-
-
-def Clstm2(n, *args, **kw):
- """2D LSTM with 3x3 pre-convolution."""
- return Cl(n, [3, 3]) | Lstm2(*args, **kw)
-
def Dws(n):
"""Depth-wise convolution + sigmoid (used after LSTM)."""
"""Depth-wise convolution + softmax (used after LSTM)."""
return Cm(n, [1, 1])
-
-# 1D LSTM
-
-Lstm1 = Fun(lstm1d.ndlstm_base)
-Lstm1to0 = Fun(lstm1d.sequence_to_final) # 1D to depth-only
-Ssm = Fun(lstm1d.sequence_softmax)
-
# Sharing of Variables
self.assertEqual(tuple(result.shape), (10, 20))
self.assertEqual(summaries.tf_spec_structure(spec, inputs), "_ sig sig")
- def testLstm2(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(1, 64, 64, 5))
- spec = "net = Lstm2(15)"
- outputs = specs.create_net(spec, inputs)
- self.assertEqual(outputs.get_shape().as_list(), [1, 64, 64, 15])
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (1, 64, 64, 15))
-
- def testLstm2to1(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(1, 64, 64, 5))
- spec = "net = Lstm2to1(15)"
- outputs = specs.create_net(spec, inputs)
- self.assertEqual(outputs.get_shape().as_list(), [1, 64, 15])
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (1, 64, 15))
-
- def testLstm2to0(self):
- with self.test_session():
- inputs = constant_op.constant(_rand(1, 64, 64, 5))
- spec = "net = Lstm2to0(15)"
- outputs = specs.create_net(spec, inputs)
- self.assertEqual(outputs.get_shape().as_list(), [1, 15])
- variables.global_variables_initializer().run()
- result = outputs.eval()
- self.assertEqual(tuple(result.shape), (1, 15))
-
def testKeywordRestriction(self):
with self.test_session():
inputs = constant_op.constant(_rand(10, 20))
'factorization',
'grid_rnn',
'labeled_tensor',
- 'ndlstm',
'quantization',
'session_bundle',
'slim',
# Block contrib.keras to de-clutter the docs
'keras',
'labeled_tensor',
- 'ndlstm',
'quantization',
'session_bundle',
'slim',
"//tensorflow/python/eager:eager_pip",
"//tensorflow/contrib/summary:summary_test_util",
# These targets don't build on Windows yet. Exclude them for now.
- # "//tensorflow/contrib/ndlstm",
# "//tensorflow/contrib/slim",
# "//tensorflow/contrib/slim/python/slim/nets:nets_pip",
# "//tensorflow/contrib/specs",
"//tensorflow/contrib/lite/toco:toco",
"//tensorflow/contrib/lite/toco/python:toco_wrapper",
"//tensorflow/contrib/lite/toco/python:toco_from_protos",
- "//tensorflow/contrib/ndlstm:ndlstm",
"//tensorflow/contrib/nn:nn_py",
"//tensorflow/contrib/predictor:predictor_pip",
"//tensorflow/contrib/py2tf:py2tf",