class Axis(object): """Size and label information for an axis. Axis contains either a tf.Dimension indicating the size of an axis, or a tuple of tick labels for the axis. If tick labels are provided, they must be unique. """ @tc.accepts(object, string_types, AxisValue) def __init__(self, name, value): """Construct an Axis. Args: name: Name of the axis. value: Either None, an int or tf.Dimension giving the size of the axis, or a sequence that is not a string additionally providing coordinate (tick) labels. Raises: ValueError: If the user provides labels with duplicate values. """ if isinstance(value, tensor_shape.Dimension): dimension = value labels = None elif isinstance(value, int) or value is None: dimension = tensor_shape.Dimension(value) labels = None else: dimension = tensor_shape.Dimension(len(value)) labels = tuple(value) if dimension.value == 0: # Treat a zero-length axis as if it has labels. labels = () if labels is not None: index = dict(zip(labels, range(len(labels)))) if len(index) != len(labels): raise ValueError('Tick labels must be unique, but got {}' .format(labels)) else: index = None self._name = name # type: string_types self._dimension = dimension # type: tensor_shape.Dimension self._labels = labels # type: Optional[tuple] self._index = index # type: Optional[Dict[Any, int]] @property @tc.returns(string_types) def name(self): return self._name @tc.returns(string_types) def __repr__(self): # Axis('x', Dimension(2)) # TODO(shoyer): make very long reprs more succint? return "%s('%s', %r)" % (type(self).__name__, self.name, self.value) @tc.returns(bool) def __eq__(self, other): return (isinstance(other, Axis) and self.name == other.name and self.size == other.size and self.labels == other.labels) def __hash__(self): return hash((self.name, self.size, self.labels)) @tc.returns(bool) def __ne__(self, other): return not self == other @tc.returns(int) def __len__(self): size = self.size if size is None: raise ValueError('axis %r has unknown length' % self.name) return size @property @tc.returns(tc.Optional(tensor_shape.Dimension)) def dimension(self): return self._dimension @property @tc.returns(tc.Optional(int)) def size(self): return self._dimension.value @property @tc.returns(tc.Union(tuple, tensor_shape.Dimension)) def value(self): """Returns the tf.Dimension or tuple specifying axis ticks.""" if self.labels is None: return self.dimension else: return self.labels @property @tc.returns(tc.Optional(tuple)) def labels(self): """Returns the tuple containing coordinate labels, else None.""" return self._labels def index(self, value): """Returns the integer position of the given tick label.""" if self._index is None: raise ValueError('Axis does not have tick labels') return self._index[value]
class LabeledTensor(object): """A tensor with annotated axes. It has the following invariants: 1) The dimensionality of the tensor is equal to the number of elements in axes. 2) The number of coordinate values in the ith dimension is equal to the size of the tensor in the ith dimension. Attributes: tensor: tf.Tensor containing the data. axes: lt.Axes containing axis names and coordinate labels. """ @tc.accepts(object, ops.Tensor, tc.Union(Axes, tc.Collection(tc.Union(string_types, AxisLike)))) def __init__(self, tensor, axes): """Construct a LabeledTenor. Args: tensor: The underlying tensor containing the data. axes: An Axes object, or a collection of strings, Axis objects or tuples of (name, value) pairs indicating the axes. Raises: ValueError: If the provided axes do not satisfy the class invariants. """ self._tensor = tensor shape = tensor.get_shape() if isinstance(axes, Axes): unvalidated_axes = axes else: mutable_axes = [] for position, axis_like in enumerate(axes): if isinstance(axis_like, string_types): # The coordinates for this axes are unlabeled. # Infer the size of the axis. value = shape[position] axis_like = (axis_like, value) mutable_axes.append(axis_like) # Construct the Axis object, which will additionally validate the contents # of the object. unvalidated_axes = Axes(mutable_axes) # Check our invariants. # First, the rank of the tensor must be equal to the number of axes. if len(shape) != len(unvalidated_axes): raise ValueError('Tensor rank was not equal to the number of axes: %r, %r' % (shape, unvalidated_axes)) # Second, the size of each tensor dimension must match the size of the # corresponding indices. for (d, axis) in zip(shape, unvalidated_axes.values()): if d != axis.size: raise ValueError( 'Provided axis size %d does not match tensor dimension size %d' % (axis.size, d)) self._axes = unvalidated_axes def __repr__(self): # <LabeledTensor 'foo' shape=(2, 3, 4) dtype=float32 # axes=[('x', Dimension(2)), # ('y', ('a', 'b', 'c'), # ('z', Dimension(4))]> axes = ["('%s', %r)" % (v.name, v.value) for v in self.axes.values()] axes_repr = (',\n' + ' ' * len(' axes=[')).join(axes) return ("<%s '%s' shape=%s dtype=%s\n axes=[%s]>" % (type(self).__name__, self.tensor.name, self.tensor.get_shape(), self.tensor.dtype.name, axes_repr)) @property def tensor(self): return self._tensor def _as_graph_element(self): """Support tf.Graph.as_graph_element on LabeledTensor objects. This allows operations such as tf.name_scope to take labeled tensors. Returns: self.tensor """ return self.tensor @property def axes(self): return self._axes # properties/methods directly borrowed from tf.Tensor: @property def dtype(self): return self._tensor.dtype @property def name(self): return self._tensor.name def get_shape(self): """Returns the TensorShape that represents the shape of this tensor. See tf.Tensor.get_shape(). Returns: A TensorShape representing the shape of this tensor. """ return self._tensor.get_shape() # TODO(shoyer): consider how/if to implement .eval(). Maybe it should return # an xarray.DataArray? def __getitem__(self, key): # This should work exactly like tf.Tensor.__getitem__, except it preserves # labels. if not isinstance(key, tuple): key = (key,) if len(key) != len(self.axes): raise ValueError('indexer %r must have the same length as the Tensor ' 'rank (%r)' % (key, len(self.axes))) selection = {a: k for a, k in zip(self.axes.keys(), key)} return slice_function(self, selection) # special methods for overloading arithmetic operations: def __abs__(self): return abs_function(self) def __neg__(self): return neg(self) def __pos__(self): return self def __add__(self, other): return add(self, other) def __radd__(self, other): return add(other, self) def __sub__(self, other): return sub(self, other) def __rsub__(self, other): return sub(other, self) def __mul__(self, other): return mul(self, other) def __rmul__(self, other): return mul(other, self) def __truediv__(self, other): return div(self, other) __div__ = __truediv__ def __rtruediv__(self, other): return div(other, self) __rdiv__ = __rtruediv__ def __mod__(self, other): return mod(self, other) def __rmod__(self, other): return mod(other, self) def __pow__(self, other): return pow_function(self, other) def __rpow__(self, other): return pow_function(other, self) # logical operations: def __invert__(self): return logical_not(self) def __and__(self, other): return logical_and(self, other) def __or__(self, other): return logical_or(self, other) def __xor__(self, other): return logical_xor(self, other) # boolean operations: def __lt__(self, other): return less(self, other) def __le__(self, other): return less_equal(self, other) def __gt__(self, other): return greater(self, other) def __ge__(self, other): return greater_equal(self, other) def __eq__(self, other): # for consistency with tf.Tensor if not isinstance(other, LabeledTensor): return False return self.tensor == other.tensor and self.axes == other.axes def __ne__(self, other): return not self == other def __hash__(self): return hash((self.tensor, self.axes))
from six import text_type from six.moves import range # pylint: disable=redefined-builtin from tensorflow.contrib.labeled_tensor.python.ops import _typecheck as tc from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops from tensorflow.python.framework import tensor_shape from tensorflow.python.ops import array_ops from tensorflow.python.ops import math_ops # pylint: disable=invalid-name # Types coercible to Axis.labels # We use this instead of collections.Sequence to exclude strings. LabelsLike = tc.Union(np.ndarray, range, list, tuple) # Types coercible to a tf.Dimension DimensionLike = tc.Optional(tc.Union(tensor_shape.Dimension, int)) # Types usable for axis values AxisValue = tc.Union(LabelsLike, DimensionLike) # Valid scalar values for TensorFlow Scalar = tc.Union(numbers.Number, bool, binary_type, text_type) # pylint: enable=invalid-name class Axis(object): """Size and label information for an axis.
@tc.returns(core.LabeledTensor) @tc.accepts(core.LabeledTensor, ops.Tensor, core.Axis, tc.Optional(string_types)) def _gather_1d_on_axis(labeled_tensor, indexer, axis, name=None): with ops.name_scope(name, 'lt_take', [labeled_tensor]) as scope: temp_axes = core.Axes( [axis] + list(labeled_tensor.axes.remove(axis.name).values())) transposed = core.transpose(labeled_tensor, temp_axes.keys()) indexed = core.LabeledTensor( array_ops.gather(transposed.tensor, indexer), temp_axes) return core.transpose(indexed, labeled_tensor.axes.keys(), name=scope) @tc.returns(core.LabeledTensor) @tc.accepts(core.LabeledTensorLike, tc.Mapping(string_types, tc.Union(slice, collections.Hashable, list)), tc.Optional(string_types)) def select(labeled_tensor, selection, name=None): """Slice out a subset of the tensor. Args: labeled_tensor: The input tensor. selection: A dictionary mapping an axis name to a scalar, slice or list of values to select. Currently supports two types of selections: (a) Any number of scalar and/or slice selections. (b) Exactly one list selection, without any scalars or slices. name: Optional op name. Returns: The selection as a `LabeledTensor`.
""" serialized = core.convert_to_labeled_tensor(serialized) unlabeled_features = _labeled_to_unlabeled_features(features) unlabeled_parsed = parsing_ops.parse_single_example( serialized.tensor, unlabeled_features, name, example_names) parsed = {} for name, parsed_feature in unlabeled_parsed.items(): parsed[name] = core.LabeledTensor(parsed_feature, features[name].axes) return parsed @tc.returns(core.LabeledTensor) @tc.accepts(dtypes.DType, tc.Collection(tc.Union(string_types, core.AxisLike)), tc.Optional(string_types)) def placeholder(dtype, axes, name=None): """Create a placeholder for a labeled tensor. For example: lt.placeholder(tf.float32, ['batch', ('channel', ['r', 'g', 'b'])]) See tf.compat.v1.placeholder for more details. Args: dtype: The type of elements in the tensor to be fed. axes: sequence of strings (denoting axes of unknown size) and/or objects convertable to lt.Axis to label the result. name: Optional op name.
class ReshapeCoder(object): """Utility class for mapping to and from another shape. For example, say you have a function `crop_center` which expects a LabeledTensor with axes named ['batch', 'row', 'column', 'depth'], and you have a LabeledTensor `masked_image_lt` with axes ['batch', 'row', 'column', 'channel', 'mask']. To call `crop_center` with `masked_image_lt` you'd normally have to write: >>> reshape_lt = lt.reshape(masked_image_lt, ['channel', 'mask'], ['depth']) >>> crop_lt = crop_center(reshape_lt) >>> result_lt = lt.reshape(crop_lt, ['depth'], ... [masked_image_lt.axes['channel'], masked_image_lt.axes['mask']]) ReshapeCoder takes care of this renaming logic for you, allowing you to instead write: >>> rc = ReshapeCoder(['channel', 'mask'], ['depth']) >>> result_lt = rc.decode(crop_center(rc.encode(masked_image_lt))) Here, `decode` restores the original axes 'channel' and 'mask', so `crop_center` must not have modified the size of the 'depth' axis. """ @tc.accepts(object, tc.Collection(str), tc.Collection(tc.Union(str, core.AxisLike)), tc.Optional(str)) def __init__(self, existing_axis_names, new_axes, name=None): self._name = name self._existing_axis_names = existing_axis_names self._new_axes = new_axes self._existing_axes = None @tc.returns(core.LabeledTensor) @tc.accepts(object, core.LabeledTensorLike) def encode(self, labeled_tensor): """Reshape the input to the target shape. If called several times, the axes named in existing_axis_names must be identical. Args: labeled_tensor: The input tensor. Returns: The input reshaped to the target shape. Raises: ValueError: If the axes in existing_axis_names don't match the axes of a tensor in a previous invocation of this method. """ with tf_ops.name_scope(self._name, 'lt_reshape_encode', [labeled_tensor]) as scope: labeled_tensor = core.convert_to_labeled_tensor(labeled_tensor) reshape_lt = ops.reshape(labeled_tensor, self._existing_axis_names, self._new_axes, name=scope) axes = [labeled_tensor.axes[n] for n in self._existing_axis_names] if self._existing_axes is not None and self._existing_axes != axes: raise ValueError( 'input axes %r do not match axes from previous method call %r' % (axes, self._existing_axes)) else: self._existing_axes = axes return reshape_lt @tc.returns(core.LabeledTensor) @tc.accepts(object, core.LabeledTensorLike) def decode(self, labeled_tensor): """Reshape the input to the original shape. This is the inverse of encode. Encode must have been called at least once prior to this method being called. Args: labeled_tensor: The input tensor. Returns: The input reshaped to the original shape. Raises: ValueError: If this method was called before encode was called. """ if self._existing_axes is None: raise ValueError('decode called before encode') with tf_ops.name_scope(self._name, 'lt_reshape_decode', [labeled_tensor]) as scope: labeled_tensor = core.convert_to_labeled_tensor(labeled_tensor) new_axis_names = [ axis if isinstance(axis, string_types) else core.as_axis(axis).name for axis in self._new_axes ] return ops.reshape(labeled_tensor, new_axis_names, self._existing_axes, name=scope)