
    2Vh,                     l    d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	  ed       G d de             Z
y	)
    )backend)keras_export)	InputSpec)Layer)argument_validationzkeras.layers.Cropping3Dc                   <     e Zd ZdZ	 d fd	Zd Zd Z fdZ xZS )
Cropping3Da  Cropping layer for 3D data (e.g. spatial or spatio-temporal).

    Example:

    >>> input_shape = (2, 28, 28, 10, 3)
    >>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
    >>> y = keras.layers.Cropping3D(cropping=(2, 4, 2))(x)
    >>> 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 three 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)`.
            When unspecified, uses `image_data_format` value found in your Keras
            config file at `~/.keras/keras.json` (if exists). Defaults to
            `"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, channels)`
        - If `data_format` is `"channels_first"`:
          `(batch_size, channels, 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, channels)`
        - If `data_format` is `"channels_first"`:
          `(batch_size, channels, first_cropped_axis, second_cropped_axis,
          third_cropped_axis)`
    c                    t        |   di | t        j                  |      | _        t        |t              r%|dk  rt        d| d      ||f||f||ff| _        nt        |d      r|t        |      dk7  rt        d| d      t        j                  |d   ddd	
      }t        j                  |d   ddd	
      }t        j                  |d   ddd	
      }|||f| _        nt        d| d      t        d      | _        y )Nr   z2`cropping` cannot be negative. Received: cropping=.__len__   z-`cropping` should have 3 elements. Received:    z1st entry of croppingT)
allow_zero   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)). Received:    )ndim )super__init__r   standardize_data_formatdata_format
isinstanceint
ValueErrorcroppinghasattrlenr   standardize_tupler   
input_spec)selfr   r   kwargsdim1_croppingdim2_croppingdim3_cropping	__class__s          U/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/reshaping/cropping3d.pyr   zCropping3D.__init__;   sI    	"6""::;Gh$!| **216 
 8$8$8$DM
 Xy)8}! CH:QO  0AAQ 7DM 0AAQ 7DM 0AAQ 7DM +M=IDM &Ja)	 	 $+    c                 h   | j                   dk(  rt        |dd       }nt        |dd       }t        dd      D ]N  }||   	||xx   t        | j                  |         z  cc<   ||   dk  s5t        d| d	| j                          | j                   dk(  r|d   |d   g|S |d   g||d   S )
Nchannels_firstr   r   r      r   r   z}Values in `cropping` argument should be smaller than the corresponding spatial dimension of the input. Received: input_shape=, cropping=)r   listrangesumr   r   )r    input_shapespatial_dimsindexs       r&   compute_output_shapezCropping3D.compute_output_shapeg   s    //Aa 01LAa 01L1a[ 		EE"*3t}}U';#<<E"a' ##.-{4==/K 		 //NKNB\BBNB\B;q>BBr'   c                 T   | j                   dk(  rt        |j                  dd       }nt        |j                  dd       }t        dd      D ]X  }||   	||xx   t	        | j
                  |         z  cc<   ||   dk  s5t        d|j                   d	| j
                          | j                   dk(  r| j
                  d   d   | j
                  d   d   cxk(  r| j
                  d   d   cxk(  rdk(  rGn 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   cxk(  rdk(  rXn nU|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   cxk(  rdk(  rXn nU|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   cxk(  rdk(  rXn nU|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(  rf|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(  rf|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(  rf|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   cxk(  r| j
                  d   d   cxk(  rdk(  rGn 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   cxk(  rdk(  rXn nU|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   cxk(  rdk(  rXn nU|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   cxk(  rdk(  rXn nU|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(  rf|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(  rf|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(  rf|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 )
Nr)   r   r   r   r*   r   r   z~Values in `cropping` argument should be smaller than the corresponding spatial dimension of the input. Received: inputs.shape=r+   )r   r,   shaper-   r.   r   r   )r    inputsr0   r1   s       r&   callzCropping3D.call}   s	   //Qq 12LQq 12L1a[ 		EE"*3t}}U';#<<E"a' $$*LL>T]]OM 		 //a #==#A&==#A& 
 MM!$Q')MM!$Q')MM!$Q')	+  q!!$a(8(;@q@MM!$Q')MM!$Q')MM!$Q'4==+;A+>*>>	@  q!!$a(8(;@q@MM!$Q'4==+;A+>*>>MM!$Q')MM!$Q')	+  q!!$a(8(;@q@MM!$Q')MM!$Q'4==+;A+>*>>MM!$Q')	+  q!!$)MM!$Q')MM!$Q'4==+;A+>*>>MM!$Q'4==+;A+>*>>	@  q!!$)MM!$Q'4==+;A+>*>>MM!$Q')MM!$Q'4==+;A+>*>>	@  q!!$)MM!$Q'4==+;A+>*>>MM!$Q'4==+;A+>*>>MM!$Q')	+  a #t}}Q'7':&::a #t}}Q'7':&::a #t}}Q'7':&::	<  a #==#A&==#A& 
 MM!$Q')MM!$Q')MM!$Q')	  q!!$a(8(;@q@MM!$Q')MM!$Q')MM!$Q'4==+;A+>*>>	  q!!$a(8(;@q@MM!$Q'4==+;A+>*>>MM!$Q')MM!$Q')	  q!!$a(8(;@q@MM!$Q')MM!$Q'4==+;A+>*>>MM!$Q')	  q!!$)MM!$Q')MM!$Q'4==+;A+>*>>MM!$Q'4==+;A+>*>>	  q!!$)MM!$Q'4==+;A+>*>>MM!$Q')MM!$Q'4==+;A+>*>>	  q!!$)MM!$Q'4==+;A+>*>>MM!$Q'4==+;A+>*>>MM!$Q')	  a #t}}Q'7':&::a #t}}Q'7':&::a #t}}Q'7':&::	 r'   c                 ^    | j                   | j                  d}t        |          }i ||S )N)r   r   )r   r   r   
get_config)r    configbase_configr%   s      r&   r8   zCropping3D.get_config  s4    "mmD<L<LMg(*(+(((r'   ))r   r   r;   r;   N)	__name__
__module____qualname____doc__r   r2   r6   r8   __classcell__)r%   s   @r&   r	   r	      s,    /d >B*,XC,Zx) )r'   r	   N)	keras.srcr   keras.src.api_exportr   keras.src.layers.input_specr   keras.src.layers.layerr   keras.src.utilsr   r	   r   r'   r&   <module>rF      s8     - 1 ( / '(S) S) )S)r'   