B
    ӻd)                @   s  d Z ddl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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dlmZ ddlmZ ddlmZ ddlmZ ddlmZ G dd deZ eddG dd de Z!eddG d d! d!e Z"ed"d#G d$d% d%e Z#ed&d'G d(d) d)e!Z$ed*d+G d,d- d-e"Z%ed.d/G d0d1 d1e#Z&G d2d3 d3e Z'ed4d5G d6d7 d7e'Z(ed8d9G d:d; d;e'Z)ed<G d=d> d>e"Z*ed?G d@dA dAeZ+edBG dCdD dDeZ,edEG dFdG dGeZ-edHG dIdJ dJeZ.edKG dLdM dMeZ/edNG dOdP dPeZ0edQG dRdS dSeZ1edTG dUdV dVeZ2edWG dXdY dYeZ3e!Z4e"Z5e#Z6e(Z7e)Z8e%Z9e&Z:e% Z;Z<e& Z=Z>dS )Zz9Keras convolution layers and image transformation layers.    N)context)tensor_shape)activations)backend)constraints)initializers)regularizers)Layer)	InputSpec)AveragePooling1D)AveragePooling2D)AveragePooling3D)MaxPooling1D)MaxPooling2D)MaxPooling3D)
conv_utils)tf_utils)	array_ops)nn)nn_ops)keras_exportc                   s~   e Zd ZdZd  fdd		Zd
d Zdd Zdd Zdd Zdd Z	dd Z
 fddZdd Zdd Zdd Zdd Z  ZS )!Convaq  Abstract N-D convolution layer (private, used as implementation base).

  This layer creates a convolution kernel that is convolved
  (actually cross-correlated) with the layer input to produce a tensor of
  outputs. If `use_bias` is True (and a `bias_initializer` is provided),
  a bias vector is created and added to the outputs. Finally, if
  `activation` is not `None`, it is applied to the outputs as well.

  Note: layer attributes cannot be modified after the layer has been called
  once (except the `trainable` attribute).

  Args:
    rank: An integer, the rank of the convolution, e.g. "2" for 2D convolution.
    filters: Integer, the dimensionality of the output space (i.e. the number
      of filters in the convolution). Could be "None", eg in the case of
      depth wise convolution.
    kernel_size: An integer or tuple/list of n integers, specifying the
      length of the convolution window.
    strides: An integer or tuple/list of n integers,
      specifying the stride length of the convolution.
      Specifying any stride value != 1 is incompatible with specifying
      any `dilation_rate` value != 1.
    padding: One of `"valid"`,  `"same"`, or `"causal"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding with zeros
      evenly to the left/right or up/down of the input such that output has the
      same height/width dimension as the input. `"causal"` results in causal
      (dilated) convolutions, e.g. `output[t]` does not depend on `input[t+1:]`.
    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, ..., channels)` while `channels_first` corresponds to
      inputs with shape `(batch_size, channels, ...)`.
      Note: `channels_first` is only available on GPUs.
    dilation_rate: An integer or tuple/list of n integers, specifying
      the dilation rate to use for dilated convolution.
      Currently, specifying any `dilation_rate` value != 1 is
      incompatible with specifying any `strides` value != 1.
    groups: A positive integer specifying the number of groups in which the
      input is split along the channel axis. Each group is convolved
      separately with `filters / groups` filters. The output is the
      concatenation of all the `groups` results along the channel axis.
      Input channels and `filters` must both be divisible by `groups`.
    activation: Activation function to use.
      If you don't specify anything, no activation is applied.
    use_bias: Boolean, whether the layer uses a bias.
    kernel_initializer: An initializer for the convolution kernel. If None, the 
      default initializer (glorot_uniform) will be used. 
    bias_initializer: An initializer for the bias vector. If None, the default
      initializer (zeros) will be used.
    kernel_regularizer: Optional regularizer for the convolution kernel.
    bias_regularizer: Optional regularizer for the bias vector.
    activity_regularizer: Optional regularizer function for the output.
    kernel_constraint: Optional projection function to be applied to the
        kernel after being updated by an `Optimizer` (e.g. used to implement
        norm constraints or value constraints for layer weights). The function
        must take as input the unprojected variable and must return the
        projected variable (which must have the same shape). Constraints are
        not safe to use when doing asynchronous distributed training.
    bias_constraint: Optional projection function to be applied to the
        bias after being updated by an `Optimizer`.
     validNTglorot_uniformzerosc                sZ  t t| jf ||t|d| || _t|tr<t|}|d k	r\|dk r\t	d| d|| _
|phd| _t||d| _t||d| _t|| _t|| _t||d| _t|	| _|
| _t|| _t|| _t|| _t|| _t|| _t|| _t | jd	 d
| _!| "  | jdk| _#| jdk| _$t%| j| jd	 | _&d S )N)	trainablenameactivity_regularizerr   zLReceived a negative value for `filters`.Was expecting a positive value, got .r   kernel_sizestridesdilation_rate   )min_ndimcausalchannels_first)'superr   __init__r   getrank
isinstancefloatint
ValueErrorfiltersgroupsr   normalize_tupler    r!   Znormalize_paddingpaddingnormalize_data_formatdata_formatr"   r   
activationuse_biasr   kernel_initializerbias_initializerkernel_regularizerbias_regularizerr   kernel_constraintbias_constraintr
   
input_spec_validate_init
_is_causal_channels_firstconvert_data_format_tf_data_format)selfr*   r/   r    r!   r2   r4   r"   r0   r5   r6   r7   r8   r9   r:   r   r;   r<   r   r   Zconv_opkwargs)	__class__ ^/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/keras/layers/convolutional.pyr(   m   sB    

zConv.__init__c             C   s   | j d k	r.| j | j dkr.td| j| j t| jsHtd| jf t| jsbtd| jf | jdkrt| t	t
fstdd S )Nr   zgThe number of filters must be evenly divisible by the number of groups. Received: groups={}, filters={}z<The argument `kernel_size` cannot contain 0(s). Received: %sz9The argument `strides` cannot contains 0(s). Received: %sr%   zCCausal padding is only supported for `Conv1D`and `SeparableConv1D`.)r/   r0   r.   formatallr    r!   r2   r+   Conv1DSeparableConv1D)rC   rF   rF   rG   r>      s    


zConv._validate_initc       	   	   C   s@  t |}| |}|| j dkr6td| j||| j|| j | jf }| jd|| j	| j
| jd| jd| _| jr| jd| jf| j| j| jd| jd| _nd | _|  }t| jd ||id| _| jd	krd
}nt| jtr| j }n| j}t| j}t| j}| jj}|dkrd}t j!t"j#|||| j$|d| _%d| _&d S )Nr   zThe number of input channels must be evenly divisible by the number of groups. Received groups={}, but the input has {} channels (full input shape is {}).kernelT)r   shapeinitializerregularizer
constraintr   dtypebiasr#   )r$   axesr%   ZVALIDrJ   Zconv1d)r!   r2   	dilationsr4   r   )'r   TensorShape_get_input_channelr0   r.   rH   r    r/   
add_weightr7   r9   r;   rQ   rL   r6   r8   r:   r<   rR   _get_channel_axisr
   r*   r=   r2   r+   strupperlistr"   r!   rE   __name__	functoolspartialr   Zconvolution_v2rB   _convolution_opbuilt)	rC   input_shapeZinput_channelkernel_shapechannel_axisZ
tf_paddingZtf_dilationsZ
tf_stridesZ
tf_op_namerF   rF   rG   build   s`    








z
Conv.buildc                s   |j } jrt| |} | j} jr|j j} jdkrj j	rjt
 jd jdf}||7 }nN|d k	r|d j kr fdd}tj|| jd d}ntj| j jd}t sԈ |}||  jd k	r |S |S )Nr   r#   c                s   t j|  j jdS )N)r4   )r   bias_addrR   rB   )o)rC   rF   rG   	_apply_fn
  s    zConv.call.<locals>._apply_fn)Z
inner_rank)r4   )rM   r?   r   pad_compute_causal_paddingr_   rL   r6   r*   r@   ZreshaperR   r/   r   Zsqueeze_batch_dimsr   re   rB   r   executing_eagerlycompute_output_shape	set_shaper5   )rC   inputsra   outputsZoutput_rankrR   rg   	out_shaperF   )rC   rG   call   s*    




z	Conv.callc                s    fddt |D S )Nc          	      s8   g | ]0\}}t j| j|  j j|  j| d qS ))r2   stridedilation)r   conv_output_lengthr    r2   r!   r"   ).0ilength)rC   rF   rG   
<listcomp>  s   z.Conv._spatial_output_shape.<locals>.<listcomp>)	enumerate)rC   Zspatial_input_shaperF   )rC   rG   _spatial_output_shape  s    
zConv._spatial_output_shapec             C   s   t | }t|| j d }| jdkrVt |d | | ||d  | jg S t |d | | jg | ||d d   S d S )Nr   channels_last)r   rU   as_listlenr*   r4   ry   r/   )rC   ra   
batch_rankrF   rF   rG   rk   '  s    
(zConv.compute_output_shapec             C   s   dS )NFrF   )rC   rm   rF   rF   rG   _recreate_conv_op4  s    zConv._recreate_conv_opc                s   | j | j| j| j| j| j| jt| j	| j
t| jt| jt| jt| jt| jt| jt| jd}tt|  }tt| t|  S )N)r/   r    r!   r2   r4   r"   r0   r5   r6   r7   r8   r9   r:   r   r;   r<   )r/   r    r!   r2   r4   r"   r0   r   	serializer5   r6   r   r7   r8   r   r9   r:   r   r   r;   r<   r'   r   
get_configdictr[   items)rC   configbase_config)rE   rF   rG   r   7  s$    






zConv.get_configc             C   s   | j d | jd d  }t|jdddkr0d}nt|jd }| jdkrhddgg| |dgddgg }nddgg| ddg|dgg }|S )z;Calculates padding for 'causal' option for 1-d conv layers.r   r   ZndimsNr#   rz   )r"   r    getattrrM   r}   r4   )rC   rm   Zleft_padr~   Zcausal_paddingrF   rF   rG   ri   ]  s    
 zConv._compute_causal_paddingc             C   s   | j dkrd| j S dS d S )Nr&   r{   )r4   r*   )rC   rF   rF   rG   rX   j  s    

zConv._get_channel_axisc             C   s,   |   }|j| jd kr tdt|| S )NzDThe channel dimension of the inputs should be defined. Found `None`.)rX   dimsvaluer.   r-   )rC   ra   rc   rF   rF   rG   rV   p  s    zConv._get_input_channelc             C   s0   | j dkrd}n| j }t|ttfs,| }|S )Nr%   r   )r2   r+   r[   tuplerZ   )rC   
op_paddingrF   rF   rG   _get_padding_opw  s    
zConv._get_padding_op)r   r   Nr   r   NTr   r   NNNNNTNN)r\   
__module____qualname____doc__r(   r>   rd   rp   ry   rk   r   r   ri   rX   rV   r   __classcell__rF   rF   )rE   rG   r   .   s:   =                ):$&r   zkeras.layers.Conv1Dzkeras.layers.Convolution1Dc                   s"   e Zd ZdZd fd	d
	Z  ZS )rJ   a  1D convolution layer (e.g. temporal convolution).

  This layer creates a convolution kernel that is convolved
  with the layer input over a single spatial (or temporal) dimension
  to produce a tensor of outputs.
  If `use_bias` is True, a bias vector is created and added to the outputs.
  Finally, if `activation` is not `None`,
  it is applied to the outputs as well.

  When using this layer as the first layer in a model,
  provide an `input_shape` argument
  (tuple of integers or `None`, e.g.
  `(10, 128)` for sequences of 10 vectors of 128-dimensional vectors,
  or `(None, 128)` for variable-length sequences of 128-dimensional vectors.

  Examples:

  >>> # The inputs are 128-length vectors with 10 timesteps, and the batch size
  >>> # is 4.
  >>> input_shape = (4, 10, 128)
  >>> x = tf.random.normal(input_shape)
  >>> y = tf.keras.layers.Conv1D(
  ... 32, 3, activation='relu',input_shape=input_shape[1:])(x)
  >>> print(y.shape)
  (4, 8, 32)

  >>> # With extended batch shape [4, 7] (e.g. weather data where batch
  >>> # dimensions correspond to spatial location and the third dimension
  >>> # corresponds to time.)
  >>> input_shape = (4, 7, 10, 128)
  >>> x = tf.random.normal(input_shape)
  >>> y = tf.keras.layers.Conv1D(
  ... 32, 3, activation='relu', input_shape=input_shape[2:])(x)
  >>> print(y.shape)
  (4, 7, 8, 32)

  Args:
    filters: Integer, the dimensionality of the output space
      (i.e. the number of output filters in the convolution).
    kernel_size: An integer or tuple/list of a single integer,
      specifying the length of the 1D convolution window.
    strides: An integer or tuple/list of a single integer,
      specifying the stride length of the convolution.
      Specifying any stride value != 1 is incompatible with specifying
      any `dilation_rate` value != 1.
    padding: One of `"valid"`, `"same"` or `"causal"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding with zeros evenly
      to the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
      `"causal"` results in causal (dilated) convolutions, e.g. `output[t]`
      does not depend on `input[t+1:]`. Useful when modeling temporal data
      where the model should not violate the temporal order.
      See [WaveNet: A Generative Model for Raw Audio, section
        2.1](https://arxiv.org/abs/1609.03499).
    data_format: A string,
      one of `channels_last` (default) or `channels_first`.
    dilation_rate: an integer or tuple/list of a single integer, specifying
      the dilation rate to use for dilated convolution.
      Currently, specifying any `dilation_rate` value != 1 is
      incompatible with specifying any `strides` value != 1.
    groups: A positive integer specifying the number of groups in which the
      input is split along the channel axis. Each group is convolved
      separately with `filters / groups` filters. The output is the
      concatenation of all the `groups` results along the channel axis.
      Input channels and `filters` must both be divisible by `groups`.
    activation: Activation function to use.
      If you don't specify anything, no activation is applied (
      see `keras.activations`).
    use_bias: Boolean, whether the layer uses a bias vector.
    kernel_initializer: Initializer for the `kernel` weights matrix (
      see `keras.initializers`). Defaults to 'glorot_uniform'.
    bias_initializer: Initializer for the bias vector (
      see `keras.initializers`). Defaults to 'zeros'.
    kernel_regularizer: Regularizer function applied to
      the `kernel` weights matrix (see `keras.regularizers`).
    bias_regularizer: Regularizer function applied to the bias vector (
      see `keras.regularizers`).
    activity_regularizer: Regularizer function applied to
      the output of the layer (its "activation") (
      see `keras.regularizers`).
    kernel_constraint: Constraint function applied to the kernel matrix (
      see `keras.constraints`).
    bias_constraint: Constraint function applied to the bias vector (
      see `keras.constraints`).

  Input shape:
    3+D tensor with shape: `batch_shape + (steps, input_dim)`

  Output shape:
    3+D tensor with shape: `batch_shape + (new_steps, filters)`
      `steps` value might have changed due to padding or strides.

  Returns:
    A tensor of rank 3 representing
    `activation(conv1d(inputs, kernel) + bias)`.

  Raises:
    ValueError: when both `strides > 1` and `dilation_rate > 1`.
  r   r   rz   NTr   r   c                sn   t t| jf d|||||||t||	t|
