σ
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   Z
 d	   Z d
   Z d d  Z d d d  Z d d  Z d d d  Z d S(   s   A small library of functions dealing with LSTMs applied to images.

Tensors in this library generally have the shape (num_images, height, width,
depth).
i    (   t   absolute_import(   t   division(   t   print_function(   t   lstm1d(   t	   array_ops(   t   variable_scopec         C` s   |  j    j   S(   s)   Get the shape of a tensor as an int list.(   t	   get_shapet   as_list(   t   tensor(    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm2d.pyt   _shape   s    c         C` sS   t  |   \ } } } } t j |  d d d d g  } t j | | | | | g  S(   sΊ   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
  i   i    i   i   (   R	   R   t	   transposet   reshape(   R   t   num_image_batchest   heightt   widtht   deptht
   transposed(    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm2d.pyt   images_to_sequence"   s    
	c         C` sY   t  |   \ } } } | | } t j |  | | | | g  } t j | d d d d g  S(   sι   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
  i   i   i    i   (   R	   R   R   R
   (   R   R   R   t   num_batchesR   R   t   reshaped(    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm2d.pyt   sequence_to_images2   s
    
	c   
      C` sΤ   t  j  | d |  g  Ά t |   \ } } } } t |   } t  j  d   t j | | d  } Wd QXt  j  d  % t j | | | d d d } Wd QXt j | | g d  } t | |  }	 |	 SWd QXd S(   sS  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
  t   HorizontalLstmt   lri   Nt   rlt   reversei   (   R   R	   R   R   t   ndlstm_baseR   t   concatR   (
   t   imagest   num_filters_outt   scopet
   batch_sizet   _t   sequencet   hidden_sequence_lrt   hidden_sequence_rlt   output_sequencet   output(    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm2d.pyt   horizontal_lstmD   s     	c         C` s¬   t  j  | d |  g   | d k r. | } n  t |  |  } t  j  d  2 t j | d d d d g  } t | |  } Wd QXt j | d d d d g  } | SWd QXd S(   s(  Run bidirectional LSTMs first horizontally then vertically.

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

  Returns:
    (num_images, height, width, num_filters_out) tensor
  t   SeparableLstmt   verticali    i   i   i   N(   R   t   NoneR%   R   R
   (   R   R   t   nhiddenR   t   hiddenR   t   output_transposedR$   (    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm2d.pyt   separable_lstm^   s    	c      
   C` s©   t  j  | d |  g   t |   \ } } } } t j |  d d d d g  } t j | | | | | g  } t j | |  }	 t j |	 | | | g  }
 |
 SWd QXd S(   s  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.
  t   ReduceToSequencei   i    i   i   N(   R   R	   R   R
   R   R   t   sequence_to_final(   R   R   R   R   R   R   R   R   R   t   reducedR$   (    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm2d.pyt   reduce_to_sequenceu   s    	c         C` s  t  j  | d |  g  π | p" | } t |   \ } } } } t j |  d d d d g  } t j | | | | | g  }	 t  j  d  M t j |	 |  }
 t j |
 | | | g  } t j | d d d g  } Wd QXt  j  d   t j | |  } Wd QX| SWd QXd S(	   s4  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.
  t   ReduceToFinali   i    i   i   t   reduce1Nt   reduce2(   R   R	   R   R
   R   R   R.   (   R   R   R)   R   R   R   R   R   R   R   R/   t   transposed_hiddenR*   R$   (    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm2d.pyt   reduce_to_final   s    		!N(   t   __doc__t
   __future__R    R   R   t    tensorflow.contrib.ndlstm.pythonR   t   tensorflow.python.opsR   R   R	   R   R   R(   R%   R,   R0   R5   (    (    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm2d.pyt   <module>   s   			