
    2VhZ1                         d Z 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 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 ddlmZ  G d de      Zy)z4Keras abstract base layer for separable convolution.    )activations)constraints)initializers)ops)regularizers)standardize_data_format)	InputSpec)Layer)compute_conv_output_shape)standardize_padding)standardize_tuplec                   d     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zd Zd Zd Z fdZ xZ	S )BaseSeparableConva)  Abstract base layer for separable convolution.

    This layer performs a depthwise convolution that acts separately on
    channels, followed by a pointwise convolution that mixes channels. If
    `use_bias` is True and a bias initializer is provided, it adds a bias vector
    to the output.

    Args:
        rank: int, the rank of the convolution, e.g. 2 for 2D convolution.
        depth_multiplier: The number of depthwise convolution output channels
            for each input channel. The total number of depthwise convolution
            output channels will be equal to `input_channel * depth_multiplier`.
        filters: int, the dimensionality of the output space (i.e. the number
            of filters in the pointwise convolution).
        kernel_size: int or tuple/list of `rank` integers, specifying the size
            of the depthwise convolution window.
        strides: int or tuple/list of `rank` integers, specifying the stride
            length of the depthwise convolution. If only one int is specified,
            the same stride size will be used for all dimensions.
            `stride value != 1` is incompatible with `dilation_rate != 1`.
        padding: string, either `"valid"` or `"same"` (case-insensitive).
            `"valid"` means no padding. `"same"` results in padding evenly to
            the left/right or up/down of the input. When `padding="same"` and
            `strides=1`, the output has the same size as the input.
        data_format: string, either `"channels_last"` or `"channels_first"`.
            The ordering of the dimensions in the inputs. `"channels_last"`
            corresponds to inputs with shape `(batch, steps, features)`
            while `"channels_first"` corresponds to inputs with shape
            `(batch, features, steps)`. 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"`.
        dilation_rate: int or tuple/list of `rank` integers, specifying the
            dilation rate to use for dilated convolution. If only one int is
            specified, the same dilation rate will be used for all dimensions.
        activation: Activation function. If `None`, no activation is applied.
        use_bias: bool, if `True`, bias will be added to the output.
        depthwise_initializer: An initializer for the depthwise convolution
            kernel. If None, then the default initializer (`"glorot_uniform"`)
            will be used.
        pointwise_initializer: An initializer for the pointwise convolution
            kernel. If None, then the default initializer (`"glorot_uniform"`)
            will be used.
        bias_initializer: An initializer for the bias vector. If None, the
            default initializer ('"zeros"') will be used.
        depthwise_regularizer: Optional regularizer for the depthwise
            convolution kernel.
        pointwise_regularizer: Optional regularizer for the pointwise
            convolution kernel.
        bias_regularizer: Optional regularizer for the bias vector.
        activity_regularizer: Optional regularizer function for the output.
        depthwise_constraint: Optional projection function to be applied to the
            depthwise kernel after being updated by an `Optimizer` (e.g. used
            for norm constraints or value constraints for layer weights). The
            function must take as input the unprojected variable and must return
            the projected variable (which must have the same shape).
        pointwise_constraint: Optional projection function to be applied to the
            pointwise kernel after being updated by an `Optimizer`.
        bias_constraint: Optional projection function to be applied to the
            bias after being updated by an `Optimizer`.
    c                    t        |   d||t        j                  |      d| || _        || _        || _        t        ||d      | _        t        ||d      | _	        t        ||d      | _
        t        |      | _        t        |      | _        t        j                  |	      | _        |
| _        t%        j                  |      | _        t%        j                  |      | _        t%        j                  |      | _        t        j                  |      | _        t        j                  |      | _        t        j                  |      | _        t3        j                  |      | _        t3        j                  |      | _        t3        j                  |      | _        | j                  | _        t;        | j                  dz         | _        | j
                  (| j
                  dk  rt?        d| j
                   d	      | j                  (| j                  dk  rt?        d
| j                   d	      tA        | j                        st?        d| j                   d	      tA        | j                        st?        d| j                         tC        | j                        dkD  r>tC        | j                        dkD  r%t?        d| j                   d| j                         y y )N)	trainablenameactivity_regularizerkernel_sizestridesdilation_rate   )min_ndimr   zmInvalid value for argument `depth_multiplier`. Expected a strictly positive value. Received depth_multiplier=.z[Invalid value for argument `filters`. Expected a strictly positive value. Received filters=zCThe argument `kernel_size` cannot contain 0. Received: kernel_size=z?The argument `strides` cannot contains 0(s). Received: strides=   zW`strides > 1` not supported in conjunction with `dilation_rate > 1`. Received: strides=z and dilation_rate= )"super__init__r   getrankdepth_multiplierfiltersr   r   r   r   r   paddingr   data_formatr   
activationuse_biasr   depthwise_initializerpointwise_initializerbias_initializerdepthwise_regularizerpointwise_regularizerbias_regularizerr   depthwise_constraintpointwise_constraintbias_constraintr	   
input_spec
ValueErrorallmax)selfr   r    r!   r   r   r"   r#   r   r$   r%   r&   r'   r(   r)   r*   r+   r   r,   r-   r.   r   r   kwargs	__class__s                           b/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/convolutional/base_separable_conv.pyr   zBaseSeparableConv.__init__N   s   4 	 	
!-!1!12F!G	
 		
 	 0,[$N($	B.4
 +732;?%//*5 %1%5%56K%L"%1%5%56K%L" , 0 01A B%1%5%56K%L"%1%5%56K%L" , 0 01A B$/OO4H$I!$/OO4H$I!*?++#TYY];  ,1F1F!1K$$($9$9#:!=  <<#(9448LL>D 
 4##$#//03 
 4<< <<.* 
 t||q S););%<q%@::>,, H!!%!3!3 46  &A     c           	         | j                   dk(  rd}|d   }nd}|d   }t        | j                  dz   ||i      | _        | j                  || j
                  fz   }d| j                  z  | j
                  |z  | j                  fz   }| j                  d|| j                  | j                  | j                  d| j                  	      | _        | j                  d
