
    6bi8                         S r SSKJs  Jr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SSKJ
r
  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  \" SS5       " S S\5      5       r\rg)zGKeras 2D transposed convolution layer (sometimes called deconvolution).    N)activations)backend)constraints)initializers)regularizers)utils)	InputSpec)Conv2D)
conv_utils)keras_exportzkeras.layers.Conv2DTransposez#keras.layers.Convolution2DTransposec                      ^  \ rS rSrSr\R                                S	U 4S jj5       rS rS r	S r
U 4S jrSrU =r$ )
Conv2DTranspose"   aT  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)`.
    When unspecified, uses `image_data_format` value found in your Keras
    config file at `~/.keras/keras.json` (if exists) else 'channels_last'.
    Defaults to "channels_last".
  dilation_rate: an integer, specifying the dilation rate for all spatial
    dimensions for dilated convolution. Specifying different dilation rates
    for different dimensions is not supported.
    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)
c                   > [         TU ]  " S	UUUUUU[        R                  " U5      U	[        R                  " U
5      [        R                  " U5      [
        R                  " U5      [
        R                  " U5      [
        R                  " U5      [        R                  " U5      [        R                  " U5      S.UD6  XPl        U R                  b}  [        R                  " U R                  SSSS9U l        [        U R                  U R                  5       H2  u  nnUU:  d  M  [        SU R                   SU R                   S35      e   g g )
N)filterskernel_sizestridespaddingdata_formatdilation_rate
activationuse_biaskernel_initializerbias_initializerkernel_regularizerbias_regularizeractivity_regularizerkernel_constraintbias_constraint   output_paddingT)
allow_zeroz>Strides must be greater than output padding. Received strides=z, output_padding=. )super__init__r   getr   r   r   r!   r   normalize_tuplezipr   
ValueError)selfr   r   r   r   r!   r   r   r   r   r   r   r   r   r   r   r   kwargsstrideout_pad	__class__s                       l/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/layers/convolutional/conv2d_transpose.pyr&   Conv2DTranspose.__init__   s@   * 	 	
##'"z2+//0BC)--.>?+//0BC)--.>?!-!1!12F!G)oo.?@'OOO<	
  !	
& -*","<"<##Q(8T#D $'t||T5H5H#If$$,,0LL> :**.*=*=)>aA  $J	 +    c           
         [         R                  " U5      n[        U5      S:w  a  [        SU S35      eU R	                  5       nUR
                  U   R                  c  [        SU SU S35      e[        X   5      n[        SX#0S9U l	        U R                  U R                  U4-   nU R                  SUU R                  U R                  U R                  S	U R                   S
9U l        U R$                  (       aN  U R                  SU R                  4U R&                  U R(                  U R*                  S	U R                   S
9U l        OS U l        S	U l        g )N   z0Inputs should have rank 4. Received input_shape=r#   zhThe channel dimension of the inputs to `Conv2DTranspose` should be defined. The input_shape received is z, where axis z> (0-based) is the channel dimension, which found to be `None`.)ndimaxeskernelT)nameshapeinitializerregularizer
constraint	trainabledtypebias)tfTensorShapelenr*   _get_channel_axisdimsvalueintr	   
input_specr   r   
add_weightr   r   r   r>   r7   r   r   r   r   r?   built)r+   input_shapechannel_axis	input_dimkernel_shapes        r0   buildConv2DTranspose.build   sg   nn[1{q ((3}A7  --/L)//7//:m <*^ ,FF  12	#,1JK''4<<*CCoo////--** & 
 ==||o 11 11//jj ( DI DI
r2   c           
         [         R                  " U5      nUS   nU R                  S:X  a  Su  pEOSu  pESu  pgUR                  R                  b"  UR                  R	                  5       nX   nX   nUb  UOX$   nUb  UOX%   nU R
                  u  pU R                  u  pU R                  c  S =pOU R                  u  p[        R                  " UU	U R                  UUU R                  S   S9n[        R                  " UU
U R                  UUU R                  S   S9nU R                  S:X  a  X0R                  UU4nOX?UU R                  4n[         R                  " U5      n[        R                  " UU R                   UU R                  U R                  U R                  U R                  S9n[         R"                  " 5       (       dG  UR                  R                  (       a,  U R%                  UR                  5      nUR'                  U5        U R(                  (       aF  [         R*                  R-                  UU R.                  [        R0                  " U R                  S	S
9S9nU R2                  b  U R3                  U5      $ U$ )Nr   channels_first)r       )   r    )NNr   r!   r-   dilationrS   )r   r   r   r   r4   )r5   )r   )r@   r9   r   rankas_listr   r   r!   r   deconv_output_lengthr   r   r   stackr   conv2d_transposer7   executing_eagerlycompute_output_shape	set_shaper   nnbias_addr?   convert_data_formatr   )r+   inputsinputs_shape
batch_sizeh_axisw_axisheightwidthrD   kernel_hkernel_wstride_hstride_w	out_pad_h	out_pad_w
out_height	out_widthoutput_shapeoutput_shape_tensoroutputs	out_shapes                        r0   callConv2DTranspose.call   sP   xx'!!_
//!NFF!NF #<<(<<'')D\FLE!-<3G*0D!--!\\&$((I	#'#6#6 I  44LL$''*

 33LL$''*
	 //&j)LL&It||LL hh|4**KKLLLL((,,
 ##%%&,,*;*;11&,,?Ii(==eenn		&::$$1 % G ??&??7++r2   c           	      $   [         R                  " U5      R                  5       n[        U5      nU R                  S:X  a  Su  p4nOSu  p4nU R
                  u  pgU R                  u  pU R                  c  S =pOU R                  u  pU R                  X#'   [        R                  " X$   UU R                  U
UU R                  S   S9X$'   [        R                  " X%   UU R                  UU	U R                  S   S9X%'   [         R                  " U5      $ )NrQ   )rS   r    rR   )rR   rS   r    r   rT   rS   )r@   rA   rW   listr   r   r   r!   r   r   rX   r   r   )r+   rJ   rp   c_axisrd   re   rh   ri   rj   rk   rl   rm   s               r0   r\   $Conv2DTranspose.compute_output_shapeD  s   nn[199;K(//%,"FF%,"FF!--!\\&$((I	#'#6#6 I#||)>> LL$''* 
  *>> LL$''* 
 ~~l++r2   c                 B   > [         TU ]  5       nU R                  US'   U$ )Nr!   )r%   
get_configr!   )r+   configr/   s     r0   r{   Conv2DTranspose.get_configg  s&    #%#'#6#6 r2   )r?   rI   rG   r7   r!   )rS   rS   validNNr~   NTglorot_uniformzerosNNNNN)__name__
__module____qualname____firstlineno____doc__r   allow_initializer_layoutr&   rN   rt   r\   r{   __static_attributes____classcell__)r/   s   @r0   r   r   "   sn    m^ ##
 + !#2 $2h)VM^!,F r2   r   )r   tensorflow.compat.v2compatv2r@   tf_keras.srcr   r   r   r   r   tf_keras.src.dtensorr   tf_keras.src.engine.input_specr	   (tf_keras.src.layers.convolutional.conv2dr
   tf_keras.src.utilsr    tensorflow.python.util.tf_exportr   r   Convolution2DTransposer$   r2   r0   <module>r      se    N " ! $   $ % % & 4 ; ) : "$IEf EET
 ) r2   