t|t|t|t|t|t|d| d S )Nr   )r*   r/   r    r!   r2   r4   r"   r0   r5   r6   r7   r8   r9   r:   r   r;   r<   )r'   rJ   r(   r   r)   r   r   r   )rC   r/   r    r!   r2   r4   r"   r0   r5   r6   r7   r8   r9   r:   r   r;   r<   rD   )rE   rF   rG   r(     s&    zConv1D.__init__)r   r   rz   r   r   NTr   r   NNNNN)r\   r   r   r   r(   r   rF   rF   )rE   rG   rJ     s   d             rJ   zkeras.layers.Conv2Dzkeras.layers.Convolution2Dc                   s"   e Zd ZdZd fd	d
	Z  ZS )Conv2Da  2D convolution layer (e.g. spatial convolution over images).

  This layer creates a convolution kernel that is convolved
  with the layer input to produce a tensor of
  outputs. If `use_bias` is True,
  a bias vector is created and added to the outputs. Finally, if
  `activation` is not `None`, it is applied to the outputs as well.

  When using this layer as the first layer in a model,
  provide the keyword argument `input_shape`
  (tuple of integers or `None`, does not include the sample axis),
  e.g. `input_shape=(128, 128, 3)` for 128x128 RGB pictures
  in `data_format="channels_last"`. You can use `None` when
  a dimension has variable size.

  Examples:

  >>> # The inputs are 28x28 RGB images with `channels_last` and the batch
  >>> # size is 4.
  >>> input_shape = (4, 28, 28, 3)
  >>> x = tf.random.normal(input_shape)
  >>> y = tf.keras.layers.Conv2D(
  ... 2, 3, activation='relu', input_shape=input_shape[1:])(x)
  >>> print(y.shape)
  (4, 26, 26, 2)

  >>> # With `dilation_rate` as 2.
  >>> input_shape = (4, 28, 28, 3)
  >>> x = tf.random.normal(input_shape)
  >>> y = tf.keras.layers.Conv2D(
  ... 2, 3, activation='relu', dilation_rate=2, input_shape=input_shape[1:])(x)
  >>> print(y.shape)
  (4, 24, 24, 2)

  >>> # With `padding` as "same".
  >>> input_shape = (4, 28, 28, 3)
  >>> x = tf.random.normal(input_shape)
  >>> y = tf.keras.layers.Conv2D(
  ... 2, 3, activation='relu', padding="same", input_shape=input_shape[1:])(x)
  >>> print(y.shape)
  (4, 28, 28, 2)

  >>> # With extended batch shape [4, 7]:
  >>> input_shape = (4, 7, 28, 28, 3)
  >>> x = tf.random.normal(input_shape)
  >>> y = tf.keras.layers.Conv2D(
  ... 2, 3, activation='relu', input_shape=input_shape[2:])(x)
  >>> print(y.shape)
  (4, 7, 26, 26, 2)


  Args:
    filters: Integer, the dimensionality of the output space (i.e. the number of
      output filters in the convolution).
    kernel_size: An integer or tuple/list of 2 integers, specifying the height
      and width of the 2D convolution window. Can be a single integer to specify
      the same value for all spatial dimensions.
    strides: An integer or tuple/list of 2 integers, specifying the strides of
      the convolution along the height and width. Can be a single integer to
      specify the same value for all spatial dimensions. Specifying any stride
      value != 1 is incompatible with specifying any `dilation_rate` value != 1.
    padding: one of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding with zeros evenly
      to the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    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`.
    dilation_rate: an integer or tuple/list of 2 integers, specifying the
      dilation rate to use for dilated convolution. Can be a single integer to
      specify the same value for all spatial dimensions. Currently, specifying
      any `dilation_rate` value != 1 is incompatible with specifying any stride
      value != 1.
    groups: A positive integer specifying the number of groups in which the
      input is split along the channel axis. Each group is convolved separately
      with `filters / groups` filters. The output is the concatenation of all
      the `groups` results along the channel axis. Input channels and `filters`
      must both be divisible by `groups`.
    activation: Activation function to use. If you don't specify anything, no
      activation is applied (see `keras.activations`).
    use_bias: Boolean, whether the layer uses a bias vector.
    kernel_initializer: Initializer for the `kernel` weights matrix (see
      `keras.initializers`). Defaults to 'glorot_uniform'.
    bias_initializer: Initializer for the bias vector (see
      `keras.initializers`). Defaults to 'zeros'.
    kernel_regularizer: Regularizer function applied to the `kernel` weights
      matrix (see `keras.regularizers`). 
    bias_regularizer: Regularizer function applied to the bias vector (see
      `keras.regularizers`). 
    activity_regularizer: Regularizer function applied to the output of the
      layer (its "activation") (see `keras.regularizers`).
    kernel_constraint: Constraint function applied to the kernel matrix (see
      `keras.constraints`).
    bias_constraint: Constraint function applied to the bias vector (see
      `keras.constraints`).
  Input shape:
    4+D tensor with shape: `batch_shape + (channels, rows, cols)` if
      `data_format='channels_first'`
    or 4+D tensor with shape: `batch_shape + (rows, cols, channels)` if
      `data_format='channels_last'`.
  Output shape:
    4+D tensor with shape: `batch_shape + (filters, new_rows, new_cols)` if
    `data_format='channels_first'` or 4+D tensor with shape: `batch_shape +
      (new_rows, new_cols, filters)` if `data_format='channels_last'`.  `rows`
      and `cols` values might have changed due to padding.

  Returns:
    A tensor of rank 4+ representing
    `activation(conv2d(inputs, kernel) + bias)`.

  Raises:
    ValueError: if `padding` is `"causal"`.
    ValueError: when both `strides > 1` and `dilation_rate > 1`.
  r   r   r   Nr   Tr   r   c                sn   t t| jf d|||||||t||	t|
t|t|t|t|t|t|d| d S )Nr#   )r*   r/   r    r!   r2   r4   r"   r0   r5   r6   r7   r8   r9   r:   r   r;   r<   )r'   r   r(   r   r)   r   r   r   )rC   r/   r    r!   r2   r4   r"   r0   r5   r6   r7   r8   r9   r:   r   r;   r<   rD   )rE   rF   rG   r(     s&    zConv2D.__init__)r   r   Nr   r   NTr   r   NNNNN)r\   r   r   r   r(   r   rF   rF   )rE   rG   r     s   w             r   zkeras.layers.Conv3Dzkeras.layers.Convolution3Dc                   s"   e Zd ZdZd fd	d
	Z  ZS )Conv3Da  3D convolution layer (e.g. spatial convolution over volumes).

  This layer creates a convolution kernel that is convolved
  with the layer input to produce a tensor of
  outputs. If `use_bias` is True,
  a bias vector is created and added to the outputs. Finally, if
  `activation` is not `None`, it is applied to the outputs as well.

  When using this layer as the first layer in a model,
  provide the keyword argument `input_shape`
  (tuple of integers or `None`, does not include the sample axis),
  e.g. `input_shape=(128, 128, 128, 1)` for 128x128x128 volumes
  with a single channel,
  in `data_format="channels_last"`.

  Examples:

  >>> # The inputs are 28x28x28 volumes with a single channel, and the
  >>> # batch size is 4
  >>> input_shape =(4, 28, 28, 28, 1)
  >>> x = tf.random.normal(input_shape)
  >>> y = tf.keras.layers.Conv3D(
  ... 2, 3, activation='relu', input_shape=input_shape[1:])(x)
  >>> print(y.shape)
  (4, 26, 26, 26, 2)

  >>> # With extended batch shape [4, 7], e.g. a batch of 4 videos of 3D frames,
  >>> # with 7 frames per video.
  >>> input_shape = (4, 7, 28, 28, 28, 1)
  >>> x = tf.random.normal(input_shape)
  >>> y = tf.keras.layers.Conv3D(
  ... 2, 3, activation='relu', input_shape=input_shape[2:])(x)
  >>> print(y.shape)
  (4, 7, 26, 26, 26, 2)

  Args:
    filters: Integer, the dimensionality of the output space (i.e. the number of
      output filters in the convolution).
    kernel_size: An integer or tuple/list of 3 integers, specifying the depth,
      height and width of the 3D convolution window. Can be a single integer to
      specify the same value for all spatial dimensions.
    strides: An integer or tuple/list of 3 integers, specifying the strides of
      the convolution along each spatial dimension. Can be a single integer to
      specify the same value for all spatial dimensions. Specifying any stride
      value != 1 is incompatible with specifying any `dilation_rate` value != 1.
    padding: one of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding with zeros evenly
      to the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    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_shape + (spatial_dim1, spatial_dim2,
      spatial_dim3, channels)` while `channels_first` corresponds to inputs with
      shape `batch_shape + (channels, spatial_dim1, spatial_dim2,
      spatial_dim3)`. 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".
    dilation_rate: an integer or tuple/list of 3 integers, specifying the
      dilation rate to use for dilated convolution. Can be a single integer to
      specify the same value for all spatial dimensions. Currently, specifying
      any `dilation_rate` value != 1 is incompatible with specifying any stride
      value != 1.
    groups: A positive integer specifying the number of groups in which the
      input is split along the channel axis. Each group is convolved separately
      with `filters / groups` filters. The output is the concatenation of all
      the `groups` results along the channel axis. Input channels and `filters`
      must both be divisible by `groups`.
    activation: Activation function to use. If you don't specify anything, no
      activation is applied (see `keras.activations`).
    use_bias: Boolean, whether the layer uses a bias vector.
    kernel_initializer: Initializer for the `kernel` weights matrix (see
      `keras.initializers`). Defaults to 'glorot_uniform'.
    bias_initializer: Initializer for the bias vector (see
      `keras.initializers`). Defaults to 'zeros'.
    kernel_regularizer: Regularizer function applied to the `kernel` weights
      matrix (see `keras.regularizers`).
    bias_regularizer: Regularizer function applied to the bias vector (see
      `keras.regularizers`).
    activity_regularizer: Regularizer function applied to the output of the
      layer (its "activation") (see `keras.regularizers`).
    kernel_constraint: Constraint function applied to the kernel matrix (see
      `keras.constraints`).
    bias_constraint: Constraint function applied to the bias vector (see
      `keras.constraints`).
  Input shape:
    5+D tensor with shape: `batch_shape + (channels, conv_dim1, conv_dim2,
      conv_dim3)` if data_format='channels_first'
    or 5+D tensor with shape: `batch_shape + (conv_dim1, conv_dim2, conv_dim3,
      channels)` if data_format='channels_last'.
  Output shape:
    5+D tensor with shape: `batch_shape + (filters, new_conv_dim1,
      new_conv_dim2, new_conv_dim3)` if data_format='channels_first'
    or 5+D tensor with shape: `batch_shape + (new_conv_dim1, new_conv_dim2,
      new_conv_dim3, filters)` if data_format='channels_last'. `new_conv_dim1`,
      `new_conv_dim2` and `new_conv_dim3` values might have changed due to
      padding.

  Returns:
    A tensor of rank 5+ representing
    `activation(conv3d(inputs, kernel) + bias)`.

  Raises:
    ValueError: if `padding` is "causal".
    ValueError: when both `strides > 1` and `dilation_rate > 1`.
  r   r   r   r   Nr   Tr   r   c                sn   t t| jf d|||||||t||	t|