|| j                  | j                  | j                  d| j                  	      | _        | j"                  rQ| j                  d| j                  f| j$                  | j&                  | j(                  d| j                  	      | _        y d | _        y )Nchannels_lastr   r   )r   axesr   depthwise_kernelT)r   shapeinitializerregularizer
constraintr   dtypepointwise_kernelbias)r#   r	   r   r/   r   r    r!   
add_weightr&   r)   r,   rB   r=   r'   r*   r-   rC   r%   r(   r+   r.   rD   )r3   input_shapechannel_axisinput_channeldepthwise_kernel_shapepointwise_kernel_shapes         r6   buildzBaseSeparableConv.build   s|   .L'OML'NM#YY],)F
 "&!1!1!!5
 "
 "&		!1!!M1LL5
 "

 !%#(222200** !0 !
 !%#(222200** !0 !
 ==||o 11 11//jj ( DI DIr7   c           	         t        j                  || j                  | j                  | j                  | j
                  | j                  | j                        }| j                  r| j                  dk(  r!d| j                  dz   z  | j                  fz   }nd| j                  fd| j                  z  z   }t        j                  | j                  |      }t        j                  ||      }| j                  | j                  |      S |S )N)r   r"   r   r#   r9   r<   r   )r   separable_convr=   rC   r   r"   r   r#   r%   r   r!   reshaperD   addr$   )r3   inputsoutputs
bias_shaperD   s        r6   callzBaseSeparableConv.call   s    $$!!!!LLLL,,((
 ==?2!TYY]3t||oE
.		1AA
;;tyy*5Dgggt,G??&??7++r7   c           	          t        || j                  | j                  | j                  | j                  | j
                  | j                        S )N)r   r"   r#   r   )r   r!   r   r   r"   r#   r   )r3   rF   s     r6   compute_output_shapez&BaseSeparableConv.compute_output_shape   sB    (LLLLLL((,,
 	
r7   c                    t         |          }|j                  i d| j                  d| j                  d| j
                  d| j                  d| j                  d| j                  d| j                  dt        j                  | j                        d	| j                  d
t        j                  | j                        dt        j                  | j                         dt        j                  | j"                        dt%        j                  | j&                        dt%        j                  | j(                        dt%        j                  | j*                        dt%        j                  | j,                        dt/        j                  | j0                        t/        j                  | j2                        t/        j                  | j4                        d       |S )Nr    r!   r   r   r"   r#   r   r$   r%   r&   r'   r(   r)   r*   r+   r   r,   )r-   r.   )r   
get_configupdater    r!   r   r   r"   r#   r   r   	serializer$   r%   r   r&   r'   r(   r   r)   r*   r+   r   r   r,   r-   r.   )r3   configr5   s     r6   rW   zBaseSeparableConv.get_config   s   #%&"D$9$9&4<<& t//& 4<<	&
 4<<& t//&  !3!3& k33DOOD& DMM& ()?)?..*& ()?)?..*&  #L$:$:))%!&& ()?)?..*'&, ()?)?..*-&2 #L$:$:))%3&8 '(>(>--)9&> '(=(=--)?&D )4(=(=--) $/#8#89M9M#NK&(	
R r7   )r   validNr   NTglorot_uniformr\   zerosNNNNNNNTN)
__name__
__module____qualname____doc__r   rK   rS   rU   rW   __classcell__)r5   s   @r6   r   r      sb    ;F .. ""!!!/Wr0d.	
+ +r7   r   N)ra   	keras.srcr   r   r   r   r   keras.src.backendr   keras.src.layers.input_specr	   keras.src.layers.layerr
   keras.src.ops.operation_utilsr   #keras.src.utils.argument_validationr   r   r   r   r7   r6   <module>ri      s7    : ! ! "  " 5 1 ( C C AV Vr7   