B
    d                 @   sn   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 ddlmZ edG d	d
 d
eZdS )z&Keras zero-padding layer for 2D input.    N)backend)Layer)	InputSpec)
conv_utils)keras_exportzkeras.layers.ZeroPadding2Dc                   s>   e Zd ZdZd fdd	Zdd Zdd	 Z fd
dZ  ZS )ZeroPadding2Dam  Zero-padding layer for 2D input (e.g. picture).

    This layer can add rows and columns of zeros
    at the top, bottom, left and right side of an image tensor.

    Examples:

    >>> input_shape = (1, 1, 2, 2)
    >>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
    >>> print(x)
    [[[[0 1]
       [2 3]]]]
    >>> y = tf.keras.layers.ZeroPadding2D(padding=1)(x)
    >>> print(y)
    tf.Tensor(
      [[[[0 0]
         [0 0]
         [0 0]
         [0 0]]
        [[0 0]
         [0 1]
         [2 3]
         [0 0]]
        [[0 0]
         [0 0]
         [0 0]
         [0 0]]]], shape=(1, 3, 4, 2), dtype=int64)

    Args:
      padding: Int, or tuple of 2 ints, or tuple of 2 tuples of 2 ints.
        - If int: the same symmetric padding
          is applied to height and width.
        - If tuple of 2 ints:
          interpreted as two different
          symmetric padding values for height and width:
          `(symmetric_height_pad, symmetric_width_pad)`.
        - If tuple of 2 tuples of 2 ints:
          interpreted as
          `((top_pad, bottom_pad), (left_pad, right_pad))`
      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, padded_rows, padded_cols, channels)`
      - If `data_format` is `"channels_first"`:
          `(batch_size, channels, padded_rows, padded_cols)`
       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.`padding` should have two elements. Received: .r   z1st entry of paddingT)Z
allow_zeror	   z2nd entry of paddingz`padding` should be either an int, a tuple of 2 ints (symmetric_height_pad, symmetric_width_pad), or a tuple of 2 tuples of 2 ints ((top_pad, bottom_pad), (left_pad, right_pad)). Received:    )ndim)super__init__r   Znormalize_data_formatdata_format
isinstanceintpaddinghasattrlen
ValueErrorZnormalize_tupler   Z
input_spec)selfr   r   kwargsZheight_paddingwidth_padding)	__class__ W/var/www/html/venv/lib/python3.7/site-packages/keras/layers/reshaping/zero_padding2d.pyr   `   s     

zZeroPadding2D.__init__c             C   s6  t | }| jdkr|d d k	rJ|d | jd d  | jd d  }nd }|d d k	r|d | jd d  | jd d  }nd }t |d |d ||gS | jdkr2|d d k	r|d | jd d  | jd d  }nd }|d d k	r|d | jd d  | jd d  }nd }t |d |||d gS d S )NZchannels_firstr   r   r	      Zchannels_last)tfZTensorShapeas_listr   r   )r   Zinput_shaperowscolsr   r   r   compute_output_shape|   s"    
&&&&z"ZeroPadding2D.compute_output_shapec             C   s   t j|| j| jdS )N)r   r   )r   Zspatial_2d_paddingr   r   )r   inputsr   r   r   call   s    zZeroPadding2D.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ZeroPadding2D.get_config)r   N)	__name__
__module____qualname____doc__r   r#   r%   r&   __classcell__r   r   )r   r   r      s
   Ar   )r.   Ztensorflow.compat.v2compatZv2r   Zkerasr   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   