t|t|t|t|t|t|d| d S )N   )r*   r/   r    r!   r2   r4   r"   r0   r5   r6   r7   r8   r9   r:   r   r;   r<   )r'   r   r(   r   r)   r   r   r   )rC   r/   r    r!   r2   r4   r"   r0   r5   r6   r7   r8   r9   r:   r   r;   r<   rD   )rE   rF   rG   r(     s&    zConv3D.__init__)r   r   Nr   r   NTr   r   NNNNN)r\   r   r   r   r(   r   rF   rF   )rE   rG   r     s   j             r   zkeras.layers.Conv1DTransposez#keras.layers.Convolution1DTransposec                   sF   e Zd ZdZd fdd		Zd
d Zdd Zdd Z fddZ  Z	S )Conv1DTransposea  Transposed convolution layer (sometimes called Deconvolution).

  The need for transposed convolutions generally arises
  from the desire to use a transformation going in the opposite direction
  of a normal convolution, i.e., from something that has the shape of the
  output of some convolution to something that has the shape of its input
  while maintaining a connectivity pattern that is compatible with
  said convolution.

  When using this layer as the first layer in a model,
  provide the keyword argument `input_shape`
  (tuple of integers or `None`, does not include the sample axis),
  e.g. `input_shape=(128, 3)` for data with 128 time steps and 3 channels.

  Args:
    filters: Integer, the dimensionality of the output space
      (i.e. the number of output filters in the convolution).
    kernel_size: An integer length of the 1D convolution window.
    strides: An integer specifying the stride of the convolution along the
      time dimension. Specifying a stride value != 1 is incompatible with
      specifying a `dilation_rate` value != 1. Defaults to 1.
    padding: one of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding with zeros evenly
      to the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    output_padding: An integer specifying the amount of padding along
      the time dimension of the output tensor.
      The amount of output padding must be lower than the stride.
      If set to `None` (default), the output shape is inferred.
    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, length, channels)` while `channels_first` corresponds to
      inputs with shape `(batch_size, channels, length)`.
    dilation_rate: an integer, specifying
      the dilation rate to use for dilated convolution.
      Currently, specifying a `dilation_rate` value != 1 is
      incompatible with specifying a stride value != 1.
      Also dilation rate larger than 1 is not currently supported.
    activation: Activation function to use.
      If you don't specify anything, no activation is applied (
      see `keras.activations`).
    use_bias: Boolean, whether the layer uses a bias vector.
    kernel_initializer: Initializer for the `kernel` weights matrix (
      see `keras.initializers`). Defaults to 'glorot_uniform'.
    bias_initializer: Initializer for the bias vector (
      see `keras.initializers`). Defaults to 'zeros'.
    kernel_regularizer: Regularizer function applied to
      the `kernel` weights matrix (see `keras.regularizers`).
    bias_regularizer: Regularizer function applied to the bias vector (
      see `keras.regularizers`).
    activity_regularizer: Regularizer function applied to
      the output of the layer (its "activation") (see `keras.regularizers`).
    kernel_constraint: Constraint function applied to the kernel matrix (
      see `keras.constraints`).
    bias_constraint: Constraint function applied to the bias vector (
      see `keras.constraints`).

  Input shape:
    3D tensor with shape:
    `(batch_size, steps, channels)`

  Output shape:
    3D tensor with shape:
    `(batch_size, new_steps, filters)`
    If `output_padding` is specified:
    ```
    new_timesteps = ((timesteps - 1) * strides + kernel_size -
    2 * padding + output_padding)
    ```

  Returns:
    A tensor of rank 3 representing
    `activation(conv1dtranspose(inputs, kernel) + bias)`.

  Raises:
    ValueError: if `padding` is "causal".
    ValueError: when both `strides` > 1 and `dilation_rate` > 1.

  References:
    - [A guide to convolution arithmetic for deep learning](
      https://arxiv.org/abs/1603.07285v1)
    - [Deconvolutional Networks](
      https://www.matthewzeiler.com/mattzeiler/deconvolutionalnetworks.pdf)
  r   r   NTr   r   c                s   t t| jf ||||||t||	t|
t|t|t|t|t|t|d| || _| jd k	rt	
| jdd| _xBt| j| jD ]0\}}||krtdt| j d t| j qW d S )N)r/   r    r!   r2   r4   r"   r5   r6   r7   r8   r9   r:   r   r;   r<   r   output_paddingzStride z% must be greater than output padding )r'   r   r(   r   r)   r   r   r   r   r   r1   zipr!   r.   rY   )rC   r/   r    r!   r2   r   r4   r"   r5   r6   r7   r8   r9   r:   r   r;   r<   rD   rq   out_pad)rE   rF   rG   r(     s4    
zConv1DTranspose.__init__c          	   C   s   t |}t|dkr&tdt| |  }|j| jd krFtdt|| }t	d||id| _
| j| j|f }| jd|| j| j| jd| jd| _| jr| jd| jf| j| j| jd| jd| _nd | _d| _d S )	Nr   z1Inputs should have rank 3. Received input shape: zDThe channel dimension of the inputs should be defined. Found `None`.)ndimrS   rL   T)r   rM   rN   rO   rP   r   rQ   rR   )r   rU   r}   r.   rY   rX   r   r   r-   r
   r=   r    r/   rW   r7   r9   r;   rQ   rL   r6   r8   r:   r<   rR   r`   )rC   ra   rc   	input_dimrb   rF   rF   rG   rd     s:    
zConv1DTranspose.buildc          	   C   s&  t |}|d }| jdkr"d}nd}|| }| jd kr>d }n
| jd }tj|| jd | j|| jd | j	d d}| jdkr|| j
|f}n||| j
f}tj| jdd}	t |}
tj|| j|
| j| j |	| j	d}t s| |j}|| | jrtj|| j|	d	}| jd k	r"| |S |S )
Nr   r&   r#   r   )r2   r   rq   rr   r   )r   )r!   r2   r4   rT   )r4   )r   rM   r4   r   r   deconv_output_lengthr    r2   r!   r"   r/   rA   stackr   Zconv1d_transposerL   rZ   r   rj   rk   rl   r6   r   re   rR   r5   )rC   rm   inputs_shape
batch_sizet_axisrv   r   Z
out_lengthoutput_shaper4   output_shape_tensorrn   ro   rF   rF   rG   rp     sJ    









zConv1DTranspose.callc             C   s   t | }t|}| jdkr*d\}}nd\}}| jd krBd }n
| jd }| j||< tj|| | j	d | j
|| jd | jd d||< t |S )Nr&   )r   r#   )r#   r   r   )r2   r   rq   rr   )r   rU   r|   r[   r4   r   r/   r   r   r    r2   r!   r"   )rC   ra   r   c_axisr   r   rF   rF   rG   rk     s"    




