
    6bi:                         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5       " S S\5      5       r\rg)zGKeras 3D transposed convolution layer (sometimes called deconvolution).    N)activations)constraints)initializers)regularizers)utils)	InputSpec)Conv3D)
conv_utils)keras_exportzkeras.layers.Conv3DTransposez#keras.layers.Convolution3DTransposec                      ^  \ 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$ )
Conv3DTranspose!   a*  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)`.
    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 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)
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/conv3d_transpose.pyr%   Conv3DTranspose.__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U R                  U R                  U4-   n[        SX#0S9U l        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 5. Received input_shape=r"   zhThe channel dimension of the inputs to `Conv3DTranspose` 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)shapeinitializerregularizer
constraint	trainabledtypebias)tfTensorShapelenr)   _get_channel_axisdimsvalueintr   r   r   
input_spec
add_weightr   r   r   r<   r6   r   r   r   r   r=   built)r*   input_shapechannel_axis	input_dimkernel_shapes        r/   buildConv3DTranspose.build   sg   nn[1{q ((3}A7  --/L)//7//:m <*^ ,FF  12	''4<<*CC#,1JKoo////--** & 
 ==||o 11 11//jj ( DI DI
r1   c                    [         R                  " U5      nUS   nU R                  S:X  a  Su  pEnOSu  pEnX$   nX%   nX&   n	U R                  u  pnU R                  u  pnU R
                  c  S =n=nnOU R
                  u  nnn[        R                  " UU
U R                  UUS9n[        R                  " UUU R                  UUS9n[        R                  " U	UU R                  UUS9nU R                  S:X  a  UU R                  UUU4nSSXU4nOUUUUU R                  4nSXUS4n[         R                  " U5      n[         R                  R                  UU R                  UU[        R                  " U R                  SS9U R                  R                  5       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,                  [        R                  " U R                  S
S9S9nU R.                  b  U R/                  U5      $ U$ )Nr   channels_first)   r      )   rP   r   r   r    r,   rR   r3   )r4   )r   r   rQ   )r   )r>   r7   r   r   r   r    r
   deconv_output_lengthr   r   stacknnconv3d_transposer6   convert_data_formatupperexecuting_eagerlyrankcompute_output_shape	set_shaper   bias_addr=   r   )r*   inputsinputs_shape
batch_sized_axish_axisw_axisdepthheightwidthkernel_dkernel_hkernel_wstride_dstride_hstride_w	out_pad_d	out_pad_h	out_pad_w	out_depth
out_height	out_widthoutput_shaper   output_shape_tensoroutputs	out_shapes                              r/   callConv3DTranspose.call   sx   xx'!!_
//%,"FF%,"FF$%$'+'7'7$H'+||$H&044I4	I.2.A.A+Iy) 33LL$
	  44LL$

 33LL$
	 //L !X:G L (h:G hh|4%%((KK"66  q LL&&( ) 	
 ##%%&,,*;*;11&,,?Ii(==eenn		&::$$1 % G ??&??7++r1   c                 B   [         R                  " U5      R                  5       n[        U5      nU R                  S:X  a  Su  p4pVOSu  p4pVU R
                  u  pxn	U R                  u  pnU R                  c  S =n=pOU R                  u  pnU R                  X#'   [        R                  " X$   UU R                  UU
S9X$'   [        R                  " X%   UU R                  UUS9X%'   [        R                  " X&   U	U R                  UUS9X&'   [         R                  " U5      $ )NrO   )rR   rP   r   rQ   )rQ   rR   rP   r   rS   )r>   r?   as_listlistr   r   r   r    r   r
   rT   r   )r*   rH   rt   c_axisrb   rc   rd   rh   ri   rj   rk   rl   rm   rn   ro   rp   s                   r/   r\   $Conv3DTranspose.compute_output_shapeW  s-   nn[199;K(//-7*FFF-7*FF'+'7'7$H'+||$H&044I4	I.2.A.A+I)#||)>> LL$ 
  *>> LL$ 
  *>> LL$ 
 ~~l++r1   c                 d   > [         TU ]  5       nUR                  S5        U R                  US'   U$ )Nr   r    )r$   
get_configpopr    )r*   configr.   s     r/   r   Conv3DTranspose.get_config  s2    #%

?##'#6#6 r1   )r=   rG   rE   r6   r    )rR   rR   rR   validNNr   NTglorot_uniformzerosNNNNN)__name__
__module____qualname____firstlineno____doc__r   allow_initializer_layoutr%   rL   rx   r\   r   __static_attributes____classcell__)r.   s   @r/   r   r   !   sn    un ##
 + !#2 $2h)VYv&,P r1   r   )r   tensorflow.compat.v2compatv2r>   tf_keras.srcr   r   r   r   tf_keras.src.dtensorr   tf_keras.src.engine.input_specr   (tf_keras.src.layers.convolutional.conv3dr	   tf_keras.src.utilsr
    tensorflow.python.util.tf_exportr   r   Convolution3DTransposer#   r1   r/   <module>r      sb    N " ! $ $ % % & 4 ; ) : "$I_f __H ) r1   