
Xc           @@ s  d  Z  d d l m Z d d l m Z d d l Z d d l Z d d l m Z d d l	 j
 Z d d l m Z d d l Z d d l Z d d l Z d d l m Z y d d	 l m Z Wn e k
 r e Z n Xd
 d d d d d  Z d
 d d d d d  Z d
 d d d d d  Z d
 d d d d d  Z d d  Z d   Z d d d d  Z d   Z  e e! d  Z" e d  Z# e$ e d  Z% d d  Z& d e' f d     YZ( d e' f d     YZ) d e) f d     YZ* d  e) f d!     YZ+ d S("   s   Fairly basic set of tools for real-time data augmentation on image data.
Can easily be extended to include new transformations,
new preprocessing methods, etc...
i    (   t   absolute_import(   t   print_functionN(   t   linalg(   t   rangei   (   t   backend(   t   Imagei   t   nearestg        c         C@ s   t  j d t  j j | |  } t  j t  j |  t  j |  d g t  j |  t  j |  d g d d d g g  } |  j | |  j | }	 }
 t | |	 |
  } t	 |  | | | |  }  |  S(   s  Performs a random rotation of a Numpy image tensor.

    # Arguments
        x: Input tensor. Must be 3D.
        rg: Rotation range, in degrees.
        row_axis: Index of axis for rows in the input tensor.
        col_axis: Index of axis for columns in the input tensor.
        channel_axis: Index of axis for channels in the input tensor.
        fill_mode: Points outside the boundaries of the input
            are filled according to the given mode
            (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
        cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.

    # Returns
        Rotated Numpy image tensor.
    i   i    i   (
   t   npt   pit   randomt   uniformt   arrayt   cost   sint   shapet   transform_matrix_offset_centert   apply_transform(   t   xt   rgt   row_axist   col_axist   channel_axist	   fill_modet   cvalt   thetat   rotation_matrixt   ht   wt   transform_matrix(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   random_rotation   s    !%c         C@ s   |  j  | |  j  | } }	 t j j | |  | }
 t j j | |  |	 } t j d d |
 g d d | g d d d g g  } | } t |  | | | |  }  |  S(   s  Performs a random spatial shift of a Numpy image tensor.

    # Arguments
        x: Input tensor. Must be 3D.
        wrg: Width shift range, as a float fraction of the width.
        hrg: Height shift range, as a float fraction of the height.
        row_axis: Index of axis for rows in the input tensor.
        col_axis: Index of axis for columns in the input tensor.
        channel_axis: Index of axis for channels in the input tensor.
        fill_mode: Points outside the boundaries of the input
            are filled according to the given mode
            (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
        cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.

    # Returns
        Shifted Numpy image tensor.
    i   i    (   R   R   R	   R
   R   R   (   R   t   wrgt   hrgR   R   R   R   R   R   R   t   txt   tyt   translation_matrixR   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   random_shift7   s    c         C@ s   t  j j | |  } t  j d t  j |  d g d t  j |  d g d d d g g  } |  j | |  j | }	 }
 t | |	 |
  } t |  | | | |  }  |  S(   s  Performs a random spatial shear of a Numpy image tensor.

    # Arguments
        x: Input tensor. Must be 3D.
        intensity: Transformation intensity.
        row_axis: Index of axis for rows in the input tensor.
        col_axis: Index of axis for columns in the input tensor.
        channel_axis: Index of axis for channels in the input tensor.
        fill_mode: Points outside the boundaries of the input
            are filled according to the given mode
            (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
        cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.

    # Returns
        Sheared Numpy image tensor.
    i   i    (	   R   R	   R
   R   R   R   R   R   R   (   R   t	   intensityR   R   R   R   R   t   sheart   shear_matrixR   R   R   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   random_shearW   s    c         C@ s   t  |  d k r$ t d |   n  | d d k rS | d d k rS d \ } } n& t j j | d | d d  \ } } t j | d d g d | d g d d d g g  }	 |  j | |  j | }
 } t |	 |
 |  } t |  | | | |  }  |  S(   s  Performs a random spatial zoom of a Numpy image tensor.

    # Arguments
        x: Input tensor. Must be 3D.
        zoom_range: Tuple of floats; zoom range for width and height.
        row_axis: Index of axis for rows in the input tensor.
        col_axis: Index of axis for columns in the input tensor.
        channel_axis: Index of axis for channels in the input tensor.
        fill_mode: Points outside the boundaries of the input
            are filled according to the given mode
            (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
        cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.

    # Returns
        Zoomed Numpy image tensor.

    # Raises
        ValueError: if `zoom_range` isn't a tuple.
    i   sB   zoom_range should be a tuple or list of two floats. Received arg: i    i   (   i   i   (	   t   lent
   ValueErrorR   R	   R
   R   R   R   R   (   R   t
   zoom_rangeR   R   R   R   R   t   zxt   zyt   zoom_matrixR   R   R   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   random_zoomu   s     &c         C@ s   t  j |  | d  }  t  j |   t  j |   } } g  |  D]/ } t  j | t  j j | |  | |  ^ q; } t  j | d d }  t  j |  d | d  }  |  S(   Ni    t   axisi   (   R   t   rollaxist   mint   maxt   clipR	   R
   t   stack(   R   R$   R   t   min_xt   max_xt	   x_channelt   channel_images(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   random_channel_shift   s    9c         C@ s   t  |  d d } t  |  d d } t j d d | g d d | g d d d g g  } t j d d | g d d | g d d d g g  } t j t j | |   |  } | S(   Ni   g      ?i   i    (   t   floatR   R   t   dot(   t   matrixR   t   yt   o_xt   o_yt   offset_matrixt   reset_matrixR   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyR      s    35c   	      C@ s   t  j |  | d  }  | d d  d d  f } | d d  d f } g  |  D]0 } t j j | | | d d d | d | ^ qN } t  j | d d }  t  j |  d | d  }  |  S(	   sd  Apply the image transformation specified by a matrix.

    # Arguments
        x: 2D numpy array, single image.
        transform_matrix: Numpy array specifying the geometric transformation.
        channel_axis: Index of axis for channels in the input tensor.
        fill_mode: Points outside the boundaries of the input
            are filled according to the given mode
            (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
        cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.

    # Returns
        The transformed version of the input.
    i    Ni   t   ordert   modeR   R/   i   (   R   R0   t   ndit   interpolationt   affine_transformR4   (	   R   R   R   R   R   t   final_affine_matrixt   final_offsetR7   R8   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyR      s    :c         C@ sJ   t  j |   j | d  }  |  d  d  d  d f }  |  j d |  }  |  S(   Ni    i.(   R   t   asarrayt   swapaxes(   R   R/   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt	   flip_axis   s    c         C@ s  t  d k r t d   n  t j |  d t j   }  |  j d k rZ t d |  j	   n  | d k ru t j
   } n  | d d h k r t d |   n  | d k r |  j d d	 d
  }  n  | r|  t t j |   d
  }  t j |   } | d
 k r|  | :}  n  |  d 9}  n  |  j	 d	 d k rAt  j |  j d  d  S|  j	 d	 d k rt  j |  d d  d d  d
 f j d  d  St d |  j	 d	   d S(   s  Converts a 3D Numpy array to a PIL Image instance.

    # Arguments
        x: Input Numpy array.
        data_format: Image data format.
        scale: Whether to rescale image values
            to be within [0, 255].

    # Returns
        A PIL Image instance.

    # Raises
        ImportError: if PIL is not available.
        ValueError: if invalid `x` or `data_format` is passed.
    sC   Could not import PIL.Image. The use of `array_to_img` requires PIL.t   dtypei   sI   Expected image array to have rank 3 (single image). Got array with shape:t   channels_firstt   channels_lasts   Invalid data_format:i   i   i    i   t   uint8t   RGBNt   Ls   Unsupported channel number: (   t	   pil_imaget   Nonet   ImportErrorR   RI   t   Kt   floatxt   ndimR)   R   t   image_data_formatt	   transposeR2   R1   t	   fromarrayt   astype(   R   t   data_formatt   scalet   x_max(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   array_to_img   s.    2c         C@ s  | d
 k r t j   } n  | d d h k r? t d |   n  t j |  d t j   } t | j  d k r | d k r| j	 d d d  } qn t | j  d k r	| d k r | j
 d | j d | j d f  } q| j
 | j d | j d d f  } n t d	 | j   | S(   s  Converts a PIL Image instance to a Numpy array.

    # Arguments
        img: PIL Image instance.
        data_format: Image data format.

    # Returns
        A 3D Numpy array.

    # Raises
        ValueError: if invalid `img` or `data_format` is passed.
    RM   RN   s   Unknown data_format: RL   i   i   i    i   s   Unsupported image shape: N(   RS   RU   RX   R)   R   RI   RV   R(   R   RY   t   reshape(   t   imgR\   R   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   img_to_array  s    ))c         C@ s   t  d k r t d   n  t  j |   } | rT | j d k ru | j d  } qu n! | j d k ru | j d  } n  | r | d | d f } | j | k r | j |  } q n  | S(   sy  Loads an image into PIL format.

    # Arguments
        path: Path to image file
        grayscale: Boolean, whether to load the image as grayscale.
        target_size: Either `None` (default to original size)
            or tuple of ints `(img_height, img_width)`.

    # Returns
        A PIL Image instance.

    # Raises
        ImportError: if PIL is not available.
    sC   Could not import PIL.Image. The use of `array_to_img` requires PIL.RQ   RP   i   i    N(   RR   RS   RT   t   openRC   t   convertt   sizet   resize(   t   patht	   grayscalet   target_sizeRa   t   wh_tuple(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   load_img.  s    s   jpg|jpeg|bmp|pngc         C@ s_   g  t  j |   D]K \ } } } | D]5 } t j d | d |  r# t  j j | |  ^ q# q S(   Ns   ([\w]+\.(?:s   ))(   t   ost   walkt   ret   matchRg   t   join(   t	   directoryt   extt   roott   _t   filest   f(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   list_picturesN  s    &t   ImageDataGeneratorc           B@ s   e  Z d  Z e e e e e d d d d d d d d e e d d d d  Z d d e d d d d d  Z d d	 d d
 d e d d d d e d  Z d   Z	 d   Z
 e d d d  Z RS(   s  Generate minibatches of image data with real-time data augmentation.

    # Arguments
        featurewise_center: set input mean to 0 over the dataset.
        samplewise_center: set each sample mean to 0.
        featurewise_std_normalization: divide inputs by std of the dataset.
        samplewise_std_normalization: divide each input by its std.
        zca_whitening: apply ZCA whitening.
        rotation_range: degrees (0 to 180).
        width_shift_range: fraction of total width.
        height_shift_range: fraction of total height.
        shear_range: shear intensity (shear angle in radians).
        zoom_range: amount of zoom. if scalar z, zoom will be randomly picked
            in the range [1-z, 1+z]. A sequence of two can be passed instead
            to select this range.
        channel_shift_range: shift range for each channels.
        fill_mode: points outside the boundaries are filled according to the
            given mode ('constant', 'nearest', 'reflect' or 'wrap'). Default
            is 'nearest'.
        cval: value used for points outside the boundaries when fill_mode is
            'constant'. Default is 0.
        horizontal_flip: whether to randomly flip images horizontally.
        vertical_flip: whether to randomly flip images vertically.
        rescale: rescaling factor. If None or 0, no rescaling is applied,
            otherwise we multiply the data by the value provided
            (before applying any other transformation).
        preprocessing_function: function that will be implied on each input.
            The function will run before any other modification on it.
            The function should take one argument:
            one image (Numpy tensor with rank 3),
            and should output a Numpy tensor with the same shape.
        data_format: 'channels_first' or 'channels_last'. In 'channels_first' mode, the channels dimension
            (the depth) is at index 1, in 'channels_last' mode it is at index 3.
            It defaults to the `image_data_format` value found in your
            Keras config file at `~/.keras/keras.json`.
            If you never set it, then it will be "channels_last".
    g        R   c         C@ s  | d  k r t j   } n  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _	 | |  _
 |	 |  _ |
 |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | d d h k r t d |   n  | |  _ | d k rd |  _ d |  _ d |  _ n  | d k r5d |  _ d |  _ d |  _ n  d  |  _ d  |  _ d  |  _ t j |
  ryd |
 d |
 g |  _ n; t |
  d k r|
 d |
 d g |  _ n t d |
   d  S(	   NRN   RM   s   data_format should be "channels_last" (channel after row and column) or "channels_first" (channel before row and column). Received arg: i   i   i   i    sM   zoom_range should be a float or a tuple or list of two floats. Received arg: (   RS   RU   RX   t   featurewise_centert   samplewise_centert   featurewise_std_normalizationt   samplewise_std_normalizationt   zca_whiteningt   rotation_ranget   width_shift_ranget   height_shift_ranget   shear_rangeR*   t   channel_shift_rangeR   R   t   horizontal_flipt   vertical_flipt   rescalet   preprocessing_functionR)   R\   R   R   R   t   meant   stdt   principal_componentsR   t   isscalarR(   (   t   selfRy   Rz   R{   R|   R}   R~   R   R   R   R*   R   R   R   R   R   R   R   R\   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   __init__{  sP    																									i    t    t   jpegc	   	      C@ s=   t  | | |  d | d | d | d |  j d | d | d | S(   Nt
   batch_sizet   shufflet   seedR\   t   save_to_dirt   save_prefixt   save_format(   t   NumpyArrayIteratorR\   (	   R   R   R=   R   R   R   R   R   R   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   flow  s    	i   t   rgbt   categoricalc         C@ sX   t  | |  d | d | d | d | d |  j d | d | d | d	 |	 d
 |
 d | d | S(   NRi   t
   color_modet   classest
   class_modeR\   R   R   R   R   R   R   t   follow_links(   t   DirectoryIteratorR\   (   R   Rq   Ri   R   R   R   R   R   R   R   R   R   R   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   flow_from_directory  s    		c         C@ s  |  j  r |  j  |  } n  |  j r4 | |  j 9} n  |  j d } |  j rl | t j | d | d t 8} n  |  j r | t j | d | d t d :} n  |  j	 r |  j d
 k	 r | |  j 8} q t j d  n  |  j r|  j d
 k	 r | |  j d :} qt j d  n  |  j r|  j d
 k	 rt j | | j  } t j | |  j  } t j | | j d | j d | j d f  } qt j d	  n  | S(   s   Apply the normalization configuration to a batch of inputs.

        # Arguments
            x: batch of inputs to be normalized.

        # Returns
            The inputs, normalized.
        i   R/   t   keepdimsgHz>s   This ImageDataGenerator specifies `featurewise_center`, but it hasn'tbeen fit on any training data. Fit it first by calling `.fit(numpy_data)`.s   This ImageDataGenerator specifies `featurewise_std_normalization`, but it hasn'tbeen fit on any training data. Fit it first by calling `.fit(numpy_data)`.i    i   s   This ImageDataGenerator specifies `zca_whitening`, but it hasn'tbeen fit on any training data. Fit it first by calling `.fit(numpy_data)`.N(   R   R   R   Rz   R   R   t   TrueR|   R   Ry   RS   t   warningst   warnR{   R}   R   R`   Re   R;   R   (   R   R   t   img_channel_axist   flatxt   whitex(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   standardize  s0    				"	&			3c         C@ s"  |  j  d } |  j d } |  j d } |  j rZ t j d t j j |  j |  j  } n d } |  j r t j j |  j |  j  | j	 | } n d } |  j
 r t j j |  j
 |  j
  | j	 | } n d } |  j r t j j |  j |  j  } n d } |  j d d k r5|  j d d k r5d \ }	 }
 n, t j j |  j d |  j d d  \ }	 }
 d	 } | d k rt j t j |  t j |  d g t j |  t j |  d g d d d g g  } | } n  | d k s| d k rFt j d d | g d d | g d d d g g  } | d	 k r1| n t j | |  } n  | d k rt j d t j |  d g d t j |  d g d d d g g  } | d	 k r| n t j | |  } n  |	 d k s|
 d k r1t j |	 d d g d |
 d g d d d g g  } | d	 k r| n t j | |  } n  | d	 k	 r| j	 | | j	 | } } t | | |  } t | | | d |  j d |  j } n  |  j d k rt | |  j |  } n  |  j rt j j   d k  rt | |  } qn  |  j rt j j   d k  rt | |  } qn  | S(
   s   Randomly augment a single image tensor.

        # Arguments
            x: 3D tensor, single image.

        # Returns
            A randomly transformed version of the input (same shape).
        i   i   i    i   R   R   g      ?(   i   i   N(   R   R   R   R~   R   R   R	   R
   R   R   R   R   R*   RS   R   R   R   R;   R   R   R   R   R   R9   R   RK   R   (   R   R   t   img_row_axist   img_col_axisR   R   R    R!   R%   R+   R,   R   R   t   shift_matrixR&   R-   R   R   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   random_transform  sn    
	*	*	*	&,%	'''		i   c         C@ sr  t  j | d t j   } | j d k rF t d t | j    n  | j |  j d d d h k r t d |  j	 d t |  j  d t |  j  d	 t | j  d
 t | j |  j  d   n  | d k	 r t  j j |  n  t  j |  } | rt  j t | | j d g t | j  d  d t j   } xX t |  D]J } xA t | j d  D], } |  j | |  | | | | j d <qeWqKW| } n  |  j r$t  j | d d |  j |  j f |  _ d d d g } | j |  j | |  j d <t  j |  j |  |  _ | |  j 8} n  |  j rt  j | d d |  j |  j f |  _ d d d g } | j |  j | |  j d <t  j |  j |  |  _ | |  j t j   :} n  |  j rnt  j | | j d | j d | j d | j d f  }	 t  j |	 j |	  |	 j d }
 t j |
  \ } } } t  j t  j | t  j  d t  j! | d    | j  |  _" n  d S(   s  Fits internal statistics to some sample data.

        Required for featurewise_center, featurewise_std_normalization
        and zca_whitening.

        # Arguments
            x: Numpy array, the data to fit on. Should have rank 4.
                In case of grayscale data,
                the channels axis should have value 1, and in case
                of RGB data, it should have value 3.
            augment: Whether to fit on randomly augmented samples
            rounds: If `augment`,
                how many augmentation passes to do over the data
            seed: random seed.

        # Raises
            ValueError: in case of invalid input `x`.
        RL   i   s<   Input to `.fit()` should have rank 4. Got array with shape: i   i   sS   Expected input to be images (as Numpy array) following the data format convention "s   " (channels on axis s3   ), i.e. expected either 1, 3 or 4 channels on axis s-   . However, it was passed an array with shape s    (s    channels).i    R/   i   g      ?gư>N(#   R   RI   RU   RV   RW   R)   t   strR   R   R\   RS   R	   R   t   copyt   zerost   tuplet   listR   R   Ry   R   R   R   R`   R{   R   t   epsilonR}   R;   t   TR   t   svdt   diagt   sqrtR   (   R   R   t   augmentt   roundsR   t   axt   rt   it   broadcast_shapet   flat_xt   sigmat   ut   sRt   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   fitb  sB    b@.		'	'	< N(   i   i   (   t   __name__t
   __module__t   __doc__t   FalseRS   R   R   R   R   R   R   R   (    (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyRx   T  sB   %0		0	Wt   Iteratorc           B@ sD   e  Z d  Z d   Z d   Z d e d d  Z d   Z d   Z	 RS(   s1  Abstract base class for image data iterators.

    # Arguments
        n: Integer, total number of samples in the dataset to loop over.
        batch_size: Integer, size of a batch.
        shuffle: Boolean, whether to shuffle the data between epochs.
        seed: Random seeding for data shuffling.
    c         C@ s[   | |  _  | |  _ | |  _ d |  _ d |  _ t j   |  _ |  j | | | |  |  _	 d  S(   Ni    (
   t   nR   R   t   batch_indext   total_batches_seent	   threadingt   Lockt   lockt   _flow_indext   index_generator(   R   R   R   R   R   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyR     s    					c         C@ s   d |  _  d  S(   Ni    (   R   (   R   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   reset  s    i    c         c@ s   |  j    x | d  k	 r3 t j j | |  j  n  |  j d k ro t j |  } | ro t j j |  } qo n  |  j | | } | | | k r | } |  j d 7_ n | | } d |  _ |  j d 7_ | | | | !| | f Vq d  S(   Ni    i   (	   R   RS   R   R	   R   R   R   t   aranget   permutation(   R   R   R   R   R   t   index_arrayt   current_indext   current_batch_size(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyR     s"    

	c         C@ s   |  S(   N(    (   R   (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   __iter__  s    c         O@ s   |  j  | |   S(   N(   t   next(   R   t   argst   kwargs(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   __next__  s    N(
   R   R   R   R   R   R   RS   R   R   R   (    (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyR     s   				R   c           B@ s5   e  Z d  Z d e d d d d d d  Z d   Z RS(   s  Iterator yielding data from a Numpy array.

    # Arguments
        x: Numpy array of input data.
        y: Numpy array of targets data.
        image_data_generator: Instance of `ImageDataGenerator`
            to use for random transformations and normalization.
        batch_size: Integer, size of a batch.
        shuffle: Boolean, whether to shuffle the data between epochs.
        seed: Random seed for data shuffling.
        data_format: String, one of `channels_first`, `channels_last`.
        save_to_dir: Optional directory where to save the pictures
            being yielded, in a viewable format. This is useful
            for visualizing the random transformations being
            applied, for debugging purposes.
        save_prefix: String prefix to use for saving sample
            images (if `save_to_dir` is set).
        save_format: Format to use for saving sample images
            (if `save_to_dir` is set).
    i    R   R   c         C@ s  | d  k	 rU t |  t |  k rU t d t j |  j t j |  j f   n  | d  k rp t j   } n  t j | d t j   |  _	 |  j	 j
 d k r t d |  j	 j   n  | d k r d n d } |  j	 j | d d d h k rNt d | d	 t |  d
 t |  d t |  j	 j  d t |  j	 j |  d   n  | d  k	 rot j |  |  _ n	 d  |  _ | |  _ | |  _ | |  _ |	 |  _ |
 |  _ t t |   j | j d | | |  d  S(   Ns_   X (images tensor) and y (labels) should have the same length. Found: X.shape = %s, y.shape = %sRL   i   sU   Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shapeRN   i   i   s=   NumpyArrayIterator is set to use the data format convention "s   " (channels on axis s3   ), i.e. expected either 1, 3 or 4 channels on axis s-   . However, it was passed an array with shape s    (s    channels).i    (   RS   R(   R)   R   RI   R   RU   RX   RV   R   RW   R   R=   t   image_data_generatorR\   R   R   R   t   superR   R   (   R   R   R=   R   R   R   R   R\   R   R   R   t   channels_axis(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyR     s,    $+\						c         C@ s  |  j   t |  j  \ } } } Wd QXt j t | g t |  j j  d  d t	 j
   } xd t |  D]V \ } } |  j | } |  j j | j t	 j
     } |  j j |  } | | | <qm W|  j r_x t |  D]{ } t | | |  j d t } d j d |  j d | | d t j j d	  d
 |  j  }	 | j t j j |  j |	   q Wn  |  j d k rr| S|  j | }
 | |
 f S(   sG   For python 2.x.

        # Returns
            The next batch.
        Ni   RL   R]   s    {prefix}_{index}_{hash}.{format}t   prefixt   indext   hashg     @t   format(    R   R   R   R   R   R   R   R   R   RU   RV   t	   enumerateR   R   R[   R   R   R   R_   R\   R   R   R   R	   t   randintR   t   saveRl   Rg   Rp   R=   RS   (   R   R   R   R   t   batch_xR   t   jR   Ra   t   fnamet   batch_y(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyR     s(    
8!	
&N(   R   R   R   R   RS   R   R   (    (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyR     s
   	"R   c           B@ sD   e  Z d  Z d	 d d
 d d e d
 d
 d
 d d e d  Z d   Z RS(   s  Iterator capable of reading images from a directory on disk.

    # Arguments
        directory: Path to the directory to read images from.
            Each subdirectory in this directory will be
            considered to contain images from one class,
            or alternatively you could specify class subdirectories
            via the `classes` argument.
        image_data_generator: Instance of `ImageDataGenerator`
            to use for random transformations and normalization.
        target_size: tuple of integers, dimensions to resize input images to.
        color_mode: One of `"rgb"`, `"grayscale"`. Color mode to read images.
        classes: Optional list of strings, names of sudirectories
            containing images from each class (e.g. `["dogs", "cats"]`).
            It will be computed automatically if not set.
        class_mode: Mode for yielding the targets:
            `"binary"`: binary targets (if there are only two classes),
            `"categorical"`: categorical targets,
            `"sparse"`: integer targets,
            `None`: no targets get yielded (only input images are yielded).
        batch_size: Integer, size of a batch.
        shuffle: Boolean, whether to shuffle the data between epochs.
        seed: Random seed for data shuffling.
        data_format: String, one of `channels_first`, `channels_last`.
        save_to_dir: Optional directory where to save the pictures
            being yielded, in a viewable format. This is useful
            for visualizing the random transformations being
            applied, for debugging purposes.
        save_prefix: String prefix to use for saving sample
            images (if `save_to_dir` is set).
        save_format: Format to use for saving sample images
            (if `save_to_dir` is set).
    i   R   R   i    R   R   c         @ s  |
 d  k r t j   }
 n  | |  _ | |  _ t |  |  _ | d d h k rc t d | d   n  | |  _ |
 |  _	 |  j d k r |  j	 d k r |  j d |  _
 q d |  j |  _
 n2 |  j	 d k r |  j d |  _
 n d |  j |  _
 | |  _ | d d	 d
 d  h k r!t d | d   n  | |  _ | |  _ | |  _ | |  _ d d d d h } d |  _ | sg  } xQ t t j |   D]7 } t j j t j j | |   r| j |  qqWn  t |  |  _ t t | t t |     |  _   f d   } x | D] } t j j | |  } x | |  D]r \ } } } x` | D]X } t } x1 | D]) } | j   j  d |  rZt! } PqZqZW| rG|  j d 7_ qGqGWq1Wq	Wt" d |  j |  j f  g  |  _# t$ j% |  j f d d |  _ d } x | D] } t j j | |  } x | |  D] \ } } } x | D] } t } x1 | D]) } | j   j  d |  rJt! } PqJqJW| r7|  j | |  j | <| d 7} t j j | |  } |  j# j t j j& | |   q7q7Wq!WqWt' t( |   j) |  j | | |	  d  S(   NR   Rh   s   Invalid color mode:s    ; expected "rgb" or "grayscale".RN   i   i   R   t   binaryt   sparses   Invalid class_mode:s=   ; expected one of "categorical", "binary", "sparse", or None.t   pngt   jpgR   t   bmpi    c         @ s"   t  t j |  d   d d   S(   Nt   followlinkst   keyc         S@ s   |  d S(   Ni    (    (   t   tpl(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   <lambda>  s    (   t   sortedRl   Rm   (   t   subpath(   R   (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   _recursive_list  s    t   .s(   Found %d images belonging to %d classes.RL   t   int32(   i   (   i   (   i   (   i   (*   RS   RU   RX   Rq   R   R   Ri   R)   R   R\   t   image_shapeR   R   R   R   R   t   samplesR   Rl   t   listdirRg   t   isdirRp   t   appendR(   t	   num_classt   dictt   zipR   t   class_indicesR   t   lowert   endswithR   t   printt	   filenamesR   R   t   relpathR   R   R   (   R   Rq   R   Ri   R   R   R   R   R   R   R\   R   R   R   R   t   white_list_formatst   subdirR   R   Rs   Rt   Ru   R   t   is_validt	   extensionR   t   absolute_path(    (   R   s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyR   X  s    												!$	
.c         C@ sY  |  j   t |  j  \ } } } Wd QXt j | f |  j d t j   } |  j d k } x t	 |  D] \ } } |  j
 | } t t j j |  j |  d | d |  j }	 t |	 d |  j }
 |  j j |
  }
 |  j j |
  }
 |
 | | <qi W|  j rx t |  D]{ } t | | |  j d t }	 d j d |  j d	 | | d
 t j j d  d |  j  } |	 j t j j |  j |   qWn  |  j  d k r|  j! | } n |  j  d k r|  j! | j" t j    } nt |  j  d k rKt j t# |  |  j$ f d t j   } x8 t	 |  j! |  D] \ } } d | | | f <q(Wn | S| | f S(   sG   For python 2.x.

        # Returns
            The next batch.
        NRL   Rh   Ri   R\   R]   s    {prefix}_{index}_{hash}.{format}R   R   R   g     @R   R   R   R   g      ?(%   R   R   R   R   R   R   RU   RV   R   R   R   Rk   Rl   Rg   Rp   Rq   Ri   Rb   R\   R   R   R   R   R   R_   R   R   R   R	   R   R   R   R   R   R[   R(   R   (   R   R   R   R   R   Rh   R   R   R   Ra   R   R   t   label(    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyR     s>    
%	
&* (   i   i   N(   R   R   R   RS   R   R   R   R   (    (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyR   5  s   !		Q(,   R   t
   __future__R    R   t   numpyR   Rn   t   scipyR   t   scipy.ndimaget   ndimageRD   t	   six.movesR   Rl   R   R   R   R   RU   t   PILR   RR   RT   RS   R   R#   R'   R.   R9   R   R   RK   R   R_   Rb   R   Rk   Rw   t   objectRx   R   R   R   (    (    (    s8   /tmp/pip-build-isqEY4/keras/keras/preprocessing/image.pyt   <module>   sL   
				'
		2"  R5[