z$Conv1DTranspose.compute_output_shapec                s   t t|  }| j|d< |S )Nr   )r'   r   r   r   )rC   r   )rE   rF   rG   r   2  s    
zConv1DTranspose.get_config)r   r   NNr   NTr   r   NNNNN)
r\   r   r   r   r(   rd   rp   rk   r   r   rF   rF   )rE   rG   r   A  s&   W             "2r   zkeras.layers.Conv2DTransposez#keras.layers.Convolution2DTransposec                   sF   e Zd ZdZd fdd		Zd
d Zdd Zdd Z fddZ  Z	S )Conv2DTransposea  Transposed convolution layer (sometimes called Deconvolution).

  The need for transposed convolutions generally arises
  from the desire to use a transformation going in the opposite direction
  of a normal convolution, i.e., from something that has the shape of the
  output of some convolution to something that has the shape of its input
  while maintaining a connectivity pattern that is compatible with
  said convolution.

  When using this layer as the first layer in a model,
  provide the keyword argument `input_shape`
  (tuple of integers or `None`, does not include the sample axis),
  e.g. `input_shape=(128, 128, 3)` for 128x128 RGB pictures
  in `data_format="channels_last"`.

  Args:
    filters: Integer, the dimensionality of the output space
      (i.e. the number of output filters in the convolution).
    kernel_size: An integer or tuple/list of 2 integers, specifying the
      height and width of the 2D convolution window.
      Can be a single integer to specify the same value for
      all spatial dimensions.
    strides: An integer or tuple/list of 2 integers,
      specifying the strides of the convolution along the height and width.
      Can be a single integer to specify the same value for
      all spatial dimensions.
      Specifying any stride value != 1 is incompatible with specifying
      any `dilation_rate` value != 1.
    padding: one of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding with zeros evenly
      to the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    output_padding: An integer or tuple/list of 2 integers,
      specifying the amount of padding along the height and width
      of the output tensor.
      Can be a single integer to specify the same value for all
      spatial dimensions.
      The amount of output padding along a given dimension must be
      lower than the stride along that same dimension.
      If set to `None` (default), the output shape is inferred.
    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".
    dilation_rate: an integer or tuple/list of 2 integers, specifying
      the dilation rate to use for dilated convolution.
      Can be a single integer to specify the same value for
      all spatial dimensions.
      Currently, specifying any `dilation_rate` value != 1 is
      incompatible with specifying any stride value != 1.
    activation: Activation function to use.
      If you don't specify anything, no activation is applied (
      see `keras.activations`).
    use_bias: Boolean, whether the layer uses a bias vector.
    kernel_initializer: Initializer for the `kernel` weights matrix (
      see `keras.initializers`). Defaults to 'glorot_uniform'.
    bias_initializer: Initializer for the bias vector (
      see `keras.initializers`). Defaults to 'zeros'.
    kernel_regularizer: Regularizer function applied to
      the `kernel` weights matrix (see `keras.regularizers`).
    bias_regularizer: Regularizer function applied to the bias vector (
      see `keras.regularizers`).
    activity_regularizer: Regularizer function applied to
      the output of the layer (its "activation") (see `keras.regularizers`).
    kernel_constraint: Constraint function applied to the kernel matrix (
      see `keras.constraints`).
    bias_constraint: Constraint function applied to the bias vector (
      see `keras.constraints`).

  Input shape:
    4D tensor with shape:
    `(batch_size, channels, rows, cols)` if data_format='channels_first'
    or 4D tensor with shape:
    `(batch_size, rows, cols, channels)` if data_format='channels_last'.

  Output shape:
    4D tensor with shape:
    `(batch_size, filters, new_rows, new_cols)` if data_format='channels_first'
    or 4D tensor with shape:
    `(batch_size, new_rows, new_cols, filters)` if data_format='channels_last'.
    `rows` and `cols` values might have changed due to padding.
    If `output_padding` is specified:
    ```
    new_rows = ((rows - 1) * strides[0] + kernel_size[0] - 2 * padding[0] +
    output_padding[0])
    new_cols = ((cols - 1) * strides[1] + kernel_size[1] - 2 * padding[1] +
    output_padding[1])
    ```

  Returns:
    A tensor of rank 4 representing
    `activation(conv2dtranspose(inputs, kernel) + bias)`.

  Raises:
    ValueError: if `padding` is "causal".
    ValueError: when both `strides` > 1 and `dilation_rate` > 1.

  References:
    - [A guide to convolution arithmetic for deep
      learning](https://arxiv.org/abs/1603.07285v1)
    - [Deconvolutional
      Networks](https://www.matthewzeiler.com/mattzeiler/deconvolutionalnetworks.pdf)
  r   r   r   NTr   r   c                s   t t| jf ||||||t||	t|
t|t|t|t|t|t|d| || _| jd k	rt	
| jdd| _xBt| j| jD ]0\}}||krtdt| j d t| j qW d S )N)r/   r    r!   r2   r4   r"   r5   r6   r7   r8   r9   r:   r   r;   r<   r#   r   zStride z% must be greater than output padding )r'   r   r(   r   r)   r   r   r   r   r   r1   r   r!   r.   rY   )rC   r/   r    r!   r2   r   r4   r"   r5   r6   r7   r8   r9   r:   r   r;   r<   rD   rq   r   )rE   rF   rG   r(     s4    
zConv2DTranspose.__init__c          	   C   s   t |}t|dkr&tdt| |  }|j| jd krFtdt|| }t	d||id| _
| j| j|f }| jd|| j| j| jd| jd| _| jr| jd| jf| j| j| jd| jd| _nd | _d| _d S )	N   z1Inputs should have rank 4. Received input shape: zDThe channel dimension of the inputs should be defined. Found `None`.)r   rS   rL   T)r   rM   rN   rO   rP   r   rQ   rR   )r   rU   r}   r.   rY   rX   r   r   r-   r
   r=   r    r/   rW   r7   r9   r;   rQ   rL   r6   r8   r:   r<   rR   r`   )rC   ra   rc   r   rb   rF   rF   rG   rd     s:    
zConv2DTranspose.buildc          	   C   s  t |}|d }| jdkr&d\}}nd\}}d\}}|jjd k	r\|j }|| }|| }|d k	rh|n|| }|d k	r||n|| }| j\}	}
| j\}}| jd krd  }}n
| j\}}tj	||	| j
||| jd d}tj	||
| j
||| jd d}| jdkr|| j||f}n|||| jf}t |}tj|| j|| j| j
| j| jd}t sh| |j}|| | jrtj|| jtj| jd	d
d}| jd k	r| |S |S )Nr   r&   )r#   r   )r   r#   )NN)r2   r   rq   rr   r   )r!   r2   r4   r"   r   )r   )r4   )r   rM   r4   r*   r|   r    r!   r   r   r   r2   r"   r/   r   r   Zconv2d_transposerL   r   rj   rk   rl   r6   r   re   rR   rA   r5   )rC   rm   r   r   h_axisw_axisheightwidthr   kernel_hkernel_wstride_hstride_w	out_pad_h	out_pad_w
out_height	out_widthr   r   rn   ro   rF   rF   rG   rp     sh    













zConv2DTranspose.callc             C   s   t | }t|}| jdkr,d\}}}n
d\}}}| j\}}| j\}}	| jd kr^d  }
}n
| j\}
}| j||< t	j
|| || j|
|| jd d||< t	j
|| || j||	| jd d||< t |S )Nr&   )r   r#   r   )r   r   r#   r   )r2   r   rq   rr   r   )r   rU   r|   r[   r4   r    r!   r   r/   r   r   r2   r"   )rC   ra   r   r   r   r   r   r   r   r   r   r   rF   rF   rG   rk   @  s4    







z$Conv2DTranspose.compute_output_shapec                s   t t|  }| j|d< |S )Nr   )r'   r   r   r   )rC   r   )rE   rF   rG   r   a  s    
zConv2DTranspose.get_config)r   r   NNr   NTr   r   NNNNN)
r\   r   r   r   r(   rd   rp   rk   r   r   rF   rF   )rE   rG   r   8  s&   o             "G!r   zkeras.layers.Conv3DTransposez#keras.layers.Convolution3DTransposec                   sF   e Zd ZdZd fdd		Zd
d Zdd Zdd Z fddZ  Z	S )Conv3DTransposea  Transposed convolution layer (sometimes called Deconvolution).

  The need for transposed convolutions generally arises
  from the desire to use a transformation going in the opposite direction
  of a normal convolution, i.e., from something that has the shape of the
  output of some convolution to something that has the shape of its input
  while maintaining a connectivity pattern that is compatible with
  said convolution.

  When using this layer as the first layer in a model,
  provide the keyword argument `input_shape`
  (tuple of integers or `None`, does not include the sample axis),
  e.g. `input_shape=(128, 128, 128, 3)` for a 128x128x128 volume with 3 channels
  if `data_format="channels_last"`.

  Args:
    filters: Integer, the dimensionality of the output space
      (i.e. the number of output filters in the convolution).
    kernel_size: An integer or tuple/list of 3 integers, specifying the
      depth, height and width of the 3D convolution window.
      Can be a single integer to specify the same value for
      all spatial dimensions.
    strides: An integer or tuple/list of 3 integers,
      specifying the strides of the convolution along the depth, height
        and width.
      Can be a single integer to specify the same value for
      all spatial dimensions.
      Specifying any stride value != 1 is incompatible with specifying
      any `dilation_rate` value != 1.
    padding: one of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding with zeros evenly
      to the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    output_padding: An integer or tuple/list of 3 integers,
      specifying the amount of padding along the depth, height, and
      width.
      Can be a single integer to specify the same value for all
      spatial dimensions.
      The amount of output padding along a given dimension must be
      lower than the stride along that same dimension.
      If set to `None` (default), the output shape is inferred.
    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, depth, height, width, channels)` while `channels_first`
      corresponds to inputs with shape
      `(batch_size, channels, depth, 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".
    dilation_rate: an integer or tuple/list of 3 integers, specifying
      the dilation rate to use for dilated convolution.
      Can be a single integer to specify the same value for
      all spatial dimensions.
      Currently, specifying any `dilation_rate` value != 1 is
      incompatible with specifying any stride value != 1.
    activation: Activation function to use.
      If you don't specify anything, no activation is applied (
      see `keras.activations`).
    use_bias: Boolean, whether the layer uses a bias vector.
    kernel_initializer: Initializer for the `kernel` weights matrix (
      see `keras.initializers`). Defaults to 'glorot_uniform'.
    bias_initializer: Initializer for the bias vector (
      see `keras.initializers`). Defaults to 'zeros'.
    kernel_regularizer: Regularizer function applied to
      the `kernel` weights matrix (
      see `keras.regularizers`).
    bias_regularizer: Regularizer function applied to the bias vector (
      see `keras.regularizers`).
    activity_regularizer: Regularizer function applied to
      the output of the layer (its "activation") (
      see `keras.regularizers`).
    kernel_constraint: Constraint function applied to the kernel matrix (
      see `keras.constraints`).
    bias_constraint: Constraint function applied to the bias vector (
      see `keras.constraints`).

  Input shape:
    5D tensor with shape:
    `(batch_size, channels, depth, rows, cols)` if data_format='channels_first'
    or 5D tensor with shape:
    `(batch_size, depth, rows, cols, channels)` if data_format='channels_last'.

  Output shape:
    5D tensor with shape:
    `(batch_size, filters, new_depth, new_rows, new_cols)` if
      data_format='channels_first'
    or 5D tensor with shape:
    `(batch_size, new_depth, new_rows, new_cols, filters)` if
      data_format='channels_last'.
    `depth` and `rows` and `cols` values might have changed due to padding.
    If `output_padding` is specified::
    ```
    new_depth = ((depth - 1) * strides[0] + kernel_size[0] - 2 * padding[0] +
    output_padding[0])
    new_rows = ((rows - 1) * strides[1] + kernel_size[1] - 2 * padding[1] +
    output_padding[1])
    new_cols = ((cols - 1) * strides[2] + kernel_size[2] - 2 * padding[2] +
    output_padding[2])
    ```

  Returns:
    A tensor of rank 5 representing
    `activation(conv3dtranspose(inputs, kernel) + bias)`.

  Raises:
    ValueError: if `padding` is "causal".
    ValueError: when both `strides` > 1 and `dilation_rate` > 1.

  References:
    - [A guide to convolution arithmetic for deep
      learning](https://arxiv.org/abs/1603.07285v1)
    - [Deconvolutional
      Networks](https://www.matthewzeiler.com/mattzeiler/deconvolutionalnetworks.pdf)
  r   r   r   r   NTr   r   c                s   t t| jf ||||||t||	t|
t|t|t|t|t|t|d| || _| jd k	rt	
| jdd| _xBt| j| jD ]0\}}||krtdt| j d t| j qW d S )N)r/   r    r!   r2   r4   r"   r5   r6   r7   r8   r9   r:   r   r;   r<   r   r   zStride z% must be greater than output padding )r'   r   r(   r   r)   r   r   r   r   r   r1   r   r!   r.   rY   )rC   r/   r    r!   r2   r   r4   r"   r5   r6   r7   r8   r9   r:   r   r;   r<   rD   rq   r   )rE   rF   rG   r(     s4    
zConv3DTranspose.__init__c          	   C   s   t |}t|dkr$tdt||  }|j| jd krLtdt| t|| }| j	| j
|f }td||id| _| jd|| j| j| jd| jd| _| jr| jd| j
f| j| j| jd| jd| _nd | _d| _d S )	N   z0Inputs should have rank 5, received input shape:zCThe channel dimension of the inputs should be defined, found None: )r   rS   rL   T)rM   rN   rO   rP   r   rQ   rR   )r   rU   r}   r.   rY   rX   r   r   r-   r    r/   r
   r=   rW   r7   r9   r;   rQ   rL   r6   r8   r:   r<   rR   r`   )rC   ra   rc   r   rb   rF   rF   rG   rd     s<    

