ó
V¾÷Xc           @` sÃ   d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l	 m
 Z
 d d l	 m Z d d	 l	 m Z d
 „  Z d d „ Z d d „ Z d d „ Z d d „ Z d S(   sè   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).
i    (   t   absolute_import(   t   division(   t   print_function(   t   layers(   t   ops(   t	   array_ops(   t   math_ops(   t
   sparse_opsc         C` s   |  j  ƒ  j ƒ  S(   s)   Get the shape of a tensor as an int list.(   t	   get_shapet   as_list(   t   tensor(    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/misc.pyt   _shape"   s    c      	   C` sY   t  j | d |  g ƒ ; t |  ƒ \ } } } } t j |  | | | | g ƒ SWd QXd S(   s1   Reduce images to vectors by combining all pixels.t   PixelsAsVectorN(   R   t
   name_scopeR   R   t   reshape(   t   imagest   scopet
   batch_sizet   heightt   widtht   depth(    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/misc.pyt   pixels_as_vector'   s    c         C` s9   t  j | d |  g ƒ  t j |  d d g ƒ SWd QXd S(   s1   Reduce images to vectors by averaging all pixels.t   PoolAsVectori   i   N(   R   R   R   t   reduce_mean(   R   R   (    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/misc.pyt   pool_as_vector.   s    c      
   C` s`   t  j | d |  g ƒ B t |  ƒ \ } t j |  | ƒ } t j | | d d | g ƒ SWd QXd S(   sƒ  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.
  t   OneHotPlanesi   N(   R   R   R   R   t   one_hot_encodingR   R   (   t   labelst   num_classesR   R   t   batched(    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/misc.pyt   one_hot_planes4   s    c      	   C` sù   t  j | d |  g ƒ Û t |  ƒ \ } } } | d k s@ t ‚ t j t j |  d d g ƒ ƒ } t | ƒ \ } } t j t j d | d ƒ d d g ƒ }	 t j	 |	 | g d ƒ }
 t
 j |
 | | g d d ƒ } t j | | | | g ƒ } | SWd QXd S(   se  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.
  t
   OneHotMaski   iÿÿÿÿi    g      ð?g        N(   R   R   R   t   AssertionErrorR   t   to_int32R   R   t   ranget   concatR   t   sparse_to_dense(   R   R   R   R   R   R   t   sparse_labelst   sparse_sizet   _t   indicest   concatedt   dense_resultt   result(    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/misc.pyt   one_hot_maskI   s    !'		N(   t   __doc__t
   __future__R    R   R   t'   tensorflow.contrib.layers.python.layersR   t   tensorflow.python.frameworkR   t   tensorflow.python.opsR   R   R   R   t   NoneR   R   R   R,   (    (    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/misc.pyt   <module>   s   	