o
    2h,                     @   sX   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 edG dd deZ
d	S )
    )backend)keras_export)	InputSpec)Layer)argument_validationz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).

    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)`
       r
   r	   r	   Nc                    s   t  jdi | t|| _t|tr-|dk r td| d||f||f||ff| _nCt	|drht
|dkr@td| dtj|d ddd	d
}tj|d ddd	d
}tj|d ddd	d
}|||f| _ntd| dtdd| _d S )Nr   z2`cropping` cannot be negative. Received: cropping=.__len__   z-`cropping` should have 3 elements. Received:    z1st entry of croppingT)
allow_zeror
   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__r   `/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/layers/reshaping/cropping3d.pyr   ;   sD   



zCropping3D.__init__c                 C   s   | j dkrt|dd }nt|dd }tddD ]'}|| d u r$q||  t| j| 8  < || dkrBtd| d	| j q| j dkrS|d |d g|R S |d g||d R 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indexr   r   r&   compute_output_shapeg   s&   

zCropping3D.compute_output_shapec                 C   s  | j dkrt|jdd }n	t|jdd }tddD ](}|| d u r&q||  t| j| 8  < || dkrEtd|j d	| j q| j dkr'| jd d | jd d   krj| jd d   krjdkrn n"|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rn n(|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rdkrn n(|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r!dkrKn n(|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rH| jd d   krHdkrln n"|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rdkrn n(|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rn n(|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r,n n(|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rd|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 )
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   inputsr.   r/   r   r   r&   call}   sV  

,.0	

000zCropping3D.callc                    s$   | j | jd}t  }i ||S )N)r   r   )r   r   r   
get_config)r   configbase_configr$   r   r&   r4     s   
zCropping3D.get_config)r   N)	__name__
__module____qualname____doc__r   r0   r3   r4   __classcell__r   r   r$   r&   r      s    2, r   N)	keras.srcr   keras.src.api_exportr   keras.src.layers.input_specr   keras.src.layers.layerr   keras.src.utilsr   r   r   r   r   r&   <module>   s    