zConv3DTranspose.buildc          	   C   s  t |}|d }| jdkr(d\}}}n
d\}}}|| }|| }|| }	| j\}
}}| j\}}}| jd krzd  } }}n| j\}}}tj||
| j||d}tj||| j||d}tj|	|| j||d}| jdkr|| j	|||f}dd|||f}n||||| j	f}d|||df}t 
|}tj|| j||tj| jdd| j d	}t sd| |j}|| | jrtj|| jtj| jd
dd}| jd k	r| |S |S )Nr   r&   )r#   r   r   )r   r#   r   )r2   r   rq   r   r   )r   )r4   r2   r   )r4   )r   rM   r4   r    r!   r   r   r   r2   r/   r   r   Zconv3d_transposerL   rA   rZ   r   rj   rk   rl   r6   re   rR   r5   )rC   rm   r   r   d_axisr   r   depthr   r   kernel_dr   r   stride_dr   r   	out_pad_dr   r   Z	out_depthr   r   r   r!   r   rn   ro   rF   rF   rG   rp   /  sl    









zConv3DTranspose.callc             C   s   t | }t|}| jdkr.d\}}}}nd\}}}}| j\}}}	| j\}
}}| jd krjd  } }}n| j\}}}| j||< t	j
|| || j||
d||< t	j
|| || j||d||< t	j
|| |	| j||d||< t |S )Nr&   )r   r#   r   r   )r   r   r#   r   )r2   r   rq   )r   rU   r|   r[   r4   r    r!   r   r/   r   r   r2   )rC   ra   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rF   rF   rG   rk   t  s<    


z$Conv3DTranspose.compute_output_shapec                s&   t t|  }|d | j|d< |S )Nr"   r   )r'   r   r   popr   )rC   r   )rE   rF   rG   r     s    

zConv3DTranspose.get_config)r   r   NNr   NTr   r   NNNNN)
r\   r   r   r   r(   rd   rp   rk   r   r   rF   rF   )rE   rG   r   g  s&   v             "E%r   c                   s>   e Zd ZdZd fdd		Zd
d Zdd Z fddZ  ZS )SeparableConva  Abstract base layer for separable nD convolution.

  This layer performs a depthwise convolution that acts separately on
  channels, followed by a pointwise convolution that mixes channels.
  If `use_bias` is True and a bias initializer is provided,
  it adds a bias vector to the output.
  It then optionally applies an activation function to produce the final output.

  Args:
    rank: An integer, the rank of the convolution, e.g. "2" for 2D convolution.
    filters: Integer, the dimensionality of the output space (i.e. the number
      of filters in the convolution).
    kernel_size: A tuple or list of integers specifying the spatial
      dimensions of the filters. Can be a single integer to specify the same
      value for all spatial dimensions.
    strides: A tuple or list of integers specifying the strides
      of the convolution. Can be a single integer to specify the same value for
      all spatial dimensions.
      Specifying any `stride` value != 1 is incompatible with specifying
      any `dilation_rate` value != 1.
    padding: One of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding with zeros evenly
      to the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    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, ..., channels)` while `channels_first` corresponds to
      inputs with shape `(batch_size, channels, ...)`.
    dilation_rate: An integer or tuple/list of 2 integers, specifying
      the dilation rate to use for dilated convolution.
      Can be a single integer to specify the same value for
      all spatial dimensions.
      Currently, specifying any `dilation_rate` value != 1 is
      incompatible with specifying any stride value != 1.
    depth_multiplier: The number of depthwise convolution output channels for
      each input channel. The total number of depthwise convolution output
      channels will be equal to `num_filters_in * depth_multiplier`.
    activation: Activation function to use.
      If you don't specify anything, no activation is applied (
      see `keras.activations`).
    use_bias: Boolean, whether the layer uses a bias.
    depthwise_initializer: An initializer for the depthwise convolution kernel (
      see `keras.initializers`). If None, then the default initializer (
      'glorot_uniform') will be used.
    pointwise_initializer: An initializer for the pointwise convolution kernel (
      see `keras.initializers`). If None, then the default initializer 
      ('glorot_uniform') will be used.
    bias_initializer: An initializer for the bias vector. If None, the default
      initializer ('zeros') will be used (see `keras.initializers`).
    depthwise_regularizer: Optional regularizer for the depthwise
      convolution kernel.
    pointwise_regularizer: Optional regularizer for the pointwise
      convolution kernel.
    bias_regularizer: Optional regularizer for the bias vector.
    activity_regularizer: Optional regularizer function for the output.
    depthwise_constraint: Optional projection function to be applied to the
      depthwise kernel after being updated by an `Optimizer` (e.g. used for
      norm constraints or value constraints for layer weights). The function
      must take as input the unprojected variable and must return the
      projected variable (which must have the same shape). Constraints are
      not safe to use when doing asynchronous distributed training.
    pointwise_constraint: Optional projection function to be applied to the
      pointwise kernel after being updated by an `Optimizer`.
    bias_constraint: Optional projection function to be applied to the
      bias after being updated by an `Optimizer`.
    trainable: Boolean, if `True` the weights of this layer will be marked as
      trainable (and listed in `layer.trainable_weights`).
  r   r   NTr   r   c                s   t t| jf |||||||t|	|
t|t|t||||d| || _t|| _t|| _	t|| _
t|| _t|| _t|| _d S )N)r*   r/   r    r!   r2   r4   r"   r5   r6   r8   r:   r   r<   r   r   )r'   r   r(   r   r)   r   r   depth_multiplierdepthwise_initializerpointwise_initializerdepthwise_regularizerpointwise_regularizerr   depthwise_constraintpointwise_constraint)rC   r*   r/   r    r!   r2   r4   r"   r   r5   r6   r   r   r8   r   r   r:   r   r   r   r<   r   r   rD   )rE   rF   rG   r(     s0    zSeparableConv.__init__c          	   C   s   t |}|  }|j| jd kr*tdt|| }t| jd ||id| _	| j
|| jf }d| j | j| | jf }| jd|| j| j| jd| jd| _| jd|| j| j| jd| jd| _| jr| jd	| jf| j| j| jd| jd| _nd | _d| _d S )
NzDThe channel dimension of the inputs should be defined. Found `None`.r#   )r   rS   )r   depthwise_kernelT)r   rM   rN   rO   rP   r   rQ   pointwise_kernelrR   )r   rU   rX   r   r   r.   r-   r
   r*   r=   r    r   r/   rW   r   r   r   rQ   r   r   r   r   r   r6   r8   r:   r<   rR   r`   )rC   ra   rc   r   depthwise_kernel_shapeZpointwise_kernel_shaperF   rF   rG   rd     sJ    


zSeparableConv.buildc             C   s   t d S )N)NotImplementedError)rC   rm   rF   rF   rG   rp   C  s    zSeparableConv.callc                s   | j | j| j| j| j| j| jt| j	| j
t| jt| jt| jt| jt| jt| jt| jt| jt| jt| jd}tt|  }tt| t|  S )N)r/   r    r!   r2   r4   r   r"   r5   r6   r   r   r8   r   r   r:   r   r   r   r<   )r/   r    r!   r2   r4   r   r"   r   r   r5   r6   r   r   r   r8   r   r   r   r:   r   r   r   r   r<   r'   r   r   r   r[   r   )rC   r   r   )rE   rF   rG   r   F  s*    









zSeparableConv.get_config)r   r   Nr   r   NTr   r   r   NNNNNNNTN)	r\   r   r   r   r(   rd   rp   r   r   rF   rF   )rE   rG   r     s.   E                  +r   zkeras.layers.SeparableConv1Dz#keras.layers.SeparableConvolution1Dc                   s*   e Zd ZdZd fdd		Zd
d Z  ZS )rK   aW  Depthwise separable 1D convolution.

  This layer performs a depthwise convolution that acts separately on
  channels, followed by a pointwise convolution that mixes channels.
  If `use_bias` is True and a bias initializer is provided,
  it adds a bias vector to the output.
  It then optionally applies an activation function to produce the final output.

  Args:
    filters: Integer, the dimensionality of the output space (i.e. the number
      of filters in the convolution).
    kernel_size: A single integer specifying the spatial
      dimensions of the filters.
    strides: A single integer specifying the strides
      of the convolution.
      Specifying any `stride` value != 1 is incompatible with specifying
      any `dilation_rate` value != 1.
    padding: One of `"valid"`, `"same"`, or `"causal"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding with zeros evenly
      to the left/right or up/down of the input such that output has the same
      height/width dimension as the input. `"causal"` results in causal
      (dilated) convolutions, e.g. `output[t]` does not depend on `input[t+1:]`.
    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, length, channels)` while `channels_first` corresponds to
      inputs with shape `(batch_size, channels, length)`.
    dilation_rate: A single integer, specifying
      the dilation rate to use for dilated convolution.
      Currently, specifying any `dilation_rate` value != 1 is
      incompatible with specifying any stride value != 1.
    depth_multiplier: The number of depthwise convolution output channels for
      each input channel. The total number of depthwise convolution output
      channels will be equal to `num_filters_in * depth_multiplier`.
    activation: Activation function to use.
      If you don't specify anything, no activation is applied (
      see `keras.activations`).
    use_bias: Boolean, whether the layer uses a bias.
    depthwise_initializer: An initializer for the depthwise convolution kernel (
      see `keras.initializers`). If None, then the default initializer (
      'glorot_uniform') will be used.
    pointwise_initializer: An initializer for the pointwise convolution kernel (
      see `keras.initializers`). If None, then the default initializer 
      ('glorot_uniform') will be used.
    bias_initializer: An initializer for the bias vector. If None, the default
      initializer ('zeros') will be used (see `keras.initializers`).
    depthwise_regularizer: Optional regularizer for the depthwise
      convolution kernel (see `keras.regularizers`).
    pointwise_regularizer: Optional regularizer for the pointwise
      convolution kernel (see `keras.regularizers`).
    bias_regularizer: Optional regularizer for the bias vector (
      see `keras.regularizers`).
    activity_regularizer: Optional regularizer function for the output (
      see `keras.regularizers`).
    depthwise_constraint: Optional projection function to be applied to the
      depthwise kernel after being updated by an `Optimizer` (e.g. used for
      norm constraints or value constraints for layer weights). The function
      must take as input the unprojected variable and must return the
      projected variable (which must have the same shape). Constraints are
      not safe to use when doing asynchronous distributed training (
      see `keras.constraints`).
    pointwise_constraint: Optional projection function to be applied to the
      pointwise kernel after being updated by an `Optimizer` (
      see `keras.constraints`).
    bias_constraint: Optional projection function to be applied to the
      bias after being updated by an `Optimizer` (
      see `keras.constraints`).
    trainable: Boolean, if `True` the weights of this layer will be marked as
      trainable (and listed in `layer.trainable_weights`).

  Input shape:
    3D tensor with shape:
    `(batch_size, channels, steps)` if data_format='channels_first'
    or 5D tensor with shape:
    `(batch_size, steps, channels)` if data_format='channels_last'.

  Output shape:
    3D tensor with shape:
    `(batch_size, filters, new_steps)` if data_format='channels_first'
    or 3D tensor with shape:
    `(batch_size,  new_steps, filters)` if data_format='channels_last'.
    `new_steps` value might have changed due to padding or strides.

  Returns:
    A tensor of rank 3 representing
    `activation(separableconv1d(inputs, kernel) + bias)`.

  Raises:
    ValueError: when both `strides` > 1 and `dilation_rate` > 1.
  r   r   NTr   r   c                s   t t| jf d|||||||t||	t|
t|t|t|t|t|t|t|t|t|d| d S )Nr   )r*   r/   r    r!   r2   r4   r"   r   r5   r6   r   r   r8   r   r   r:   r   r   r   r<   )r'   rK   r(   r   r)   r   r   r   )rC   r/   r    r!   r2   r4   r"   r   r5   r6   r   r   r8   r   r   r:   r   r   r   r<   rD   )rE   rF   rG   r(     s,    zSeparableConv1D.__init__c       	      C   s  | j dkrt|| |}| jdkr>d| jd  d }d}nd| jd  }d}t||}t| jd}t| jd}d| j	 }| j dkrd}n| j }t
j||||| |tj| jd	d
d}| jrt
j|| jtj| jd	d
d}t||g}| jd k	r| |S |S )Nr%   rz   )r   r#   r   )r   r   r   r   r   )r   )r!   r2   rater4   )r4   )r2   r   rh   ri   r4   r!   Zexpand_dimsr   r   r"   r   separable_conv2drZ   r   rA   r6   re   rR   Zsqueezer5   )	rC   rm   r!   Zspatial_start_dimr   r   r"   r   rn   rF   rF   rG   rp     s>    




