o
    ={c|.                     @   sb   d Z ddlm  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 )
z"Keras cropping layer for 3D input.    N)Layer)	InputSpec)
conv_utils)keras_exportz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 )
Cropping3Dac  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   Nc                    s   t  jdi | t|| _t|tr!||f||f||ff| _nCt|dr\t	|dkr4t
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 )N__len__   z-`cropping` should have 3 elements. Received: .r      z1st entry of croppingT)Z
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   Znormalize_data_formatdata_format
isinstanceintcroppinghasattrlen
ValueErrorZnormalize_tupler   Z
input_spec)selfr   r   kwargsZdim1_croppingZdim2_croppingZdim3_cropping	__class__r   Alib/python3.10/site-packages/keras/layers/reshaping/cropping3d.pyr   P   s8   


zCropping3D.__init__c                 C   s  t | }| jdkrk|d d ur%|d | jd d  | jd d  }nd }|d d ur@|d | jd d  | jd d  }nd }|d d ur[|d | jd d  | jd d  }nd }t |d |d |||gS | jdkr|d d ur|d | jd d  | jd d  }nd }|d d ur|d | jd d  | jd d  }nd }|d d ur|d | jd d  | jd d  }nd }t |d ||||d gS d S )Nchannels_firstr   r   r	   r      Zchannels_last)tfZTensorShapeZas_listr   r   )r   Zinput_shapeZdim1Zdim2Zdim3r   r   r   compute_output_shapex   sD   
"""
"""zCropping3D.compute_output_shapec                 C   s  | j dkr| jd d | jd d   kr$| jd d   kr$dkrHn 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r\d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rn 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rdkrn 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rs|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r$n 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r:dkrdn 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rzd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rdkrn 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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rT|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inputsr   r   r   call   s6  
,,.	

000zCropping3D.callc                    s4   | j | jd}t  }tt| t|  S )N)r   r   )r   r   r   
get_configdictlistitems)r   ZconfigZbase_configr   r   r   r$   5  s   
zCropping3D.get_config)r   N)	__name__
__module____qualname____doc__r   r"   r#   r$   __classcell__r   r   r   r   r      s    3(0 r   )r+   Ztensorflow.compat.v2compatZv2r!   Zkeras.engine.base_layerr   Zkeras.engine.input_specr   Zkeras.utilsr   Z tensorflow.python.util.tf_exportr   r   r   r   r   r   <module>   s   