B
    d                  @   sb   d Z ddlm  mZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ edG dd	 d	eZdS )
z"Keras cropping layer for 2D input.    N)Layer)	InputSpec)
conv_utils)keras_exportzkeras.layers.Cropping2Dc                   s>   e Zd ZdZd fdd	Zdd Zdd	 Z fd
dZ  ZS )
Cropping2DaK  Cropping layer for 2D input (e.g. picture).

    It crops along spatial dimensions, i.e. height and width.

    Examples:

    >>> input_shape = (2, 28, 28, 3)
    >>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
    >>> y = tf.keras.layers.Cropping2D(cropping=((2, 2), (4, 4)))(x)
    >>> print(y.shape)
    (2, 24, 20, 3)

    Args:
      cropping: Int, or tuple of 2 ints, or tuple of 2 tuples of 2 ints.
        - If int: the same symmetric cropping
          is applied to height and width.
        - If tuple of 2 ints:
          interpreted as two different
          symmetric cropping values for height and width:
          `(symmetric_height_crop, symmetric_width_crop)`.
        - If tuple of 2 tuples of 2 ints:
          interpreted as
          `((top_crop, bottom_crop), (left_crop, right_crop))`
      data_format: A string,
        one of `channels_last` (default) or `channels_first`.
        The ordering of the dimensions in the inputs.
        `channels_last` corresponds to inputs with shape
        `(batch_size, height, width, channels)` while `channels_first`
        corresponds to inputs with shape
        `(batch_size, channels, height, width)`.
        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".

    Input shape:
      4D tensor with shape:
      - If `data_format` is `"channels_last"`:
        `(batch_size, rows, cols, channels)`
      - If `data_format` is `"channels_first"`:
        `(batch_size, channels, rows, cols)`

    Output shape:
      4D tensor with shape:
      - If `data_format` is `"channels_last"`:
        `(batch_size, cropped_rows, cropped_cols, channels)`
      - If `data_format` is `"channels_first"`:
        `(batch_size, channels, cropped_rows, cropped_cols)`
    )r   r   )r   r   Nc                s   t  jf | t|| _t|tr8||f||ff| _nnt|drt	|dkr^t
d| dtj|d dddd}tj|d	 dd
dd}||f| _nt
d| dtdd| _d S )N__len__   z/`cropping` should have two elements. Received: .r   z1st entry of croppingT)Z
allow_zero   z2nd entry of croppingz`cropping` should be either an int, a tuple of 2 ints (symmetric_height_crop, symmetric_width_crop), or a tuple of 2 tuples of 2 ints ((top_crop, bottom_crop), (left_crop, right_crop)). Received:    )ndim)super__init__r   Znormalize_data_formatdata_format
isinstanceintcroppinghasattrlen
ValueErrorZnormalize_tupler   Z
input_spec)selfr   r   kwargsZheight_croppingZwidth_cropping)	__class__ S/var/www/html/venv/lib/python3.7/site-packages/keras/layers/reshaping/cropping2d.pyr   O   s     

zCropping2D.__init__c             C   s   t | }| jdkrt |d |d |d rT|d | jd d  | jd d  nd |d r|d | jd d  | jd d  nd gS t |d |d r|d | jd d  | jd d  nd |d r|d | jd d  | jd d  nd |d gS d S )Nchannels_firstr   r   r	      )tfZTensorShapeas_listr   r   )r   Zinput_shaper   r   r   compute_output_shapel   s     
,,,,zCropping2D.compute_output_shapec             C   s  | j dkr|jd d k	r2t| jd |jd ksX|jd d k	rpt| jd |jd krptd|j d| j | jd d | jd d   krdkrn n4|d d d d | jd d d | jd d d f S | jd d dkr$|d d d d | jd d d | jd d | jd d  f S | jd d dkrx|d d d d | jd d | jd d  | jd d d f S |d d d d | jd d | jd d  | jd d | jd d  f S |jd d k	rt| jd |jd ks|jd d k	r0t| jd |jd kr0td|j d| j | jd d | jd d   kr\dkrn n4|d d | jd d d | jd d d d d f S | jd d dkr|d d | jd d d | jd d | jd d  d d f S | jd d dkr<|d d | jd d | jd d  | jd d d d d f S |d d | jd d | jd d  | jd d | jd d  d d f S d S )Nr   r	   r   r   r   zQArgument `cropping` must be greater than the input shape. Received: inputs.shape=z, and cropping=)r   shapesumr   r   )r   inputsr   r   r   call   sR    ,&""0,(zCropping2D.callc                s4   | j | jd}t  }tt| t|  S )N)r   r   )r   r   r   
get_configdictlistitems)r   configZbase_config)r   r   r   r%      s    
zCropping2D.get_config)r   N)	__name__
__module____qualname____doc__r   r    r$   r%   __classcell__r   r   )r   r   r      s
   1Mr   )r-   Ztensorflow.compat.v2compatZv2r   Zkeras.engine.base_layerr   Zkeras.engine.input_specr   Zkeras.utilsr   Z tensorflow.python.util.tf_exportr   r   r   r   r   r   <module>   s   