zSeparableConv1D.call)r   r   Nr   r   NTr   r   r   NNNNNNN)r\   r   r   r   r(   rp   r   rF   rF   )rE   rG   rK   s  s&   \                rK   zkeras.layers.SeparableConv2Dz#keras.layers.SeparableConvolution2Dc                   s*   e Zd ZdZd fd	d
	Zdd Z  ZS )SeparableConv2Da'  Depthwise separable 2D convolution.

  Separable convolutions consist of first performing
  a depthwise spatial convolution
  (which acts on each input channel separately)
  followed by a pointwise convolution which mixes the resulting
  output channels. The `depth_multiplier` argument controls how many
  output channels are generated per input channel in the depthwise step.

  Intuitively, separable convolutions can be understood as
  a way to factorize a convolution kernel into two smaller kernels,
  or as an extreme version of an Inception block.

  Args:
    filters: Integer, the dimensionality of the output space
      (i.e. the number of output filters in the convolution).
    kernel_size: An integer or tuple/list of 2 integers, specifying the
      height and width of the 2D convolution window.
      Can be a single integer to specify the same value for
      all spatial dimensions.
    strides: An integer or tuple/list of 2 integers,
      specifying the strides of the convolution along the height and width.
      Can be a single integer to specify the same value for
      all spatial dimensions. Current implementation only supports equal 
      length strides in the row and column dimensions.
      Specifying any stride value != 1 is incompatible with specifying
      any `dilation_rate` value != 1.
    padding: one of `"valid"` or `"same"` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding with zeros evenly
      to the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    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".
    dilation_rate: An integer or tuple/list of 2 integers, specifying
      the dilation rate to use for dilated convolution.
      Currently, specifying any `dilation_rate` value != 1 is
      incompatible with specifying any `strides` value != 1.
    depth_multiplier: The number of depthwise convolution output channels
      for each input channel.
      The total number of depthwise convolution output
      channels will be equal to `filters_in * depth_multiplier`.
    activation: Activation function to use.
      If you don't specify anything, no activation is applied (
      see `keras.activations`).
    use_bias: Boolean, whether the layer uses a bias vector.
    depthwise_initializer: An initializer for the depthwise convolution kernel (
      see `keras.initializers`). If None, then the default initializer (
      'glorot_uniform') will be used.
    pointwise_initializer: An initializer for the pointwise convolution kernel (
      see `keras.initializers`). If None, then the default initializer 
      ('glorot_uniform') will be used.
    bias_initializer: An initializer for the bias vector. If None, the default
      initializer ('zeros') will be used (see `keras.initializers`).
    depthwise_regularizer: Regularizer function applied to
      the depthwise kernel matrix (see `keras.regularizers`).
    pointwise_regularizer: Regularizer function applied to
      the pointwise kernel matrix (see `keras.regularizers`).
    bias_regularizer: Regularizer function applied to the bias vector (
      see `keras.regularizers`).
    activity_regularizer: Regularizer function applied to
      the output of the layer (its "activation") (
      see `keras.regularizers`).
    depthwise_constraint: Constraint function applied to
      the depthwise kernel matrix (
      see `keras.constraints`).
    pointwise_constraint: Constraint function applied to
      the pointwise kernel matrix (
      see `keras.constraints`).
    bias_constraint: Constraint function applied to the bias vector (
      see `keras.constraints`).

  Input shape:
    4D tensor with shape:
    `(batch_size, channels, rows, cols)` if data_format='channels_first'
    or 4D tensor with shape:
    `(batch_size, rows, cols, channels)` if data_format='channels_last'.

  Output shape:
    4D tensor with shape:
    `(batch_size, filters, new_rows, new_cols)` if data_format='channels_first'
    or 4D tensor with shape:
    `(batch_size, new_rows, new_cols, filters)` if data_format='channels_last'.
    `rows` and `cols` values might have changed due to padding.

  Returns:
    A tensor of rank 4 representing
    `activation(separableconv2d(inputs, kernel) + bias)`.

  Raises:
    ValueError: if `padding` is "causal".
    ValueError: when both `strides` > 1 and `dilation_rate` > 1.
  r   r   r   Nr   Tr   r   c                s   t t| jf d|||||||t||	t|
t|t|t|t|t|t|t|t|t|d| d S )Nr#   )r*   r/   r    r!   r2   r4   r"   r   r5   r6   r   r   r8   r   r   r:   r   r   r   r<   )r'   r   r(   r   r)   r   r   r   )rC   r/   r    r!   r2   r4   r"   r   r5   r6   r   r   r8   r   r   r:   r   r   r   r<   rD   )rE   rF   rG   r(     s,    zSeparableConv2D.__init__c             C   s   | j dkrd| j d }n
d| j }tj|| j| j|| j | jt	j
| j ddd}| jrxtj|| jt	j
| j ddd}| jd k	r| |S |S )Nrz   )r   )r   r   r   )r   )r!   r2   r   r4   )r4   )r4   r!   r   r   r   r   r2   rZ   r"   r   rA   r6   re   rR   r5   )rC   rm   r!   rn   rF   rF   rG   rp     s&    



zSeparableConv2D.call)r   r   Nr   r   NTr   r   r   NNNNNNN)r\   r   r   r   r(   rp   r   rF   rF   )rE   rG   r   (  s&   f                r   zkeras.layers.DepthwiseConv2Dc                   sL   e Zd ZdZd fd	d
	Zdd Zdd Zejdd Z	 fddZ
  ZS )DepthwiseConv2Da  Depthwise 2D convolution.

  Depthwise convolution is a type of convolution in which a single convolutional
  filter is apply to each input channel (i.e. in a depthwise way).
  You can understand depthwise convolution as being
  the first step in a depthwise separable convolution.

  It is implemented via the following steps:

  - Split the input into individual channels.
  - Convolve each input with the layer's kernel (called a depthwise kernel).
  - Stack the convolved outputs together (along the channels axis).

  Unlike a regular 2D convolution, depthwise convolution does not mix
  information across different input channels.

  The `depth_multiplier` argument controls how many
  output channels are generated per input channel in the depthwise step.

  Args:
    kernel_size: An integer or tuple/list of 2 integers, specifying the
      height and width of the 2D convolution window.
      Can be a single integer to specify the same value for
      all spatial dimensions.
    strides: An integer or tuple/list of 2 integers,
      specifying the strides of the convolution along the height and width.
      Can be a single integer to specify the same value for
      all spatial dimensions.
      Specifying any stride value != 1 is incompatible with specifying
      any `dilation_rate` value != 1.
    padding: one of `'valid'` or `'same'` (case-insensitive).
      `"valid"` means no padding. `"same"` results in padding with zeros evenly
      to the left/right or up/down of the input such that output has the same
      height/width dimension as the input.
    depth_multiplier: The number of depthwise convolution output channels
      for each input channel.
      The total number of depthwise convolution output
      channels will be equal to `filters_in * depth_multiplier`.
    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'.
    dilation_rate: An integer or tuple/list of 2 integers, specifying
      the dilation rate to use for dilated convolution.
      Currently, specifying any `dilation_rate` value != 1 is
      incompatible with specifying any `strides` value != 1.
    activation: Activation function to use.
      If you don't specify anything, no activation is applied (
      see `keras.activations`).
    use_bias: Boolean, whether the layer uses a bias vector.
    depthwise_initializer: Initializer for the depthwise kernel matrix (
      see `keras.initializers`). If None, the default initializer (
      'glorot_uniform') will be used.
    bias_initializer: Initializer for the bias vector (
      see `keras.initializers`). If None, the default initializer (
      'zeros') will bs used.
    depthwise_regularizer: Regularizer function applied to
      the depthwise kernel matrix (see `keras.regularizers`).
    bias_regularizer: Regularizer function applied to the bias vector (
      see `keras.regularizers`).
    activity_regularizer: Regularizer function applied to
      the output of the layer (its 'activation') (
      see `keras.regularizers`).
    depthwise_constraint: Constraint function applied to
      the depthwise kernel matrix (
      see `keras.constraints`).
    bias_constraint: Constraint function applied to the bias vector (
      see `keras.constraints`).

  Input shape:
    4D tensor with shape:
    `[batch_size, channels, rows, cols]` if data_format='channels_first'
    or 4D tensor with shape:
    `[batch_size, rows, cols, channels]` if data_format='channels_last'.

  Output shape:
    4D tensor with shape:
    `[batch_size, channels * depth_multiplier, new_rows, new_cols]` if
    data_format='channels_first' or 4D tensor with shape:
    `[batch_size, new_rows, new_cols, channels * depth_multiplier]` if
    data_format='channels_last'. `rows` and `cols` values might have
    changed due to padding.

  Returns:
    A tensor of rank 4 representing
    `activation(depthwiseconv2d(inputs, kernel) + bias)`.

  Raises:
    ValueError: if `padding` is "causal".
    ValueError: when both `strides` > 1 and `dilation_rate` > 1.
  r   r   r   r   NTr   r   c                sh   t t| jf d ||||||||||d| || _t|	| _t|| _t	|| _
t|
| _d S )N)r/   r    r!   r2   r4   r"   r5   r6   r:   r   r<   )r'   r   r(   r   r   r)   r   r   r   r   r   r8   )rC   r    r!   r2   r   r4   r"   r5   r6   r   r8   r   r:   r   r   r<   rD   )rE   rF   rG   r(   :	  s$    zDepthwiseConv2D.__init__c             C   s   t |dk rtdt|t|}|  }|j| jd krDtdt|| }| j	d | j	d || j
f}| j|| jd| j| jd| _| jr| j|| j
 f| jd| j| jd| _nd | _td||id	| _d
| _d S )Nr   zEInputs to `DepthwiseConv2D` should have rank 4. Received input shape:zYThe channel dimension of the inputs to `DepthwiseConv2D` should be defined. Found `None`.r   r   r   )rM   rN   r   rO   rP   rR   )r   rS   T)r}   r.   rY   r   rU   rX   r   r   r-   r    r   rW   r   r   r   r   r6   r8   r:   r<   rR   r
   r=   r`   )rC   ra   rc   r   r   rF   rF   rG   rd   ^	  s6    

zDepthwiseConv2D.buildc             C   sR   t j|| j| j| j| j| jd}| jr:t j|| j	| jd}| j
d k	rN| 
|S |S )N)r!   r2   r"   r4   )r4   )r   Zdepthwise_conv2dr   r!   r2   r"   r4   r6   re   rR   r5   )rC   rm   rn   rF   rF   rG   rp   	  s    



zDepthwiseConv2D.callc             C   s   | j dkr*|d }|d }|d | j }n(| j dkrR|d }|d }|d | j }t|| jd | j| jd | jd }t|| jd | j| jd | jd }| j dkr|d |||fS | j dkr|d |||fS d S )Nr&   r#   r   r   rz   r   )r4   r   r   rs   r    r2   r!   r"   )rC   ra   rowscolsZout_filtersrF   rF   rG   rk   	  s(    



z$DepthwiseConv2D.compute_output_shapec                st   t t|  }|d |d |d |d | j|d< t| j|d< t| j	|d< t
| j|d< |S )	Nr/   r7   r9   r;   r   r   r   r   )r'   r   r   r   r   r   r   r   r   r   r   r   )rC   r   )rE   rF   rG   r   	  s    




zDepthwiseConv2D.get_config)r   r   r   Nr   NTr   r   NNNNN)r\   r   r   r   r(   rd   rp   r   Zshape_type_conversionrk   r   r   rF   rF   )rE   rG   r     s&   b             #r   zkeras.layers.UpSampling1Dc                   s>   e Zd ZdZd fdd	Zdd Zdd Z fd	d
Z  ZS )UpSampling1Da  Upsampling layer for 1D inputs.

  Repeats each temporal step `size` times along the time axis.

  Examples:

  >>> input_shape = (2, 2, 3)
  >>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
  >>> print(x)
  [[[ 0  1  2]
    [ 3  4  5]]
   [[ 6  7  8]
    [ 9 10 11]]]
  >>> y = tf.keras.layers.UpSampling1D(size=2)(x)
  >>> print(y)
  tf.Tensor(
    [[[ 0  1  2]
      [ 0  1  2]
      [ 3  4  5]
      [ 3  4  5]]
     [[ 6  7  8]
      [ 6  7  8]
      [ 9 10 11]
      [ 9 10 11]]], shape=(2, 4, 3), dtype=int64)

  Args:
    size: Integer. Upsampling factor.

  Input shape:
    3D tensor with shape: `(batch_size, steps, features)`.

  Output shape:
    3D tensor with shape: `(batch_size, upsampled_steps, features)`.
  r#   c                s,   t t| jf | t|| _tdd| _d S )Nr   )r   )r'   r   r(   r-   sizer
   r=   )rC   r   rD   )rE   rF   rG   r(   	  s    
zUpSampling1D.__init__c             C   sD   t | }|d d k	r(| j|d  nd }t |d ||d gS )Nr   r   r#   )r   rU   r|   r   )rC   ra   r   rF   rF   rG   rk   	  s    z!UpSampling1D.compute_output_shapec             C   s   t j|| jdd}|S )Nr   )Zaxis)r   Zrepeat_elementsr   )rC   rm   outputrF   rF   rG   rp   	  s    zUpSampling1D.callc                s4   d| j i}tt|  }tt| t|  S )Nr   )r   r'   r   r   r   r[   r   )rC   r   r   )rE   rF   rG   r   	  s    
zUpSampling1D.get_config)r#   )	r\   r   r   r   r(   rk   rp   r   r   rF   rF   )rE   rG   r   	  s
   #r   zkeras.layers.UpSampling2Dc                   s>   e Zd ZdZd fdd	Zdd Zd	d
 Z fddZ  ZS )UpSampling2Da  Upsampling layer for 2D inputs.

  Repeats the rows and columns of the data
  by `size[0]` and `size[1]` respectively.

  Examples:

  >>> input_shape = (2, 2, 1, 3)
  >>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
  >>> print(x)
  [[[[ 0  1  2]]
    [[ 3  4  5]]]
   [[[ 6  7  8]]
    [[ 9 10 11]]]]
  >>> y = tf.keras.layers.UpSampling2D(size=(1, 2))(x)
  >>> print(y)
  tf.Tensor(
    [[[[ 0  1  2]
       [ 0  1  2]]
      [[ 3  4  5]
       [ 3  4  5]]]
     [[[ 6  7  8]
       [ 6  7  8]]
      [[ 9 10 11]
       [ 9 10 11]]]], shape=(2, 2, 2, 3), dtype=int64)

  Args:
    size: Int, or tuple of 2 integers.
      The upsampling factors for rows and columns.
    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".
    interpolation: A string, one of `nearest` or `bilinear`.

  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, upsampled_rows, upsampled_cols, channels)`
    - If `data_format` is `"channels_first"`:
        `(batch_size, channels, upsampled_rows, upsampled_cols)`
  r#   r#   Nnearestc                sT   t t| jf | t|| _t|dd| _|dkr>td|| _	t
dd| _d S )Nr#   r   >   bilinearr   zF`interpolation` argument should be one of `"nearest"` or `"bilinear"`.r   )r   )r'   r   r(   r   r3   r4   r1   r   r.   interpolationr
   r=   )rC   r   r4   r   rD   )rE   rF   rG   r(   0
  s    zUpSampling2D.__init__c             C   s   t | }| jdkrv|d d k	r6| jd |d  nd }|d d k	rX| jd |d  nd }t |d |d ||gS |d d k	r| jd |d  nd }|d d k	r| jd |d  nd }t |d |||d gS d S )Nr&   r#   r   r   r   )r   rU   r|   r4   r   )rC   ra   r   r   rF   rF   rG   rk   >
  s    
""""z!UpSampling2D.compute_output_shapec             C   s$   t j|| jd | jd | j| jdS )Nr   r   )r   )r   Zresize_imagesr   r4   r   )rC   rm   rF   rF   rG   rp   O
  s    zUpSampling2D.callc                s<   | j | j| jd}tt|  }tt| t|  S )N)r   r4   r   )	r   r4   r   r'   r   r   r   r[   r   )rC   r   r   )rE   rF   rG   r   T
  s
    
zUpSampling2D.get_config)r   Nr   )	r\   r   r   r   r(   rk   rp   r   r   rF   rF   )rE   rG   r   	  s   8  r   zkeras.layers.UpSampling3Dc                   s>   e Zd ZdZd fdd	Zdd Zdd	 Z fd
dZ  ZS )UpSampling3Da  Upsampling layer for 3D inputs.

  Repeats the 1st, 2nd and 3rd dimensions
  of the data by `size[0]`, `size[1]` and `size[2]` respectively.

  Examples:

  >>> input_shape = (2, 1, 2, 1, 3)
  >>> x = tf.constant(1, shape=input_shape)
  >>> y = tf.keras.layers.UpSampling3D(size=2)(x)
  >>> print(y.shape)
  (2, 2, 4, 2, 3)

  Args:
    size: Int, or tuple of 3 integers.
      The upsampling factors for dim1, dim2 and dim3.
    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, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
      while `channels_first` corresponds to inputs with shape
      `(batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)`.
      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:
    5D tensor with shape:
    - If `data_format` is `"channels_last"`:
        `(batch_size, dim1, dim2, dim3, channels)`
    - If `data_format` is `"channels_first"`:
        `(batch_size, channels, dim1, dim2, dim3)`

  Output shape:
    5D tensor with shape:
    - If `data_format` is `"channels_last"`:
        `(batch_size, upsampled_dim1, upsampled_dim2, upsampled_dim3, channels)`
    - If `data_format` is `"channels_first"`:
        `(batch_size, channels, upsampled_dim1, upsampled_dim2, upsampled_dim3)`
  r#   r#   r#   Nc                s>   t || _t |dd| _tdd| _tt| j	f | d S )Nr   r   r   )r   )
r   r3   r4   r1   r   r
   r=   r'   r   r(   )rC   r   r4   rD   )rE   rF   rG   r(   
  s    zUpSampling3D.__init__c             C   s   t | }| jdkr|d d k	r6| jd |d  nd }|d d k	rX| jd |d  nd }|d d k	rz| jd |d  nd }t |d |d |||gS |d d k	r| jd |d  nd }|d d k	r| jd |d  nd }|d d k	r| jd |d  nd }t |d ||||d gS d S )Nr&   r#   r   r   r   r   )r   rU   r|   r4   r   )rC   ra   dim1dim2dim3rF   rF   rG   rk   
  s    
""""""z!UpSampling3D.compute_output_shapec             C   s&   t || jd | jd | jd | jS )Nr   r   r#   )r   Zresize_volumesr   r4   )rC   rm   rF   rF   rG   rp   
  s    zUpSampling3D.callc                s8   | j | jd}tt|  }tt| t|  S )N)r   r4   )r   r4   r'   r   r   r   r[   r   )rC   r   r   )rE   rF   rG   r   
  s    zUpSampling3D.get_config)r   N)	r\   r   r   r   r(   rk   rp   r   r   rF   rF   )rE   rG   r   ^
  s
   *r   zkeras.layers.ZeroPadding1Dc                   s>   e Zd ZdZd fdd	Zdd Zdd Z fd	d
Z  ZS )ZeroPadding1Da=  Zero-padding layer for 1D input (e.g. temporal sequence).

  Examples:

  >>> input_shape = (2, 2, 3)
  >>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
  >>> print(x)
  [[[ 0  1  2]
    [ 3  4  5]]
   [[ 6  7  8]
    [ 9 10 11]]]
  >>> y = tf.keras.layers.ZeroPadding1D(padding=2)(x)
  >>> print(y)
  tf.Tensor(
    [[[ 0  0  0]
      [ 0  0  0]
      [ 0  1  2]
      [ 3  4  5]
      [ 0  0  0]
      [ 0  0  0]]
     [[ 0  0  0]
      [ 0  0  0]
      [ 6  7  8]
      [ 9 10 11]
      [ 0  0  0]
      [ 0  0  0]]], shape=(2, 6, 3), dtype=int64)

  Args:
      padding: Int, or tuple of int (length 2), or dictionary.
          - If int:
          How many zeros to add at the beginning and end of
          the padding dimension (axis 1).
          - If tuple of int (length 2):
          How many zeros to add at the beginning and the end of
          the padding dimension (`(left_pad, right_pad)`).

  Input shape:
      3D tensor with shape `(batch_size, axis_to_pad, features)`

  Output shape:
      3D tensor with shape `(batch_size, padded_axis, features)`
  r   c                s2   t t| jf | t|dd| _tdd| _d S )Nr#   r2   r   )r   )r'   r   r(   r   r1   r2   r
   r=   )rC   r2   rD   )rE   rF   rG   r(   
  s    zZeroPadding1D.__init__c             C   sF   |d d k	r*|d | j d  | j d  }nd }t|d ||d gS )Nr   r   r#   )r2   r   rU   )rC   ra   rv   rF   rF   rG   rk   
  s    z"ZeroPadding1D.compute_output_shapec             C   s   t j|| jdS )N)r2   )r   Ztemporal_paddingr2   )rC   rm   rF   rF   rG   rp   
  s    zZeroPadding1D.callc                s4   d| j i}tt|  }tt| t|  S )Nr2   )r2   r'   r   r   r   r[   r   )rC   r   r   )rE   rF   rG   r   
  s    
zZeroPadding1D.get_config)r   )	r\   r   r   r   r(   rk   rp   r   r   rF   rF   )rE   rG   r   
  s
   +r   z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 )ZeroPadding2Da  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   r   Nc                s   t t| jf | t|| _t|tr<||f||ff| _nft	|drt
|dkrbtdt| t|d dd}t|d dd}||f| _ntdt| td	d
| _d S )N__len__r#   z+`padding` should have two elements. Found: r   z1st entry of paddingr   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)). Found: r   )r   )r'   r   r(   r   r3   r4   r+   r-   r2   hasattrr}   r.   rY   r1   r
   r=   )rC   r2   r4   rD   Zheight_paddingwidth_padding)rE   rF   rG   r(   4  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 )Nr&   r#   r   r   r   rz   )r   rU   r|   r4   r2   )rC   ra   r   r   rF   rF   rG   rk   K  s&    
&&&&z"ZeroPadding2D.compute_output_shapec             C   s   t j|| j| jdS )N)r2   r4   )r   Zspatial_2d_paddingr2   r4   )rC   rm   rF   rF   rG   rp   d  s    zZeroPadding2D.callc                s8   | j | jd}tt|  }tt| t|  S )N)r2   r4   )r2   r4   r'   r   r   r   r[   r   )rC   r   r   )rE   rF   rG   r   h  s    zZeroPadding2D.get_config)r   N)	r\   r   r   r   r(   rk   rp   r   r   rF   rF   )rE   rG   r   
  s
   Ar   zkeras.layers.ZeroPadding3Dc                   s>   e Zd ZdZd fdd	Zdd Zdd	 Z fd
dZ  ZS )ZeroPadding3Da  Zero-padding layer for 3D data (spatial or spatio-temporal).

  Examples:

  >>> input_shape = (1, 1, 2, 2, 3)
  >>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
  >>> y = tf.keras.layers.ZeroPadding3D(padding=2)(x)
  >>> print(y.shape)
  (1, 5, 6, 6, 3)

  Args:
    padding: Int, or tuple of 3 ints, or tuple of 3 tuples of 2 ints.
      - If int: the same symmetric padding
        is applied to height and width.
      - If tuple of 3 ints:
        interpreted as two different
        symmetric padding values for height and width:
        `(symmetric_dim1_pad, symmetric_dim2_pad, symmetric_dim3_pad)`.
      - If tuple of 3 tuples of 2 ints:
        interpreted as
        `((left_dim1_pad, right_dim1_pad), (left_dim2_pad,
          right_dim2_pad), (left_dim3_pad, right_dim3_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, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
      while `channels_first` corresponds to inputs with shape
      `(batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)`.
      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:
    5D tensor with shape:
    - If `data_format` is `"channels_last"`:
        `(batch_size, first_axis_to_pad, second_axis_to_pad, third_axis_to_pad,
          depth)`
    - If `data_format` is `"channels_first"`:
        `(batch_size, depth, first_axis_to_pad, second_axis_to_pad,
          third_axis_to_pad)`

  Output shape:
    5D tensor with shape:
    - If `data_format` is `"channels_last"`:
        `(batch_size, first_padded_axis, second_padded_axis, third_axis_to_pad,
          depth)`
    - If `data_format` is `"channels_first"`:
        `(batch_size, depth, first_padded_axis, second_padded_axis,
          third_axis_to_pad)`
  r   r   r   Nc                s   t t| jf | t|| _t|trB||f||f||ff| _nzt	|drt
|dkrhtdt| t|d dd}t|d dd}t|d dd	}|||f| _ntd
t| tdd| _d S )Nr   r   z)`padding` should have 3 elements. Found: r   r#   z1st entry of paddingr   z2nd entry of paddingz3rd entry of paddinga   `padding` should be either an int, a tuple of 3 ints (symmetric_dim1_pad, symmetric_dim2_pad, symmetric_dim3_pad), or a tuple of 3 tuples of 2 ints ((left_dim1_pad, right_dim1_pad), (left_dim2_pad, right_dim2_pad), (left_dim3_pad, right_dim2_pad)). Found: r   )r   )r'   r   r(   r   r3   r4   r+   r-   r2   r   r}   r.   rY   r1   r
   r=   )rC   r2   r4   rD   Zdim1_paddingZdim2_paddingZdim3_padding)rE   rF   rG   r(     s(    

zZeroPadding3D.__init__c             C   s  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 }|d d k	r|d | jd d  | jd d  }nd }t |d |d |||gS | jdkr|d d k	r|d | jd d  | jd d  }nd }|d d k	rN|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 )Nr&   r#   r   r   r   r   rz   )r   rU   r|   r4   r2   )rC   ra   r   r   r   rF   rF   rG   rk     s2    
&&&&&&z"ZeroPadding3D.compute_output_shapec             C   s   t j|| j| jdS )N)r2   r4   )r   Zspatial_3d_paddingr2   r4   )rC   rm   rF   rF   rG   rp     s    zZeroPadding3D.callc                s8   | j | jd}tt|  }tt| t|  S )N)r2   r4   )r2   r4   r'   r   r   r   r[   r   )rC   r   r   )rE   rF   rG   r     s    zZeroPadding3D.get_config)r   N)	r\   r   r   r   r(   rk   rp   r   r   rF   rF   )rE   rG   r   n  s
   4!r   zkeras.layers.Cropping1Dc                   s>   e Zd ZdZd fdd	Zdd Zdd Z fd	d
Z  ZS )
Cropping1DaD  Cropping layer for 1D input (e.g. temporal sequence).

  It crops along the time dimension (axis 1).

  Examples:

  >>> input_shape = (2, 3, 2)
  >>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
  >>> print(x)
  [[[ 0  1]
    [ 2  3]
    [ 4  5]]
   [[ 6  7]
    [ 8  9]
    [10 11]]]
  >>> y = tf.keras.layers.Cropping1D(cropping=1)(x)
  >>> print(y)
  tf.Tensor(
    [[[2 3]]
     [[8 9]]], shape=(2, 1, 2), dtype=int64)

  Args:
    cropping: Int or tuple of int (length 2)
      How many units should be trimmed off at the beginning and end of
      the cropping dimension (axis 1).
      If a single int is provided, the same value will be used for both.

  Input shape:
    3D tensor with shape `(batch_size, axis_to_crop, features)`

  Output shape:
    3D tensor with shape `(batch_size, cropped_axis, features)`
  r   r   c                s2   t t| jf | t|dd| _tdd| _d S )Nr#   croppingr   )r   )r'   r   r(   r   r1   r   r
   r=   )rC   r   rD   )rE   rF   rG   r(     s    zCropping1D.__init__c             C   sT   t | }|d d k	r8|d | jd  | jd  }nd }t |d ||d gS )Nr   r   r#   )r   rU   r|   r   )rC   ra   rv   rF   rF   rG   rk     s
    zCropping1D.compute_output_shapec             C   sZ   | j d dkr.|d d | j d d d d f S |d d | j d | j d  d d f S d S )Nr   r   )r   )rC   rm   rF   rF   rG   rp     s     zCropping1D.callc                s4   d| j i}tt|  }tt| t|  S )Nr   )r   r'   r   r   r   r[   r   )rC   r   r   )rE   rF   rG   r   #  s    
zCropping1D.get_config)r   )	r\   r   r   r   r(   rk   rp   r   r   rF   rF   )rE   rG   r     s
   "r   z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 )
Cropping2Da  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 t| jf | t|| _t|tr<||f||ff| _nft	|drt
|dkrbtdt| t|d dd}t|d dd}||f| _ntdt| td	d
| _d S )Nr   r#   z,`cropping` should have two elements. Found: r   z1st entry of croppingr   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)). Found: r   )r   )r'   r   r(   r   r3   r4   r+   r-   r   r   r}   r.   rY   r1   r
   r=   )rC   r   r4   rD   Zheight_croppingZwidth_cropping)rE   rF   rG   r(   \  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 )Nr&   r   r   r#   r   )r   rU   r|   r4   r   )rC   ra   rF   rF   rG   rk   s  s    
.4.zCropping2D.compute_output_shapec             C   s  | j dkr^| jd d | jd d   kr4dkrln 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r|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 | jd d   krd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rj|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   )r4   r   )rC   rm   rF   rF   rG   rp     s*    ,4***"04$$(zCropping2D.callc                s8   | j | jd}tt|  }tt| t|  S )N)r   r4   )r   r4   r'   r   r   r   r[   r   )rC   r   r   )rE   rF   rG   r     s    zCropping2D.get_config)r   N)	r\   r   r   r   r(   rk   rp   r   r   rF   rF   )rE   rG   r   )  s
   1r   zkeras.layers.Cropping3Dc                   s>   e Zd ZdZd fdd	Zdd Zdd	 Z fd
dZ  ZS )
Cropping3Da  Cropping layer for 3D data (e.g. spatial or spatio-temporal).

    Examples:

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

  Args:
    cropping: Int, or tuple of 3 ints, or tuple of 3 tuples of 2 ints.
      - If int: the same symmetric cropping
        is applied to depth, height, and width.
      - If tuple of 3 ints: interpreted as two different
        symmetric cropping values for depth, height, and width:
        `(symmetric_dim1_crop, symmetric_dim2_crop, symmetric_dim3_crop)`.
      - If tuple of 3 tuples of 2 ints: interpreted as
        `((left_dim1_crop, right_dim1_crop), (left_dim2_crop,
          right_dim2_crop), (left_dim3_crop, right_dim3_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, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
      while `channels_first` corresponds to inputs with shape
      `(batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)`.
      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:
    5D tensor with shape:
    - If `data_format` is `"channels_last"`:
      `(batch_size, first_axis_to_crop, second_axis_to_crop, third_axis_to_crop,
        depth)`
    - If `data_format` is `"channels_first"`:
      `(batch_size, depth, first_axis_to_crop, second_axis_to_crop,
        third_axis_to_crop)`

  Output shape:
    5D tensor with shape:
    - If `data_format` is `"channels_last"`:
      `(batch_size, first_cropped_axis, second_cropped_axis, third_cropped_axis,
        depth)`
    - If `data_format` is `"channels_first"`:
      `(batch_size, depth, first_cropped_axis, second_cropped_axis,
        third_cropped_axis)`
  )r   r   )r   r   )r   r   Nc                s   t t| jf | t|| _t|trB||f||f||ff| _nzt	|drt
|dkrhtdt| t|d dd}t|d dd}t|d dd	}|||f| _ntd
t| tdd| _d S )Nr   r   z*`cropping` should have 3 elements. Found: r   r#   z1st entry of croppingr   z2nd entry of croppingz3rd entry of croppinga
  `cropping` should be either an int, a tuple of 3 ints (symmetric_dim1_crop, symmetric_dim2_crop, symmetric_dim3_crop), or a tuple of 3 tuples of 2 ints ((left_dim1_crop, right_dim1_crop), (left_dim2_crop, right_dim2_crop), (left_dim3_crop, right_dim2_crop)). Found: r   )r   )r'   r   r(   r   r3   r4   r+   r-   r   r   r}   r.   rY   r1   r
   r=   )rC   r   r4   rD   Zdim1_croppingZdim2_croppingZdim3_cropping)rE   rF   rG   r(     s(    

zCropping3D.__init__c             C   s  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 }|d d k	r|d | jd d  | jd d  }nd }t |d |d |||gS | jdkr|d d k	r|d | jd d  | jd d  }nd }|d d k	rN|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 )Nr&   r#   r   r   r   r   rz   )r   rU   r|   r4   r   )rC   ra   r   r   r   rF   rF   rG   rk     s2    
&&&&&&zCropping3D.compute_output_shapec             C   s  | j dkr| jd d | jd d   krH| jd d   krHdkrn nD|d d d d | jd d d | jd d d | jd d d f S | jd d | jd d   krdkrn nP|d d d d | jd d d | jd d d | jd d | jd d  f S | jd d | jd d   kr:dkrn nP|d d d d | jd d | jd d  | jd d d | jd d d f S | jd d | jd d   krdkrn nP|d d d d | jd d d | jd d | j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  | jd d | jd d  f S | jd d dkr|d d d d | jd d | jd d  | jd d d | jd d | jd d  f S | jd d dkr^|d d d d | jd d | j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  | jd d | jd d  f S | jd d | jd d   kr| jd d   krdkrPn nD|d d | jd d d | jd d d | jd d d d d f S | jd d | jd d   kr|dkrn nP|d d | jd d d | jd d d | jd d | jd d  d d f S | jd d | jd d   krdkrPn nP|d d | jd d | jd d  | jd d d | jd d d d d f S | jd d | jd d   kr|dkrn nP|d d | jd d d | jd d | j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  | 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 | 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 | 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  | jd d | jd d  d d f S d S )Nr&   r   r   r#   )r4   r   )rC   rm   rF   rF   rG   rp     sh    @..."0*&0*&&6....*$F(0((0$,0$,  0$ ($zCropping3D.callc                s8   | j | jd}tt|  }tt| t|  S )N)r   r4   )r   r4   r'   r   r   r   r[   r   )rC   r   r   )rE   rF   rG   r   X  s    zCropping3D.get_config)r   N)	r\   r   r   r   r(   rk   rp   r   r   rF   rF   )rE   rG   r     s   2 #9r   )?r   r]   Ztensorflow.python.eagerr   Ztensorflow.python.frameworkr   Ztensorflow.python.kerasr   r   r   r   r   Z)tensorflow.python.keras.engine.base_layerr	   Z)tensorflow.python.keras.engine.input_specr
   Z&tensorflow.python.keras.layers.poolingr   r   r   r   r   r   Ztensorflow.python.keras.utilsr   r   Ztensorflow.python.opsr   r   r   Z tensorflow.python.util.tf_exportr   r   rJ   r   r   r   r   r   r   rK   r   r   r   r   r   r   r   r   r   r   r   ZConvolution1DZConvolution2DZConvolution3DZSeparableConvolution1DZSeparableConvolution2DZConvolution2DTransposeZConvolution3DTransposeZDeconvolution2DZDeconv2DZDeconvolution3DZDeconv3DrF   rF   rF   rG   <module>   s     U     v  /  9 T 4 - g8gPA|}<~ 8